From f63a275c8e30aba8903d59a42cc478631c167921 Mon Sep 17 00:00:00 2001 From: hoserdude Date: Tue, 11 Feb 2014 15:53:48 -0800 Subject: [PATCH] Adding CompositeItemProcessor use case test + gitignore, + latest milestone of batch 3.0 --- .gitignore | 17 +++ pom.xml | 26 +++++ ...letonScopedCompositeItemProcessorTest.java | 108 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 src/test/java/org/example/SingletonScopedCompositeItemProcessorTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd66b81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.sw? +.#* +*# +*~ +.classpath +.project +.settings +bin +build +target +dependency-reduced-pom.xml +*.sublime-* +/scratch +.gradle +.idea +*.iml +README.html diff --git a/pom.xml b/pom.xml index f494f99..9f9a308 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,32 @@ + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + + + + spring-snapshots + http://repo.spring.io/libs-snapshot + true + + + + + + spring-snapshots + http://repo.spring.io/libs-snapshot + true + + \ No newline at end of file diff --git a/src/test/java/org/example/SingletonScopedCompositeItemProcessorTest.java b/src/test/java/org/example/SingletonScopedCompositeItemProcessorTest.java new file mode 100644 index 0000000..734ca31 --- /dev/null +++ b/src/test/java/org/example/SingletonScopedCompositeItemProcessorTest.java @@ -0,0 +1,108 @@ +package org.example; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.support.CompositeItemProcessor; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class SingletonScopedCompositeItemProcessorTest { + @Configuration + @EnableBatchProcessing + static class TestConfig { + @Autowired + private JobBuilderFactory jobBuilder; + @Autowired + private StepBuilderFactory stepBuilder; + + @Bean + JobLauncherTestUtils jobLauncherTestUtils() { + return new JobLauncherTestUtils(); + } + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder(); + return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-hsqldb.sql") + .addScript("classpath:org/springframework/batch/core/schema-hsqldb.sql") + .setType(EmbeddedDatabaseType.HSQL) + .build(); + } + + @Bean + public Job jobUnderTest() { + return jobBuilder.get("job-under-test") + .start(stepUnderTest()) + .build(); + } + + @Bean + public Step stepUnderTest() { + return stepBuilder.get("step-under-test") + .chunk(1) + .reader(reader()) + .processor(compositeProcessor()) + .writer(writer()) + .build(); + } + + @Bean + public ItemReader reader() { + return new StatefulItemReader(); + } + + @Bean + public ItemProcessor compositeProcessor() { + CompositeItemProcessor cip = new CompositeItemProcessor(); + List> itemProcessors = new ArrayList>(); + itemProcessors.add(processor()); + itemProcessors.add(processor()); + cip.setDelegates(itemProcessors); + return cip; + } + + @Bean + public ItemProcessor processor() { + return new StatefulItemProcessor(); + } + + @Bean + public ItemWriter writer() { + return new StatefulItemWriter(); + } + } + + @Autowired + JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void testStepExecution() { + JobExecution jobExecution = jobLauncherTestUtils.launchStep("step-under-test"); + assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +}