Remove deprecated APIs

Resolves #3836
This commit is contained in:
Mahmoud Ben Hassine
2021-08-17 20:55:08 +02:00
parent f8bdf5521e
commit bb3809cf55
37 changed files with 217 additions and 2712 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 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.
@@ -250,22 +250,6 @@ public class JobExecution extends Entity {
return status == BatchStatus.STOPPING;
}
/**
* Signal the {@link JobExecution} to stop. Iterates through the associated
* {@link StepExecution}s, calling {@link StepExecution#setTerminateOnly()}.
*
* @deprecated Use {@link org.springframework.batch.core.launch.JobOperator#stop(long)}
* or {@link org.springframework.batch.core.launch.support.CommandLineJobRunner}
* with the "-stop" option instead.
*/
@Deprecated
public void stop() {
for (StepExecution stepExecution : stepExecutions) {
stepExecution.setTerminateOnly();
}
status = BatchStatus.STOPPING;
}
/**
* Sets the {@link ExecutionContext} for this execution
*

View File

@@ -70,27 +70,6 @@ public class JobParameters implements Serializable {
return value==null ? null : ((Long)value).longValue();
}
/**
* Typesafe Getter for the Long represented by the provided key. If the
* key does not exist, the default value will be returned.
*
* @param key to return the value for
* @param defaultValue to return if the value doesn't exist
* @return the parameter represented by the provided key, defaultValue
* otherwise.
* @deprecated Use {@link JobParameters#getLong(java.lang.String, java.lang.Long)}
* instead. This method will be removed in a future release.
*/
@Deprecated
public Long getLong(String key, long defaultValue){
if(parameters.containsKey(key)){
return getLong(key);
}
else{
return defaultValue;
}
}
/**
* Typesafe Getter for the Long represented by the provided key. If the
* key does not exist, the default value will be returned.
@@ -156,27 +135,6 @@ public class JobParameters implements Serializable {
return value==null ? null : value.doubleValue();
}
/**
* Typesafe Getter for the Double represented by the provided key. If the
* key does not exist, the default value will be returned.
*
* @param key to return the value for
* @param defaultValue to return if the value doesn't exist
* @return the parameter represented by the provided key, defaultValue
* otherwise.
* @deprecated Use {@link JobParameters#getDouble(java.lang.String, java.lang.Double)}
* instead. This method will be removed in a future release.
*/
@Deprecated
public Double getDouble(String key, double defaultValue){
if(parameters.containsKey(key)){
return getDouble(key);
}
else{
return defaultValue;
}
}
/**
* Typesafe Getter for the Double represented by the provided key. If the
* key does not exist, the default value will be returned.

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2006-2007 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.configuration.support;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
/**
* {@link ApplicationContextFactory} implementation that takes a parent context
* and a path to the context to create. When createApplicationContext method is
* called, the child {@link ApplicationContext} will be returned. The child
* context is not re-created every time it is requested, it is lazily
* initialized and cached. Clients should ensure that it is closed when it is no
* longer needed. If a path is not set, the parent will always be returned.
*
* @deprecated use {@link GenericApplicationContextFactory} instead
*/
@Deprecated
public class ClassPathXmlApplicationContextFactory extends GenericApplicationContextFactory {
/**
* Create an application context factory for the resource specified.
*
* @param resource a resource (XML configuration file)
*/
public ClassPathXmlApplicationContextFactory(Resource resource) {
super(resource);
}
}

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2008-2010 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.configuration.support;
/**
* Placeholder for deprecation warning.
*
* @author Dave Syer
*
* @deprecated in version 2.1, please us {@link AutomaticJobRegistrar} instead
*/
@Deprecated
public abstract class ClassPathXmlJobRegistry {
}

View File

@@ -1,112 +0,0 @@
/*
* Copyright 2006-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.launch.support;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.lang.Nullable;
/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
* @deprecated as of v4.3 in favor of
* {@link org.springframework.batch.core.converter.DefaultJobParametersConverter}
* and scheduled for removal in v5.0.
*/
@Deprecated
public class ScheduledJobParametersFactory implements JobParametersConverter {
public static final String SCHEDULE_DATE_KEY = "schedule.date";
private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
/*
* (non-Javadoc)
*
* @see org.springframework.batch.core.runtime.JobParametersFactory#getJobParameters(java.util.Properties)
*/
@Override
public JobParameters getJobParameters(@Nullable Properties props) {
if (props == null || props.isEmpty()) {
return new JobParameters();
}
JobParametersBuilder propertiesBuilder = new JobParametersBuilder();
for (Entry<Object, Object> entry : props.entrySet()) {
if (entry.getKey().equals(SCHEDULE_DATE_KEY)) {
Date scheduleDate;
try {
scheduleDate = dateFormat.parse(entry.getValue().toString());
} catch (ParseException ex) {
throw new IllegalArgumentException("Date format is invalid: [" + entry.getValue() + "]");
}
propertiesBuilder.addDate(entry.getKey().toString(), scheduleDate);
} else {
propertiesBuilder.addString(entry.getKey().toString(), entry.getValue().toString());
}
}
return propertiesBuilder.toJobParameters();
}
/**
* Convert schedule date to Date, and assume all other parameters can be represented by their default string value.
*
* @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters)
*/
@Override
public Properties getProperties(@Nullable JobParameters params) {
if (params == null || params.isEmpty()) {
return new Properties();
}
Map<String, JobParameter> parameters = params.getParameters();
Properties result = new Properties();
for (Entry<String, JobParameter> entry : parameters.entrySet()) {
String key = entry.getKey();
JobParameter jobParameter = entry.getValue();
if (key.equals(SCHEDULE_DATE_KEY)) {
result.setProperty(key, dateFormat.format(jobParameter.getValue()));
} else {
result.setProperty(key, "" + jobParameter.getValue());
}
}
return result;
}
/**
* Public setter for injecting a date format.
*
* @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd"
*/
public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2018 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.
@@ -84,26 +84,6 @@ public class SimpleStepExecutionSplitter implements StepExecutionSplitter, Initi
this.stepName = stepName;
}
/**
* Construct a {@link SimpleStepExecutionSplitter} from its mandatory
* properties.
*
* @param jobRepository the {@link JobRepository}
* @param step the target step (a local version of it), used to extract the
* name and allowStartIfComplete flags
* @param partitioner a {@link Partitioner} to use for generating input
* parameters
*
* @deprecated use {@link #SimpleStepExecutionSplitter(JobRepository, boolean, String, Partitioner)} instead
*/
@Deprecated
public SimpleStepExecutionSplitter(JobRepository jobRepository, Step step, Partitioner partitioner) {
this.jobRepository = jobRepository;
this.allowStartIfComplete = step.isAllowStartIfComplete();
this.partitioner = partitioner;
this.stepName = step.getName();
}
/**
* Check mandatory properties (step name, job repository and partitioner).
*
@@ -247,22 +227,6 @@ public class SimpleStepExecutionSplitter implements StepExecutionSplitter, Initi
* @throws JobExecutionException if unable to check if the step execution is startable
*/
protected boolean isStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException {
return getStartable(stepExecution, context);
}
/**
* Check if a step execution is startable.
* @param stepExecution the step execution to check
* @param context the execution context of the step
* @return true if the step execution is startable, false otherwise
* @throws JobExecutionException if unable to check if the step execution is startable
* @deprecated This method is deprecated in favor of
* {@link SimpleStepExecutionSplitter#isStartable} and will be removed in a
* future version.
*/
@Deprecated
protected boolean getStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException {
JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance();
String stepName = stepExecution.getStepName();
StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName);

View File

