IN PROGRESS - BATCH-710: updated tests to use SpringJUnit4ClassRunner and SimpleJdbcTemplate

This commit is contained in:
trisberg
2008-07-30 16:00:49 +00:00
parent 92192964b1
commit 55874a84fa
8 changed files with 133 additions and 107 deletions

View File

@@ -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<Foo> 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<Object> returnedItems = new ArrayList<Object>();
Object item;
@@ -41,12 +48,4 @@ public class ItemReaderAdapterTests extends AbstractDependencyInjectionSpringCon
}
}
public void setProvider(ItemReaderAdapter<Foo> provider) {
this.provider = provider;
}
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
}

View File

@@ -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<Foo> 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<Foo> processor) {
this.processor = processor;
}
// setter for auto-injection
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
}

View File

@@ -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<Foo> 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<Foo> processor) {
this.processor = processor;
}
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
}

View File

@@ -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<Foo> reader;
protected ExecutionContext executionContext;
/**
* @return configured input source ready for use
*/
protected abstract ItemReader<Foo> 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<Foo> 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());
}

View File

@@ -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<Foo> 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();

View File

@@ -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() {

View File

@@ -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<Foo> 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();

View File

@@ -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<Foo> createItemReader() throws Exception {
JdbcCursorItemReader<Foo> result = new JdbcCursorItemReader<Foo>();
result.setDataSource(super.getJdbcTemplate().getDataSource());
result.setDataSource(dataSource);
result.setSql("select ID, NAME, VALUE from T_FOOS");
result.setIgnoreWarnings(true);
result.setVerifyCursorPosition(true);