IN PROGRESS - BATCH-672: modified item.reader and item.writer tests. Upgraded entire project to EasyMock 2.4

This commit is contained in:
trisberg
2008-07-23 02:48:02 +00:00
parent 4f55f8d970
commit 3f9ce6500d
22 changed files with 314 additions and 203 deletions

View File

@@ -487,9 +487,9 @@
<version>1.8.0.7</version>
</dependency>
<dependency>
<groupId>easymock</groupId>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>1.1</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -62,7 +62,7 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>easymock</groupId>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
</dependency>
<dependency>

View File

@@ -174,7 +174,7 @@ public class JobRepositoryFactoryBeanTests extends TestCase {
// wrong meta data
fail("Expected IllegalArgumentException");
}
catch (AssertionFailedError e) {
catch (AssertionError e) {
// expected exception from txControl - wrong isolation level used in
// comparison
assertEquals("Unexpected method call", e.getMessage().substring(3, 25));
@@ -188,8 +188,14 @@ public class JobRepositoryFactoryBeanTests extends TestCase {
DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
txControl.expectAndReturn(transactionManager.getTransaction(transactionDefinition), null);
dataSourceControl.expectAndReturn(dataSource.getConnection(), MockControl.createControl(Connection.class)
.getMock());
MockControl connectionControl = MockControl.createControl(Connection.class);
Connection conn = (Connection) connectionControl.getMock();
conn.prepareStatement("SELECT JOB_INSTANCE_ID from TEST_BATCH_PREFIX_JOB_INSTANCE where JOB_NAME = ? and (JOB_KEY = ? OR JOB_KEY is NULL)");
connectionControl.setReturnValue(null);
conn.close();
connectionControl.setVoidCallable();
dataSourceControl.expectAndReturn(dataSource.getConnection(), conn);
connectionControl.replay();
dataSourceControl.replay();
txControl.replay();
try {
@@ -200,6 +206,9 @@ public class JobRepositoryFactoryBeanTests extends TestCase {
}
catch (IllegalArgumentException e) {
// expected exception from DataSourceUtils
System.out.println("******* " + e);
System.out.println("******* " + e);
System.out.println("******* " + e);
assertEquals("No Statement specified", e.getMessage());
}
}
@@ -212,8 +221,14 @@ public class JobRepositoryFactoryBeanTests extends TestCase {
DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_READ_UNCOMMITTED);
txControl.expectAndReturn(transactionManager.getTransaction(transactionDefinition), null);
dataSourceControl.expectAndReturn(dataSource.getConnection(), MockControl.createControl(Connection.class)
.getMock());
MockControl connectionControl = MockControl.createControl(Connection.class);
Connection conn = (Connection) connectionControl.getMock();
conn.prepareStatement("SELECT JOB_INSTANCE_ID from TEST_BATCH_PREFIX_JOB_INSTANCE where JOB_NAME = ? and (JOB_KEY = ? OR JOB_KEY is NULL)");
connectionControl.setReturnValue(null);
conn.close();
connectionControl.setVoidCallable();
dataSourceControl.expectAndReturn(dataSource.getConnection(), conn);
connectionControl.replay();
dataSourceControl.replay();
txControl.replay();
try {

View File

@@ -109,7 +109,7 @@
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>easymock</groupId>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
</dependency>
<dependency>

View File

@@ -145,7 +145,7 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>easymock</groupId>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
</dependency>
<dependency>

View File

@@ -135,7 +135,6 @@
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.4</version>
</dependency>
<!-- optional dependency from infrastructure -->
<dependency>

View File

@@ -18,6 +18,7 @@ import org.springframework.batch.item.ReaderNotOpenException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.sample.item.writer.StagingItemWriter;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
@@ -107,7 +108,7 @@ public class StagingItemReader<T> extends JdbcDaoSupport implements ItemStream,
}
@SuppressWarnings("unchecked")
public T read() throws Exception {
public T read() throws DataAccessException {
Long id = doRead();
if (id == null) {

View File

@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<import resource="classpath:data-source-context.xml" />
<bean id="processor"
class="org.springframework.batch.sample.item.writer.StagingItemWriter">
<property name="incrementer">
<bean id="jobIncrementer" parent="incrementerParent">
<property name="incrementerName"
value="BATCH_STAGING_SEQ" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="provider"
class="org.springframework.batch.sample.item.reader.StagingItemReader">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processor"
class="org.springframework.batch.sample.item.writer.StagingItemWriter">
<property name="incrementer">
<bean id="jobIncrementer" parent="incrementerParent">
<property name="incrementerName"
value="BATCH_STAGING_SEQ" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="provider"
class="org.springframework.batch.sample.item.reader.StagingItemReader">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

View File

@@ -1,13 +1,14 @@
package org.springframework.batch.sample.item.reader;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* Tests for {@link GeneratingItemReader}.
*
* @author Robert Kasanicky
*/
public class GeneratingItemReaderTests extends TestCase {
public class GeneratingItemReaderTests {
private GeneratingItemReader reader = new GeneratingItemReader();
@@ -15,6 +16,7 @@ public class GeneratingItemReaderTests extends TestCase {
* Generates a given number of not-null records,
* consecutive calls return null.
*/
@Test
public void testRead() throws Exception {
int counter = 0;
int limit = 10;

View File

@@ -1,10 +1,11 @@
package org.springframework.batch.sample.item.reader;
import static org.junit.Assert.*;
import static org.easymock.EasyMock.*;
import java.util.Iterator;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
@@ -15,25 +16,20 @@ import org.springframework.batch.sample.domain.Customer;
import org.springframework.batch.sample.domain.LineItem;
import org.springframework.batch.sample.domain.Order;
import org.springframework.batch.sample.domain.ShippingInfo;
import org.junit.Before;
import org.junit.Test;
public class OrderItemReaderTests extends TestCase {
public class OrderItemReaderTests {
private OrderItemReader provider;
private MockControl inputControl;
private ItemReader<FieldSet> input;
private MockControl mapperControl;
@SuppressWarnings("unchecked")
private FieldSetMapper mapper;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
inputControl = MockControl.createControl(ItemReader.class);
input = (ItemReader<FieldSet>) inputControl.getMock();
input = (ItemReader<FieldSet>) createMock(ItemReader.class);
provider = new OrderItemReader();
provider.setFieldSetReader(input);
@@ -50,6 +46,7 @@ public class OrderItemReaderTests extends TestCase {
* need create mock objects for input source, mapper and validator.
*/
@SuppressWarnings("unchecked")
@Test
public void testNext() throws Exception {
// create fieldsets and set return values for input source
@@ -63,25 +60,16 @@ public class OrderItemReaderTests extends TestCase {
FieldSet footerFS = new DefaultFieldSet(new String[] { Order.LINE_ID_FOOTER, "100", "3", "3" }, new String[] {
"ID", "TOTAL_PRICE", "TOTAL_LINE_ITEMS", "TOTAL_ITEMS" });
input.read();
inputControl.setReturnValue(headerFS);
input.read();
inputControl.setReturnValue(customerFS);
input.read();
inputControl.setReturnValue(billingFS);
input.read();
inputControl.setReturnValue(shippingFS);
input.read();
inputControl.setReturnValue(billingInfoFS);
input.read();
inputControl.setReturnValue(shippingInfoFS);
input.read();
inputControl.setReturnValue(itemFS, 3);
input.read();
inputControl.setReturnValue(footerFS);
input.read();
inputControl.setReturnValue(null);
inputControl.replay();
expect(input.read()).andReturn(headerFS);
expect(input.read()).andReturn(customerFS);
expect(input.read()).andReturn(billingFS);
expect(input.read()).andReturn(shippingFS);
expect(input.read()).andReturn(billingInfoFS);
expect(input.read()).andReturn(shippingInfoFS);
expect(input.read()).andReturn(itemFS).times(3);
expect(input.read()).andReturn(footerFS);
expect(input.read()).andReturn(null);
replay(input);
// create value objects
Order order = new Order();
@@ -93,24 +81,16 @@ public class OrderItemReaderTests extends TestCase {
LineItem item = new LineItem();
// create mock mapper
mapperControl = MockControl.createControl(FieldSetMapper.class);
mapper = (FieldSetMapper) mapperControl.getMock();
FieldSetMapper mapper = createMock(FieldSetMapper.class);
// set how mapper should respond - set return values for mapper
mapper.mapLine(headerFS, -1);
mapperControl.setReturnValue(order);
mapper.mapLine(customerFS, -1);
mapperControl.setReturnValue(customer);
mapper.mapLine(billingFS, -1);
mapperControl.setReturnValue(billing);
mapper.mapLine(shippingFS, -1);
mapperControl.setReturnValue(shipping);
mapper.mapLine(billingInfoFS, -1);
mapperControl.setReturnValue(billingInfo);
mapper.mapLine(shippingInfoFS, -1);
mapperControl.setReturnValue(shippingInfo);
mapper.mapLine(itemFS, -1);
mapperControl.setReturnValue(item, 3);
mapperControl.replay();
expect(mapper.mapLine(headerFS, -1)).andReturn(order);
expect(mapper.mapLine(customerFS, -1)).andReturn(customer);
expect(mapper.mapLine(billingFS, -1)).andReturn(billing);
expect(mapper.mapLine(shippingFS, -1)).andReturn(shipping);
expect(mapper.mapLine(billingInfoFS, -1)).andReturn(billingInfo);
expect(mapper.mapLine(shippingInfoFS, -1)).andReturn(shippingInfo);
expect(mapper.mapLine(itemFS, -1)).andReturn(item).times(3);
replay(mapper);
// set-up provider: set mappers
provider.setAddressMapper(mapper);
@@ -149,8 +129,8 @@ public class OrderItemReaderTests extends TestCase {
assertNull(provider.read());
// verify method calls on input source, mapper and validator
inputControl.verify();
mapperControl.verify();
verify(input);
verify(mapper);
}
}

View File

@@ -1,47 +1,64 @@
package org.springframework.batch.sample.item.reader;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.sample.item.writer.StagingItemWriter;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
public class StagingItemReaderTests extends AbstractTransactionalDataSourceSpringContextTests {
import javax.sql.DataSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class StagingItemReaderTests {
private JdbcOperations jdbcTemplate;
@Autowired
private PlatformTransactionManager transactionManager;
@Autowired
private StagingItemWriter writer;
@Autowired
private StagingItemReader reader;
private Long jobId = new Long(11);
private Long jobId = 11L;
public void setProcessor(StagingItemWriter writer) {
this.writer = writer;
public JdbcOperations getJdbcTemplate() {
return jdbcTemplate;
}
public void setProvider(StagingItemReader reader) {
this.reader = reader;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
protected String[] getConfigLocations() {
return new String[] { ClassUtils.addResourcePathToPackagePath(StagingItemWriter.class,
"staging-test-context.xml") };
}
/*
* (non-Javadoc)
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpBeforeTransaction()
*/
protected void onSetUpBeforeTransaction() throws Exception {
@Before
public void onSetUpBeforeTransaction() throws Exception {
StepExecution stepExecution = new StepExecution("stepName", new JobExecution(new JobInstance(jobId,
new JobParameters(), "testJob")));
reader.beforeStep(stepExecution);
writer.beforeStep(stepExecution);
}
protected void onSetUpInTransaction() throws Exception {
writer.write("FOO");
writer.write("BAR");
writer.write("SPAM");
@@ -49,105 +66,151 @@ public class StagingItemReaderTests extends AbstractTransactionalDataSourceSprin
reader.open(new ExecutionContext());
}
protected void onTearDownAfterTransaction() throws Exception {
@After
public void onTearDownAfterTransaction() throws Exception {
reader.close(null);
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
}
@Transactional @Test
public void testReaderUpdatesProcessIndicator() throws Exception {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
new Object[] { id }, String.class);
assertEquals(StagingItemWriter.NEW, before);
Object item = reader.read();
assertEquals("FOO", item);
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
new Object[] { id }, String.class);
assertEquals(StagingItemWriter.DONE, after);
}
@Transactional @Test
public void testUpdateProcessIndicatorAfterCommit() throws Exception {
testReaderUpdatesProcessIndicator();
setComplete();
endTransaction();
startNewTransaction();
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemWriter.DONE, before);
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before =
(String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
assertEquals(StagingItemWriter.DONE, before);
return null;
}
});
}
@Transactional @Test
public void testProviderRollsBackMultipleTimes() throws Exception {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
reader.mark();
setComplete();
endTransaction();
startNewTransaction();
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
new Object[] { jobId, StagingItemWriter.NEW });
assertEquals(4, count);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
new Object[] { jobId, StagingItemWriter.NEW });
assertEquals(4, count);
Object item = reader.read();
assertEquals("FOO", item);
item = reader.read();
assertEquals("BAR", item);
Object item = reader.read();
assertEquals("FOO", item);
item = reader.read();
assertEquals("BAR", item);
transactionStatus.setRollbackOnly();
return null;
}
});
reader.reset();
endTransaction();
startNewTransaction();
item = reader.read();
assertEquals("FOO", item);
item = reader.read();
assertEquals("BAR", item);
item = reader.read();
assertEquals("SPAM", item);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
Object item = reader.read();
assertEquals("FOO", item);
item = reader.read();
assertEquals("BAR", item);
item = reader.read();
assertEquals("SPAM", item);
transactionStatus.setRollbackOnly();
return null;
}
});
reader.reset();
endTransaction();
startNewTransaction();
item = reader.read();
assertEquals("FOO", item);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
Object item = reader.read();
assertEquals("FOO", item);
transactionStatus.setRollbackOnly();
return null;
}
});
}
@Transactional @Test
public void testProviderRollsBackProcessIndicator() throws Exception {
TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
reader.mark();
setComplete();
endTransaction();
startNewTransaction();
// After a rollback we have to resynchronize the TX to simulate a real
// batch
final Long idToUse = (Long)txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemWriter.NEW, before);
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
new Object[] { jobId });
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { id }, String.class);
assertEquals(StagingItemWriter.NEW, before);
Object item = reader.read();
assertEquals("FOO", item);
Object item = reader.read();
assertEquals("FOO", item);
transactionStatus.setRollbackOnly();
return id;
}
});
reader.reset();
endTransaction();
startNewTransaction();
// After a rollback we have to resynchronize the TX to simulate a real
// batch
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus transactionStatus) {
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { new Long(id) }, String.class);
assertEquals(StagingItemWriter.NEW, after);
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
new Object[] { idToUse }, String.class);
assertEquals(StagingItemWriter.NEW, after);
Object item = reader.read();
assertEquals("FOO", item);
transactionStatus.setRollbackOnly();
return null;
}
});
item = reader.read();
assertEquals("FOO", item);
}
}

