From 55874a84fad385185bc24df3975bd9fb78df5322 Mon Sep 17 00:00:00 2001 From: trisberg Date: Wed, 30 Jul 2008 16:00:49 +0000 Subject: [PATCH] IN PROGRESS - BATCH-710: updated tests to use SpringJUnit4ClassRunner and SimpleJdbcTemplate --- .../item/adapter/ItemReaderAdapterTests.java | 29 ++-- .../item/adapter/ItemWriterAdapterTests.java | 30 ++--- ...egatingItemProccessorIntegrationTests.java | 29 ++-- ...tDataSourceItemReaderIntegrationTests.java | 124 ++++++++++-------- ...rnateCursorItemReaderIntegrationTests.java | 9 +- ...sorItemReaderStatefulIntegrationTests.java | 5 + .../IbatisItemReaderIntegrationTests.java | 7 +- .../JdbcCursorItemReaderIntegrationTests.java | 7 +- 8 files changed, 133 insertions(+), 107 deletions(-) diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemReaderAdapterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemReaderAdapterTests.java index 7b0a1616c..b512d6e5a 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemReaderAdapterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemReaderAdapterTests.java @@ -1,30 +1,37 @@ package org.springframework.batch.item.adapter; +import static org.junit.Assert.*; + import java.util.ArrayList; import java.util.List; import org.springframework.batch.item.sample.Foo; import org.springframework.batch.item.sample.FooService; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.junit.Test; +import org.junit.runner.RunWith; /** * Tests for {@link ItemReaderAdapter}. * * @author Robert Kasanicky */ -public class ItemReaderAdapterTests extends AbstractDependencyInjectionSpringContextTests { +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "delegating-item-provider.xml") +public class ItemReaderAdapterTests { + @Autowired private ItemReaderAdapter provider; + @Autowired private FooService fooService; - protected String getConfigPath() { - return "delegating-item-provider.xml"; - } - - /** + /* * Regular usage scenario - items are retrieved from the service injected invoker points to. */ + @Test public void testNext() throws Exception { List returnedItems = new ArrayList(); Object item; @@ -41,12 +48,4 @@ public class ItemReaderAdapterTests extends AbstractDependencyInjectionSpringCon } } - public void setProvider(ItemReaderAdapter provider) { - this.provider = provider; - } - - public void setFooService(FooService fooService) { - this.fooService = fooService; - } - } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemWriterAdapterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemWriterAdapterTests.java index 549b6e63c..f2f4c9f8a 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemWriterAdapterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/ItemWriterAdapterTests.java @@ -1,30 +1,37 @@ package org.springframework.batch.item.adapter; +import static org.junit.Assert.*; + import java.util.List; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.sample.Foo; import org.springframework.batch.item.sample.FooService; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.junit.runner.RunWith; +import org.junit.Test; /** * Tests for {@link ItemWriterAdapter}. * * @author Robert Kasanicky */ -public class ItemWriterAdapterTests extends AbstractDependencyInjectionSpringContextTests { +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "delegating-item-writer.xml") +public class ItemWriterAdapterTests { + @Autowired private ItemWriter processor; + @Autowired private FooService fooService; - protected String getConfigPath() { - return "delegating-item-writer.xml"; - } - - /** + /* * Regular usage scenario - input object should be passed to the service the injected invoker points to. */ + @Test public void testProcess() throws Exception { Foo foo; while ((foo = fooService.generateFoo()) != null) { @@ -42,13 +49,4 @@ public class ItemWriterAdapterTests extends AbstractDependencyInjectionSpringCon } - // setter for auto-injection - public void setProcessor(ItemWriter processor) { - this.processor = processor; - } - - // setter for auto-injection - public void setFooService(FooService fooService) { - this.fooService = fooService; - } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/PropertyExtractingDelegatingItemProccessorIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/PropertyExtractingDelegatingItemProccessorIntegrationTests.java index a33c22225..7703786c1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/PropertyExtractingDelegatingItemProccessorIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/adapter/PropertyExtractingDelegatingItemProccessorIntegrationTests.java @@ -1,30 +1,37 @@ package org.springframework.batch.item.adapter; +import static org.junit.Assert.*; +import org.junit.runner.RunWith; +import org.junit.Test; + import java.util.List; import org.springframework.batch.item.sample.Foo; import org.springframework.batch.item.sample.FooService; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.beans.factory.annotation.Autowired; /** * Tests for {@link PropertyExtractingDelegatingItemWriter} * * @author Robert Kasanicky */ -public class PropertyExtractingDelegatingItemProccessorIntegrationTests extends - AbstractDependencyInjectionSpringContextTests { +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "pe-delegating-item-writer.xml") +public class PropertyExtractingDelegatingItemProccessorIntegrationTests { + @Autowired private PropertyExtractingDelegatingItemWriter processor; + @Autowired private FooService fooService; - protected String getConfigPath() { - return "pe-delegating-item-writer.xml"; - } - - /** + /* * Regular usage scenario - input object should be passed to the service the injected invoker points to. */ + @Test public void testProcess() throws Exception { Foo foo; while ((foo = fooService.generateFoo()) != null) { @@ -46,12 +53,4 @@ public class PropertyExtractingDelegatingItemProccessorIntegrationTests extends } - public void setProcessor(PropertyExtractingDelegatingItemWriter processor) { - this.processor = processor; - } - - public void setFooService(FooService fooService) { - this.fooService = fooService; - } - } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java index c30e64804..796639abe 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractDataSourceItemReaderIntegrationTests.java @@ -1,12 +1,20 @@ package org.springframework.batch.item.database; +import static org.junit.Assert.*; + import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.sample.Foo; import org.springframework.beans.factory.InitializingBean; -import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.util.Assert; +import org.springframework.transaction.annotation.Transactional; +import org.junit.Before; +import org.junit.Test; + +import javax.sql.DataSource; /** * Common scenarios for testing {@link ItemReader} implementations which read @@ -15,78 +23,76 @@ import org.springframework.util.Assert; * @author Lucas Ward * @author Robert Kasanicky */ -public abstract class AbstractDataSourceItemReaderIntegrationTests extends - AbstractTransactionalDataSourceSpringContextTests { +public abstract class AbstractDataSourceItemReaderIntegrationTests { protected ItemReader reader; + protected ExecutionContext executionContext; - /** - * @return configured input source ready for use - */ - protected abstract ItemReader createItemReader() throws Exception; + protected DataSource dataSource; - protected String[] getConfigLocations() { - return new String[] { "org/springframework/batch/item/database/data-source-context.xml" }; + @Autowired + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; } - /* - * (non-Javadoc) - * @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction() - */ - protected void onSetUpInTransaction() throws Exception { - super.onSetUpInTransaction(); + protected abstract ItemReader createItemReader() throws Exception; + +// protected String[] getConfigLocations() { +// return new String[] { "org/springframework/batch/item/database/data-source-context.xml" }; +// } + + @Before + public void onSetUpInTransaction() throws Exception { reader = createItemReader(); executionContext = new ExecutionContext(); } - /* - * (non-Javadoc) - * @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction() - */ - protected void onTearDownAfterTransaction() throws Exception { + @AfterTransaction + public void onTearDownAfterTransaction() throws Exception { getAsItemStream(reader).close(null); - super.onTearDownAfterTransaction(); } - /** + /* * Regular scenario - read all rows and eventually return null. */ + @Transactional @Test public void testNormalProcessing() throws Exception { getAsInitializingBean(reader).afterPropertiesSet(); getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); assertEquals(2, foo2.getValue()); - Foo foo3 = (Foo) reader.read(); + Foo foo3 = reader.read(); assertEquals(3, foo3.getValue()); - Foo foo4 = (Foo) reader.read(); + Foo foo4 = reader.read(); assertEquals(4, foo4.getValue()); - Foo foo5 = (Foo) reader.read(); + Foo foo5 = reader.read(); assertEquals(5, foo5.getValue()); assertNull(reader.read()); } - /** + /* * Restart scenario - read records, save restart data, create new input * source and restore from restart data - the new input source should * continue where the old one finished. */ + @Transactional @Test public void testRestart() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); assertEquals(2, foo2.getValue()); getAsItemStream(reader).update(executionContext); @@ -96,21 +102,22 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends getAsItemStream(reader).open(executionContext); - Foo fooAfterRestart = (Foo) reader.read(); + Foo fooAfterRestart = reader.read(); assertEquals(3, fooAfterRestart.getValue()); } - /** + /* * Reading from an input source and then trying to restore causes an error. */ + @Transactional @Test public void testInvalidRestore() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); assertEquals(1, foo1.getValue()); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); assertEquals(2, foo2.getValue()); getAsItemStream(reader).update(executionContext); @@ -119,7 +126,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends reader = createItemReader(); getAsItemStream(reader).open(new ExecutionContext()); - Foo foo = (Foo) reader.read(); + Foo foo = reader.read(); assertEquals(1, foo.getValue()); try { @@ -131,32 +138,32 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends } } - /** + /* * Empty restart data should be handled gracefully. - * @throws Exception */ + @Transactional @Test public void testRestoreFromEmptyData() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo = (Foo) reader.read(); + Foo foo = reader.read(); assertEquals(1, foo.getValue()); } - /** + /* * Rollback scenario - input source rollbacks to last commit point. - * @throws Exception */ + @Transactional @Test public void testRollback() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); commit(); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); Assert.state(!foo2.equals(foo1)); - Foo foo3 = (Foo) reader.read(); + Foo foo3 = reader.read(); Assert.state(!foo2.equals(foo3)); rollback(); @@ -164,23 +171,23 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends assertEquals(foo2, reader.read()); } - /** + /* * Rollback scenario with restart - input source rollbacks to last * commit point. - * @throws Exception */ + @Transactional @Test public void testRollbackAndRestart() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); commit(); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); Assert.state(!foo2.equals(foo1)); - Foo foo3 = (Foo) reader.read(); + Foo foo3 = reader.read(); Assert.state(!foo2.equals(foo3)); rollback(); @@ -196,21 +203,21 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends assertEquals(foo3, reader.read()); } - /** + /* * Rollback scenario with restart - input source rollbacks to last * commit point. - * @throws Exception */ + @Transactional @Test public void testRollbackOnFirstChunkAndRestart() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); Assert.state(!foo2.equals(foo1)); - Foo foo3 = (Foo) reader.read(); + Foo foo3 = reader.read(); Assert.state(!foo2.equals(foo3)); rollback(); @@ -226,18 +233,19 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends assertEquals(foo2, reader.read()); } + @Transactional @Test public void testMultipleRestarts() throws Exception { getAsItemStream(reader).open(executionContext); - Foo foo1 = (Foo) reader.read(); + Foo foo1 = reader.read(); commit(); - Foo foo2 = (Foo) reader.read(); + Foo foo2 = reader.read(); Assert.state(!foo2.equals(foo1)); - Foo foo3 = (Foo) reader.read(); + Foo foo3 = reader.read(); Assert.state(!foo2.equals(foo3)); rollback(); @@ -261,8 +269,8 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends getAsItemStream(reader).open(executionContext); - Foo foo4 = (Foo)reader.read(); - Foo foo5 = (Foo)reader.read(); + Foo foo4 = reader.read(); + Foo foo5 = reader.read(); assertEquals(4, foo4.getValue()); assertEquals(5, foo5.getValue()); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderIntegrationTests.java index 35bb243f1..b5a6f8ece 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderIntegrationTests.java @@ -1,5 +1,8 @@ package org.springframework.batch.item.database; +import static org.junit.Assert.*; +import org.junit.runner.RunWith; + import org.hibernate.SessionFactory; import org.hibernate.StatelessSession; import org.springframework.batch.item.ExecutionContext; @@ -8,17 +11,21 @@ import org.springframework.batch.item.sample.Foo; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.orm.hibernate3.LocalSessionFactoryBean; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; /** * Tests for {@link HibernateCursorItemReader} using {@link StatelessSession}. * * @author Robert Kasanicky */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "data-source-context.xml") public class HibernateCursorItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests { protected ItemReader createItemReader() throws Exception { LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); - factoryBean.setDataSource(super.getJdbcTemplate().getDataSource()); + factoryBean.setDataSource(dataSource); factoryBean.setMappingLocations(new Resource[] { new ClassPathResource("Foo.hbm.xml", getClass()) }); factoryBean.afterPropertiesSet(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderStatefulIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderStatefulIntegrationTests.java index 1a5f3e46f..8b0657230 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderStatefulIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/HibernateCursorItemReaderStatefulIntegrationTests.java @@ -6,12 +6,17 @@ import org.hibernate.SessionFactory; import org.hibernate.classic.Session; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.sample.Foo; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; +import org.junit.runner.RunWith; /** * Tests for {@link HibernateCursorItemReader} using standard hibernate {@link Session}. * * @author Robert Kasanicky */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "data-source-context.xml") public class HibernateCursorItemReaderStatefulIntegrationTests extends HibernateCursorItemReaderIntegrationTests { protected boolean isUseStatelessSession() { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisItemReaderIntegrationTests.java index 966d89489..668cf2dde 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisItemReaderIntegrationTests.java @@ -5,6 +5,9 @@ import org.springframework.batch.item.database.support.IbatisKeyCollector; import org.springframework.batch.item.sample.Foo; import org.springframework.core.io.ClassPathResource; import org.springframework.orm.ibatis.SqlMapClientFactoryBean; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; +import org.junit.runner.RunWith; import com.ibatis.sqlmap.client.SqlMapClient; @@ -14,13 +17,15 @@ import com.ibatis.sqlmap.client.SqlMapClient; * @author Robert Kasanicky */ @SuppressWarnings("unchecked") +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "data-source-context.xml") public class IbatisItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests { protected ItemReader createItemReader() throws Exception { SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean(); factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass())); - factory.setDataSource(super.getJdbcTemplate().getDataSource()); + factory.setDataSource(dataSource); factory.afterPropertiesSet(); SqlMapClient sqlMapClient = (SqlMapClient) factory.getObject(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderIntegrationTests.java index 544ba084c..c8cf803f3 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcCursorItemReaderIntegrationTests.java @@ -2,17 +2,22 @@ package org.springframework.batch.item.database; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.sample.Foo; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.ContextConfiguration; +import org.junit.runner.RunWith; /** * Tests for {@link JdbcCursorItemReader} * * @author Robert Kasanicky */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "data-source-context.xml") public class JdbcCursorItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests { protected ItemReader createItemReader() throws Exception { JdbcCursorItemReader result = new JdbcCursorItemReader(); - result.setDataSource(super.getJdbcTemplate().getDataSource()); + result.setDataSource(dataSource); result.setSql("select ID, NAME, VALUE from T_FOOS"); result.setIgnoreWarnings(true); result.setVerifyCursorPosition(true);