@@ -1,122 +0,0 @@
/*
* Copyright 2006-2018 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.repository.dao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Map;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;
import org.springframework.util.Assert;
/**
* Implementation that uses XStream and Jettison to provide serialization.
*
* @author Thomas Risberg
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @since 2.0
* @see ExecutionContextSerializer
* @deprecated Due to the incompatibilities between current Jettison versions and XStream
* versions, this serializer is deprecated in favor of
* {@link Jackson2ExecutionContextStringSerializer}
*/
@Deprecated
public class XStreamExecutionContextStringSerializer implements ExecutionContextSerializer, InitializingBean {
private ReflectionProvider reflectionProvider = null;
private HierarchicalStreamDriver hierarchicalStreamDriver;
private XStream xstream;
public void setReflectionProvider(ReflectionProvider reflectionProvider) {
this.reflectionProvider = reflectionProvider;
}
public void setHierarchicalStreamDriver(HierarchicalStreamDriver hierarchicalStreamDriver) {
this.hierarchicalStreamDriver = hierarchicalStreamDriver;
}
@Override
public void afterPropertiesSet() throws Exception {
init();
}
public synchronized void init() throws Exception {
if (hierarchicalStreamDriver == null) {
this.hierarchicalStreamDriver = new JettisonMappedXmlDriver();
}
if (reflectionProvider == null) {
xstream = new XStream(hierarchicalStreamDriver);
}
else {
xstream = new XStream(reflectionProvider, hierarchicalStreamDriver);
}
}
/**
* Serializes the passed execution context to the supplied OutputStream.
*
* @param context {@link Map} containing the context information.
* @param out {@link OutputStream} where the serialized context information
* will be written.
*
* @see Serializer#serialize(Object, OutputStream)
*/
@Override
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
Assert.notNull(context, "context is required");
Assert.notNull(out, "An OutputStream is required");
out.write(xstream.toXML(context).getBytes());
}
/**
* Deserializes the supplied input stream into a new execution context.
*
* @param in {@link InputStream} containing the information to be deserialized.
* @return a reconstructed execution context
* @see Deserializer#deserialize(InputStream)
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> deserialize(InputStream in) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return (Map<String, Object>) xstream.fromXML(sb.toString());
}
}

View File

@@ -72,19 +72,6 @@ public class JobExecutionTests {
assertFalse(execution.isRunning());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getEndTime()}.
*/
@Test
public void testIsRunningWithStoppedExecution() {
execution.setStartTime(new Date());
assertTrue(execution.isRunning());
execution.stop();
assertTrue(execution.isRunning());
assertTrue(execution.isStopping());
}
/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getStartTime()}.
@@ -206,14 +193,6 @@ public class JobExecutionTests {
assertEquals(2, execution.getStepExecutions().size());
}
@Test
public void testStop() throws Exception {
StepExecution stepExecution = execution.createStepExecution("step");
assertFalse(stepExecution.isTerminateOnly());
execution.stop();
assertTrue(stepExecution.isTerminateOnly());
}
@Test
public void testToString() throws Exception {
assertTrue("JobExecution string does not contain id", execution.toString().indexOf("id=") >= 0);

View File

@@ -102,7 +102,6 @@ public class SimpleJobTests {
@Before
public void setUp() throws Exception {
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
@@ -394,17 +393,6 @@ public class SimpleJobTests {
"no steps configured") >= 0);
}
@Test
public void testNotExecutedIfAlreadyStopped() throws Exception {
jobExecution.stop();
job.execute(jobExecution);
assertEquals(0, list.size());
checkRepository(BatchStatus.STOPPED, ExitStatus.NOOP);
ExitStatus exitStatus = jobExecution.getExitStatus();
assertEquals(ExitStatus.NOOP.getExitCode(), exitStatus.getExitCode());
}
@Test
public void testRestart() throws Exception {
step1.setAllowStartIfComplete(true);
@@ -493,31 +481,6 @@ public class SimpleJobTests {
assertFalse(step2.passedInJobContext.isEmpty());
}
@Test
public void testInterruptJob() throws Exception {
step1 = new StubStep("interruptStep", jobRepository) {
@Override
public void execute(StepExecution stepExecution) throws JobInterruptedException,
UnexpectedJobExecutionException {
stepExecution.getJobExecution().stop();
super.execute(stepExecution);
}
};
job.setSteps(Arrays.asList(new Step[] { step1, step2 }));
job.execute(jobExecution);
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());
assertEquals(1, jobExecution.getAllFailureExceptions().size());
Throwable expected = jobExecution.getAllFailureExceptions().get(0);
assertTrue("Wrong exception " + expected, expected instanceof JobInterruptedException);
assertEquals("JobExecution interrupted.", expected.getMessage());
assertNull("Second step was not supposed to be executed", step2.passedInStepContext);
}
@Test
public void testGetStepExists() {
step1 = new StubStep("step1", jobRepository);

View File

@@ -1,111 +0,0 @@
/*
* Copyright 2006-2013 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.launch.support;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.util.StringUtils;
/**
* @author Lucas Ward
*
*/
public class ScheduledJobParametersFactoryTests extends TestCase {
ScheduledJobParametersFactory factory;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
@Override
protected void setUp() throws Exception {
super.setUp();
factory = new ScheduledJobParametersFactory();
}
public void testGetParameters() throws Exception {
String jobKey = "job.key=myKey";
String scheduleDate = "schedule.date=2008/01/23";
String vendorId = "vendor.id=33243243";
String[] args = new String[] { jobKey, scheduleDate, vendorId };
JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
assertNotNull(props);
assertEquals("myKey", props.getString("job.key"));
assertEquals("33243243", props.getString("vendor.id"));
Date date = dateFormat.parse("01/23/2008");
assertEquals(date, props.getDate("schedule.date"));
}
public void testGetProperties() throws Exception {
JobParameters parameters = new JobParametersBuilder().addDate("schedule.date", dateFormat.parse("01/23/2008"))
.addString("job.key", "myKey").addString("vendor.id", "33243243").toJobParameters();
Properties props = factory.getProperties(parameters);
assertNotNull(props);
assertEquals("myKey", props.getProperty("job.key"));
assertEquals("33243243", props.getProperty("vendor.id"));
assertEquals("2008/01/23", props.getProperty("schedule.date"));
}
public void testEmptyArgs() {
JobParameters props = factory.getJobParameters(new Properties());
assertTrue(props.getParameters().isEmpty());
}
public void testNullArgs() {
assertEquals(new JobParameters(), factory.getJobParameters(null));
assertEquals(new Properties(), factory.getProperties(null));
}
public void testGetParametersWithDateFormat() throws Exception {
String[] args = new String[] { "schedule.date=2008/23/01" };
factory.setDateFormat(new SimpleDateFormat("yyyy/dd/MM"));
JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
assertNotNull(props);
Date date = dateFormat.parse("01/23/2008");
assertEquals(date, props.getDate("schedule.date"));
}
public void testGetParametersWithBogusDate() throws Exception {
String[] args = new String[] { "schedule.date=20080123" };
try {
factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "="));
} catch (IllegalArgumentException e) {
String message = e.getMessage();
assertTrue("Message should contain wrong date: " + message, contains(message, "20080123"));
}
}
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}
}

