Change svn:eol-style to LF
This commit is contained in:
@@ -1,141 +1,141 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.item.writer.CustomerCreditIncreaseWriter;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* Test case for jobs that are expected to update customer credit value by fixed
|
||||
* amount.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
protected JdbcOperations jdbcTemplate;
|
||||
|
||||
protected PlatformTransactionManager transactionManager;
|
||||
|
||||
private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseWriter.FIXED_AMOUNT;
|
||||
|
||||
private static String[] customers = { "INSERT INTO customer (id, version, name, credit) VALUES (1, 0, 'customer1', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (2, 0, 'customer2', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (3, 0, 'customer3', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (4, 0, 'customer4', 100000)"};
|
||||
|
||||
private static String DELETE_CUSTOMERS = "DELETE FROM customer";
|
||||
|
||||
private static final String ALL_CUSTOMERS = "select * from CUSTOMER order by ID";
|
||||
|
||||
private static final String CREDIT_COLUMN = "CREDIT";
|
||||
|
||||
protected static final String ID_COLUMN = "ID";
|
||||
|
||||
private List creditsBeforeUpdate;
|
||||
|
||||
/**
|
||||
* @param jdbcTemplate
|
||||
*/
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the PlatformTransactionManager.
|
||||
* @param transactionManager the transactionManager to set
|
||||
*/
|
||||
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* All customers have the same credit
|
||||
*/
|
||||
protected void validatePreConditions() throws Exception {
|
||||
super.validatePreConditions();
|
||||
ensureState();
|
||||
creditsBeforeUpdate = (List) new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
|
||||
return jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return rs.getBigDecimal(CREDIT_COLUMN);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the state of the database is accurate by delete all the contents of the
|
||||
* customer table and reading the expected defaults.
|
||||
*/
|
||||
private void ensureState(){
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback(){
|
||||
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
jdbcTemplate.update(DELETE_CUSTOMERS);
|
||||
for(int i = 0; i < customers.length;i++){
|
||||
jdbcTemplate.update(customers[i]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Credit was increased by CREDIT_INCREASE
|
||||
*/
|
||||
protected void validatePostConditions() throws Exception {
|
||||
|
||||
final List matches = new ArrayList();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() {
|
||||
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
final BigDecimal creditBeforeUpdate = (BigDecimal) creditsBeforeUpdate.get(rowNum);
|
||||
final BigDecimal expectedCredit = creditBeforeUpdate.add(CREDIT_INCREASE);
|
||||
if (expectedCredit.equals(rs.getBigDecimal(CREDIT_COLUMN))) {
|
||||
matches.add(rs.getBigDecimal(ID_COLUMN));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(getExpectedMatches(), matches.size());
|
||||
checkMatches(matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param matches
|
||||
*/
|
||||
protected void checkMatches(List matches) {
|
||||
// no-op...
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the expected number of matches in the updated credits.
|
||||
*/
|
||||
protected int getExpectedMatches() {
|
||||
return creditsBeforeUpdate.size();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.item.writer.CustomerCreditIncreaseWriter;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* Test case for jobs that are expected to update customer credit value by fixed
|
||||
* amount.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AbstractCustomerCreditIncreaseTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
protected JdbcOperations jdbcTemplate;
|
||||
|
||||
protected PlatformTransactionManager transactionManager;
|
||||
|
||||
private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseWriter.FIXED_AMOUNT;
|
||||
|
||||
private static String[] customers = { "INSERT INTO customer (id, version, name, credit) VALUES (1, 0, 'customer1', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (2, 0, 'customer2', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (3, 0, 'customer3', 100000)",
|
||||
"INSERT INTO customer (id, version, name, credit) VALUES (4, 0, 'customer4', 100000)"};
|
||||
|
||||
private static String DELETE_CUSTOMERS = "DELETE FROM customer";
|
||||
|
||||
private static final String ALL_CUSTOMERS = "select * from CUSTOMER order by ID";
|
||||
|
||||
private static final String CREDIT_COLUMN = "CREDIT";
|
||||
|
||||
protected static final String ID_COLUMN = "ID";
|
||||
|
||||
private List creditsBeforeUpdate;
|
||||
|
||||
/**
|
||||
* @param jdbcTemplate
|
||||
*/
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the PlatformTransactionManager.
|
||||
* @param transactionManager the transactionManager to set
|
||||
*/
|
||||
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* All customers have the same credit
|
||||
*/
|
||||
protected void validatePreConditions() throws Exception {
|
||||
super.validatePreConditions();
|
||||
ensureState();
|
||||
creditsBeforeUpdate = (List) new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
|
||||
return jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() {
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return rs.getBigDecimal(CREDIT_COLUMN);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the state of the database is accurate by delete all the contents of the
|
||||
* customer table and reading the expected defaults.
|
||||
*/
|
||||
private void ensureState(){
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback(){
|
||||
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
jdbcTemplate.update(DELETE_CUSTOMERS);
|
||||
for(int i = 0; i < customers.length;i++){
|
||||
jdbcTemplate.update(customers[i]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Credit was increased by CREDIT_INCREASE
|
||||
*/
|
||||
protected void validatePostConditions() throws Exception {
|
||||
|
||||
final List matches = new ArrayList();
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
jdbcTemplate.query(ALL_CUSTOMERS, new RowMapper() {
|
||||
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
final BigDecimal creditBeforeUpdate = (BigDecimal) creditsBeforeUpdate.get(rowNum);
|
||||
final BigDecimal expectedCredit = creditBeforeUpdate.add(CREDIT_INCREASE);
|
||||
if (expectedCredit.equals(rs.getBigDecimal(CREDIT_COLUMN))) {
|
||||
matches.add(rs.getBigDecimal(ID_COLUMN));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(getExpectedMatches(), matches.size());
|
||||
checkMatches(matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param matches
|
||||
*/
|
||||
protected void checkMatches(List matches) {
|
||||
// no-op...
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the expected number of matches in the updated credits.
|
||||
*/
|
||||
protected int getExpectedMatches() {
|
||||
return creditsBeforeUpdate.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
/**
|
||||
* Test for BatchSqlUpdateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class BatchSqlUpdateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
/**
|
||||
* Test for BatchSqlUpdateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class BatchSqlUpdateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,91 +1,91 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
|
||||
|
||||
public class CompositeProcessorSampleFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade order by isin";
|
||||
|
||||
private static final String EXPECTED_OUTPUT_FILE =
|
||||
"Trade: [isin=UK21341EAH41,quantity=211,price=31.11,customer=customer1]" +
|
||||
"Trade: [isin=UK21341EAH42,quantity=212,price=32.11,customer=customer2]" +
|
||||
"Trade: [isin=UK21341EAH43,quantity=213,price=33.11,customer=customer3]" +
|
||||
"Trade: [isin=UK21341EAH44,quantity=214,price=34.11,customer=customer4]" +
|
||||
"Trade: [isin=UK21341EAH45,quantity=215,price=35.11,customer=customer5]";
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
private int activeRow = 0;
|
||||
|
||||
private int before;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.sample.AbstractLifecycleSpringContextTests#validatePreConditions()
|
||||
*/
|
||||
protected void validatePreConditions() throws Exception {
|
||||
jdbcTemplate.update("DELETE from TRADE");
|
||||
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
checkOutputFile();
|
||||
checkOutputTable();
|
||||
}
|
||||
|
||||
private void checkOutputTable() {
|
||||
final List trades = new ArrayList() {{
|
||||
add(new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1"));
|
||||
add(new Trade("UK21341EAH42", 212, new BigDecimal("32.11"), "customer2"));
|
||||
add(new Trade("UK21341EAH43", 213, new BigDecimal("33.11"), "customer3"));
|
||||
add(new Trade("UK21341EAH44", 214, new BigDecimal("34.11"), "customer4"));
|
||||
add(new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5"));
|
||||
}};
|
||||
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
|
||||
assertEquals(before+5, after);
|
||||
|
||||
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Trade trade = (Trade)trades.get(activeRow++);
|
||||
|
||||
assertEquals(trade.getIsin(), rs.getString(1));
|
||||
assertEquals(trade.getQuantity(), rs.getLong(2));
|
||||
assertEquals(trade.getPrice(), rs.getBigDecimal(3));
|
||||
assertEquals(trade.getCustomer(), rs.getString(4));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void checkOutputFile() throws FileNotFoundException, IOException {
|
||||
List outputLines = IOUtils.readLines(
|
||||
new FileInputStream("target/test-outputs/20070122.testStream.ParallelCustomerReportStep.TEMP.txt"));
|
||||
|
||||
String output = "";
|
||||
for (Iterator iterator = outputLines.listIterator(); iterator.hasNext();) {
|
||||
String line = (String) iterator.next();
|
||||
output += line;
|
||||
}
|
||||
|
||||
assertEquals(EXPECTED_OUTPUT_FILE, output);
|
||||
}
|
||||
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
|
||||
|
||||
public class CompositeProcessorSampleFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade order by isin";
|
||||
|
||||
private static final String EXPECTED_OUTPUT_FILE =
|
||||
"Trade: [isin=UK21341EAH41,quantity=211,price=31.11,customer=customer1]" +
|
||||
"Trade: [isin=UK21341EAH42,quantity=212,price=32.11,customer=customer2]" +
|
||||
"Trade: [isin=UK21341EAH43,quantity=213,price=33.11,customer=customer3]" +
|
||||
"Trade: [isin=UK21341EAH44,quantity=214,price=34.11,customer=customer4]" +
|
||||
"Trade: [isin=UK21341EAH45,quantity=215,price=35.11,customer=customer5]";
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
private int activeRow = 0;
|
||||
|
||||
private int before;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.sample.AbstractLifecycleSpringContextTests#validatePreConditions()
|
||||
*/
|
||||
protected void validatePreConditions() throws Exception {
|
||||
jdbcTemplate.update("DELETE from TRADE");
|
||||
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
checkOutputFile();
|
||||
checkOutputTable();
|
||||
}
|
||||
|
||||
private void checkOutputTable() {
|
||||
final List trades = new ArrayList() {{
|
||||
add(new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1"));
|
||||
add(new Trade("UK21341EAH42", 212, new BigDecimal("32.11"), "customer2"));
|
||||
add(new Trade("UK21341EAH43", 213, new BigDecimal("33.11"), "customer3"));
|
||||
add(new Trade("UK21341EAH44", 214, new BigDecimal("34.11"), "customer4"));
|
||||
add(new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5"));
|
||||
}};
|
||||
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
|
||||
assertEquals(before+5, after);
|
||||
|
||||
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Trade trade = (Trade)trades.get(activeRow++);
|
||||
|
||||
assertEquals(trade.getIsin(), rs.getString(1));
|
||||
assertEquals(trade.getQuantity(), rs.getLong(2));
|
||||
assertEquals(trade.getPrice(), rs.getBigDecimal(3));
|
||||
assertEquals(trade.getCustomer(), rs.getString(4));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void checkOutputFile() throws FileNotFoundException, IOException {
|
||||
List outputLines = IOUtils.readLines(
|
||||
new FileInputStream("target/test-outputs/20070122.testStream.ParallelCustomerReportStep.TEMP.txt"));
|
||||
|
||||
String output = "";
|
||||
for (Iterator iterator = outputLines.listIterator(); iterator.hasNext();) {
|
||||
String line = (String) iterator.next();
|
||||
output += line;
|
||||
}
|
||||
|
||||
assertEquals(EXPECTED_OUTPUT_FILE, output);
|
||||
}
|
||||
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,96 +1,96 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.core.configuration.ListableJobRegistry;
|
||||
import org.springframework.batch.core.domain.Job;
|
||||
import org.springframework.batch.core.repository.NoSuchJobException;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.PropertyAccessorUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class DefaultJobLoader implements JobLoader,
|
||||
ApplicationContextAware {
|
||||
|
||||
private ListableJobRegistry registry;
|
||||
private ApplicationContext applicationContext;
|
||||
private Map configurations = new HashMap();
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public void setRegistry(ListableJobRegistry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public Map getConfigurations() {
|
||||
Map result = new HashMap(configurations);
|
||||
for (Iterator iterator = registry.getJobNames().iterator(); iterator
|
||||
.hasNext();) {
|
||||
try {
|
||||
Job configuration = (Job) registry.getJob((String) iterator.next());
|
||||
String name = configuration.getName();
|
||||
if (!configurations.containsKey(name)) {
|
||||
result.put(name, "<unknown path>: " + configuration);
|
||||
}
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
throw new IllegalStateException("Registry could not locate its own job (NoSuchJobException).");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadResource(String path) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
new String[] { path }, applicationContext);
|
||||
String[] names = context.getBeanNamesForType(Job.class);
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
String name = names[i];
|
||||
configurations.put(name, path);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getJobConfiguration(String name) {
|
||||
try {
|
||||
return registry.getJob(name);
|
||||
} catch (NoSuchJobException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getProperty(String path) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
return wrapper.getPropertyValue(key);
|
||||
}
|
||||
|
||||
public void setProperty(String path, String value) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
wrapper.setPropertyValue(key, value);
|
||||
}
|
||||
|
||||
private BeanWrapperImpl createBeanWrapper(String path, int index) {
|
||||
Assert.state(index>0, "Path must be nested, e.g. bean.value");
|
||||
String name = path.substring(0,index);
|
||||
Object bean = getJobConfiguration(name);
|
||||
Assert.notNull(bean, "No JobConfiguration exists with name="+name);
|
||||
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.core.configuration.ListableJobRegistry;
|
||||
import org.springframework.batch.core.domain.Job;
|
||||
import org.springframework.batch.core.repository.NoSuchJobException;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.PropertyAccessorUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class DefaultJobLoader implements JobLoader,
|
||||
ApplicationContextAware {
|
||||
|
||||
private ListableJobRegistry registry;
|
||||
private ApplicationContext applicationContext;
|
||||
private Map configurations = new HashMap();
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public void setRegistry(ListableJobRegistry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public Map getConfigurations() {
|
||||
Map result = new HashMap(configurations);
|
||||
for (Iterator iterator = registry.getJobNames().iterator(); iterator
|
||||
.hasNext();) {
|
||||
try {
|
||||
Job configuration = (Job) registry.getJob((String) iterator.next());
|
||||
String name = configuration.getName();
|
||||
if (!configurations.containsKey(name)) {
|
||||
result.put(name, "<unknown path>: " + configuration);
|
||||
}
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
throw new IllegalStateException("Registry could not locate its own job (NoSuchJobException).");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadResource(String path) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
new String[] { path }, applicationContext);
|
||||
String[] names = context.getBeanNamesForType(Job.class);
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
String name = names[i];
|
||||
configurations.put(name, path);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getJobConfiguration(String name) {
|
||||
try {
|
||||
return registry.getJob(name);
|
||||
} catch (NoSuchJobException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getProperty(String path) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
return wrapper.getPropertyValue(key);
|
||||
}
|
||||
|
||||
public void setProperty(String path, String value) {
|
||||
int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path);
|
||||
BeanWrapperImpl wrapper = createBeanWrapper(path, index);
|
||||
String key = path.substring(index+1);
|
||||
wrapper.setPropertyValue(key, value);
|
||||
}
|
||||
|
||||
private BeanWrapperImpl createBeanWrapper(String path, int index) {
|
||||
Assert.state(index>0, "Path must be nested, e.g. bean.value");
|
||||
String name = path.substring(0,index);
|
||||
Object bean = getJobConfiguration(name);
|
||||
Assert.notNull(bean, "No JobConfiguration exists with name="+name);
|
||||
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.sample.domain.PersonService;
|
||||
|
||||
public class DelegatingJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private PersonService personService;
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertTrue(personService.getReturnedCount() > 0);
|
||||
assertEquals(personService.getReturnedCount(), personService.getReceivedCount());
|
||||
|
||||
}
|
||||
|
||||
// setter for auto-injection
|
||||
public void setPersonService(PersonService personService) {
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.sample.domain.PersonService;
|
||||
|
||||
public class DelegatingJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private PersonService personService;
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertTrue(personService.getReturnedCount() > 0);
|
||||
assertEquals(personService.getReturnedCount(), personService.getReceivedCount());
|
||||
|
||||
}
|
||||
|
||||
// setter for auto-injection
|
||||
public void setPersonService(PersonService personService) {
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface ExportedJobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
String getJob(String path);
|
||||
|
||||
String getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface ExportedJobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
String getJob(String path);
|
||||
|
||||
String getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class FootballJobFunctionalTests extends
|
||||
AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int count = jdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY");
|
||||
assertTrue(count>0);
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class FootballJobFunctionalTests extends
|
||||
AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int count = jdbcTemplate.queryForInt("SELECT COUNT(*) from PLAYER_SUMMARY");
|
||||
assertTrue(count>0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,89 +1,89 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.JobParametersBuilder;
|
||||
import org.springframework.batch.sample.dao.HibernateCreditDao;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
import org.springframework.orm.hibernate3.HibernateJdbcException;
|
||||
|
||||
/**
|
||||
* Test for HibernateJob - checks that customer credit has been updated to
|
||||
* expected value.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class HibernateFailureJobFunctionalTests extends
|
||||
HibernateJobFunctionalTests {
|
||||
|
||||
private HibernateCreditDao writer;
|
||||
|
||||
/**
|
||||
* Public setter for the {@link HibernateCreditDao} property.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to set
|
||||
*/
|
||||
public void setWriter(HibernateCreditDao writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#onTearDown()
|
||||
*/
|
||||
protected void onTearDown() throws Exception {
|
||||
super.onTearDown();
|
||||
writer.setFailOnFlush(-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractValidatingBatchLauncherTests#testLaunchJob()
|
||||
*/
|
||||
public void testLaunchJob() throws Exception {
|
||||
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();
|
||||
setJobParameters(params);
|
||||
writer.setFailOnFlush(2);
|
||||
|
||||
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) from CUSTOMER");
|
||||
assertTrue(before > 0);
|
||||
try {
|
||||
super.testLaunchJob();
|
||||
} catch (HibernateJdbcException e) {
|
||||
// This is what would happen if the flush happened outside the
|
||||
// RepeatContext:
|
||||
throw e;
|
||||
} catch (UncategorizedSQLException e) {
|
||||
// This is what would happen if the job wasn't configured to skip
|
||||
// exceptions at the step level.
|
||||
// assertEquals(1, writer.getErrors().size());
|
||||
throw e;
|
||||
}
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from CUSTOMER");
|
||||
assertEquals(before, after);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractCustomerCreditIncreaseTests#checkMatches(java.util.List)
|
||||
*/
|
||||
protected void checkMatches(List matches) {
|
||||
assertFalse(matches.contains(new BigDecimal(2)));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractCustomerCreditIncreaseTests#getExpectedMatches()
|
||||
*/
|
||||
protected int getExpectedMatches() {
|
||||
// One record was skipped, so it won't be processed in the final state.
|
||||
return super.getExpectedMatches() - 1;
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.JobParametersBuilder;
|
||||
import org.springframework.batch.sample.dao.HibernateCreditDao;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
import org.springframework.orm.hibernate3.HibernateJdbcException;
|
||||
|
||||
/**
|
||||
* Test for HibernateJob - checks that customer credit has been updated to
|
||||
* expected value.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class HibernateFailureJobFunctionalTests extends
|
||||
HibernateJobFunctionalTests {
|
||||
|
||||
private HibernateCreditDao writer;
|
||||
|
||||
/**
|
||||
* Public setter for the {@link HibernateCreditDao} property.
|
||||
*
|
||||
* @param writer
|
||||
* the writer to set
|
||||
*/
|
||||
public void setWriter(HibernateCreditDao writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#onTearDown()
|
||||
*/
|
||||
protected void onTearDown() throws Exception {
|
||||
super.onTearDown();
|
||||
writer.setFailOnFlush(-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractValidatingBatchLauncherTests#testLaunchJob()
|
||||
*/
|
||||
public void testLaunchJob() throws Exception {
|
||||
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();
|
||||
setJobParameters(params);
|
||||
writer.setFailOnFlush(2);
|
||||
|
||||
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) from CUSTOMER");
|
||||
assertTrue(before > 0);
|
||||
try {
|
||||
super.testLaunchJob();
|
||||
} catch (HibernateJdbcException e) {
|
||||
// This is what would happen if the flush happened outside the
|
||||
// RepeatContext:
|
||||
throw e;
|
||||
} catch (UncategorizedSQLException e) {
|
||||
// This is what would happen if the job wasn't configured to skip
|
||||
// exceptions at the step level.
|
||||
// assertEquals(1, writer.getErrors().size());
|
||||
throw e;
|
||||
}
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from CUSTOMER");
|
||||
assertEquals(before, after);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractCustomerCreditIncreaseTests#checkMatches(java.util.List)
|
||||
*/
|
||||
protected void checkMatches(List matches) {
|
||||
assertFalse(matches.contains(new BigDecimal(2)));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.sample.AbstractCustomerCreditIncreaseTests#getExpectedMatches()
|
||||
*/
|
||||
protected int getExpectedMatches() {
|
||||
// One record was skipped, so it won't be processed in the final state.
|
||||
return super.getExpectedMatches() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
/**
|
||||
* Test for HibernateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class HibernateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
/**
|
||||
* Test for HibernateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class HibernateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
|
||||
/**
|
||||
* Test for IbatisJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class IbatisJobFunctionalTests extends AbstractCustomerCreditIncreaseTests{
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
|
||||
/**
|
||||
* Test for IbatisJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class IbatisJobFunctionalTests extends AbstractCustomerCreditIncreaseTests{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface JobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
Object getJobConfiguration(String path);
|
||||
|
||||
Object getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface JobLoader {
|
||||
|
||||
void loadResource(String path);
|
||||
|
||||
Map getConfigurations();
|
||||
|
||||
Object getJobConfiguration(String path);
|
||||
|
||||
Object getProperty(String path);
|
||||
|
||||
void setProperty(String path, String value);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class MultilineJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
// The output is grouped together in two lines, instead of all the
|
||||
// trades coming out on a single line.
|
||||
private static final String EXPECTED_RESULT = "[Trade: [isin=UK21341EAH45,quantity=978,price=98.34,customer=customer1], Trade: [isin=UK21341EAH46,quantity=112,price=18.12,customer=customer2]]"
|
||||
+ "[Trade: [isin=UK21341EAH47,quantity=245,price=12.78,customer=customer2], Trade: [isin=UK21341EAH48,quantity=108,price=9.25,customer=customer3], Trade: [isin=UK21341EAH49,quantity=854,price=23.39,customer=customer4]]";
|
||||
|
||||
private Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream()), System
|
||||
.getProperty("line.separator"), ""));
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class MultilineJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
// The output is grouped together in two lines, instead of all the
|
||||
// trades coming out on a single line.
|
||||
private static final String EXPECTED_RESULT = "[Trade: [isin=UK21341EAH45,quantity=978,price=98.34,customer=customer1], Trade: [isin=UK21341EAH46,quantity=112,price=18.12,customer=customer2]]"
|
||||
+ "[Trade: [isin=UK21341EAH47,quantity=245,price=12.78,customer=customer2], Trade: [isin=UK21341EAH48,quantity=108,price=9.25,customer=customer3], Trade: [isin=UK21341EAH49,quantity=854,price=23.39,customer=customer4]]";
|
||||
|
||||
private Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream()), System
|
||||
.getProperty("line.separator"), ""));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class ParallelJobFunctionalTests extends
|
||||
AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int count;
|
||||
count = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
|
||||
new Object[] {StagingItemWriter.NEW});
|
||||
assertEquals(0, count);
|
||||
int total = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING");
|
||||
count = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
|
||||
new Object[] {StagingItemWriter.DONE});
|
||||
assertEquals(total, count);
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class ParallelJobFunctionalTests extends
|
||||
AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int count;
|
||||
count = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
|
||||
new Object[] {StagingItemWriter.NEW});
|
||||
assertEquals(0, count);
|
||||
int total = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING");
|
||||
count = jdbcTemplate.queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING where PROCESSED=?",
|
||||
new Object[] {StagingItemWriter.DONE});
|
||||
assertEquals(total, count);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.sample.item.reader.GeneratingItemReader;
|
||||
import org.springframework.batch.sample.item.writer.RetrySampleItemWriter;
|
||||
|
||||
/**
|
||||
* Checks that expected number of items have been processed.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RetrySampleFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private GeneratingItemReader itemGenerator;
|
||||
|
||||
private RetrySampleItemWriter itemProcessor;
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
//items processed = items read + 2 exceptions
|
||||
assertEquals(itemGenerator.getLimit()+2, itemProcessor.getCounter());
|
||||
}
|
||||
|
||||
public void setItemGenerator(GeneratingItemReader itemGenerator) {
|
||||
this.itemGenerator = itemGenerator;
|
||||
}
|
||||
|
||||
public void setItemProcessor(RetrySampleItemWriter itemProcessor) {
|
||||
this.itemProcessor = itemProcessor;
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.sample.item.reader.GeneratingItemReader;
|
||||
import org.springframework.batch.sample.item.writer.RetrySampleItemWriter;
|
||||
|
||||
/**
|
||||
* Checks that expected number of items have been processed.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RetrySampleFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private GeneratingItemReader itemGenerator;
|
||||
|
||||
private RetrySampleItemWriter itemProcessor;
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
//items processed = items read + 2 exceptions
|
||||
assertEquals(itemGenerator.getLimit()+2, itemProcessor.getCounter());
|
||||
}
|
||||
|
||||
public void setItemGenerator(GeneratingItemReader itemGenerator) {
|
||||
this.itemGenerator = itemGenerator;
|
||||
}
|
||||
|
||||
public void setItemProcessor(RetrySampleItemWriter itemProcessor) {
|
||||
this.itemProcessor = itemProcessor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* Test for job that rolls back a trade that is processed.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RollbackJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
int before = -1;
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void onSetUp() throws Exception {
|
||||
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
assertEquals(before+5, after);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* Test for job that rolls back a trade that is processed.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RollbackJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
int before = -1;
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void onSetUp() throws Exception {
|
||||
before = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE");
|
||||
assertEquals(before+5, after);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,201 +1,201 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
|
||||
|
||||
|
||||
public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade";
|
||||
private static final String GET_CUSTOMERS = "SELECT name, credit FROM customer";
|
||||
|
||||
private List customers;
|
||||
private List trades;
|
||||
private int activeRow = 0;
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
private Map credits = new HashMap();
|
||||
|
||||
/**
|
||||
* @param jdbcTemplate the jdbcTemplate to set
|
||||
*/
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#onSetUp()
|
||||
*/
|
||||
protected void onSetUp() throws Exception {
|
||||
super.onSetUp();
|
||||
jdbcTemplate.update("delete from TRADE");
|
||||
List list = jdbcTemplate.queryForList("select name, CREDIT from customer");
|
||||
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
|
||||
Map map = (Map) iterator.next();
|
||||
credits.put(map.get("NAME"), map.get("CREDIT"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testLaunchJob() throws Exception{
|
||||
super.testLaunchJob();
|
||||
}
|
||||
|
||||
protected void validatePostConditions() {
|
||||
|
||||
// assertTrue(((Resource)applicationContext.getBean("customerFileLocator")).exists());
|
||||
|
||||
customers = new ArrayList() {{add(new Customer("customer1", (((Double)credits.get("customer1")).doubleValue() - 98.34)));
|
||||
add(new Customer("customer2", (((Double)credits.get("customer2")).doubleValue() - 18.12 - 12.78)));
|
||||
add(new Customer("customer3", (((Double)credits.get("customer3")).doubleValue() - 109.25)));
|
||||
add(new Customer("customer4", (((Double)credits.get("customer4")).doubleValue() - 123.39)));}};
|
||||
|
||||
trades = new ArrayList() {{add(new Trade("UK21341EAH45", 978, new BigDecimal("98.34"), "customer1"));
|
||||
add(new Trade("UK21341EAH46", 112, new BigDecimal("18.12"), "customer2"));
|
||||
add(new Trade("UK21341EAH47", 245, new BigDecimal("12.78"), "customer2"));
|
||||
add(new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3"));
|
||||
add(new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));}};
|
||||
|
||||
// check content of the trade table
|
||||
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Trade trade = (Trade)trades.get(activeRow++);
|
||||
|
||||
assertTrue(trade.getIsin().equals(rs.getString(1)));
|
||||
assertTrue(trade.getQuantity() == rs.getLong(2));
|
||||
assertTrue(trade.getPrice().equals(rs.getBigDecimal(3)));
|
||||
assertTrue(trade.getCustomer().equals(rs.getString(4)));
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(trades.size() == activeRow);
|
||||
|
||||
// check content of the customer table
|
||||
activeRow = 0;
|
||||
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Customer customer = (Customer)customers.get(activeRow++);
|
||||
|
||||
assertEquals(customer.getName(),rs.getString(1));
|
||||
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(customers.size(), activeRow);
|
||||
|
||||
// check content of the output file
|
||||
|
||||
// Clean up
|
||||
((FileSystemResource)applicationContext.getBean("customerFileLocator")).getFile().delete();
|
||||
}
|
||||
|
||||
protected void validatePreConditions() {
|
||||
assertTrue(((Resource)applicationContext.getBean("fileLocator")).exists());
|
||||
}
|
||||
|
||||
private static class Customer {
|
||||
private String name;
|
||||
private double credit;
|
||||
|
||||
public Customer(String name, double credit) {
|
||||
this.name = name;
|
||||
this.credit = credit;
|
||||
}
|
||||
|
||||
public Customer(){
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the credit
|
||||
*/
|
||||
public double getCredit() {
|
||||
return credit;
|
||||
}
|
||||
/**
|
||||
* @param credit the credit to set
|
||||
*/
|
||||
public void setCredit(double credit) {
|
||||
this.credit = credit;
|
||||
}
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* @param name the name to set
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
final int PRIME = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(credit);
|
||||
result = PRIME * result + (int) (temp ^ (temp >>> 32));
|
||||
result = PRIME * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Customer other = (Customer) obj;
|
||||
if (Double.doubleToLongBits(credit) != Double.doubleToLongBits(other.credit))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
|
||||
|
||||
|
||||
public class TradeJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM trade";
|
||||
private static final String GET_CUSTOMERS = "SELECT name, credit FROM customer";
|
||||
|
||||
private List customers;
|
||||
private List trades;
|
||||
private int activeRow = 0;
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
private Map credits = new HashMap();
|
||||
|
||||
/**
|
||||
* @param jdbcTemplate the jdbcTemplate to set
|
||||
*/
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#onSetUp()
|
||||
*/
|
||||
protected void onSetUp() throws Exception {
|
||||
super.onSetUp();
|
||||
jdbcTemplate.update("delete from TRADE");
|
||||
List list = jdbcTemplate.queryForList("select name, CREDIT from customer");
|
||||
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
|
||||
Map map = (Map) iterator.next();
|
||||
credits.put(map.get("NAME"), map.get("CREDIT"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testLaunchJob() throws Exception{
|
||||
super.testLaunchJob();
|
||||
}
|
||||
|
||||
protected void validatePostConditions() {
|
||||
|
||||
// assertTrue(((Resource)applicationContext.getBean("customerFileLocator")).exists());
|
||||
|
||||
customers = new ArrayList() {{add(new Customer("customer1", (((Double)credits.get("customer1")).doubleValue() - 98.34)));
|
||||
add(new Customer("customer2", (((Double)credits.get("customer2")).doubleValue() - 18.12 - 12.78)));
|
||||
add(new Customer("customer3", (((Double)credits.get("customer3")).doubleValue() - 109.25)));
|
||||
add(new Customer("customer4", (((Double)credits.get("customer4")).doubleValue() - 123.39)));}};
|
||||
|
||||
trades = new ArrayList() {{add(new Trade("UK21341EAH45", 978, new BigDecimal("98.34"), "customer1"));
|
||||
add(new Trade("UK21341EAH46", 112, new BigDecimal("18.12"), "customer2"));
|
||||
add(new Trade("UK21341EAH47", 245, new BigDecimal("12.78"), "customer2"));
|
||||
add(new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3"));
|
||||
add(new Trade("UK21341EAH49", 854, new BigDecimal("123.39"), "customer4"));}};
|
||||
|
||||
// check content of the trade table
|
||||
jdbcTemplate.query(GET_TRADES, new RowCallbackHandler() {
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Trade trade = (Trade)trades.get(activeRow++);
|
||||
|
||||
assertTrue(trade.getIsin().equals(rs.getString(1)));
|
||||
assertTrue(trade.getQuantity() == rs.getLong(2));
|
||||
assertTrue(trade.getPrice().equals(rs.getBigDecimal(3)));
|
||||
assertTrue(trade.getCustomer().equals(rs.getString(4)));
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(trades.size() == activeRow);
|
||||
|
||||
// check content of the customer table
|
||||
activeRow = 0;
|
||||
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
Customer customer = (Customer)customers.get(activeRow++);
|
||||
|
||||
assertEquals(customer.getName(),rs.getString(1));
|
||||
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(customers.size(), activeRow);
|
||||
|
||||
// check content of the output file
|
||||
|
||||
// Clean up
|
||||
((FileSystemResource)applicationContext.getBean("customerFileLocator")).getFile().delete();
|
||||
}
|
||||
|
||||
protected void validatePreConditions() {
|
||||
assertTrue(((Resource)applicationContext.getBean("fileLocator")).exists());
|
||||
}
|
||||
|
||||
private static class Customer {
|
||||
private String name;
|
||||
private double credit;
|
||||
|
||||
public Customer(String name, double credit) {
|
||||
this.name = name;
|
||||
this.credit = credit;
|
||||
}
|
||||
|
||||
public Customer(){
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the credit
|
||||
*/
|
||||
public double getCredit() {
|
||||
return credit;
|
||||
}
|
||||
/**
|
||||
* @param credit the credit to set
|
||||
*/
|
||||
public void setCredit(double credit) {
|
||||
this.credit = credit;
|
||||
}
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* @param name the name to set
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
public int hashCode() {
|
||||
final int PRIME = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(credit);
|
||||
result = PRIME * result + (int) (temp ^ (temp >>> 32));
|
||||
result = PRIME * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Customer other = (Customer) obj;
|
||||
if (Double.doubleToLongBits(credit) != Double.doubleToLongBits(other.credit))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.io.FileReader;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
|
||||
public class XmlStaxJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/20070918.testStream.xmlFileStep.output.xml";
|
||||
private static final String EXPECTED_OUTPUT_FILE = "src/main/resources/data/staxJob/output/expected-output.xml";
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
protected void validatePostConditions() throws Exception {
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
XMLAssert.assertXMLEqual(new FileReader(EXPECTED_OUTPUT_FILE), new FileReader(OUTPUT_FILE));
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.io.FileReader;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
|
||||
public class XmlStaxJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/20070918.testStream.xmlFileStep.output.xml";
|
||||
private static final String EXPECTED_OUTPUT_FILE = "src/main/resources/data/staxJob/output/expected-output.xml";
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
protected void validatePostConditions() throws Exception {
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
XMLAssert.assertXMLEqual(new FileReader(EXPECTED_OUTPUT_FILE), new FileReader(OUTPUT_FILE));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
/*
|
||||
* 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.advice;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.Notification;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.jmx.export.notification.NotificationPublisher;
|
||||
import org.springframework.jmx.export.notification.UnableToSendNotificationException;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobExecutionNotificationPublisherTests extends TestCase {
|
||||
|
||||
JobExecutionNotificationPublisher publisher = new JobExecutionNotificationPublisher();
|
||||
|
||||
public void testRepeatOperationsOpenUsed() throws Exception {
|
||||
final List list = new ArrayList();
|
||||
publisher.setNotificationPublisher(new NotificationPublisher() {
|
||||
public void sendNotification(Notification notification) throws UnableToSendNotificationException {
|
||||
list.add(notification);
|
||||
}
|
||||
});
|
||||
publisher.onApplicationEvent(new SimpleMessageApplicationEvent(this, "foo"));
|
||||
assertEquals(1, list.size());
|
||||
String message = ((Notification) list.get(0)).getMessage();
|
||||
assertTrue("Message does not contain 'foo': ", message.indexOf("foo") > 0);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.advice;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.Notification;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.jmx.export.notification.NotificationPublisher;
|
||||
import org.springframework.jmx.export.notification.UnableToSendNotificationException;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobExecutionNotificationPublisherTests extends TestCase {
|
||||
|
||||
JobExecutionNotificationPublisher publisher = new JobExecutionNotificationPublisher();
|
||||
|
||||
public void testRepeatOperationsOpenUsed() throws Exception {
|
||||
final List list = new ArrayList();
|
||||
publisher.setNotificationPublisher(new NotificationPublisher() {
|
||||
public void sendNotification(Notification notification) throws UnableToSendNotificationException {
|
||||
list.add(notification);
|
||||
}
|
||||
});
|
||||
publisher.onApplicationEvent(new SimpleMessageApplicationEvent(this, "foo"));
|
||||
assertEquals(1, list.size());
|
||||
String message = ((Notification) list.get(0)).getMessage();
|
||||
assertTrue("Message does not contain 'foo': ", message.indexOf("foo") > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,82 +1,82 @@
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
public class FlatFileCustomerCreditDaoTests extends TestCase {
|
||||
|
||||
private MockControl outputControl;
|
||||
private ResourceLifecycleItemWriter output;
|
||||
private FlatFileCustomerCreditDao writer;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
//create mock for OutputSource
|
||||
outputControl = MockControl.createControl(ResourceLifecycleItemWriter.class);
|
||||
output = (ResourceLifecycleItemWriter)outputControl.getMock();
|
||||
|
||||
//create new writer
|
||||
writer = new FlatFileCustomerCreditDao();
|
||||
writer.setOutputSource(output);
|
||||
}
|
||||
|
||||
public void testOpen() throws Exception {
|
||||
ExecutionContext executionContext = new ExecutionContext();
|
||||
//set-up outputSource mock
|
||||
output.open(executionContext);
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.open(executionContext);
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
public void testClose() throws Exception{
|
||||
|
||||
//set-up outputSource mock
|
||||
output.close(null);
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.close();
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
public void testWrite() throws Exception {
|
||||
|
||||
//Create and set-up CustomerCredit
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setCredit(new BigDecimal(1));
|
||||
credit.setName("testName");
|
||||
|
||||
//set separator
|
||||
writer.setSeparator(";");
|
||||
|
||||
//set-up OutputSource mock
|
||||
output.write("testName;1");
|
||||
output.open(new ExecutionContext());
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.writeCredit(credit);
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
private interface ResourceLifecycleItemWriter extends ItemWriter, ItemStream{
|
||||
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
|
||||
public class FlatFileCustomerCreditDaoTests extends TestCase {
|
||||
|
||||
private MockControl outputControl;
|
||||
private ResourceLifecycleItemWriter output;
|
||||
private FlatFileCustomerCreditDao writer;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
//create mock for OutputSource
|
||||
outputControl = MockControl.createControl(ResourceLifecycleItemWriter.class);
|
||||
output = (ResourceLifecycleItemWriter)outputControl.getMock();
|
||||
|
||||
//create new writer
|
||||
writer = new FlatFileCustomerCreditDao();
|
||||
writer.setOutputSource(output);
|
||||
}
|
||||
|
||||
public void testOpen() throws Exception {
|
||||
ExecutionContext executionContext = new ExecutionContext();
|
||||
//set-up outputSource mock
|
||||
output.open(executionContext);
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.open(executionContext);
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
public void testClose() throws Exception{
|
||||
|
||||
//set-up outputSource mock
|
||||
output.close(null);
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.close();
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
public void testWrite() throws Exception {
|
||||
|
||||
//Create and set-up CustomerCredit
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setCredit(new BigDecimal(1));
|
||||
credit.setName("testName");
|
||||
|
||||
//set separator
|
||||
writer.setSeparator(";");
|
||||
|
||||
//set-up OutputSource mock
|
||||
output.write("testName;1");
|
||||
output.open(new ExecutionContext());
|
||||
outputControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.writeCredit(credit);
|
||||
|
||||
//verify method calls
|
||||
outputControl.verify();
|
||||
}
|
||||
|
||||
private interface ResourceLifecycleItemWriter extends ItemWriter, ItemStream{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.writer.AbstractItemWriter;
|
||||
import org.springframework.batch.sample.StubLineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
public class FlatFileOrderWriterTests extends TestCase {
|
||||
|
||||
List list = new ArrayList();
|
||||
|
||||
private ItemWriter output = new AbstractItemWriter() {
|
||||
public void write(Object output) {
|
||||
list.add(output);
|
||||
}
|
||||
};
|
||||
|
||||
private FlatFileOrderWriter writer;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
//create new writer
|
||||
writer = new FlatFileOrderWriter();
|
||||
writer.setDelegate(output);
|
||||
}
|
||||
|
||||
public void testWrite() throws Exception {
|
||||
|
||||
//Create and set-up Order
|
||||
Order order = new Order();
|
||||
|
||||
order.setOrderDate(new GregorianCalendar(2007, GregorianCalendar.JUNE, 1).getTime());
|
||||
order.setCustomer(new Customer());
|
||||
order.setBilling(new BillingInfo());
|
||||
order.setBillingAddress(new Address());
|
||||
List lineItems = new ArrayList();
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(BigDecimal.valueOf(0));
|
||||
lineItems.add(item);
|
||||
lineItems.add(item);
|
||||
order.setLineItems(lineItems);
|
||||
order.setTotalPrice(BigDecimal.valueOf(0));
|
||||
|
||||
//create aggregator stub
|
||||
LineAggregator aggregator = new StubLineAggregator();
|
||||
|
||||
//create map of aggregators and set it to writer
|
||||
Map aggregators = new HashMap();
|
||||
|
||||
OrderTransformer converter = new OrderTransformer();
|
||||
aggregators.put("header", aggregator);
|
||||
aggregators.put("customer", aggregator);
|
||||
aggregators.put("address", aggregator);
|
||||
aggregators.put("billing", aggregator);
|
||||
aggregators.put("item", aggregator);
|
||||
aggregators.put("footer", aggregator);
|
||||
converter.setAggregators(aggregators);
|
||||
writer.setTransformer(converter);
|
||||
|
||||
//call tested method
|
||||
writer.write(order);
|
||||
|
||||
//verify method calls
|
||||
assertEquals(1, list.size());
|
||||
assertTrue(list.get(0) instanceof List);
|
||||
assertEquals("02007/06/01", ((List) list.get(0)).get(0));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.transform.LineAggregator;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.writer.AbstractItemWriter;
|
||||
import org.springframework.batch.sample.StubLineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
public class FlatFileOrderWriterTests extends TestCase {
|
||||
|
||||
List list = new ArrayList();
|
||||
|
||||
private ItemWriter output = new AbstractItemWriter() {
|
||||
public void write(Object output) {
|
||||
list.add(output);
|
||||
}
|
||||
};
|
||||
|
||||
private FlatFileOrderWriter writer;
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
//create new writer
|
||||
writer = new FlatFileOrderWriter();
|
||||
writer.setDelegate(output);
|
||||
}
|
||||
|
||||
public void testWrite() throws Exception {
|
||||
|
||||
//Create and set-up Order
|
||||
Order order = new Order();
|
||||
|
||||
order.setOrderDate(new GregorianCalendar(2007, GregorianCalendar.JUNE, 1).getTime());
|
||||
order.setCustomer(new Customer());
|
||||
order.setBilling(new BillingInfo());
|
||||
order.setBillingAddress(new Address());
|
||||
List lineItems = new ArrayList();
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(BigDecimal.valueOf(0));
|
||||
lineItems.add(item);
|
||||
lineItems.add(item);
|
||||
order.setLineItems(lineItems);
|
||||
order.setTotalPrice(BigDecimal.valueOf(0));
|
||||
|
||||
//create aggregator stub
|
||||
LineAggregator aggregator = new StubLineAggregator();
|
||||
|
||||
//create map of aggregators and set it to writer
|
||||
Map aggregators = new HashMap();
|
||||
|
||||
OrderTransformer converter = new OrderTransformer();
|
||||
aggregators.put("header", aggregator);
|
||||
aggregators.put("customer", aggregator);
|
||||
aggregators.put("address", aggregator);
|
||||
aggregators.put("billing", aggregator);
|
||||
aggregators.put("item", aggregator);
|
||||
aggregators.put("footer", aggregator);
|
||||
converter.setAggregators(aggregators);
|
||||
writer.setTransformer(converter);
|
||||
|
||||
//call tested method
|
||||
writer.write(order);
|
||||
|
||||
//verify method calls
|
||||
assertEquals(1, list.size());
|
||||
assertTrue(list.get(0) instanceof List);
|
||||
assertEquals("02007/06/01", ((List) list.get(0)).get(0));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.CustomerDebit;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
public class JdbcCustomerDebitDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
//insert customer credit
|
||||
jdbcTemplate.execute("INSERT INTO customer VALUES (99, 0, 'testName', 100)");
|
||||
|
||||
//create writer and set jdbcTemplate
|
||||
JdbcCustomerDebitDao writer = new JdbcCustomerDebitDao();
|
||||
writer.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
//create customer debit
|
||||
CustomerDebit customerDebit = new CustomerDebit();
|
||||
customerDebit.setName("testName");
|
||||
customerDebit.setDebit(BigDecimal.valueOf(5));
|
||||
|
||||
//call writer
|
||||
writer.write(customerDebit);
|
||||
|
||||
//verify customer credit
|
||||
jdbcTemplate.query("SELECT name, credit FROM customer WHERE name = 'testName'", new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals(95, rs.getLong("credit"));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.CustomerDebit;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
public class JdbcCustomerDebitDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
//insert customer credit
|
||||
jdbcTemplate.execute("INSERT INTO customer VALUES (99, 0, 'testName', 100)");
|
||||
|
||||
//create writer and set jdbcTemplate
|
||||
JdbcCustomerDebitDao writer = new JdbcCustomerDebitDao();
|
||||
writer.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
//create customer debit
|
||||
CustomerDebit customerDebit = new CustomerDebit();
|
||||
customerDebit.setName("testName");
|
||||
customerDebit.setDebit(BigDecimal.valueOf(5));
|
||||
|
||||
//call writer
|
||||
writer.write(customerDebit);
|
||||
|
||||
//verify customer credit
|
||||
jdbcTemplate.query("SELECT name, credit FROM customer WHERE name = 'testName'", new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals(95, rs.getLong("credit"));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Game;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcGameDaoIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JdbcGameDao gameDao;
|
||||
|
||||
private Game game = new Game();
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
gameDao = new JdbcGameDao();
|
||||
gameDao.setJdbcTemplate(getJdbcTemplate());
|
||||
|
||||
game.setId("XXXXX00");
|
||||
game.setYear(1996);
|
||||
game.setTeam("mia");
|
||||
game.setWeek(10);
|
||||
game.setOpponent("nwe");
|
||||
game.setAttempts(0);
|
||||
game.setCompletes(0);
|
||||
game.setPassingYards(0);
|
||||
game.setPassingTd(0);
|
||||
game.setInterceptions(0);
|
||||
game.setRushes(29);
|
||||
game.setRushYards(109);
|
||||
game.setReceptions(1);
|
||||
game.setReceptionYards(16);
|
||||
game.setTotalTd(2);
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
gameDao.write(game);
|
||||
|
||||
Game tempGame = (Game) getJdbcTemplate().queryForObject("SELECT * FROM GAMES where PLAYER_ID=? AND YEAR_NO=?",
|
||||
new Object[] { game.getId(), new Integer(game.getYear()) }, new GameRowMapper());
|
||||
assertEquals(tempGame, game);
|
||||
}
|
||||
|
||||
public static class GameRowMapper implements RowMapper {
|
||||
|
||||
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
|
||||
|
||||
if (rs == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Game game = new Game();
|
||||
game.setId(rs.getString("PLAYER_ID").trim());
|
||||
game.setYear(rs.getInt("year_no"));
|
||||
game.setTeam(rs.getString("team"));
|
||||
game.setWeek(rs.getInt("week"));
|
||||
game.setOpponent(rs.getString("opponent"));
|
||||
game.setCompletes(rs.getInt("completes"));
|
||||
game.setAttempts(rs.getInt("attempts"));
|
||||
game.setPassingYards(rs.getInt("passing_Yards"));
|
||||
game.setPassingTd(rs.getInt("passing_Td"));
|
||||
game.setInterceptions(rs.getInt("interceptions"));
|
||||
game.setRushes(rs.getInt("rushes"));
|
||||
game.setRushYards(rs.getInt("rush_Yards"));
|
||||
game.setReceptions(rs.getInt("receptions"));
|
||||
game.setReceptionYards(rs.getInt("receptions_Yards"));
|
||||
game.setTotalTd(rs.getInt("total_Td"));
|
||||
|
||||
return game;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Game;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcGameDaoIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JdbcGameDao gameDao;
|
||||
|
||||
private Game game = new Game();
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
gameDao = new JdbcGameDao();
|
||||
gameDao.setJdbcTemplate(getJdbcTemplate());
|
||||
|
||||
game.setId("XXXXX00");
|
||||
game.setYear(1996);
|
||||
game.setTeam("mia");
|
||||
game.setWeek(10);
|
||||
game.setOpponent("nwe");
|
||||
game.setAttempts(0);
|
||||
game.setCompletes(0);
|
||||
game.setPassingYards(0);
|
||||
game.setPassingTd(0);
|
||||
game.setInterceptions(0);
|
||||
game.setRushes(29);
|
||||
game.setRushYards(109);
|
||||
game.setReceptions(1);
|
||||
game.setReceptionYards(16);
|
||||
game.setTotalTd(2);
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
gameDao.write(game);
|
||||
|
||||
Game tempGame = (Game) getJdbcTemplate().queryForObject("SELECT * FROM GAMES where PLAYER_ID=? AND YEAR_NO=?",
|
||||
new Object[] { game.getId(), new Integer(game.getYear()) }, new GameRowMapper());
|
||||
assertEquals(tempGame, game);
|
||||
}
|
||||
|
||||
public static class GameRowMapper implements RowMapper {
|
||||
|
||||
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
|
||||
|
||||
if (rs == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Game game = new Game();
|
||||
game.setId(rs.getString("PLAYER_ID").trim());
|
||||
game.setYear(rs.getInt("year_no"));
|
||||
game.setTeam(rs.getString("team"));
|
||||
game.setWeek(rs.getInt("week"));
|
||||
game.setOpponent(rs.getString("opponent"));
|
||||
game.setCompletes(rs.getInt("completes"));
|
||||
game.setAttempts(rs.getInt("attempts"));
|
||||
game.setPassingYards(rs.getInt("passing_Yards"));
|
||||
game.setPassingTd(rs.getInt("passing_Td"));
|
||||
game.setInterceptions(rs.getInt("interceptions"));
|
||||
game.setRushes(rs.getInt("rushes"));
|
||||
game.setRushYards(rs.getInt("rush_Yards"));
|
||||
game.setReceptions(rs.getInt("receptions"));
|
||||
game.setReceptionYards(rs.getInt("receptions_Yards"));
|
||||
game.setTotalTd(rs.getInt("total_Td"));
|
||||
|
||||
return game;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,204 +1,204 @@
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.domain.BatchStatus;
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
public class JdbcJobRepositoryTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JobRepository repository;
|
||||
|
||||
private JobSupport jobConfiguration;
|
||||
|
||||
private Set jobExecutionIds = new HashSet();
|
||||
|
||||
private Set jobIds = new HashSet();
|
||||
|
||||
private List list = new ArrayList();
|
||||
|
||||
public void setRepository(JobRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "simple-job-launcher-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
jobConfiguration = new JobSupport("test-job");
|
||||
jobConfiguration.setRestartable(true);
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
startNewTransaction();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STEP_EXECUTION_CONTEXT");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STEP_EXECUTION");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_EXECUTION");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_PARAMS");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_INSTANCE");
|
||||
setComplete();
|
||||
endTransaction();
|
||||
}
|
||||
|
||||
protected void onTearDownAfterTransaction() throws Exception {
|
||||
startNewTransaction();
|
||||
for (Iterator iterator = jobExecutionIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", new Object[] { id });
|
||||
}
|
||||
for (Iterator iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
}
|
||||
setComplete();
|
||||
endTransaction();
|
||||
for (Iterator iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
assertEquals(0, count);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFindOrCreateJob() throws Exception {
|
||||
jobConfiguration.setName("foo");
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(before + 1, after);
|
||||
assertNotNull(execution.getId());
|
||||
}
|
||||
|
||||
public void testFindOrCreateJobConcurrently() throws Exception {
|
||||
|
||||
jobConfiguration.setName("bar");
|
||||
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(0, before);
|
||||
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
JobExecution execution = null;
|
||||
long t0 = System.currentTimeMillis();
|
||||
try {
|
||||
execution = doConcurrentStart();
|
||||
fail("Expected JobExecutionAlreadyRunningException");
|
||||
}
|
||||
catch (JobExecutionAlreadyRunningException e) {
|
||||
// expected
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
if (execution == null) {
|
||||
execution = (JobExecution) list.get(0);
|
||||
}
|
||||
|
||||
assertNotNull(execution);
|
||||
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before + 1, after);
|
||||
|
||||
logger.info("Duration: " + (t1 - t0)
|
||||
+ " - the second transaction did not block if this number is less than about 1000.");
|
||||
}
|
||||
|
||||
public void testFindOrCreateJobConcurrentlyWhenJobAlreadyExists() throws Exception {
|
||||
|
||||
jobConfiguration.setName("spam");
|
||||
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
execution.setEndTime(new Timestamp(System.currentTimeMillis()));
|
||||
repository.saveOrUpdate(execution);
|
||||
execution.setStatus(BatchStatus.FAILED);
|
||||
setComplete();
|
||||
endTransaction();
|
||||
|
||||
startNewTransaction();
|
||||
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(1, before);
|
||||
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
try {
|
||||
execution = doConcurrentStart();
|
||||
fail("Expected JobExecutionAlreadyRunningException");
|
||||
}
|
||||
catch (JobExecutionAlreadyRunningException e) {
|
||||
// expected
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before, after);
|
||||
|
||||
logger.info("Duration: " + (t1 - t0)
|
||||
+ " - the second transaction did not block if this number is less than about 1000.");
|
||||
}
|
||||
|
||||
private void cacheJobIds(JobExecution execution) {
|
||||
if (execution == null)
|
||||
return;
|
||||
jobExecutionIds.add(execution.getId());
|
||||
jobIds.add(execution.getJobId());
|
||||
}
|
||||
|
||||
private JobExecution doConcurrentStart() throws Exception {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(org.springframework.transaction.TransactionStatus status) {
|
||||
try {
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
list.add(execution);
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch (Exception e) {
|
||||
list.add(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
list.add(e);
|
||||
}
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
||||
Thread.sleep(400);
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
|
||||
int count = 0;
|
||||
while (list.size() == 0 && count++ < 10) {
|
||||
Thread.sleep(200);
|
||||
}
|
||||
|
||||
assertEquals("Timed out waiting for JobExecution to be created", 1, list.size());
|
||||
assertTrue("JobExecution not created in thread", list.get(0) instanceof JobExecution);
|
||||
return (JobExecution) list.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.domain.BatchStatus;
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
public class JdbcJobRepositoryTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JobRepository repository;
|
||||
|
||||
private JobSupport jobConfiguration;
|
||||
|
||||
private Set jobExecutionIds = new HashSet();
|
||||
|
||||
private Set jobIds = new HashSet();
|
||||
|
||||
private List list = new ArrayList();
|
||||
|
||||
public void setRepository(JobRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "simple-job-launcher-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
jobConfiguration = new JobSupport("test-job");
|
||||
jobConfiguration.setRestartable(true);
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
startNewTransaction();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STEP_EXECUTION_CONTEXT");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STEP_EXECUTION");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_EXECUTION");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_PARAMS");
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_INSTANCE");
|
||||
setComplete();
|
||||
endTransaction();
|
||||
}
|
||||
|
||||
protected void onTearDownAfterTransaction() throws Exception {
|
||||
startNewTransaction();
|
||||
for (Iterator iterator = jobExecutionIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_EXECUTION where JOB_EXECUTION_ID=?", new Object[] { id });
|
||||
}
|
||||
for (Iterator iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
}
|
||||
setComplete();
|
||||
endTransaction();
|
||||
for (Iterator iterator = jobIds.iterator(); iterator.hasNext();) {
|
||||
Long id = (Long) iterator.next();
|
||||
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", new Object[] { id });
|
||||
assertEquals(0, count);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFindOrCreateJob() throws Exception {
|
||||
jobConfiguration.setName("foo");
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(before + 1, after);
|
||||
assertNotNull(execution.getId());
|
||||
}
|
||||
|
||||
public void testFindOrCreateJobConcurrently() throws Exception {
|
||||
|
||||
jobConfiguration.setName("bar");
|
||||
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(0, before);
|
||||
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
JobExecution execution = null;
|
||||
long t0 = System.currentTimeMillis();
|
||||
try {
|
||||
execution = doConcurrentStart();
|
||||
fail("Expected JobExecutionAlreadyRunningException");
|
||||
}
|
||||
catch (JobExecutionAlreadyRunningException e) {
|
||||
// expected
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
if (execution == null) {
|
||||
execution = (JobExecution) list.get(0);
|
||||
}
|
||||
|
||||
assertNotNull(execution);
|
||||
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before + 1, after);
|
||||
|
||||
logger.info("Duration: " + (t1 - t0)
|
||||
+ " - the second transaction did not block if this number is less than about 1000.");
|
||||
}
|
||||
|
||||
public void testFindOrCreateJobConcurrentlyWhenJobAlreadyExists() throws Exception {
|
||||
|
||||
jobConfiguration.setName("spam");
|
||||
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
execution.setEndTime(new Timestamp(System.currentTimeMillis()));
|
||||
repository.saveOrUpdate(execution);
|
||||
execution.setStatus(BatchStatus.FAILED);
|
||||
setComplete();
|
||||
endTransaction();
|
||||
|
||||
startNewTransaction();
|
||||
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertEquals(1, before);
|
||||
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
try {
|
||||
execution = doConcurrentStart();
|
||||
fail("Expected JobExecutionAlreadyRunningException");
|
||||
}
|
||||
catch (JobExecutionAlreadyRunningException e) {
|
||||
// expected
|
||||
}
|
||||
long t1 = System.currentTimeMillis();
|
||||
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
|
||||
assertNotNull(execution.getId());
|
||||
assertEquals(before, after);
|
||||
|
||||
logger.info("Duration: " + (t1 - t0)
|
||||
+ " - the second transaction did not block if this number is less than about 1000.");
|
||||
}
|
||||
|
||||
private void cacheJobIds(JobExecution execution) {
|
||||
if (execution == null)
|
||||
return;
|
||||
jobExecutionIds.add(execution.getId());
|
||||
jobIds.add(execution.getJobId());
|
||||
}
|
||||
|
||||
private JobExecution doConcurrentStart() throws Exception {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
|
||||
public Object doInTransaction(org.springframework.transaction.TransactionStatus status) {
|
||||
try {
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
list.add(execution);
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch (Exception e) {
|
||||
list.add(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
list.add(e);
|
||||
}
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
||||
Thread.sleep(400);
|
||||
JobExecution execution = repository.createJobExecution(jobConfiguration, new JobParameters());
|
||||
cacheJobIds(execution);
|
||||
|
||||
int count = 0;
|
||||
while (list.size() == 0 && count++ < 10) {
|
||||
Thread.sleep(200);
|
||||
}
|
||||
|
||||
assertEquals("Timed out waiting for JobExecution to be created", 1, list.size());
|
||||
assertTrue("JobExecution not created in thread", list.get(0) instanceof JobExecution);
|
||||
return (JobExecution) list.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Player;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcPlayerDaoIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JdbcPlayerDao playerDao;
|
||||
private Player player;
|
||||
private static final String GET_PLAYER = "SELECT * from PLAYERS";
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] {"data-source-context.xml"};
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
playerDao = new JdbcPlayerDao();
|
||||
playerDao.setJdbcTemplate(this.jdbcTemplate);
|
||||
|
||||
player = new Player();
|
||||
player.setID("AKFJDL00");
|
||||
player.setFirstName("John");
|
||||
player.setLastName("Doe");
|
||||
player.setPosition("QB");
|
||||
player.setBirthYear(1975);
|
||||
player.setDebutYear(1998);
|
||||
}
|
||||
|
||||
public void testSavePlayer(){
|
||||
|
||||
playerDao.savePlayer(player);
|
||||
|
||||
getJdbcTemplate().query(GET_PLAYER, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");
|
||||
assertEquals(rs.getString("LAST_NAME"), "Doe");
|
||||
assertEquals(rs.getString("FIRST_NAME"), "John");
|
||||
assertEquals(rs.getString("POS"), "QB");
|
||||
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
|
||||
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Player;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcPlayerDaoIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private JdbcPlayerDao playerDao;
|
||||
private Player player;
|
||||
private static final String GET_PLAYER = "SELECT * from PLAYERS";
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] {"data-source-context.xml"};
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
playerDao = new JdbcPlayerDao();
|
||||
playerDao.setJdbcTemplate(this.jdbcTemplate);
|
||||
|
||||
player = new Player();
|
||||
player.setID("AKFJDL00");
|
||||
player.setFirstName("John");
|
||||
player.setLastName("Doe");
|
||||
player.setPosition("QB");
|
||||
player.setBirthYear(1975);
|
||||
player.setDebutYear(1998);
|
||||
}
|
||||
|
||||
public void testSavePlayer(){
|
||||
|
||||
playerDao.savePlayer(player);
|
||||
|
||||
getJdbcTemplate().query(GET_PLAYER, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals(rs.getString("PLAYER_ID"), "AKFJDL00");
|
||||
assertEquals(rs.getString("LAST_NAME"), "Doe");
|
||||
assertEquals(rs.getString("FIRST_NAME"), "John");
|
||||
assertEquals(rs.getString("POS"), "QB");
|
||||
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
|
||||
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
|
||||
import org.springframework.batch.sample.domain.PlayerSummary;
|
||||
import org.springframework.batch.sample.mapping.PlayerSummaryMapper;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcPlayerSummaryDaoIntegrationTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
JdbcPlayerSummaryDao playerSummaryDao;
|
||||
PlayerSummary summary;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
playerSummaryDao = new JdbcPlayerSummaryDao();
|
||||
playerSummaryDao.setJdbcTemplate(getJdbcTemplate());
|
||||
|
||||
summary = new PlayerSummary();
|
||||
summary.setId("AikmTr00");
|
||||
summary.setYear(1997);
|
||||
summary.setCompletes(294);
|
||||
summary.setAttempts(517);
|
||||
summary.setPassingYards(3283);
|
||||
summary.setPassingTd(19);
|
||||
summary.setInterceptions(12);
|
||||
summary.setRushes(25);
|
||||
summary.setRushYards(79);
|
||||
summary.setReceptions(0);
|
||||
summary.setReceptionYards(0);
|
||||
summary.setTotalTd(0);
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
playerSummaryDao.write(summary);
|
||||
|
||||
PlayerSummary testSummary = (PlayerSummary) getJdbcTemplate()
|
||||
.queryForObject("SELECT * FROM PLAYER_SUMMARY",
|
||||
new PlayerSummaryMapper());
|
||||
|
||||
assertEquals(testSummary, summary);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
|
||||
import org.springframework.batch.sample.domain.PlayerSummary;
|
||||
import org.springframework.batch.sample.mapping.PlayerSummaryMapper;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class JdbcPlayerSummaryDaoIntegrationTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
JdbcPlayerSummaryDao playerSummaryDao;
|
||||
PlayerSummary summary;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
protected void onSetUpBeforeTransaction() throws Exception {
|
||||
super.onSetUpBeforeTransaction();
|
||||
|
||||
playerSummaryDao = new JdbcPlayerSummaryDao();
|
||||
playerSummaryDao.setJdbcTemplate(getJdbcTemplate());
|
||||
|
||||
summary = new PlayerSummary();
|
||||
summary.setId("AikmTr00");
|
||||
summary.setYear(1997);
|
||||
summary.setCompletes(294);
|
||||
summary.setAttempts(517);
|
||||
summary.setPassingYards(3283);
|
||||
summary.setPassingTd(19);
|
||||
summary.setInterceptions(12);
|
||||
summary.setRushes(25);
|
||||
summary.setRushYards(79);
|
||||
summary.setReceptions(0);
|
||||
summary.setReceptionYards(0);
|
||||
summary.setTotalTd(0);
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
playerSummaryDao.write(summary);
|
||||
|
||||
PlayerSummary testSummary = (PlayerSummary) getJdbcTemplate()
|
||||
.queryForObject("SELECT * FROM PLAYER_SUMMARY",
|
||||
new PlayerSummaryMapper());
|
||||
|
||||
assertEquals(testSummary, summary);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
public class JdbcTradeWriterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
JdbcTradeDao writer = new JdbcTradeDao();
|
||||
|
||||
AbstractDataFieldMaxValueIncrementer incrementer = (AbstractDataFieldMaxValueIncrementer)applicationContext.getBean("incrementerParent");
|
||||
incrementer.setIncrementerName("TRADE_SEQ");
|
||||
|
||||
writer.setIncrementer(incrementer);
|
||||
writer.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setCustomer("testCustomer");
|
||||
trade.setIsin("5647238492");
|
||||
trade.setPrice(new BigDecimal(Double.toString(99.69)));
|
||||
trade.setQuantity(5);
|
||||
|
||||
writer.writeTrade(trade);
|
||||
|
||||
jdbcTemplate.query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals("testCustomer", rs.getString("CUSTOMER"));
|
||||
assertEquals(new BigDecimal(Double.toString(99.69)), rs.getBigDecimal("PRICE"));
|
||||
assertEquals(5,rs.getLong("QUANTITY"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
public class JdbcTradeWriterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { "data-source-context.xml" };
|
||||
}
|
||||
|
||||
public void testWrite() {
|
||||
|
||||
JdbcTradeDao writer = new JdbcTradeDao();
|
||||
|
||||
AbstractDataFieldMaxValueIncrementer incrementer = (AbstractDataFieldMaxValueIncrementer)applicationContext.getBean("incrementerParent");
|
||||
incrementer.setIncrementerName("TRADE_SEQ");
|
||||
|
||||
writer.setIncrementer(incrementer);
|
||||
writer.setJdbcTemplate(jdbcTemplate);
|
||||
|
||||
Trade trade = new Trade();
|
||||
trade.setCustomer("testCustomer");
|
||||
trade.setIsin("5647238492");
|
||||
trade.setPrice(new BigDecimal(Double.toString(99.69)));
|
||||
trade.setQuantity(5);
|
||||
|
||||
writer.writeTrade(trade);
|
||||
|
||||
jdbcTemplate.query("SELECT * FROM TRADE WHERE ISIN = '5647238492'", new RowCallbackHandler() {
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
assertEquals("testCustomer", rs.getString("CUSTOMER"));
|
||||
assertEquals(new BigDecimal(Double.toString(99.69)), rs.getBigDecimal("PRICE"));
|
||||
assertEquals(5,rs.getLong("QUANTITY"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.transform.DelimitedLineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class OrderTransformerTests extends TestCase {
|
||||
|
||||
private OrderTransformer converter = new OrderTransformer();
|
||||
|
||||
public void testConvert() throws Exception {
|
||||
converter.setAggregators(new HashMap() {
|
||||
{
|
||||
put("header", new DelimitedLineAggregator());
|
||||
put("customer", new DelimitedLineAggregator());
|
||||
put("address", new DelimitedLineAggregator());
|
||||
put("billing", new DelimitedLineAggregator());
|
||||
put("item", new DelimitedLineAggregator());
|
||||
put("footer", new DelimitedLineAggregator());
|
||||
}
|
||||
});
|
||||
Order order = new Order();
|
||||
order.setOrderDate(new Date());
|
||||
order.setCustomer(new Customer());
|
||||
order.setBillingAddress(new Address());
|
||||
order.setBilling(new BillingInfo());
|
||||
order.setLineItems(Collections.EMPTY_LIST);
|
||||
order.setTotalPrice(new BigDecimal(10));
|
||||
Object result = converter.transform(order);
|
||||
assertTrue(result instanceof Collection);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.dao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.transform.DelimitedLineAggregator;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class OrderTransformerTests extends TestCase {
|
||||
|
||||
private OrderTransformer converter = new OrderTransformer();
|
||||
|
||||
public void testConvert() throws Exception {
|
||||
converter.setAggregators(new HashMap() {
|
||||
{
|
||||
put("header", new DelimitedLineAggregator());
|
||||
put("customer", new DelimitedLineAggregator());
|
||||
put("address", new DelimitedLineAggregator());
|
||||
put("billing", new DelimitedLineAggregator());
|
||||
put("item", new DelimitedLineAggregator());
|
||||
put("footer", new DelimitedLineAggregator());
|
||||
}
|
||||
});
|
||||
Order order = new Order();
|
||||
order.setOrderDate(new Date());
|
||||
order.setCustomer(new Customer());
|
||||
order.setBillingAddress(new Address());
|
||||
order.setBilling(new BillingInfo());
|
||||
order.setLineItems(Collections.EMPTY_LIST);
|
||||
order.setTotalPrice(new BigDecimal(10));
|
||||
Object result = converter.transform(order);
|
||||
assertTrue(result instanceof Collection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for {@link GeneratingItemReader}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class GeneratingItemReaderTests extends TestCase {
|
||||
|
||||
private GeneratingItemReader reader = new GeneratingItemReader();
|
||||
|
||||
/**
|
||||
* Generates a given number of not-null records,
|
||||
* consecutive calls return null.
|
||||
*/
|
||||
public void testRead() throws Exception {
|
||||
int counter = 0;
|
||||
int limit = 10;
|
||||
reader.setLimit(limit);
|
||||
|
||||
while (reader.read() != null) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
assertEquals(null, reader.read());
|
||||
assertEquals(limit, counter);
|
||||
assertEquals(counter, reader.getCounter());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for {@link GeneratingItemReader}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class GeneratingItemReaderTests extends TestCase {
|
||||
|
||||
private GeneratingItemReader reader = new GeneratingItemReader();
|
||||
|
||||
/**
|
||||
* Generates a given number of not-null records,
|
||||
* consecutive calls return null.
|
||||
*/
|
||||
public void testRead() throws Exception {
|
||||
int counter = 0;
|
||||
int limit = 10;
|
||||
reader.setLimit(limit);
|
||||
|
||||
while (reader.read() != null) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
assertEquals(null, reader.read());
|
||||
assertEquals(limit, counter);
|
||||
assertEquals(counter, reader.getCounter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,151 +1,151 @@
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
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;
|
||||
|
||||
public class OrderItemReaderTests extends TestCase {
|
||||
|
||||
private OrderItemReader provider;
|
||||
private MockControl inputControl;
|
||||
private ItemReader input;
|
||||
private MockControl mapperControl;
|
||||
private FieldSetMapper mapper;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
inputControl = MockControl.createControl(ItemReader.class);
|
||||
input = (ItemReader)inputControl.getMock();
|
||||
|
||||
provider = new OrderItemReader();
|
||||
provider.setItemReader(input);
|
||||
}
|
||||
|
||||
/*
|
||||
* OrderItemProvider is resposible for retrieving validated value object from input source.
|
||||
* OrderItemProvider.next():
|
||||
* - reads lines from the input source - returned as fieldsets
|
||||
* - pass fieldsets to the mapper - mapper will create value object
|
||||
* - pass value object to validator
|
||||
* - returns validated object
|
||||
*
|
||||
* In testNext method we are going to test these responsibilities. So we need create mock
|
||||
* objects for input source, mapper and validator.
|
||||
*/
|
||||
public void testNext() throws Exception {
|
||||
|
||||
//create fieldsets and set return values for input source
|
||||
FieldSet headerFS = new DefaultFieldSet(new String[] {Order.LINE_ID_HEADER});
|
||||
FieldSet customerFS = new DefaultFieldSet(new String[] {Customer.LINE_ID_NON_BUSINESS_CUST});
|
||||
FieldSet billingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_BILLING_ADDR});
|
||||
FieldSet shippingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_SHIPPING_ADDR});
|
||||
FieldSet billingInfoFS = new DefaultFieldSet(new String[] {BillingInfo.LINE_ID_BILLING_INFO});
|
||||
FieldSet shippingInfoFS = new DefaultFieldSet(new String[] {ShippingInfo.LINE_ID_SHIPPING_INFO});
|
||||
FieldSet itemFS = new DefaultFieldSet(new String[] {LineItem.LINE_ID_ITEM});
|
||||
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();
|
||||
|
||||
//create value objects
|
||||
Order order = new Order();
|
||||
Customer customer = new Customer();
|
||||
Address billing = new Address();
|
||||
Address shipping = new Address();
|
||||
BillingInfo billingInfo = new BillingInfo();
|
||||
ShippingInfo shippingInfo = new ShippingInfo();
|
||||
LineItem item = new LineItem();
|
||||
|
||||
//create mock mapper
|
||||
mapperControl = MockControl.createControl(FieldSetMapper.class);
|
||||
mapper = (FieldSetMapper)mapperControl.getMock();
|
||||
//set how mapper should respond - set return values for mapper
|
||||
mapper.mapLine(headerFS);
|
||||
mapperControl.setReturnValue(order);
|
||||
mapper.mapLine(customerFS);
|
||||
mapperControl.setReturnValue(customer);
|
||||
mapper.mapLine(billingFS);
|
||||
mapperControl.setReturnValue(billing);
|
||||
mapper.mapLine(shippingFS);
|
||||
mapperControl.setReturnValue(shipping);
|
||||
mapper.mapLine(billingInfoFS);
|
||||
mapperControl.setReturnValue(billingInfo);
|
||||
mapper.mapLine(shippingInfoFS);
|
||||
mapperControl.setReturnValue(shippingInfo);
|
||||
mapper.mapLine(itemFS);
|
||||
mapperControl.setReturnValue(item,3);
|
||||
mapperControl.replay();
|
||||
|
||||
|
||||
//set-up provider: set mappers
|
||||
provider.setAddressMapper(mapper);
|
||||
provider.setBillingMapper(mapper);
|
||||
provider.setCustomerMapper(mapper);
|
||||
provider.setHeaderMapper(mapper);
|
||||
provider.setItemMapper(mapper);
|
||||
provider.setShippingMapper(mapper);
|
||||
|
||||
//call tested method
|
||||
Object result = provider.read();
|
||||
|
||||
//verify result
|
||||
assertNotNull(result);
|
||||
//result should be Order
|
||||
assertTrue(result instanceof Order);
|
||||
|
||||
//verify whether order is constructed correctly
|
||||
//Order object should contain same instances as returned by mapper
|
||||
Order o = (Order) result;
|
||||
assertEquals(o,order);
|
||||
assertEquals(o.getCustomer(),customer);
|
||||
//is it non-bussines customer
|
||||
assertFalse(o.getCustomer().isBusinessCustomer());
|
||||
assertEquals(o.getBillingAddress(),billing);
|
||||
assertEquals(o.getShippingAddress(),shipping);
|
||||
assertEquals(o.getBilling(),billingInfo);
|
||||
assertEquals(o.getShipping(), shippingInfo);
|
||||
//there should be 3 line items
|
||||
assertEquals(3, o.getLineItems().size());
|
||||
for (Iterator i = o.getLineItems().iterator(); i.hasNext();) {
|
||||
assertEquals(i.next(),item);
|
||||
}
|
||||
|
||||
//try to retrieve next object - nothing should be returned
|
||||
assertNull(provider.read());
|
||||
|
||||
//verify method calls on input source, mapper and validator
|
||||
inputControl.verify();
|
||||
mapperControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
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;
|
||||
|
||||
public class OrderItemReaderTests extends TestCase {
|
||||
|
||||
private OrderItemReader provider;
|
||||
private MockControl inputControl;
|
||||
private ItemReader input;
|
||||
private MockControl mapperControl;
|
||||
private FieldSetMapper mapper;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
inputControl = MockControl.createControl(ItemReader.class);
|
||||
input = (ItemReader)inputControl.getMock();
|
||||
|
||||
provider = new OrderItemReader();
|
||||
provider.setItemReader(input);
|
||||
}
|
||||
|
||||
/*
|
||||
* OrderItemProvider is resposible for retrieving validated value object from input source.
|
||||
* OrderItemProvider.next():
|
||||
* - reads lines from the input source - returned as fieldsets
|
||||
* - pass fieldsets to the mapper - mapper will create value object
|
||||
* - pass value object to validator
|
||||
* - returns validated object
|
||||
*
|
||||
* In testNext method we are going to test these responsibilities. So we need create mock
|
||||
* objects for input source, mapper and validator.
|
||||
*/
|
||||
public void testNext() throws Exception {
|
||||
|
||||
//create fieldsets and set return values for input source
|
||||
FieldSet headerFS = new DefaultFieldSet(new String[] {Order.LINE_ID_HEADER});
|
||||
FieldSet customerFS = new DefaultFieldSet(new String[] {Customer.LINE_ID_NON_BUSINESS_CUST});
|
||||
FieldSet billingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_BILLING_ADDR});
|
||||
FieldSet shippingFS = new DefaultFieldSet(new String[] {Address.LINE_ID_SHIPPING_ADDR});
|
||||
FieldSet billingInfoFS = new DefaultFieldSet(new String[] {BillingInfo.LINE_ID_BILLING_INFO});
|
||||
FieldSet shippingInfoFS = new DefaultFieldSet(new String[] {ShippingInfo.LINE_ID_SHIPPING_INFO});
|
||||
FieldSet itemFS = new DefaultFieldSet(new String[] {LineItem.LINE_ID_ITEM});
|
||||
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();
|
||||
|
||||
//create value objects
|
||||
Order order = new Order();
|
||||
Customer customer = new Customer();
|
||||
Address billing = new Address();
|
||||
Address shipping = new Address();
|
||||
BillingInfo billingInfo = new BillingInfo();
|
||||
ShippingInfo shippingInfo = new ShippingInfo();
|
||||
LineItem item = new LineItem();
|
||||
|
||||
//create mock mapper
|
||||
mapperControl = MockControl.createControl(FieldSetMapper.class);
|
||||
mapper = (FieldSetMapper)mapperControl.getMock();
|
||||
//set how mapper should respond - set return values for mapper
|
||||
mapper.mapLine(headerFS);
|
||||
mapperControl.setReturnValue(order);
|
||||
mapper.mapLine(customerFS);
|
||||
mapperControl.setReturnValue(customer);
|
||||
mapper.mapLine(billingFS);
|
||||
mapperControl.setReturnValue(billing);
|
||||
mapper.mapLine(shippingFS);
|
||||
mapperControl.setReturnValue(shipping);
|
||||
mapper.mapLine(billingInfoFS);
|
||||
mapperControl.setReturnValue(billingInfo);
|
||||
mapper.mapLine(shippingInfoFS);
|
||||
mapperControl.setReturnValue(shippingInfo);
|
||||
mapper.mapLine(itemFS);
|
||||
mapperControl.setReturnValue(item,3);
|
||||
mapperControl.replay();
|
||||
|
||||
|
||||
//set-up provider: set mappers
|
||||
provider.setAddressMapper(mapper);
|
||||
provider.setBillingMapper(mapper);
|
||||
provider.setCustomerMapper(mapper);
|
||||
provider.setHeaderMapper(mapper);
|
||||
provider.setItemMapper(mapper);
|
||||
provider.setShippingMapper(mapper);
|
||||
|
||||
//call tested method
|
||||
Object result = provider.read();
|
||||
|
||||
//verify result
|
||||
assertNotNull(result);
|
||||
//result should be Order
|
||||
assertTrue(result instanceof Order);
|
||||
|
||||
//verify whether order is constructed correctly
|
||||
//Order object should contain same instances as returned by mapper
|
||||
Order o = (Order) result;
|
||||
assertEquals(o,order);
|
||||
assertEquals(o.getCustomer(),customer);
|
||||
//is it non-bussines customer
|
||||
assertFalse(o.getCustomer().isBusinessCustomer());
|
||||
assertEquals(o.getBillingAddress(),billing);
|
||||
assertEquals(o.getShippingAddress(),shipping);
|
||||
assertEquals(o.getBilling(),billingInfo);
|
||||
assertEquals(o.getShipping(), shippingInfo);
|
||||
//there should be 3 line items
|
||||
assertEquals(3, o.getLineItems().size());
|
||||
for (Iterator i = o.getLineItems().iterator(); i.hasNext();) {
|
||||
assertEquals(i.next(),item);
|
||||
}
|
||||
|
||||
//try to retrieve next object - nothing should be returned
|
||||
assertNull(provider.read());
|
||||
|
||||
//verify method calls on input source, mapper and validator
|
||||
inputControl.verify();
|
||||
mapperControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,154 +1,154 @@
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.batch.sample.tasklet.StepSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemReaderTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private StagingItemWriter writer;
|
||||
|
||||
private StagingItemReader reader;
|
||||
|
||||
private Long jobId = new Long(11);
|
||||
|
||||
public void setProcessor(StagingItemWriter writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public void setProvider(StagingItemReader reader) {
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
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 {
|
||||
StepExecution stepExecution = new StepExecution(new StepSupport("stepName"),
|
||||
new JobExecution(new JobInstance(jobId, new JobParameters(), new JobSupport("testJob"))));
|
||||
reader.beforeStep(stepExecution);
|
||||
writer.beforeStep(stepExecution);
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
writer.write("FOO");
|
||||
writer.write("BAR");
|
||||
writer.write("SPAM");
|
||||
writer.write("BUCKET");
|
||||
reader.open(new ExecutionContext());
|
||||
}
|
||||
|
||||
protected void onTearDownAfterTransaction() throws Exception {
|
||||
reader.close(null);
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
assertEquals(StagingItemWriter.DONE, after);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void testProviderRollsBackMultipleTimes() throws Exception {
|
||||
|
||||
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);
|
||||
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
item = reader.read();
|
||||
assertEquals("SPAM", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
}
|
||||
|
||||
public void testProviderRollsBackProcessIndicator() throws Exception {
|
||||
|
||||
reader.mark();
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
|
||||
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);
|
||||
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
|
||||
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemWriter.NEW, after);
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.item.reader;
|
||||
|
||||
import org.springframework.batch.core.domain.JobExecution;
|
||||
import org.springframework.batch.core.domain.JobInstance;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.sample.item.writer.StagingItemWriter;
|
||||
import org.springframework.batch.sample.tasklet.JobSupport;
|
||||
import org.springframework.batch.sample.tasklet.StepSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemReaderTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private StagingItemWriter writer;
|
||||
|
||||
private StagingItemReader reader;
|
||||
|
||||
private Long jobId = new Long(11);
|
||||
|
||||
public void setProcessor(StagingItemWriter writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public void setProvider(StagingItemReader reader) {
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
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 {
|
||||
StepExecution stepExecution = new StepExecution(new StepSupport("stepName"),
|
||||
new JobExecution(new JobInstance(jobId, new JobParameters(), new JobSupport("testJob"))));
|
||||
reader.beforeStep(stepExecution);
|
||||
writer.beforeStep(stepExecution);
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
writer.write("FOO");
|
||||
writer.write("BAR");
|
||||
writer.write("SPAM");
|
||||
writer.write("BUCKET");
|
||||
reader.open(new ExecutionContext());
|
||||
}
|
||||
|
||||
protected void onTearDownAfterTransaction() throws Exception {
|
||||
reader.close(null);
|
||||
getJdbcTemplate().update("DELETE FROM BATCH_STAGING");
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
assertEquals(StagingItemWriter.DONE, after);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void testProviderRollsBackMultipleTimes() throws Exception {
|
||||
|
||||
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);
|
||||
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
item = reader.read();
|
||||
assertEquals("BAR", item);
|
||||
item = reader.read();
|
||||
assertEquals("SPAM", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
}
|
||||
|
||||
public void testProviderRollsBackProcessIndicator() throws Exception {
|
||||
|
||||
reader.mark();
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
|
||||
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);
|
||||
|
||||
Object item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
reader.reset();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
|
||||
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemWriter.NEW, after);
|
||||
|
||||
item = reader.read();
|
||||
assertEquals("FOO", item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
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.CustomerCreditIncreaseWriter;
|
||||
|
||||
/**
|
||||
* Tests for {@link CustomerCreditIncreaseWriter}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerCreditIncreaseProcessorTests extends TestCase{
|
||||
|
||||
private CustomerCreditIncreaseWriter writer = new CustomerCreditIncreaseWriter();
|
||||
|
||||
private CustomerCreditDao outputSource;
|
||||
private MockControl outputSourceControl = MockControl.createStrictControl(CustomerCreditDao.class);
|
||||
|
||||
private CustomerCredit customerCredit = new CustomerCredit();
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
customerCredit.setId(1);
|
||||
customerCredit.setName("testCustomer");
|
||||
|
||||
outputSource = (CustomerCreditDao) outputSourceControl.getMock();
|
||||
writer.setCustomerCreditDao(outputSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases customer's credit by fixed value
|
||||
*/
|
||||
public void testProcess() throws Exception {
|
||||
BigDecimal oldCredit = new BigDecimal(10.54);
|
||||
customerCredit.setCredit(oldCredit);
|
||||
|
||||
outputSource.writeCredit(customerCredit);
|
||||
outputSourceControl.setVoidCallable();
|
||||
outputSourceControl.replay();
|
||||
|
||||
writer.write(customerCredit);
|
||||
|
||||
BigDecimal newCredit = customerCredit.getCredit();
|
||||
BigDecimal expectedCredit = oldCredit.add(CustomerCreditIncreaseWriter.FIXED_AMOUNT);
|
||||
assertTrue(newCredit.compareTo(expectedCredit) == 0);
|
||||
outputSourceControl.verify();
|
||||
}
|
||||
}
|
||||
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.CustomerCreditIncreaseWriter;
|
||||
|
||||
/**
|
||||
* Tests for {@link CustomerCreditIncreaseWriter}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CustomerCreditIncreaseProcessorTests extends TestCase{
|
||||
|
||||
private CustomerCreditIncreaseWriter writer = new CustomerCreditIncreaseWriter();
|
||||
|
||||
private CustomerCreditDao outputSource;
|
||||
private MockControl outputSourceControl = MockControl.createStrictControl(CustomerCreditDao.class);
|
||||
|
||||
private CustomerCredit customerCredit = new CustomerCredit();
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
customerCredit.setId(1);
|
||||
customerCredit.setName("testCustomer");
|
||||
|
||||
outputSource = (CustomerCreditDao) outputSourceControl.getMock();
|
||||
writer.setCustomerCreditDao(outputSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases customer's credit by fixed value
|
||||
*/
|
||||
public void testProcess() throws Exception {
|
||||
BigDecimal oldCredit = new BigDecimal(10.54);
|
||||
customerCredit.setCredit(oldCredit);
|
||||
|
||||
outputSource.writeCredit(customerCredit);
|
||||
outputSourceControl.setVoidCallable();
|
||||
outputSourceControl.replay();
|
||||
|
||||
writer.write(customerCredit);
|
||||
|
||||
BigDecimal newCredit = customerCredit.getCredit();
|
||||
BigDecimal expectedCredit = oldCredit.add(CustomerCreditIncreaseWriter.FIXED_AMOUNT);
|
||||
assertTrue(newCredit.compareTo(expectedCredit) == 0);
|
||||
outputSourceControl.verify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
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;
|
||||
|
||||
public class CustomerCreditUpdateProcessorTests extends TestCase {
|
||||
|
||||
private MockControl daoControl;
|
||||
private CustomerCreditDao dao;
|
||||
private CustomerCreditUpdateWriter writer;
|
||||
private static final double CREDIT_FILTER = 355.0;
|
||||
|
||||
public void setUp() {
|
||||
//create mock writer
|
||||
daoControl = MockControl.createControl(CustomerCreditDao.class);
|
||||
dao = (CustomerCreditDao) daoControl.getMock();
|
||||
//create processor, set writer and credit filter
|
||||
writer = new CustomerCreditUpdateWriter();
|
||||
writer.setWriter(dao);
|
||||
writer.setCreditFilter(CREDIT_FILTER);
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
//set-up mock writer - no writer's method should be called
|
||||
daoControl.replay();
|
||||
|
||||
//create credit and set it to same value as credit filter
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setCredit(new BigDecimal(CREDIT_FILTER));
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
//verify method calls - no method should be called
|
||||
//because credit is not greater then credit filter
|
||||
daoControl.verify();
|
||||
|
||||
//change credit to be greater than credit filter
|
||||
credit.setCredit(new BigDecimal(CREDIT_FILTER + 1));
|
||||
//reset and set-up writer - write method is expected to be called
|
||||
daoControl.reset();
|
||||
dao.writeCredit(credit);
|
||||
daoControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
|
||||
//verify method calls
|
||||
daoControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
|
||||
public class CustomerCreditUpdateProcessorTests extends TestCase {
|
||||
|
||||
private MockControl daoControl;
|
||||
private CustomerCreditDao dao;
|
||||
private CustomerCreditUpdateWriter writer;
|
||||
private static final double CREDIT_FILTER = 355.0;
|
||||
|
||||
public void setUp() {
|
||||
//create mock writer
|
||||
daoControl = MockControl.createControl(CustomerCreditDao.class);
|
||||
dao = (CustomerCreditDao) daoControl.getMock();
|
||||
//create processor, set writer and credit filter
|
||||
writer = new CustomerCreditUpdateWriter();
|
||||
writer.setWriter(dao);
|
||||
writer.setCreditFilter(CREDIT_FILTER);
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
//set-up mock writer - no writer's method should be called
|
||||
daoControl.replay();
|
||||
|
||||
//create credit and set it to same value as credit filter
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setCredit(new BigDecimal(CREDIT_FILTER));
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
//verify method calls - no method should be called
|
||||
//because credit is not greater then credit filter
|
||||
daoControl.verify();
|
||||
|
||||
//change credit to be greater than credit filter
|
||||
credit.setCredit(new BigDecimal(CREDIT_FILTER + 1));
|
||||
//reset and set-up writer - write method is expected to be called
|
||||
daoControl.reset();
|
||||
dao.writeCredit(credit);
|
||||
daoControl.replay();
|
||||
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
|
||||
//verify method calls
|
||||
daoControl.verify();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.sample.dao.JdbcCustomerDebitDao;
|
||||
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 void testProcess() {
|
||||
|
||||
//create trade object
|
||||
Trade trade = new Trade();
|
||||
trade.setCustomer("testCustomerName");
|
||||
trade.setPrice(new BigDecimal(123.0));
|
||||
|
||||
//create dao
|
||||
JdbcCustomerDebitDao dao = new JdbcCustomerDebitDao() {
|
||||
public void write(CustomerDebit customerDebit) {
|
||||
assertEquals("testCustomerName", customerDebit.getName());
|
||||
assertEquals(new BigDecimal(123.0), customerDebit.getDebit());
|
||||
}
|
||||
};
|
||||
|
||||
//create processor and set dao
|
||||
CustomerUpdateWriter processor = new CustomerUpdateWriter();
|
||||
processor.setDao(dao);
|
||||
|
||||
//call tested method - see asserts in dao.write() method
|
||||
processor.write(trade);
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.sample.dao.JdbcCustomerDebitDao;
|
||||
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 void testProcess() {
|
||||
|
||||
//create trade object
|
||||
Trade trade = new Trade();
|
||||
trade.setCustomer("testCustomerName");
|
||||
trade.setPrice(new BigDecimal(123.0));
|
||||
|
||||
//create dao
|
||||
JdbcCustomerDebitDao dao = new JdbcCustomerDebitDao() {
|
||||
public void write(CustomerDebit customerDebit) {
|
||||
assertEquals("testCustomerName", customerDebit.getName());
|
||||
assertEquals(new BigDecimal(123.0), customerDebit.getDebit());
|
||||
}
|
||||
};
|
||||
|
||||
//create processor and set dao
|
||||
CustomerUpdateWriter processor = new CustomerUpdateWriter();
|
||||
processor.setDao(dao);
|
||||
|
||||
//call tested method - see asserts in dao.write() method
|
||||
processor.write(trade);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +1,54 @@
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
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 {
|
||||
|
||||
private MockControl writerControl;
|
||||
private OrderWriter processor;
|
||||
private ItemWriter writer;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
//create mock writer
|
||||
writerControl = MockControl.createControl(ItemWriter.class);
|
||||
writer = (ItemWriter)writerControl.getMock();
|
||||
|
||||
//create processor
|
||||
processor = new OrderWriter();
|
||||
processor.setDelegate(writer);
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
Order order = new Order();
|
||||
//set-up mock writer
|
||||
writer.write(order);
|
||||
writerControl.replay();
|
||||
|
||||
//call tested method
|
||||
processor.write(order);
|
||||
|
||||
//verify method calls
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testProcessWithException() throws Exception {
|
||||
|
||||
writerControl.replay();
|
||||
//call tested method
|
||||
try {
|
||||
processor.write(this);
|
||||
fail("Batch critical exception was expected");
|
||||
} catch (InfrastructureException bce) {
|
||||
assertTrue(true);
|
||||
}
|
||||
writerControl.verify();
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
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 {
|
||||
|
||||
private MockControl writerControl;
|
||||
private OrderWriter processor;
|
||||
private ItemWriter writer;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
//create mock writer
|
||||
writerControl = MockControl.createControl(ItemWriter.class);
|
||||
writer = (ItemWriter)writerControl.getMock();
|
||||
|
||||
//create processor
|
||||
processor = new OrderWriter();
|
||||
processor.setDelegate(writer);
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
Order order = new Order();
|
||||
//set-up mock writer
|
||||
writer.write(order);
|
||||
writerControl.replay();
|
||||
|
||||
//call tested method
|
||||
processor.write(order);
|
||||
|
||||
//verify method calls
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testProcessWithException() throws Exception {
|
||||
|
||||
writerControl.replay();
|
||||
//call tested method
|
||||
try {
|
||||
processor.write(this);
|
||||
fail("Batch critical exception was expected");
|
||||
} catch (InfrastructureException bce) {
|
||||
assertTrue(true);
|
||||
}
|
||||
writerControl.verify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for {@link RetrySampleItemWriter}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RetrySampleItemWriterTests extends TestCase {
|
||||
|
||||
private RetrySampleItemWriter processor = new RetrySampleItemWriter();
|
||||
|
||||
/**
|
||||
* Processing throws exception on 2nd and 3rd call.
|
||||
*/
|
||||
public void testProcess() throws Exception {
|
||||
Object item = null;
|
||||
processor.write(item);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
processor.write(item);
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
processor.write(item);
|
||||
|
||||
assertEquals(4, processor.getCounter());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.item.writer;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests for {@link RetrySampleItemWriter}.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class RetrySampleItemWriterTests extends TestCase {
|
||||
|
||||
private RetrySampleItemWriter processor = new RetrySampleItemWriter();
|
||||
|
||||
/**
|
||||
* Processing throws exception on 2nd and 3rd call.
|
||||
*/
|
||||
public void testProcess() throws Exception {
|
||||
Object item = null;
|
||||
processor.write(item);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
processor.write(item);
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
processor.write(item);
|
||||
|
||||
assertEquals(4, processor.getCounter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
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;
|
||||
|
||||
public class TradeProcessorTests extends TestCase {
|
||||
|
||||
private MockControl writerControl;
|
||||
private TradeDao writer;
|
||||
private TradeWriter processor;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
//create mock writer
|
||||
writerControl = MockControl.createControl(TradeDao.class);
|
||||
writer = (TradeDao)writerControl.getMock();
|
||||
|
||||
//create processor
|
||||
processor = new TradeWriter();
|
||||
processor.setDao(writer);
|
||||
}
|
||||
|
||||
public void testProcess() {
|
||||
|
||||
Trade trade = new Trade();
|
||||
//set-up mock writer
|
||||
writer.writeTrade(trade);
|
||||
writerControl.replay();
|
||||
|
||||
//call tested method
|
||||
processor.write(trade);
|
||||
|
||||
//verify method calls
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testProcessNonTradeObject() {
|
||||
|
||||
writerControl.replay();
|
||||
//call tested method
|
||||
processor.write(this);
|
||||
writerControl.verify();
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
public class TradeProcessorTests extends TestCase {
|
||||
|
||||
private MockControl writerControl;
|
||||
private TradeDao writer;
|
||||
private TradeWriter processor;
|
||||
|
||||
public void setUp() {
|
||||
|
||||
//create mock writer
|
||||
writerControl = MockControl.createControl(TradeDao.class);
|
||||
writer = (TradeDao)writerControl.getMock();
|
||||
|
||||
//create processor
|
||||
processor = new TradeWriter();
|
||||
processor.setDao(writer);
|
||||
}
|
||||
|
||||
public void testProcess() {
|
||||
|
||||
Trade trade = new Trade();
|
||||
//set-up mock writer
|
||||
writer.writeTrade(trade);
|
||||
writerControl.replay();
|
||||
|
||||
//call tested method
|
||||
processor.write(trade);
|
||||
|
||||
//verify method calls
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testProcessNonTradeObject() {
|
||||
|
||||
writerControl.replay();
|
||||
//call tested method
|
||||
processor.write(this);
|
||||
writerControl.verify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Encapsulates basic logic for testing custom {@link FieldSetMapper} implementations.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractFieldSetMapperTests extends TestCase {
|
||||
|
||||
/**
|
||||
* @return <code>FieldSet</code> used for mapping
|
||||
*/
|
||||
protected abstract FieldSet fieldSet();
|
||||
|
||||
/**
|
||||
* @return domain object excepted as a result of mapping the <code>FieldSet</code>
|
||||
* returned by <code>this.fieldSet()</code>
|
||||
*/
|
||||
protected abstract Object expectedDomainObject();
|
||||
|
||||
/**
|
||||
* @return mapper which takes <code>this.fieldSet()</code> and maps it to
|
||||
* domain object.
|
||||
*/
|
||||
protected abstract FieldSetMapper fieldSetMapper();
|
||||
|
||||
|
||||
/**
|
||||
* Regular usage scenario.
|
||||
* Assumes the domain object implements sensible <code>equals(Object other)</code>
|
||||
*/
|
||||
public void testRegularUse() {
|
||||
assertEquals(expectedDomainObject(), fieldSetMapper().mapLine(fieldSet()));
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Encapsulates basic logic for testing custom {@link FieldSetMapper} implementations.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractFieldSetMapperTests extends TestCase {
|
||||
|
||||
/**
|
||||
* @return <code>FieldSet</code> used for mapping
|
||||
*/
|
||||
protected abstract FieldSet fieldSet();
|
||||
|
||||
/**
|
||||
* @return domain object excepted as a result of mapping the <code>FieldSet</code>
|
||||
* returned by <code>this.fieldSet()</code>
|
||||
*/
|
||||
protected abstract Object expectedDomainObject();
|
||||
|
||||
/**
|
||||
* @return mapper which takes <code>this.fieldSet()</code> and maps it to
|
||||
* domain object.
|
||||
*/
|
||||
protected abstract FieldSetMapper fieldSetMapper();
|
||||
|
||||
|
||||
/**
|
||||
* Regular usage scenario.
|
||||
* Assumes the domain object implements sensible <code>equals(Object other)</code>
|
||||
*/
|
||||
public void testRegularUse() {
|
||||
assertEquals(expectedDomainObject(), fieldSetMapper().mapLine(fieldSet()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* Encapsulates logic for testing custom {@link RowMapper} implementations.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractRowMapperTests extends TestCase {
|
||||
|
||||
//row number should be irrelevant
|
||||
private static final int IGNORED_ROW_NUMBER = 0;
|
||||
|
||||
//mock result set
|
||||
private MockControl rsControl = MockControl.createControl(ResultSet.class);
|
||||
private ResultSet rs = (ResultSet) rsControl.getMock();
|
||||
|
||||
/**
|
||||
* @return Expected result of mapping the mock <code>ResultSet</code> by
|
||||
* the mapper being tested.
|
||||
*/
|
||||
abstract protected Object expectedDomainObject();
|
||||
|
||||
/**
|
||||
* @return <code>RowMapper</code> implementation that is being tested.
|
||||
*/
|
||||
abstract protected RowMapper rowMapper();
|
||||
|
||||
/**
|
||||
* Define the behaviour of mock <code>ResultSet</code>.
|
||||
*/
|
||||
abstract protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException;
|
||||
|
||||
|
||||
/**
|
||||
* Regular usage scenario.
|
||||
*/
|
||||
public void testRegularUse() throws SQLException {
|
||||
setUpResultSetMock(rs, rsControl);
|
||||
rsControl.replay();
|
||||
|
||||
assertEquals(expectedDomainObject(), rowMapper().mapRow(rs, IGNORED_ROW_NUMBER));
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
/**
|
||||
* Encapsulates logic for testing custom {@link RowMapper} implementations.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractRowMapperTests extends TestCase {
|
||||
|
||||
//row number should be irrelevant
|
||||
private static final int IGNORED_ROW_NUMBER = 0;
|
||||
|
||||
//mock result set
|
||||
private MockControl rsControl = MockControl.createControl(ResultSet.class);
|
||||
private ResultSet rs = (ResultSet) rsControl.getMock();
|
||||
|
||||
/**
|
||||
* @return Expected result of mapping the mock <code>ResultSet</code> by
|
||||
* the mapper being tested.
|
||||
*/
|
||||
abstract protected Object expectedDomainObject();
|
||||
|
||||
/**
|
||||
* @return <code>RowMapper</code> implementation that is being tested.
|
||||
*/
|
||||
abstract protected RowMapper rowMapper();
|
||||
|
||||
/**
|
||||
* Define the behaviour of mock <code>ResultSet</code>.
|
||||
*/
|
||||
abstract protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException;
|
||||
|
||||
|
||||
/**
|
||||
* Regular usage scenario.
|
||||
*/
|
||||
public void testRegularUse() throws SQLException {
|
||||
setUpResultSetMock(rs, rsControl);
|
||||
rsControl.replay();
|
||||
|
||||
assertEquals(expectedDomainObject(), rowMapper().mapRow(rs, IGNORED_ROW_NUMBER));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.mapping.AddressFieldSetMapper;
|
||||
|
||||
|
||||
public class AddressFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final String ADDRESSEE = "Jan Hrach";
|
||||
private static final String ADDRESS_LINE_1 = "Plynarenska 7c";
|
||||
private static final String ADDRESS_LINE_2 = "";
|
||||
private static final String CITY = "Bratislava";
|
||||
private static final String STATE = "";
|
||||
private static final String COUNTRY = "Slovakia";
|
||||
private static final String ZIP_CODE = "80000";
|
||||
|
||||
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Address address = new Address();
|
||||
address.setAddressee(ADDRESSEE);
|
||||
address.setAddrLine1(ADDRESS_LINE_1);
|
||||
address.setAddrLine2(ADDRESS_LINE_2);
|
||||
address.setCity(CITY);
|
||||
address.setState(STATE);
|
||||
address.setCountry(COUNTRY);
|
||||
address.setZipCode(ZIP_CODE);
|
||||
return address;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens =
|
||||
new String[]{ADDRESSEE, ADDRESS_LINE_1, ADDRESS_LINE_2, CITY, STATE, COUNTRY, ZIP_CODE};
|
||||
String[] columnNames =
|
||||
new String[]{
|
||||
AddressFieldSetMapper.ADDRESSEE_COLUMN,
|
||||
AddressFieldSetMapper.ADDRESS_LINE1_COLUMN,
|
||||
AddressFieldSetMapper.ADDRESS_LINE2_COLUMN,
|
||||
AddressFieldSetMapper.CITY_COLUMN,
|
||||
AddressFieldSetMapper.STATE_COLUMN,
|
||||
AddressFieldSetMapper.COUNTRY_COLUMN,
|
||||
AddressFieldSetMapper.ZIP_CODE_COLUMN };
|
||||
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new AddressFieldSetMapper();
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Address;
|
||||
import org.springframework.batch.sample.mapping.AddressFieldSetMapper;
|
||||
|
||||
|
||||
public class AddressFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final String ADDRESSEE = "Jan Hrach";
|
||||
private static final String ADDRESS_LINE_1 = "Plynarenska 7c";
|
||||
private static final String ADDRESS_LINE_2 = "";
|
||||
private static final String CITY = "Bratislava";
|
||||
private static final String STATE = "";
|
||||
private static final String COUNTRY = "Slovakia";
|
||||
private static final String ZIP_CODE = "80000";
|
||||
|
||||
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Address address = new Address();
|
||||
address.setAddressee(ADDRESSEE);
|
||||
address.setAddrLine1(ADDRESS_LINE_1);
|
||||
address.setAddrLine2(ADDRESS_LINE_2);
|
||||
address.setCity(CITY);
|
||||
address.setState(STATE);
|
||||
address.setCountry(COUNTRY);
|
||||
address.setZipCode(ZIP_CODE);
|
||||
return address;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens =
|
||||
new String[]{ADDRESSEE, ADDRESS_LINE_1, ADDRESS_LINE_2, CITY, STATE, COUNTRY, ZIP_CODE};
|
||||
String[] columnNames =
|
||||
new String[]{
|
||||
AddressFieldSetMapper.ADDRESSEE_COLUMN,
|
||||
AddressFieldSetMapper.ADDRESS_LINE1_COLUMN,
|
||||
AddressFieldSetMapper.ADDRESS_LINE2_COLUMN,
|
||||
AddressFieldSetMapper.CITY_COLUMN,
|
||||
AddressFieldSetMapper.STATE_COLUMN,
|
||||
AddressFieldSetMapper.COUNTRY_COLUMN,
|
||||
AddressFieldSetMapper.ZIP_CODE_COLUMN };
|
||||
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new AddressFieldSetMapper();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
|
||||
public class BillingFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String PAYMENT_ID = "777";
|
||||
private static final String PAYMENT_DESC = "My last penny";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
BillingInfo bInfo = new BillingInfo();
|
||||
bInfo.setPaymentDesc(PAYMENT_DESC);
|
||||
bInfo.setPaymentId(PAYMENT_ID);
|
||||
return bInfo;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
PAYMENT_ID,
|
||||
PAYMENT_DESC};
|
||||
String[] columnNames = new String[]{
|
||||
BillingFieldSetMapper.PAYMENT_TYPE_ID_COLUMN,
|
||||
BillingFieldSetMapper.PAYMENT_DESC_COLUMN};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new BillingFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.BillingInfo;
|
||||
|
||||
public class BillingFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String PAYMENT_ID = "777";
|
||||
private static final String PAYMENT_DESC = "My last penny";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
BillingInfo bInfo = new BillingInfo();
|
||||
bInfo.setPaymentDesc(PAYMENT_DESC);
|
||||
bInfo.setPaymentId(PAYMENT_ID);
|
||||
return bInfo;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
PAYMENT_ID,
|
||||
PAYMENT_DESC};
|
||||
String[] columnNames = new String[]{
|
||||
BillingFieldSetMapper.PAYMENT_TYPE_ID_COLUMN,
|
||||
BillingFieldSetMapper.PAYMENT_DESC_COLUMN};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new BillingFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
import org.springframework.batch.sample.mapping.CustomerCreditRowMapper;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class CustomerCreditRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final int ID = 12;
|
||||
private static final String CUSTOMER = "Jozef Mak";
|
||||
private static final BigDecimal CREDIT = new BigDecimal(0.1);
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setId(ID);
|
||||
credit.setCredit(CREDIT);
|
||||
credit.setName(CUSTOMER);
|
||||
return credit;
|
||||
}
|
||||
|
||||
protected RowMapper rowMapper() {
|
||||
return new CustomerCreditRowMapper();
|
||||
}
|
||||
|
||||
protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException {
|
||||
rs.getInt(CustomerCreditRowMapper.ID_COLUMN);
|
||||
rsControl.setReturnValue(ID);
|
||||
rs.getString(CustomerCreditRowMapper.NAME_COLUMN);
|
||||
rsControl.setReturnValue(CUSTOMER);
|
||||
rs.getBigDecimal(CustomerCreditRowMapper.CREDIT_COLUMN);
|
||||
rsControl.setReturnValue(CREDIT);
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.sample.domain.CustomerCredit;
|
||||
import org.springframework.batch.sample.mapping.CustomerCreditRowMapper;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class CustomerCreditRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final int ID = 12;
|
||||
private static final String CUSTOMER = "Jozef Mak";
|
||||
private static final BigDecimal CREDIT = new BigDecimal(0.1);
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setId(ID);
|
||||
credit.setCredit(CREDIT);
|
||||
credit.setName(CUSTOMER);
|
||||
return credit;
|
||||
}
|
||||
|
||||
protected RowMapper rowMapper() {
|
||||
return new CustomerCreditRowMapper();
|
||||
}
|
||||
|
||||
protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException {
|
||||
rs.getInt(CustomerCreditRowMapper.ID_COLUMN);
|
||||
rsControl.setReturnValue(ID);
|
||||
rs.getString(CustomerCreditRowMapper.NAME_COLUMN);
|
||||
rsControl.setReturnValue(CUSTOMER);
|
||||
rs.getBigDecimal(CustomerCreditRowMapper.CREDIT_COLUMN);
|
||||
rsControl.setReturnValue(CREDIT);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.mapping.CustomerFieldSetMapper;
|
||||
|
||||
public class CustomerFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final boolean BUSINESS_CUSTOMER = false;
|
||||
//private static final String COMPANY_NAME = "Accenture";
|
||||
private static final String FIRST_NAME = "Jan";
|
||||
private static final String LAST_NAME = "Hrach";
|
||||
private static final String MIDDLE_NAME = "";
|
||||
private static final boolean REGISTERED = true;
|
||||
private static final long REG_ID = 1;
|
||||
private static final boolean VIP = true;
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Customer cs = new Customer();
|
||||
cs.setBusinessCustomer(BUSINESS_CUSTOMER);
|
||||
cs.setFirstName(FIRST_NAME);
|
||||
cs.setLastName(LAST_NAME);
|
||||
cs.setMiddleName(MIDDLE_NAME);
|
||||
cs.setRegistered(REGISTERED);
|
||||
cs.setRegistrationId(REG_ID);
|
||||
cs.setVip(VIP);
|
||||
return cs;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
Customer.LINE_ID_NON_BUSINESS_CUST,
|
||||
FIRST_NAME,
|
||||
LAST_NAME,
|
||||
MIDDLE_NAME,
|
||||
CustomerFieldSetMapper.TRUE_SYMBOL,
|
||||
String.valueOf(REG_ID),
|
||||
CustomerFieldSetMapper.TRUE_SYMBOL};
|
||||
String[] columnNames = new String[]{
|
||||
CustomerFieldSetMapper.LINE_ID_COLUMN,
|
||||
CustomerFieldSetMapper.FIRST_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.LAST_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.MIDDLE_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.REGISTERED_COLUMN,
|
||||
CustomerFieldSetMapper.REG_ID_COLUMN,
|
||||
CustomerFieldSetMapper.VIP_COLUMN};
|
||||
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new CustomerFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Customer;
|
||||
import org.springframework.batch.sample.mapping.CustomerFieldSetMapper;
|
||||
|
||||
public class CustomerFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final boolean BUSINESS_CUSTOMER = false;
|
||||
//private static final String COMPANY_NAME = "Accenture";
|
||||
private static final String FIRST_NAME = "Jan";
|
||||
private static final String LAST_NAME = "Hrach";
|
||||
private static final String MIDDLE_NAME = "";
|
||||
private static final boolean REGISTERED = true;
|
||||
private static final long REG_ID = 1;
|
||||
private static final boolean VIP = true;
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Customer cs = new Customer();
|
||||
cs.setBusinessCustomer(BUSINESS_CUSTOMER);
|
||||
cs.setFirstName(FIRST_NAME);
|
||||
cs.setLastName(LAST_NAME);
|
||||
cs.setMiddleName(MIDDLE_NAME);
|
||||
cs.setRegistered(REGISTERED);
|
||||
cs.setRegistrationId(REG_ID);
|
||||
cs.setVip(VIP);
|
||||
return cs;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
Customer.LINE_ID_NON_BUSINESS_CUST,
|
||||
FIRST_NAME,
|
||||
LAST_NAME,
|
||||
MIDDLE_NAME,
|
||||
CustomerFieldSetMapper.TRUE_SYMBOL,
|
||||
String.valueOf(REG_ID),
|
||||
CustomerFieldSetMapper.TRUE_SYMBOL};
|
||||
String[] columnNames = new String[]{
|
||||
CustomerFieldSetMapper.LINE_ID_COLUMN,
|
||||
CustomerFieldSetMapper.FIRST_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.LAST_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.MIDDLE_NAME_COLUMN,
|
||||
CustomerFieldSetMapper.REGISTERED_COLUMN,
|
||||
CustomerFieldSetMapper.REG_ID_COLUMN,
|
||||
CustomerFieldSetMapper.VIP_COLUMN};
|
||||
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new CustomerFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
public class HeaderFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final long ORDER_ID = 1;
|
||||
private static final String DATE = "2007-01-01";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Order order = new Order();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2007, 0, 1, 0, 0, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
order.setOrderDate(calendar.getTime());
|
||||
order.setOrderId(ORDER_ID);
|
||||
return order;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
String.valueOf(ORDER_ID),
|
||||
DATE
|
||||
};
|
||||
String[] columnNames = new String[]{
|
||||
HeaderFieldSetMapper.ORDER_ID_COLUMN,
|
||||
HeaderFieldSetMapper.ORDER_DATE_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new HeaderFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.Order;
|
||||
|
||||
public class HeaderFieldSetMapperTests extends AbstractFieldSetMapperTests {
|
||||
|
||||
private static final long ORDER_ID = 1;
|
||||
private static final String DATE = "2007-01-01";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Order order = new Order();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2007, 0, 1, 0, 0, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
order.setOrderDate(calendar.getTime());
|
||||
order.setOrderId(ORDER_ID);
|
||||
return order;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
String.valueOf(ORDER_ID),
|
||||
DATE
|
||||
};
|
||||
String[] columnNames = new String[]{
|
||||
HeaderFieldSetMapper.ORDER_ID_COLUMN,
|
||||
HeaderFieldSetMapper.ORDER_DATE_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new HeaderFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
public class OrderItemFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final BigDecimal DISCOUNT_AMOUNT = new BigDecimal(1);
|
||||
private static final BigDecimal DISCOUNT_PERC = new BigDecimal(2);
|
||||
private static final BigDecimal HANDLING_PRICE = new BigDecimal(3);
|
||||
private static final long ITEM_ID = 4;
|
||||
private static final BigDecimal PRICE = new BigDecimal(5);
|
||||
private static final int QUANTITY = 6;
|
||||
private static final BigDecimal SHIPPING_PRICE = new BigDecimal(7);
|
||||
private static final BigDecimal TOTAL_PRICE = new BigDecimal(8);
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(DISCOUNT_AMOUNT);
|
||||
item.setDiscountPerc(DISCOUNT_PERC);
|
||||
item.setHandlingPrice(HANDLING_PRICE);
|
||||
item.setItemId(ITEM_ID);
|
||||
item.setPrice(PRICE);
|
||||
item.setQuantity(QUANTITY);
|
||||
item.setShippingPrice(SHIPPING_PRICE);
|
||||
item.setTotalPrice(TOTAL_PRICE);
|
||||
return item;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
String.valueOf(DISCOUNT_AMOUNT),
|
||||
String.valueOf(DISCOUNT_PERC),
|
||||
String.valueOf(HANDLING_PRICE),
|
||||
String.valueOf(ITEM_ID),
|
||||
String.valueOf(PRICE),
|
||||
String.valueOf(QUANTITY),
|
||||
String.valueOf(SHIPPING_PRICE),
|
||||
String.valueOf(TOTAL_PRICE)
|
||||
};
|
||||
String[] columnNames = new String[]{
|
||||
OrderItemFieldSetMapper.DISCOUNT_AMOUNT_COLUMN,
|
||||
OrderItemFieldSetMapper.DISCOUNT_PERC_COLUMN,
|
||||
OrderItemFieldSetMapper.HANDLING_PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.ITEM_ID_COLUMN,
|
||||
OrderItemFieldSetMapper.PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.QUANTITY_COLUMN,
|
||||
OrderItemFieldSetMapper.SHIPPING_PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.TOTAL_PRICE_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new OrderItemFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
public class OrderItemFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final BigDecimal DISCOUNT_AMOUNT = new BigDecimal(1);
|
||||
private static final BigDecimal DISCOUNT_PERC = new BigDecimal(2);
|
||||
private static final BigDecimal HANDLING_PRICE = new BigDecimal(3);
|
||||
private static final long ITEM_ID = 4;
|
||||
private static final BigDecimal PRICE = new BigDecimal(5);
|
||||
private static final int QUANTITY = 6;
|
||||
private static final BigDecimal SHIPPING_PRICE = new BigDecimal(7);
|
||||
private static final BigDecimal TOTAL_PRICE = new BigDecimal(8);
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(DISCOUNT_AMOUNT);
|
||||
item.setDiscountPerc(DISCOUNT_PERC);
|
||||
item.setHandlingPrice(HANDLING_PRICE);
|
||||
item.setItemId(ITEM_ID);
|
||||
item.setPrice(PRICE);
|
||||
item.setQuantity(QUANTITY);
|
||||
item.setShippingPrice(SHIPPING_PRICE);
|
||||
item.setTotalPrice(TOTAL_PRICE);
|
||||
return item;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{
|
||||
String.valueOf(DISCOUNT_AMOUNT),
|
||||
String.valueOf(DISCOUNT_PERC),
|
||||
String.valueOf(HANDLING_PRICE),
|
||||
String.valueOf(ITEM_ID),
|
||||
String.valueOf(PRICE),
|
||||
String.valueOf(QUANTITY),
|
||||
String.valueOf(SHIPPING_PRICE),
|
||||
String.valueOf(TOTAL_PRICE)
|
||||
};
|
||||
String[] columnNames = new String[]{
|
||||
OrderItemFieldSetMapper.DISCOUNT_AMOUNT_COLUMN,
|
||||
OrderItemFieldSetMapper.DISCOUNT_PERC_COLUMN,
|
||||
OrderItemFieldSetMapper.HANDLING_PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.ITEM_ID_COLUMN,
|
||||
OrderItemFieldSetMapper.PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.QUANTITY_COLUMN,
|
||||
OrderItemFieldSetMapper.SHIPPING_PRICE_COLUMN,
|
||||
OrderItemFieldSetMapper.TOTAL_PRICE_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new OrderItemFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.ShippingInfo;
|
||||
import org.springframework.batch.sample.mapping.ShippingFieldSetMapper;
|
||||
|
||||
public class ShippingFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String SHIPPER_ID = "1";
|
||||
private static final String SHIPPING_INFO = "most interesting and informative shipping info ever";
|
||||
private static final String SHIPPING_TYPE_ID = "X";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
ShippingInfo info = new ShippingInfo();
|
||||
info.setShipperId(SHIPPER_ID);
|
||||
info.setShippingInfo(SHIPPING_INFO);
|
||||
info.setShippingTypeId(SHIPPING_TYPE_ID);
|
||||
return info;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{SHIPPER_ID, SHIPPING_INFO, SHIPPING_TYPE_ID};
|
||||
String[] columnNames = new String[]{
|
||||
ShippingFieldSetMapper.SHIPPER_ID_COLUMN,
|
||||
ShippingFieldSetMapper.ADDITIONAL_SHIPPING_INFO_COLUMN,
|
||||
ShippingFieldSetMapper.SHIPPING_TYPE_ID_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new ShippingFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.sample.domain.ShippingInfo;
|
||||
import org.springframework.batch.sample.mapping.ShippingFieldSetMapper;
|
||||
|
||||
public class ShippingFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String SHIPPER_ID = "1";
|
||||
private static final String SHIPPING_INFO = "most interesting and informative shipping info ever";
|
||||
private static final String SHIPPING_TYPE_ID = "X";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
ShippingInfo info = new ShippingInfo();
|
||||
info.setShipperId(SHIPPER_ID);
|
||||
info.setShippingInfo(SHIPPING_INFO);
|
||||
info.setShippingTypeId(SHIPPING_TYPE_ID);
|
||||
return info;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[]{SHIPPER_ID, SHIPPING_INFO, SHIPPING_TYPE_ID};
|
||||
String[] columnNames = new String[]{
|
||||
ShippingFieldSetMapper.SHIPPER_ID_COLUMN,
|
||||
ShippingFieldSetMapper.ADDITIONAL_SHIPPING_INFO_COLUMN,
|
||||
ShippingFieldSetMapper.SHIPPING_TYPE_ID_COLUMN
|
||||
};
|
||||
return new DefaultFieldSet(tokens, columnNames);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new ShippingFieldSetMapper();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.reader.AggregateItemReader;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.batch.sample.mapping.TradeFieldSetMapper;
|
||||
|
||||
public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String CUSTOMER = "Mike Tomcat";
|
||||
private static final BigDecimal PRICE = new BigDecimal(1.3);
|
||||
private static final long QUANTITY = 7;
|
||||
private static final String ISIN = "fj893gnsalX";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Trade trade = new Trade();
|
||||
trade.setIsin(ISIN);
|
||||
trade.setQuantity(QUANTITY);
|
||||
trade.setPrice(PRICE);
|
||||
trade.setCustomer(CUSTOMER);
|
||||
return trade;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[4];
|
||||
tokens[TradeFieldSetMapper.ISIN_COLUMN] = ISIN;
|
||||
tokens[TradeFieldSetMapper.QUANTITY_COLUMN] = String.valueOf(QUANTITY);
|
||||
tokens[TradeFieldSetMapper.PRICE_COLUMN] = String.valueOf(PRICE);
|
||||
tokens[TradeFieldSetMapper.CUSTOMER_COLUMN] = CUSTOMER;
|
||||
|
||||
return new DefaultFieldSet(tokens);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new TradeFieldSetMapper();
|
||||
}
|
||||
|
||||
public void testBeginRecord() throws Exception {
|
||||
assertEquals(AggregateItemReader.BEGIN_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"BEGIN"})));
|
||||
}
|
||||
|
||||
public void testEndRecord() throws Exception {
|
||||
assertEquals(AggregateItemReader.END_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"END"})));
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.batch.io.file.mapping.DefaultFieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSet;
|
||||
import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.item.reader.AggregateItemReader;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.batch.sample.mapping.TradeFieldSetMapper;
|
||||
|
||||
public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests{
|
||||
|
||||
private static final String CUSTOMER = "Mike Tomcat";
|
||||
private static final BigDecimal PRICE = new BigDecimal(1.3);
|
||||
private static final long QUANTITY = 7;
|
||||
private static final String ISIN = "fj893gnsalX";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Trade trade = new Trade();
|
||||
trade.setIsin(ISIN);
|
||||
trade.setQuantity(QUANTITY);
|
||||
trade.setPrice(PRICE);
|
||||
trade.setCustomer(CUSTOMER);
|
||||
return trade;
|
||||
}
|
||||
|
||||
protected FieldSet fieldSet() {
|
||||
String[] tokens = new String[4];
|
||||
tokens[TradeFieldSetMapper.ISIN_COLUMN] = ISIN;
|
||||
tokens[TradeFieldSetMapper.QUANTITY_COLUMN] = String.valueOf(QUANTITY);
|
||||
tokens[TradeFieldSetMapper.PRICE_COLUMN] = String.valueOf(PRICE);
|
||||
tokens[TradeFieldSetMapper.CUSTOMER_COLUMN] = CUSTOMER;
|
||||
|
||||
return new DefaultFieldSet(tokens);
|
||||
}
|
||||
|
||||
protected FieldSetMapper fieldSetMapper() {
|
||||
return new TradeFieldSetMapper();
|
||||
}
|
||||
|
||||
public void testBeginRecord() throws Exception {
|
||||
assertEquals(AggregateItemReader.BEGIN_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"BEGIN"})));
|
||||
}
|
||||
|
||||
public void testEndRecord() throws Exception {
|
||||
assertEquals(AggregateItemReader.END_RECORD, fieldSetMapper().mapLine(new DefaultFieldSet(new String[] {"END"})));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.batch.sample.mapping.TradeRowMapper;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class TradeRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
private static final String ISIN = "jsgk342";
|
||||
private static final long QUANTITY = 0;
|
||||
private static final BigDecimal PRICE = new BigDecimal(1.1);
|
||||
private static final String CUSTOMER = "Martin Hrancok";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Trade trade = new Trade();
|
||||
trade.setIsin(ISIN);
|
||||
trade.setQuantity(QUANTITY);
|
||||
trade.setPrice(PRICE);
|
||||
trade.setCustomer(CUSTOMER);
|
||||
return trade;
|
||||
}
|
||||
|
||||
protected RowMapper rowMapper() {
|
||||
return new TradeRowMapper();
|
||||
}
|
||||
|
||||
protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException {
|
||||
rs.getString(TradeRowMapper.ISIN_COLUMN);
|
||||
rsControl.setReturnValue(ISIN);
|
||||
|
||||
rs.getLong(TradeRowMapper.QUANTITY_COLUMN);
|
||||
rsControl.setReturnValue(QUANTITY);
|
||||
|
||||
rs.getBigDecimal(TradeRowMapper.PRICE_COLUMN);
|
||||
rsControl.setReturnValue(PRICE);
|
||||
|
||||
rs.getString(TradeRowMapper.CUSTOMER_COLUMN);
|
||||
rsControl.setReturnValue(CUSTOMER);
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.mapping;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
import org.springframework.batch.sample.mapping.TradeRowMapper;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class TradeRowMapperTests extends AbstractRowMapperTests {
|
||||
|
||||
private static final String ISIN = "jsgk342";
|
||||
private static final long QUANTITY = 0;
|
||||
private static final BigDecimal PRICE = new BigDecimal(1.1);
|
||||
private static final String CUSTOMER = "Martin Hrancok";
|
||||
|
||||
protected Object expectedDomainObject() {
|
||||
Trade trade = new Trade();
|
||||
trade.setIsin(ISIN);
|
||||
trade.setQuantity(QUANTITY);
|
||||
trade.setPrice(PRICE);
|
||||
trade.setCustomer(CUSTOMER);
|
||||
return trade;
|
||||
}
|
||||
|
||||
protected RowMapper rowMapper() {
|
||||
return new TradeRowMapper();
|
||||
}
|
||||
|
||||
protected void setUpResultSetMock(ResultSet rs, MockControl rsControl) throws SQLException {
|
||||
rs.getString(TradeRowMapper.ISIN_COLUMN);
|
||||
rsControl.setReturnValue(ISIN);
|
||||
|
||||
rs.getLong(TradeRowMapper.QUANTITY_COLUMN);
|
||||
rsControl.setReturnValue(QUANTITY);
|
||||
|
||||
rs.getBigDecimal(TradeRowMapper.PRICE_COLUMN);
|
||||
rsControl.setReturnValue(PRICE);
|
||||
|
||||
rs.getString(TradeRowMapper.CUSTOMER_COLUMN);
|
||||
rsControl.setReturnValue(CUSTOMER);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
import org.springframework.batch.item.reader.ListItemReader;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
|
||||
public class ExceptionThrowingItemReaderProxyTests extends TestCase {
|
||||
|
||||
//expected call count before exception is thrown (exception should be thrown in next iteration)
|
||||
private static final int ITER_COUNT = 5;
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
RepeatSynchronizationManager.clear();
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
//create module and set item processor and iteration count
|
||||
ExceptionThrowingItemReaderProxy itemReader = new ExceptionThrowingItemReaderProxy();
|
||||
itemReader.setItemReader(new ListItemReader(new ArrayList() {{
|
||||
add("a");
|
||||
add("b");
|
||||
add("c");
|
||||
add("d");
|
||||
add("e");
|
||||
add("f");
|
||||
}}));
|
||||
|
||||
itemReader.setThrowExceptionOnRecordNumber(ITER_COUNT + 1);
|
||||
|
||||
RepeatSynchronizationManager.register(new RepeatContextSupport(null));
|
||||
|
||||
//call process method multiple times and verify whether exception is thrown when expected
|
||||
for (int i = 0; i <= ITER_COUNT; i++) {
|
||||
try {
|
||||
itemReader.read();
|
||||
assertTrue(i < ITER_COUNT);
|
||||
} catch (InfrastructureException bce) {
|
||||
assertEquals(ITER_COUNT,i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
import org.springframework.batch.item.reader.ListItemReader;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
|
||||
public class ExceptionThrowingItemReaderProxyTests extends TestCase {
|
||||
|
||||
//expected call count before exception is thrown (exception should be thrown in next iteration)
|
||||
private static final int ITER_COUNT = 5;
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
RepeatSynchronizationManager.clear();
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
|
||||
//create module and set item processor and iteration count
|
||||
ExceptionThrowingItemReaderProxy itemReader = new ExceptionThrowingItemReaderProxy();
|
||||
itemReader.setItemReader(new ListItemReader(new ArrayList() {{
|
||||
add("a");
|
||||
add("b");
|
||||
add("c");
|
||||
add("d");
|
||||
add("e");
|
||||
add("f");
|
||||
}}));
|
||||
|
||||
itemReader.setThrowExceptionOnRecordNumber(ITER_COUNT + 1);
|
||||
|
||||
RepeatSynchronizationManager.register(new RepeatContextSupport(null));
|
||||
|
||||
//call process method multiple times and verify whether exception is thrown when expected
|
||||
for (int i = 0; i <= ITER_COUNT; i++) {
|
||||
try {
|
||||
itemReader.read();
|
||||
assertTrue(i < ITER_COUNT);
|
||||
} catch (InfrastructureException bce) {
|
||||
assertEquals(ITER_COUNT,i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.dao.TradeDao;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
|
||||
public class SimpleTradeTaskletTests extends TestCase {
|
||||
|
||||
private boolean inputCalled = false;
|
||||
private boolean writerCalled = false;
|
||||
|
||||
public void testReadAndProcess() throws Exception {
|
||||
|
||||
//create input
|
||||
FlatFileItemReader input = new FlatFileItemReader() {
|
||||
|
||||
private boolean done = false;
|
||||
|
||||
public Object read() {
|
||||
if (!done) {
|
||||
Trade trade = new Trade("1234", 5, new BigDecimal(100), "testName");
|
||||
inputCalled = true;
|
||||
done = true;
|
||||
return trade;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//create writer
|
||||
TradeDao dao = new TradeDao() {
|
||||
public void writeTrade(Trade trade) {
|
||||
assertEquals("1234",trade.getIsin());
|
||||
assertEquals(5, trade.getQuantity());
|
||||
assertEquals(new BigDecimal(100), trade.getPrice());
|
||||
assertEquals("testName", trade.getCustomer());
|
||||
writerCalled = true;
|
||||
}
|
||||
};
|
||||
|
||||
//create module
|
||||
SimpleTradeWriter module = new SimpleTradeWriter();
|
||||
module.setTradeDao(dao);
|
||||
|
||||
module.write(input.read());
|
||||
|
||||
//verify whether input and writer were called
|
||||
assertTrue(inputCalled);
|
||||
assertTrue(writerCalled);
|
||||
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.dao.TradeDao;
|
||||
import org.springframework.batch.sample.domain.Trade;
|
||||
|
||||
public class SimpleTradeTaskletTests extends TestCase {
|
||||
|
||||
private boolean inputCalled = false;
|
||||
private boolean writerCalled = false;
|
||||
|
||||
public void testReadAndProcess() throws Exception {
|
||||
|
||||
//create input
|
||||
FlatFileItemReader input = new FlatFileItemReader() {
|
||||
|
||||
private boolean done = false;
|
||||
|
||||
public Object read() {
|
||||
if (!done) {
|
||||
Trade trade = new Trade("1234", 5, new BigDecimal(100), "testName");
|
||||
inputCalled = true;
|
||||
done = true;
|
||||
return trade;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//create writer
|
||||
TradeDao dao = new TradeDao() {
|
||||
public void writeTrade(Trade trade) {
|
||||
assertEquals("1234",trade.getIsin());
|
||||
assertEquals(5, trade.getQuantity());
|
||||
assertEquals(new BigDecimal(100), trade.getPrice());
|
||||
assertEquals("testName", trade.getCustomer());
|
||||
writerCalled = true;
|
||||
}
|
||||
};
|
||||
|
||||
//create module
|
||||
SimpleTradeWriter module = new SimpleTradeWriter();
|
||||
module.setTradeDao(dao);
|
||||
|
||||
module.write(input.read());
|
||||
|
||||
//verify whether input and writer were called
|
||||
assertTrue(inputCalled);
|
||||
assertTrue(writerCalled);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +1,117 @@
|
||||
/*
|
||||
* 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.tasklet;
|
||||
|
||||
import org.springframework.batch.core.domain.JobInterruptedException;
|
||||
import org.springframework.batch.core.domain.Step;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
|
||||
/**
|
||||
* Basic no-op support implementation for use as base class for {@link Step}. Implements {@link BeanNameAware} so that
|
||||
* if no name is provided explicitly it will be inferred from the bean definition in Spring configuration.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class StepSupport implements Step, BeanNameAware {
|
||||
|
||||
private String name;
|
||||
|
||||
private int startLimit = Integer.MAX_VALUE;
|
||||
|
||||
private boolean allowStartIfComplete;
|
||||
|
||||
/**
|
||||
* Default constructor for {@link StepSupport}.
|
||||
*/
|
||||
public StepSupport() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
*/
|
||||
public StepSupport(String string) {
|
||||
super();
|
||||
this.name = string;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name property if it is not already set. Because of the order of the callbacks in a Spring container the
|
||||
* name property will be set first if it is present. Care is needed with bean definition inheritance - if a parent
|
||||
* bean has a name, then its children need an explicit name as well, otherwise they will not be unique.
|
||||
*
|
||||
* @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
|
||||
*/
|
||||
public void setBeanName(String name) {
|
||||
if (this.name == null) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name property. Always overrides the default value if this object is a Spring bean.
|
||||
*
|
||||
* @see #setBeanName(java.lang.String)
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getStartLimit() {
|
||||
return this.startLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the startLimit.
|
||||
*
|
||||
* @param startLimit the startLimit to set
|
||||
*/
|
||||
public void setStartLimit(int startLimit) {
|
||||
this.startLimit = startLimit;
|
||||
}
|
||||
|
||||
public boolean isAllowStartIfComplete() {
|
||||
return this.allowStartIfComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the shouldAllowStartIfComplete.
|
||||
*
|
||||
* @param allowStartIfComplete the shouldAllowStartIfComplete to set
|
||||
*/
|
||||
public void setAllowStartIfComplete(boolean allowStartIfComplete) {
|
||||
this.allowStartIfComplete = allowStartIfComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not supported but provided so that tests can easily create a step.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.Step#execute(org.springframework.batch.core.domain.StepExecution)
|
||||
*/
|
||||
public void execute(StepExecution stepExecution) throws JobInterruptedException, InfrastructureException {
|
||||
throw new UnsupportedOperationException(
|
||||
"Cannot process a StepExecution. Use a smarter subclass of StepSupport.");
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.tasklet;
|
||||
|
||||
import org.springframework.batch.core.domain.JobInterruptedException;
|
||||
import org.springframework.batch.core.domain.Step;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.io.exception.InfrastructureException;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
|
||||
/**
|
||||
* Basic no-op support implementation for use as base class for {@link Step}. Implements {@link BeanNameAware} so that
|
||||
* if no name is provided explicitly it will be inferred from the bean definition in Spring configuration.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class StepSupport implements Step, BeanNameAware {
|
||||
|
||||
private String name;
|
||||
|
||||
private int startLimit = Integer.MAX_VALUE;
|
||||
|
||||
private boolean allowStartIfComplete;
|
||||
|
||||
/**
|
||||
* Default constructor for {@link StepSupport}.
|
||||
*/
|
||||
public StepSupport() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
*/
|
||||
public StepSupport(String string) {
|
||||
super();
|
||||
this.name = string;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name property if it is not already set. Because of the order of the callbacks in a Spring container the
|
||||
* name property will be set first if it is present. Care is needed with bean definition inheritance - if a parent
|
||||
* bean has a name, then its children need an explicit name as well, otherwise they will not be unique.
|
||||
*
|
||||
* @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
|
||||
*/
|
||||
public void setBeanName(String name) {
|
||||
if (this.name == null) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name property. Always overrides the default value if this object is a Spring bean.
|
||||
*
|
||||
* @see #setBeanName(java.lang.String)
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getStartLimit() {
|
||||
return this.startLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the startLimit.
|
||||
*
|
||||
* @param startLimit the startLimit to set
|
||||
*/
|
||||
public void setStartLimit(int startLimit) {
|
||||
this.startLimit = startLimit;
|
||||
}
|
||||
|
||||
public boolean isAllowStartIfComplete() {
|
||||
return this.allowStartIfComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the shouldAllowStartIfComplete.
|
||||
*
|
||||
* @param allowStartIfComplete the shouldAllowStartIfComplete to set
|
||||
*/
|
||||
public void setAllowStartIfComplete(boolean allowStartIfComplete) {
|
||||
this.allowStartIfComplete = allowStartIfComplete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not supported but provided so that tests can easily create a step.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.Step#execute(org.springframework.batch.core.domain.StepExecution)
|
||||
*/
|
||||
public void execute(StepExecution stepExecution) throws JobInterruptedException, InfrastructureException {
|
||||
throw new UnsupportedOperationException(
|
||||
"Cannot process a StepExecution. Use a smarter subclass of StepSupport.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +1,61 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class FutureDateFunctionTests extends TestCase {
|
||||
|
||||
private FutureDateFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new FutureDateFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testFunctionWithNonDateValue() {
|
||||
|
||||
//set-up mock argument - set return value to non Date value
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(this);
|
||||
argumentControl.replay();
|
||||
|
||||
//call tested method - exception is expected because non date value
|
||||
try {
|
||||
function.doGetResult(null);
|
||||
fail("Exception was expected.");
|
||||
} catch (Exception e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFunctionWithFutureDate() throws Exception {
|
||||
|
||||
//set-up mock argument - set return value to future Date
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(new Date(Long.MAX_VALUE));
|
||||
argumentControl.replay();
|
||||
|
||||
//vefify result - should be true because of future date
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testFunctionWithPastDate() throws Exception {
|
||||
|
||||
//set-up mock argument - set return value to future Date
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(new Date(0));
|
||||
argumentControl.replay();
|
||||
|
||||
//vefify result - should be false because of past date
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class FutureDateFunctionTests extends TestCase {
|
||||
|
||||
private FutureDateFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new FutureDateFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testFunctionWithNonDateValue() {
|
||||
|
||||
//set-up mock argument - set return value to non Date value
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(this);
|
||||
argumentControl.replay();
|
||||
|
||||
//call tested method - exception is expected because non date value
|
||||
try {
|
||||
function.doGetResult(null);
|
||||
fail("Exception was expected.");
|
||||
} catch (Exception e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFunctionWithFutureDate() throws Exception {
|
||||
|
||||
//set-up mock argument - set return value to future Date
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(new Date(Long.MAX_VALUE));
|
||||
argumentControl.replay();
|
||||
|
||||
//vefify result - should be true because of future date
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testFunctionWithPastDate() throws Exception {
|
||||
|
||||
//set-up mock argument - set return value to future Date
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(new Date(0));
|
||||
argumentControl.replay();
|
||||
|
||||
//vefify result - should be false because of past date
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +1,82 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TotalOrderItemsFunctionTests extends TestCase {
|
||||
|
||||
private TotalOrderItemsFunction function;
|
||||
private MockControl argument1Control;
|
||||
private Function argument1;
|
||||
private MockControl argument2Control;
|
||||
private Function argument2;
|
||||
|
||||
public void setUp() {
|
||||
//create mock for first argument - set count to 3
|
||||
argument1Control = MockControl.createControl(Function.class);
|
||||
argument1 = (Function) argument1Control.getMock();
|
||||
argument1.getResult(null);
|
||||
argument1Control.setReturnValue(new Integer(3));
|
||||
argument1Control.replay();
|
||||
|
||||
argument2Control = MockControl.createControl(Function.class);
|
||||
argument2 = (Function) argument2Control.getMock();
|
||||
|
||||
//create function
|
||||
function = new TotalOrderItemsFunction(new Function[] {argument1, argument2}, 0, 0);
|
||||
}
|
||||
|
||||
public void testFunctionWithNonListValue() {
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(this);
|
||||
argument2Control.replay();
|
||||
|
||||
//call tested method - exception is expected because non list value
|
||||
try {
|
||||
function.doGetResult(null);
|
||||
fail("Exception was expected.");
|
||||
} catch (Exception e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFunctionWithCorrectItemCount() throws Exception {
|
||||
|
||||
//create list with correct item count
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(3);
|
||||
List list = new ArrayList();
|
||||
list.add(item);
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(list);
|
||||
argument2Control.replay();
|
||||
|
||||
//vefify result
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testFunctionWithIncorrectItemCount() throws Exception {
|
||||
|
||||
//create list with incorrect item count
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(99);
|
||||
List list = new ArrayList();
|
||||
list.add(item);
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(list);
|
||||
argument2Control.replay();
|
||||
|
||||
//vefify result
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TotalOrderItemsFunctionTests extends TestCase {
|
||||
|
||||
private TotalOrderItemsFunction function;
|
||||
private MockControl argument1Control;
|
||||
private Function argument1;
|
||||
private MockControl argument2Control;
|
||||
private Function argument2;
|
||||
|
||||
public void setUp() {
|
||||
//create mock for first argument - set count to 3
|
||||
argument1Control = MockControl.createControl(Function.class);
|
||||
argument1 = (Function) argument1Control.getMock();
|
||||
argument1.getResult(null);
|
||||
argument1Control.setReturnValue(new Integer(3));
|
||||
argument1Control.replay();
|
||||
|
||||
argument2Control = MockControl.createControl(Function.class);
|
||||
argument2 = (Function) argument2Control.getMock();
|
||||
|
||||
//create function
|
||||
function = new TotalOrderItemsFunction(new Function[] {argument1, argument2}, 0, 0);
|
||||
}
|
||||
|
||||
public void testFunctionWithNonListValue() {
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(this);
|
||||
argument2Control.replay();
|
||||
|
||||
//call tested method - exception is expected because non list value
|
||||
try {
|
||||
function.doGetResult(null);
|
||||
fail("Exception was expected.");
|
||||
} catch (Exception e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFunctionWithCorrectItemCount() throws Exception {
|
||||
|
||||
//create list with correct item count
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(3);
|
||||
List list = new ArrayList();
|
||||
list.add(item);
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(list);
|
||||
argument2Control.replay();
|
||||
|
||||
//vefify result
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testFunctionWithIncorrectItemCount() throws Exception {
|
||||
|
||||
//create list with incorrect item count
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(99);
|
||||
List list = new ArrayList();
|
||||
list.add(item);
|
||||
|
||||
argument2.getResult(null);
|
||||
argument2Control.setReturnValue(list);
|
||||
argument2Control.replay();
|
||||
|
||||
//vefify result
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,166 +1,166 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateDiscountsFunctionTests extends TestCase {
|
||||
|
||||
private ValidateDiscountsFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateDiscountsFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testDiscountPercentageMin() throws Exception {
|
||||
|
||||
//create line item with correct discount percentage and zero discount amount
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(1.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount percentages are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative percentage
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(-1.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount percentage
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPercentageMax() throws Exception {
|
||||
|
||||
//create line item with correct discount percentage and zero discount amount
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(99.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount percentages are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with discount percentage above 100
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(101.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount percentage
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct discount amount and zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(10.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount amounts are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative discount amount
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(-1.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount amount
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct discount amount and zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(99.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount amounts are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with discount amount above item price
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(101.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount amount
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testBothDiscountValuesNonZero() throws Exception {
|
||||
|
||||
//create line item with non-zero discount amount and non-zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(10.0));
|
||||
item.setDiscountAmount(new BigDecimal(99.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be false - only one of the discount values is empty
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateDiscountsFunctionTests extends TestCase {
|
||||
|
||||
private ValidateDiscountsFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateDiscountsFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testDiscountPercentageMin() throws Exception {
|
||||
|
||||
//create line item with correct discount percentage and zero discount amount
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(1.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount percentages are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative percentage
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(-1.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount percentage
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPercentageMax() throws Exception {
|
||||
|
||||
//create line item with correct discount percentage and zero discount amount
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(99.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount percentages are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with discount percentage above 100
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(101.0));
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount percentage
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct discount amount and zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(10.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount amounts are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative discount amount
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(-1.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount amount
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testDiscountPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct discount amount and zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(99.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all discount amounts are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with discount amount above item price
|
||||
item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setDiscountAmount(new BigDecimal(101.0));
|
||||
item.setPrice(new BigDecimal(100.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid discount amount
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testBothDiscountValuesNonZero() throws Exception {
|
||||
|
||||
//create line item with non-zero discount amount and non-zero discount percentage
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountPerc(new BigDecimal(10.0));
|
||||
item.setDiscountAmount(new BigDecimal(99.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be false - only one of the discount values is empty
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateHandlingPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateHandlingPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateHandlingPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testHandlingPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct handling price
|
||||
LineItem item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all handling prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative handling price
|
||||
item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid handling price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testHandlingPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct handling price
|
||||
LineItem item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all handling prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with handling price above allowed max
|
||||
item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid handling price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateHandlingPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateHandlingPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateHandlingPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testHandlingPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct handling price
|
||||
LineItem item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all handling prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative handling price
|
||||
item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid handling price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testHandlingPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct handling price
|
||||
LineItem item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all handling prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with handling price above allowed max
|
||||
item = new LineItem();
|
||||
item.setHandlingPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid handling price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +1,80 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
|
||||
public class ValidateIdsFunctionTests extends TestCase {
|
||||
|
||||
private ValidateIdsFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateIdsFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testIdMin() throws Exception {
|
||||
|
||||
//create line item with correct item id
|
||||
LineItem item = new LineItem();
|
||||
item.setItemId(1);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all ids are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative id
|
||||
item = new LineItem();
|
||||
item.setItemId(-1);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid id
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testIdMax() throws Exception {
|
||||
|
||||
//create line item with correct item id
|
||||
LineItem item = new LineItem();
|
||||
item.setItemId(9999999999L);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item ids are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item id above allowed max
|
||||
item = new LineItem();
|
||||
item.setItemId(10000000000L);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item id
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
|
||||
public class ValidateIdsFunctionTests extends TestCase {
|
||||
|
||||
private ValidateIdsFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateIdsFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testIdMin() throws Exception {
|
||||
|
||||
//create line item with correct item id
|
||||
LineItem item = new LineItem();
|
||||
item.setItemId(1);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all ids are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative id
|
||||
item = new LineItem();
|
||||
item.setItemId(-1);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid id
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testIdMax() throws Exception {
|
||||
|
||||
//create line item with correct item id
|
||||
LineItem item = new LineItem();
|
||||
item.setItemId(9999999999L);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item ids are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item id above allowed max
|
||||
item = new LineItem();
|
||||
item.setItemId(10000000000L);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item id
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +1,82 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidatePricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidatePricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidatePricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testItemPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct item price
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative item price
|
||||
item = new LineItem();
|
||||
item.setPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testItemPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct item price
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item price above allowed max
|
||||
item = new LineItem();
|
||||
item.setPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidatePricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidatePricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidatePricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testItemPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct item price
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative item price
|
||||
item = new LineItem();
|
||||
item.setPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testItemPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct item price
|
||||
LineItem item = new LineItem();
|
||||
item.setPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item price above allowed max
|
||||
item = new LineItem();
|
||||
item.setPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,80 +1,80 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateQuantitiesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateQuantitiesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateQuantitiesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testQuantityMin() throws Exception {
|
||||
|
||||
//create line item with correct item quantity
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(1);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all quantities are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative quantity
|
||||
item = new LineItem();
|
||||
item.setQuantity(-1);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid quantity
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testQuantityMax() throws Exception {
|
||||
|
||||
//create line item with correct item quantity
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(9999);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item quantities are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item quantity above allowed max
|
||||
item = new LineItem();
|
||||
item.setQuantity(10000);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item quantity
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateQuantitiesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateQuantitiesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateQuantitiesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testQuantityMin() throws Exception {
|
||||
|
||||
//create line item with correct item quantity
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(1);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all quantities are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative quantity
|
||||
item = new LineItem();
|
||||
item.setQuantity(-1);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid quantity
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testQuantityMax() throws Exception {
|
||||
|
||||
//create line item with correct item quantity
|
||||
LineItem item = new LineItem();
|
||||
item.setQuantity(9999);
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all item quantities are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with item quantity above allowed max
|
||||
item = new LineItem();
|
||||
item.setQuantity(10000);
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid item quantity
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateShippingPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateShippingPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateShippingPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testShippingPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct shipping price
|
||||
LineItem item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all shipping prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative shipping price
|
||||
item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid shipping price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testShippingPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct shipping price
|
||||
LineItem item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all shipping prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with shipping price above allowed max
|
||||
item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid shipping price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateShippingPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateShippingPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateShippingPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testShippingPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct shipping price
|
||||
LineItem item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all shipping prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative shipping price
|
||||
item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid shipping price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testShippingPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct shipping price
|
||||
LineItem item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all shipping prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with shipping price above allowed max
|
||||
item = new LineItem();
|
||||
item.setShippingPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid shipping price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,133 +1,133 @@
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateTotalPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateTotalPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateTotalPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testTotalPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct total price
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(0.0));
|
||||
item.setShippingPrice(new BigDecimal(0.0));
|
||||
item.setPrice(new BigDecimal(1.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative item price
|
||||
item = new LineItem();
|
||||
item.setTotalPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testTotalPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct total price
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(0.0));
|
||||
item.setShippingPrice(new BigDecimal(0.0));
|
||||
item.setPrice(new BigDecimal(99999999.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with total price above allowed max
|
||||
item = new LineItem();
|
||||
item.setTotalPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testTotalPriceCalculation() throws Exception {
|
||||
|
||||
//create line item
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(5.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
item.setShippingPrice(new BigDecimal(2.0));
|
||||
item.setPrice(new BigDecimal(250.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(248.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with incorrect total price
|
||||
item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(5.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
item.setShippingPrice(new BigDecimal(2.0));
|
||||
item.setPrice(new BigDecimal(250.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(253.0));
|
||||
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has incorrect total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
package org.springframework.batch.sample.validation.valang.custom;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.batch.sample.domain.LineItem;
|
||||
|
||||
import org.springmodules.validation.valang.functions.Function;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ValidateTotalPricesFunctionTests extends TestCase {
|
||||
|
||||
private ValidateTotalPricesFunction function;
|
||||
private MockControl argumentControl;
|
||||
private Function argument;
|
||||
|
||||
public void setUp() {
|
||||
argumentControl = MockControl.createControl(Function.class);
|
||||
argument = (Function) argumentControl.getMock();
|
||||
|
||||
//create function
|
||||
function = new ValidateTotalPricesFunction(new Function[] {argument}, 0, 0);
|
||||
}
|
||||
|
||||
public void testTotalPriceMin() throws Exception {
|
||||
|
||||
//create line item with correct total price
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(0.0));
|
||||
item.setShippingPrice(new BigDecimal(0.0));
|
||||
item.setPrice(new BigDecimal(1.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(1.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with negative item price
|
||||
item = new LineItem();
|
||||
item.setTotalPrice(new BigDecimal(-1.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testTotalPriceMax() throws Exception {
|
||||
|
||||
//create line item with correct total price
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(0.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(0.0));
|
||||
item.setShippingPrice(new BigDecimal(0.0));
|
||||
item.setPrice(new BigDecimal(99999999.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(99999999.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with total price above allowed max
|
||||
item = new LineItem();
|
||||
item.setTotalPrice(new BigDecimal(100000000.0));
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has invalid total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
|
||||
public void testTotalPriceCalculation() throws Exception {
|
||||
|
||||
//create line item
|
||||
LineItem item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(5.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
item.setShippingPrice(new BigDecimal(2.0));
|
||||
item.setPrice(new BigDecimal(250.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(248.0));
|
||||
|
||||
//add it to line items list
|
||||
List items = new ArrayList();
|
||||
items.add(item);
|
||||
|
||||
//set return value for mock argument
|
||||
argument.getResult(null);
|
||||
argumentControl.setReturnValue(items,2);
|
||||
argumentControl.replay();
|
||||
|
||||
//verify result - should be true - all total prices are correct
|
||||
assertTrue(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
|
||||
//now add line item with incorrect total price
|
||||
item = new LineItem();
|
||||
item.setDiscountAmount(new BigDecimal(5.0));
|
||||
item.setDiscountPerc(new BigDecimal(0.0));
|
||||
item.setHandlingPrice(new BigDecimal(1.0));
|
||||
item.setShippingPrice(new BigDecimal(2.0));
|
||||
item.setPrice(new BigDecimal(250.0));
|
||||
item.setQuantity(1);
|
||||
item.setTotalPrice(new BigDecimal(253.0));
|
||||
|
||||
items.add(item);
|
||||
|
||||
//verify result - should be false - second item has incorrect total price
|
||||
assertFalse(((Boolean)function.doGetResult(null)).booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user