From 150fff9f4184c3da721b91970f09d40aedb31604 Mon Sep 17 00:00:00 2001 From: dsyer Date: Sat, 22 Aug 2009 11:20:38 +0000 Subject: [PATCH] Add sample for jdbc paging reader --- .../resources/jobs/iosample/delimited.xml | 2 +- .../resources/jobs/iosample/jdbcPaging.xml | 61 ++++++++++++ .../src/main/resources/log4j.properties | 8 +- .../iosample/JdbcPagingFunctionalTests.java | 46 +++++++++ ...oJobInstancesDelimitedFunctionalTests.java | 94 +++++++++++++++++++ .../TwoJobInstancesPagingFunctionalTests.java | 65 +++++++++++++ 6 files changed, 270 insertions(+), 6 deletions(-) create mode 100644 spring-batch-samples/src/main/resources/jobs/iosample/jdbcPaging.xml create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcPagingFunctionalTests.java create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesDelimitedFunctionalTests.java create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml b/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml index 6333f2651..59bfe1750 100644 --- a/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml +++ b/spring-batch-samples/src/main/resources/jobs/iosample/delimited.xml @@ -33,7 +33,7 @@ - + diff --git a/spring-batch-samples/src/main/resources/jobs/iosample/jdbcPaging.xml b/spring-batch-samples/src/main/resources/jobs/iosample/jdbcPaging.xml new file mode 100644 index 000000000..7db6414d5 --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/iosample/jdbcPaging.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/log4j.properties b/spring-batch-samples/src/main/resources/log4j.properties index 168d462ec..d65988c7f 100644 --- a/spring-batch-samples/src/main/resources/log4j.properties +++ b/spring-batch-samples/src/main/resources/log4j.properties @@ -1,3 +1,6 @@ +log4j.rootLogger=info, stdout +# log4j.rootLogger=info, stdout, chainsaw + ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out @@ -14,9 +17,6 @@ log4j.appender.chainsaw.layout=org.apache.log4j.xml.XMLLayout ### set log levels - for more verbose logging change 'info' to 'debug' ### -log4j.rootLogger=info, stdout -# log4j.rootLogger=info, stdout, chainsaw - ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace @@ -30,5 +30,3 @@ log4j.rootLogger=info, stdout ### debug your specific package or classes with the following example log4j.logger.org.springframework.batch=debug log4j.logger.org.springframework.batch.sample=debug -log4j.logger.org.springframework.batch.sample.module.OrderDataProvider=debug -log4j.logger.org.springframework.batch.container.common.module.process.support.DefaultXmlDataProvider=debug diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcPagingFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcPagingFunctionalTests.java new file mode 100644 index 000000000..84b2b28d4 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcPagingFunctionalTests.java @@ -0,0 +1,46 @@ +/* + * 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 + * + * http://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.sample.iosample; + +import org.junit.runner.RunWith; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.sample.domain.trade.CustomerCredit; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Dan Garrette + * @since 2.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml", + "/jobs/iosample/jdbcPaging.xml" }) +public class JdbcPagingFunctionalTests extends AbstractIoSampleTests { + + @Override + protected void pointReaderToOutput(ItemReader reader) { + // no-op + } + + @Override + protected JobParameters getUniqueJobParameters() { + return new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 10000.).toJobParameters(); + } + +} diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesDelimitedFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesDelimitedFunctionalTests.java new file mode 100644 index 000000000..bbc3d5d3a --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesDelimitedFunctionalTests.java @@ -0,0 +1,94 @@ +/* + * 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 + * + * http://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.sample.iosample; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; + +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.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.job.AbstractJob; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.file.FlatFileItemReader; +import org.springframework.batch.sample.domain.trade.CustomerCredit; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Dave Syer + * @since 2.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml", + "/jobs/iosample/delimited.xml" }) +public class TwoJobInstancesDelimitedFunctionalTests { + + @Autowired + private JobLauncher launcher; + + @Autowired + private AbstractJob job; + + @Autowired + private FlatFileItemReader reader; + + @Autowired + private Resource outputResource; + + @Test + public void testLaunchJobTwice() throws Exception { + JobExecution jobExecution = launcher.run(this.job, getJobParameters("data/iosample/input/delimited.csv")); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + verifyOutput(6); + jobExecution = launcher.run(this.job, getJobParameters("data/iosample/input/delimited2.csv")); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + verifyOutput(2); + } + + private void verifyOutput(int expected) throws Exception { + + reader.setResource(outputResource); + reader.open(new ExecutionContext()); + + int count = 0; + try { + while (reader.read() != null) { + count++; + } + } + finally { + reader.close(); + } + + assertEquals(expected, count); + + } + + protected JobParameters getJobParameters(String fileName) { + return new JobParametersBuilder().addLong("timestamp", new Date().getTime()).addString("fileName", fileName) + .toJobParameters(); + } + +} \ No newline at end of file diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java new file mode 100644 index 000000000..1c89e9a11 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/TwoJobInstancesPagingFunctionalTests.java @@ -0,0 +1,65 @@ +/* + * 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 + * + * http://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.sample.iosample; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; + +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.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.job.AbstractJob; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Dave Syer + * @since 2.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml", + "/jobs/iosample/jdbcPaging.xml" }) +public class TwoJobInstancesPagingFunctionalTests { + + @Autowired + private JobLauncher launcher; + + @Autowired + private AbstractJob job; + + @Test + public void testLaunchJobTwice() throws Exception { + JobExecution jobExecution = launcher.run(this.job, getJobParameters(1000.)); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + assertEquals(4, jobExecution.getStepExecutions().iterator().next().getWriteCount()); + jobExecution = launcher.run(this.job, getJobParameters(1000000.)); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + assertEquals(0, jobExecution.getStepExecutions().iterator().next().getWriteCount()); + } + + protected JobParameters getJobParameters(double amount) { + return new JobParametersBuilder().addLong("timestamp", new Date().getTime()).addDouble("credit", amount) + .toJobParameters(); + } + +} \ No newline at end of file