View File

@@ -1,42 +0,0 @@
/*
* Copyright 2008-2016 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.repository.dao;
import org.junit.Before;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
/**
* @author Thomas Risberg
* @author Michael Minella
*/
public class XStreamExecutionContextStringSerializerTests extends AbstractExecutionContextSerializerTests {
ExecutionContextSerializer serializer;
@Before
public void onSetUp() throws Exception {
@SuppressWarnings("deprecation")
XStreamExecutionContextStringSerializer serializerDeserializer = new XStreamExecutionContextStringSerializer();
(serializerDeserializer).afterPropertiesSet();
serializer = serializerDeserializer;
}
@Override
protected ExecutionContextSerializer getSerializer() {
return this.serializer;
}
}

View File

@@ -1,146 +0,0 @@
/*
* Copyright 2006-2013 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.resource;
import static org.junit.Assert.assertEquals;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.support.ListPreparedStatementSetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Lucas Ward
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml",
"/org/springframework/batch/core/repository/dao/data-source-context.xml" })
public class ListPreparedStatementSetterTests {
ListPreparedStatementSetter pss;
StepExecution stepExecution;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Autowired
private AbstractJob job;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private FooStoringItemWriter fooStoringItemWriter;
@Before
public void onSetUpInTransaction() throws Exception {
List<Long> parameters = new ArrayList<>();
parameters.add(1L);
parameters.add(4L);
pss = new ListPreparedStatementSetter(parameters);
}
@Transactional
@Test
public void testSetValues() {
final List<String> results = new ArrayList<>();
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss,
new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
results.add(rs.getString(1));
}
});
assertEquals(2, results.size());
assertEquals("bar2", results.get(0));
assertEquals("bar3", results.get(1));
}
@Transactional
@Test(expected = IllegalArgumentException.class)
public void testAfterPropertiesSet() throws Exception {
pss = new ListPreparedStatementSetter(null);
pss.afterPropertiesSet();
}
@Test
public void testXmlConfiguration() throws Exception {
this.jdbcTemplate.update("create table FOO (ID integer, NAME varchar(40), VALUE integer)");
try {
this.jdbcTemplate.update("insert into FOO values (?,?,?)", 0, "zero", 0);
this.jdbcTemplate.update("insert into FOO values (?,?,?)", 1, "one", 1);
this.jdbcTemplate.update("insert into FOO values (?,?,?)", 2, "two", 2);
this.jdbcTemplate.update("insert into FOO values (?,?,?)", 3, "three", 3);
JobParametersBuilder builder = new JobParametersBuilder().addLong("min.id", 1L).addLong("max.id", 2L);
JobExecution jobExecution = this.jobLauncher.run(this.job, builder.toJobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
List<Foo> foos = fooStoringItemWriter.getFoos();
assertEquals(2, foos.size());
System.err.println(foos.get(0));
System.err.println(foos.get(1));
assertEquals(new Foo(1, "one", 1), foos.get(0));
assertEquals(new Foo(2, "two", 2), foos.get(1));
}
finally {
this.jdbcTemplate.update("drop table FOO");
}
}
public static class FooStoringItemWriter implements ItemWriter<Foo> {
private List<Foo> foos = new ArrayList<>();
@Override
public void write(List<? extends Foo> items) throws Exception {
foos.addAll(items);
}
public List<Foo> getFoos() {
return foos;
}
}
}

View File

@@ -35,5 +35,5 @@
<constructor-arg ref="dataSource" />
</bean>
<bean id="serializer" class="org.springframework.batch.core.repository.dao.XStreamExecutionContextStringSerializer"/>
<bean id="serializer" class="org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer"/>
</beans>