View File

@@ -15,19 +15,21 @@
*/
package org.springframework.batch.sample.item.writer;
import java.math.BigDecimal;
import static org.junit.Assert.*;
import junit.framework.TestCase;
import java.math.BigDecimal;
import org.easymock.MockControl;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.CustomerCredit;
import org.junit.Before;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
public class BatchSqlCustomerCreditIncreaseWriterTests {
private BatchSqlCustomerCreditIncreaseWriter writer = new BatchSqlCustomerCreditIncreaseWriter();
@@ -37,11 +39,8 @@ public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
private CustomerCredit customerCredit;
/*
* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
delegate = (ItemWriter) control.getMock();
writer.setDelegate(delegate);
customerCredit = new CustomerCredit();
@@ -50,6 +49,7 @@ public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
customerCredit.setName("foo");
}
@Test
public void testAfterPropertiesSet() throws Exception {
try {
writer.afterPropertiesSet();
@@ -66,6 +66,7 @@ public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#write(java.lang.Object)}.
* @throws Exception
*/
@Test
public void testWrite() throws Exception {
delegate.write(customerCredit);
control.setVoidCallable();
@@ -78,6 +79,7 @@ public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
* Test method for
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#clear()}.
*/
@Test
public void testClear() {
delegate.clear();
control.setVoidCallable();
@@ -90,6 +92,7 @@ public class BatchSqlCustomerCreditIncreaseWriterTests extends TestCase {
* Test method for
* {@link org.springframework.batch.sample.item.writer.BatchSqlCustomerCreditIncreaseWriter#flush()}.
*/
@Test
public void testFlush() {
delegate.flush();
control.setVoidCallable();

View File

@@ -1,8 +1,9 @@
package org.springframework.batch.sample.item.writer;
import java.math.BigDecimal;
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
import java.math.BigDecimal;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
@@ -12,13 +13,14 @@ import org.springframework.batch.sample.domain.CustomerCredit;
*
* @author Robert Kasanicky
*/
public class CustomerCreditIncreaseProcessorTests extends TestCase {
public class CustomerCreditIncreaseProcessorTests {
private CustomerCreditIncreaseWriter writer = new CustomerCreditIncreaseWriter();
/**
* Increases customer's credit by fixed value
*/
@Test
public void testProcess() throws Exception {
final BigDecimal oldCredit = new BigDecimal(10.54);

View File

@@ -19,16 +19,16 @@ import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.sample.domain.CustomerCredit;
import org.junit.Before;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class CustomerCreditUpdatePreparedStatementSetterTests extends TestCase {
public class CustomerCreditUpdatePreparedStatementSetterTests {
private CustomerCreditUpdatePreparedStatementSetter setter = new CustomerCreditUpdatePreparedStatementSetter();
@@ -38,10 +38,8 @@ public class CustomerCreditUpdatePreparedStatementSetterTests extends TestCase {
private MockControl control = MockControl.createControl(PreparedStatement.class);
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
@Before
public void setUp() throws Exception {
ps = (PreparedStatement) control.getMock();
credit = new CustomerCredit();
credit.setId(13);
@@ -52,6 +50,7 @@ public class CustomerCreditUpdatePreparedStatementSetterTests extends TestCase {
* Test method for {@link org.springframework.batch.sample.item.writer.CustomerCreditUpdatePreparedStatementSetter#setValues(java.lang.Object, java.sql.PreparedStatement)}.
* @throws SQLException
*/
@Test
public void testSetValues() throws SQLException {
ps.setBigDecimal(1, credit.getCredit());
control.setVoidCallable();

View File

@@ -2,20 +2,20 @@ package org.springframework.batch.sample.item.writer;
import java.math.BigDecimal;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
import org.springframework.batch.sample.item.writer.CustomerCreditUpdateWriter;
import org.junit.Before;
import org.junit.Test;
public class CustomerCreditUpdateProcessorTests extends TestCase {
public class CustomerCreditUpdateProcessorTests {
private MockControl daoControl;
private CustomerCreditDao dao;
private CustomerCreditUpdateWriter writer;
private static final double CREDIT_FILTER = 355.0;
@Before
public void setUp() {
//create mock writer
daoControl = MockControl.createControl(CustomerCreditDao.class);
@@ -26,6 +26,7 @@ public class CustomerCreditUpdateProcessorTests extends TestCase {
writer.setCreditFilter(CREDIT_FILTER);
}
@Test
public void testProcess() throws Exception {
//set-up mock writer - no writer's method should be called

View File

@@ -1,17 +1,17 @@
package org.springframework.batch.sample.item.writer;
import static org.junit.Assert.*;
import org.junit.Test;
import java.math.BigDecimal;
import junit.framework.TestCase;
import org.springframework.batch.sample.dao.JdbcCustomerDebitDao;
import org.springframework.batch.sample.dao.CustomerDebitDao;
import org.springframework.batch.sample.domain.CustomerDebit;
import org.springframework.batch.sample.domain.Trade;
import org.springframework.batch.sample.item.writer.CustomerUpdateWriter;
public class CustomerUpdateProcessorTests extends TestCase {
public class CustomerUpdateProcessorTests {
@Test
public void testProcess() {
//create trade object

View File

@@ -1,19 +1,21 @@
package org.springframework.batch.sample.item.writer;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.easymock.MockControl;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.Order;
import org.springframework.batch.sample.item.writer.OrderWriter;
public class OrderWriterTests extends TestCase {
public class OrderWriterTests {
private MockControl writerControl;
private OrderWriter processor;
private ItemWriter writer;
@Before
public void setUp() {
//create mock writer
@@ -25,6 +27,7 @@ public class OrderWriterTests extends TestCase {
processor.setDelegate(writer);
}
@Test
public void testProcess() throws Exception {
Order order = new Order();
@@ -39,6 +42,7 @@ public class OrderWriterTests extends TestCase {
writerControl.verify();
}
@Test
public void testProcessWithException() throws Exception {
writerControl.replay();

View File

@@ -1,19 +1,21 @@
package org.springframework.batch.sample.item.writer;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Tests for {@link RetrySampleItemWriter}.
*
* @author Robert Kasanicky
*/
public class RetrySampleItemWriterTests extends TestCase {
public class RetrySampleItemWriterTests {
private RetrySampleItemWriter processor = new RetrySampleItemWriter();
/**
* Processing throws exception on 2nd and 3rd call.
*/
@Test
public void testProcess() throws Exception {
Object item = null;
processor.write(item);

View File

@@ -15,36 +15,53 @@
*/
package org.springframework.batch.sample.item.writer;
import static org.junit.Assert.assertEquals;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.util.ClassUtils;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.junit.Test;
public class StagingItemWriterTests extends AbstractTransactionalDataSourceSpringContextTests {
import javax.sql.DataSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class StagingItemWriterTests {
private JdbcOperations jdbcTemplate;
@Autowired
private StagingItemWriter writer;
public void setWriter(StagingItemWriter processor) {
this.writer = processor;
public JdbcOperations getJdbcTemplate() {
return jdbcTemplate;
}
protected String[] getConfigLocations() {
return new String[] { ClassUtils.addResourcePathToPackagePath(StagingItemWriter.class,
"staging-test-context.xml") };
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/*
* (non-Javadoc)
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpBeforeTransaction()
*/
protected void onSetUpBeforeTransaction() throws Exception {
@Before
public void onSetUpBeforeTransaction() throws Exception {
StepExecution stepExecution = new StepExecution("stepName", new JobExecution(new JobInstance(new Long(12L),
new JobParameters(), "testJob")));
writer.beforeStep(stepExecution);
}
@Transactional
@Test
public void testProcessInsertsNewItem() throws Exception {
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
writer.write("FOO");

View File

@@ -1,18 +1,19 @@
package org.springframework.batch.sample.item.writer;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.sample.dao.TradeDao;
import org.springframework.batch.sample.domain.Trade;
import org.springframework.batch.sample.item.writer.TradeWriter;
import org.junit.Before;
import org.junit.Test;
public class TradeProcessorTests extends TestCase {
public class TradeProcessorTests {
private MockControl writerControl;
private TradeDao writer;
private TradeWriter processor;
@Before
public void setUp() {
//create mock writer
@@ -24,6 +25,7 @@ public class TradeProcessorTests extends TestCase {
processor.setDao(writer);
}
@Test
public void testProcess() {
Trade trade = new Trade();
@@ -38,6 +40,7 @@ public class TradeProcessorTests extends TestCase {
writerControl.verify();
}
@Test
public void testProcessNonTradeObject() {
writerControl.replay();

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<import resource="classpath:staging-test-context.xml"/>
</beans>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<import resource="classpath:staging-test-context.xml"/>
</beans>