Skip to content

Change date param default to null for non-date type job params #3869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -344,13 +343,13 @@ private void insertParameter(Long executionId, ParameterType type, String key,
String identifyingFlag = identifying? "Y":"N";

if (type == ParameterType.STRING) {
args = new Object[] { executionId, key, type, value, new Timestamp(0L),
args = new Object[] { executionId, key, type, value, null,
0L, 0D, identifyingFlag};
} else if (type == ParameterType.LONG) {
args = new Object[] { executionId, key, type, "", new Timestamp(0L),
args = new Object[] { executionId, key, type, "", null,
value, new Double(0), identifyingFlag};
} else if (type == ParameterType.DOUBLE) {
args = new Object[] { executionId, key, type, "", new Timestamp(0L), 0L,
args = new Object[] { executionId, key, type, "", null, 0L,
value, identifyingFlag};
} else if (type == ParameterType.DATE) {
args = new Object[] { executionId, key, type, "", value, 0L, 0D, identifyingFlag};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,33 @@
*/
package org.springframework.batch.core.repository.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Parikshit Dutta
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "sql-dao-test.xml" })
public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
Expand Down Expand Up @@ -62,4 +80,35 @@ protected StepExecutionDao getStepExecutionDao() {
return stepExecutionDao;
}

@Transactional
@Test
public void testSavedDateIsNullForNonDateTypeJobParams() {
final String FIND_DATE_PARAM_FROM_ID = "SELECT DATE_VAL " +
"from %PREFIX%JOB_EXECUTION_PARAMS where JOB_EXECUTION_ID = :JOB_EXECUTION_ID";

Map<String,JobParameter> parameters = new HashMap<>();
parameters.put("string-param", new JobParameter("value"));
parameters.put("long-param", new JobParameter(1L));
parameters.put("double-param", new JobParameter(1D));

JobExecution execution = new JobExecution(jobInstance, new JobParameters(parameters));
dao.saveJobExecution(execution);

List<JobExecution> executions = dao.findJobExecutions(jobInstance);
JobExecution savedJobExecution = executions.get(0);

NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
jdbcTemplate.getDataSource());

JdbcJobExecutionDao jdbcJobExecutionDao = (JdbcJobExecutionDao) jobExecutionDao;
String query = jdbcJobExecutionDao.getQuery(FIND_DATE_PARAM_FROM_ID);

SqlParameterSource namedParameters = new MapSqlParameterSource()
.addValue("JOB_EXECUTION_ID", savedJobExecution.getJobId());

List<Date> paramValues = namedParameterJdbcTemplate.queryForList(query, namedParameters, Date.class);
for (Date paramValue: paramValues) {
assertNull(paramValue);
}
}
}