BATCH-1928: Converted to using JdbcOperations over JdbcTemplate

This commit is contained in:
Michael Minella
2012-12-17 09:16:30 -06:00
parent bdece6640c
commit 40975b6548
29 changed files with 164 additions and 134 deletions

View File

@@ -2,7 +2,7 @@ package org.springframework.batch.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.JdbcOperations;
/**
* <p>
@@ -25,7 +25,7 @@ public final class JdbcTestUtils {
* @param tableName table name to count rows in
* @return the number of rows in the table
*/
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) {
public static int countRowsInTable(JdbcOperations jdbcTemplate, String tableName) {
return jdbcTemplate.queryForInt("SELECT COUNT(0) FROM " + tableName);
}
@@ -35,7 +35,7 @@ public final class JdbcTestUtils {
* @param tableNames the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
*/
public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames) {
public static int deleteFromTables(JdbcOperations jdbcTemplate, String... tableNames) {
int totalRowCount = 0;
for (String tableName : tableNames) {
int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);

View File

@@ -7,19 +7,20 @@ import javax.sql.DataSource;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Simple minded partitioner for a range of values of a column in a database
* table. Works best if the values are uniformly distributed (e.g.
* auto-generated primary key values).
*
*
* @author Dave Syer
*
*
*/
public class ColumnRangePartitioner implements Partitioner {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private String table;
@@ -27,7 +28,7 @@ public class ColumnRangePartitioner implements Partitioner {
/**
* The name of the SQL table the data are in.
*
*
* @param table the name of the table
*/
public void setTable(String table) {
@@ -36,7 +37,7 @@ public class ColumnRangePartitioner implements Partitioner {
/**
* The name of the column to partition.
*
*
* @param column the column name.
*/
public void setColumn(String column) {
@@ -45,7 +46,7 @@ public class ColumnRangePartitioner implements Partitioner {
/**
* The data source for connecting to the database.
*
*
* @param dataSource a {@link DataSource}
*/
public void setDataSource(DataSource dataSource) {
@@ -57,7 +58,7 @@ public class ColumnRangePartitioner implements Partitioner {
* are uniformly distributed. The execution context values will have keys
* <code>minValue</code> and <code>maxValue</code> specifying the range of
* values to consider in each partition.
*
*
* @see Partitioner#partition(int)
*/
public Map<String, ExecutionContext> partition(int gridSize) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -22,6 +22,7 @@ import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
@@ -31,7 +32,7 @@ import org.springframework.util.Assert;
*/
public class StagingItemListener extends StepListenerSupport<Long, Long> implements InitializingBean {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);

View File

@@ -5,26 +5,27 @@ import javax.sql.DataSource;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
/**
* Marks the input row as 'processed'. (This change will rollback if there is
* problem later)
*
*
* @param <T> item type
*
*
* @see StagingItemReader
* @see StagingItemWriter
* @see ProcessIndicatorItemWrapper
*
*
* @author Robert Kasanicky
*/
public class StagingItemProcessor<T> implements ItemProcessor<ProcessIndicatorItemWrapper<T>, T>, InitializingBean {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -34,14 +34,15 @@ import org.springframework.batch.support.SerializationUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.util.Assert;
/**
* Thread-safe database {@link ItemReader} implementing the process indicator
* pattern.
*
*
* To achieve restartability use together with {@link StagingItemProcessor}.
*/
public class StagingItemReader<T> implements ItemReader<ProcessIndicatorItemWrapper<T>>, StepExecutionListener,
@@ -57,7 +58,7 @@ public class StagingItemReader<T> implements ItemReader<ProcessIndicatorItemWrap
private volatile Iterator<Long> keys;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -16,13 +16,14 @@
package org.springframework.batch.sample.domain.football.internal;
import javax.sql.DataSource;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.batch.sample.domain.football.PlayerDao;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.sql.DataSource;
/**
* @author Lucas Ward
*
@@ -33,7 +34,7 @@ public class JdbcPlayerDao implements PlayerDao {
"INSERT into PLAYERS (player_id, last_name, first_name, pos, year_of_birth, year_drafted)" +
" values (:id, :lastName, :firstName, :position, :birthYear, :debutYear)";
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private NamedParameterJdbcOperations namedParameterJdbcTemplate;
public void savePlayer(Player player) {
namedParameterJdbcTemplate.update(INSERT_PLAYER, new BeanPropertySqlParameterSource(player));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -18,13 +18,14 @@ package org.springframework.batch.sample.domain.football.internal;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.football.PlayerSummary;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.sql.DataSource;
public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
private static final String INSERT_SUMMARY = "INSERT into PLAYER_SUMMARY(ID, YEAR_NO, COMPLETES, ATTEMPTS, PASSING_YARDS, PASSING_TD, "
@@ -32,7 +33,7 @@ public class JdbcPlayerSummaryDao implements ItemWriter<PlayerSummary> {
+ "values(:id, :year, :completes, :attempts, :passingYards, :passingTd, "
+ ":interceptions, :rushes, :rushYards, :receptions, :receptionYards, :totalTd)";
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private NamedParameterJdbcOperations namedParameterJdbcTemplate;
public void write(List<? extends PlayerSummary> summaries) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -21,19 +21,20 @@ import javax.sql.DataSource;
import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.batch.sample.domain.trade.CustomerDebitDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Reduces customer's credit by the provided amount.
*
*
* @author Robert Kasanicky
*/
public class JdbcCustomerDebitDao implements CustomerDebitDao {
private static final String UPDATE_CREDIT = "UPDATE CUSTOMER SET credit= credit-? WHERE name=?";
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
public void write(CustomerDebit customerDebit) {
jdbcTemplate.update(UPDATE_CREDIT, customerDebit.getDebit(), customerDebit.getName());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.TradeDao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
@@ -41,7 +42,7 @@ public class JdbcTradeDao implements TradeDao {
/**
* handles the processing of sql query
*/
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
/**
* database is not expected to be setup for autoincrement

View File

@@ -18,8 +18,9 @@ import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -35,7 +36,7 @@ public class CompositeItemWriterSampleFunctionalTests {
+ "Trade: [isin=UK21341EAH44,quantity=214,price=34.11,customer=customer4]"
+ "Trade: [isin=UK21341EAH45,quantity=215,price=35.11,customer=customer5]";
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -34,8 +34,9 @@ import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -48,7 +49,7 @@ public class CustomerFilterJobFunctionalTests {
private List<Customer> customers;
private int activeRow = 0;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private Map<String, Double> credits = new HashMap<String, Double>();
@Autowired
@@ -135,9 +136,10 @@ public class CustomerFilterJobFunctionalTests {
/*
* (non-Javadoc)
*
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
@@ -150,9 +152,10 @@ public class CustomerFilterJobFunctionalTests {
/*
* (non-Javadoc)
*
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;

View File

@@ -8,6 +8,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -19,7 +20,7 @@ public class FootballJobFunctionalTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {

View File

@@ -20,9 +20,10 @@ import org.springframework.batch.sample.domain.trade.internal.HibernateCreditDao
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateJdbcException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -34,7 +35,7 @@ import org.springframework.transaction.support.TransactionTemplate;
/**
* Test for HibernateJob - checks that customer credit has been updated to
* expected value.
*
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -45,17 +46,17 @@ public class HibernateFailureJobFunctionalTests {
@Autowired
private HibernateCreditDao writer;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private PlatformTransactionManager transactionManager;
private static final BigDecimal CREDIT_INCREASE = CustomerCreditIncreaseProcessor.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";
@@ -81,9 +82,9 @@ public class HibernateFailureJobFunctionalTests {
@Test
public void testLaunchJob() throws Exception {
validatePreConditions();
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();
writer.setFailOnFlush(2);
@@ -101,9 +102,9 @@ public class HibernateFailureJobFunctionalTests {
}
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) from CUSTOMER");
assertEquals(4, after);
validatePostConditions();
}
/**
@@ -113,7 +114,7 @@ public class HibernateFailureJobFunctionalTests {
protected void validatePreConditions() throws Exception {
ensureState();
creditsBeforeUpdate = (List<BigDecimal>) new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
public Object doInTransaction(TransactionStatus status) {
return jdbcTemplate.query(ALL_CUSTOMERS, new ParameterizedRowMapper<BigDecimal>() {
public BigDecimal mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getBigDecimal(CREDIT_COLUMN);
@@ -140,7 +141,7 @@ public class HibernateFailureJobFunctionalTests {
});
}
/**
* Credit was increased by CREDIT_INCREASE
*/
@@ -161,13 +162,13 @@ public class HibernateFailureJobFunctionalTests {
matches.add(rs.getBigDecimal(ID_COLUMN));
}
}
});
return null;
}
});
assertEquals((creditsBeforeUpdate.size() - 1), matches.size());
assertFalse(matches.contains(new BigDecimal(2)));
assertFalse(matches.contains(new BigDecimal(2)));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -26,13 +26,14 @@ import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Sample using a step to launch a job.
*
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -43,7 +44,7 @@ public class JobStepFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
// auto-injected attributes
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
@@ -54,7 +55,7 @@ public class JobStepFunctionalTests {
public void testJobLaunch() throws Exception {
jdbcTemplate.update("DELETE FROM TRADE");
jobLauncherTestUtils.launchJob(new DefaultJobParametersConverter()
.getJobParameters(PropertiesConverter
.stringToProperties("run.id(long)=1,parameter=true,run.date=20070122,input.file=classpath:data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt")));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2010 the original author or authors.
* Copyright 2006-2012 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.
@@ -32,6 +32,7 @@ import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandle
import org.springframework.batch.sample.domain.mail.internal.TestMailSender;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
@@ -41,7 +42,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Dan Garrette
* @author Dave Syer
*
*
* @Since 2.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -66,7 +67,7 @@ public class MailJobFunctionalTests {
private static final Object[] USER8 = new Object[] { 8, "Martin Van Buren", email };
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -24,12 +24,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.support.JdbcTestUtils;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.batch.support.JdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/parallelJob.xml",
@@ -39,7 +40,7 @@ public class ParallelJobFunctionalTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -28,6 +28,7 @@ import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -35,7 +36,7 @@ import org.springframework.test.context.transaction.BeforeTransaction;
/**
* Simple restart scenario.
*
*
* @author Robert Kasanicky
* @author Dave Syer
*/
@@ -48,7 +49,7 @@ public class RestartFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;
// auto-injected attributes
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
@@ -66,7 +67,7 @@ public class RestartFunctionalTests {
* finish successfully, because it continues execution where the previous
* run stopped (module throws exception after fixed number of processed
* records).
*
*
* @throws Exception
*/
@Test

View File

@@ -24,18 +24,19 @@ import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteExcep
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.sample.common.SkipCheckingListener;
import org.springframework.batch.sample.domain.trade.internal.TradeWriter;
import org.springframework.batch.support.JdbcTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.batch.support.JdbcTestUtils;
/**
* Error is encountered during writing - transaction is rolled back and the
* error item is skipped on second attempt to process the chunk.
*
*
* @author Robert Kasanicky
* @author Dan Garrette
*/
@@ -43,7 +44,7 @@ import org.springframework.batch.support.JdbcTestUtils;
@ContextConfiguration(locations = { "/skipSample-job-launcher-context.xml" })
public class SkipSampleFunctionalTests {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private JobExplorer jobExplorer;
@@ -172,7 +173,7 @@ public class SkipSampleFunctionalTests {
// to output
// System.err.println(jdbcTemplate.queryForList("SELECT * FROM TRADE"));
assertEquals(5, jdbcTemplate.queryForInt("SELECT COUNT(*) from TRADE where VERSION=?", 1));
// 1 record skipped in processing second step
assertEquals(1, SkipCheckingListener.getProcessSkips());
@@ -217,7 +218,7 @@ public class SkipSampleFunctionalTests {
/**
* Launch the entire job, including all steps, in order.
*
*
* @return JobExecution, so that the test may validate the exit status
*/
public long launchJobWithIncrementer() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -36,8 +36,9 @@ import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -48,12 +49,12 @@ public class TradeJobFunctionalTests {
private static final String GET_TRADES = "select ISIN, QUANTITY, PRICE, CUSTOMER, ID, VERSION from TRADE order by ISIN";
private static final String GET_CUSTOMERS = "select NAME, CREDIT from CUSTOMER order by NAME";
private List<Customer> customers;
private List<Trade> trades;
private int activeRow = 0;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private Map<String, Double> credits = new HashMap<String, Double>();
@Autowired
@@ -63,7 +64,7 @@ public class TradeJobFunctionalTests {
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void onSetUp() throws Exception {
jdbcTemplate.update("delete from TRADE");
@@ -72,7 +73,7 @@ public class TradeJobFunctionalTests {
credits.put((String) map.get("NAME"), ((Number) map.get("CREDIT")).doubleValue());
}
}
@After
public void tearDown() throws Exception {
jdbcTemplate.update("delete from TRADE");
@@ -80,9 +81,9 @@ public class TradeJobFunctionalTests {
@Test
public void testLaunchJob() throws Exception {
jobLauncherTestUtils.launchJob();
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)),
new Customer("customer2", (credits.get("customer2") - 18.12 - 12.78)),
new Customer("customer3", (credits.get("customer3") - 109.25)),
@@ -93,48 +94,48 @@ public class TradeJobFunctionalTests {
new Trade("UK21341EAH47", 245, new BigDecimal("12.78"), "customer2"),
new Trade("UK21341EAH48", 108, new BigDecimal("109.25"), "customer3"),
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 = 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)));
}
});
assertEquals(activeRow, trades.size());
// check content of the customer table
activeRow = 0;
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
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
}
private static class Customer {
private String name;
private double credit;
public Customer(String name, double credit) {
this.name = name;
this.credit = credit;
}
/**
* @return the credit
*/
@@ -178,9 +179,9 @@ public class TradeJobFunctionalTests {
return false;
return true;
}
}
}

View File

@@ -11,6 +11,7 @@ import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
@@ -22,7 +23,7 @@ public class ErrorLogTasklet implements Tasklet, StepExecutionListener {
protected final Log logger = LogFactory.getLog(getClass());
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private String jobName;

View File

@@ -14,6 +14,7 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -30,7 +31,7 @@ import org.springframework.transaction.support.TransactionTemplate;
@ContextConfiguration()
public class StagingItemReaderTests {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private PlatformTransactionManager transactionManager;
@@ -73,9 +74,9 @@ public class StagingItemReaderTests {
assertEquals(StagingItemWriter.NEW, before);
ProcessIndicatorItemWrapper<String> wrapper = reader.read();
String item = wrapper.getItem();
String item = wrapper.getItem();
assertEquals("FOO", item);
StagingItemProcessor<String> updater = new StagingItemProcessor<String>();
updater.setJdbcTemplate(jdbcTemplate);
updater.process(wrapper);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -29,6 +29,7 @@ import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -38,7 +39,7 @@ import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration
public class StagingItemWriterTests {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private StagingItemWriter<String> writer;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2012 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.
@@ -28,15 +28,16 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.football.Game;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Lucas Ward
*
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/data-source-context.xml"})
@@ -46,7 +47,7 @@ public class JdbcGameDaoIntegrationTests {
private Game game = new Game();
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2012 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.
@@ -27,8 +27,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -40,21 +41,21 @@ import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/data-source-context.xml"})
public class JdbcPlayerDaoIntegrationTests {
private JdbcPlayerDao playerDao;
private Player player;
private static final String GET_PLAYER = "SELECT * from PLAYERS";
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void init(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
playerDao = new JdbcPlayerDao();
playerDao.setDataSource(dataSource);
playerDao.setDataSource(dataSource);
player = new Player();
player.setId("AKFJDL00");
@@ -63,7 +64,7 @@ public class JdbcPlayerDaoIntegrationTests {
player.setPosition("QB");
player.setBirthYear(1975);
player.setDebutYear(1998);
}
@@ -76,7 +77,7 @@ public class JdbcPlayerDaoIntegrationTests {
@Transactional @Test
public void testSavePlayer(){
playerDao.savePlayer(player);
jdbcTemplate.query(GET_PLAYER, new RowCallbackHandler(){
@@ -88,8 +89,8 @@ public class JdbcPlayerDaoIntegrationTests {
assertEquals(rs.getString("POS"), "QB");
assertEquals(rs.getInt("YEAR_OF_BIRTH"), 1975);
assertEquals(rs.getInt("YEAR_DRAFTED"), 1998);
}
}
});
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2012 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.
@@ -26,6 +26,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.football.PlayerSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -33,7 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
/**
* @author Lucas Ward
*
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/data-source-context.xml" })
@@ -43,7 +44,7 @@ public class JdbcPlayerSummaryDaoIntegrationTests {
private PlayerSummary summary;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void init(DataSource dataSource) {

View File

@@ -8,6 +8,7 @@ import javax.sql.DataSource;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
public class ItemTrackingTradeItemWriter implements ItemWriter<Trade> {
@@ -16,7 +17,7 @@ public class ItemTrackingTradeItemWriter implements ItemWriter<Trade> {
private String writeFailureISIN;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2012 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.
@@ -27,8 +27,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.trade.CustomerDebit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -37,7 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration()
public class JdbcCustomerDebitDaoTests {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
private JdbcCustomerDebitDao writer;
@@ -50,7 +51,7 @@ public class JdbcCustomerDebitDaoTests {
@Transactional @Test
public void testWrite() {
//insert customer credit
//insert customer credit
jdbcTemplate.execute("INSERT INTO CUSTOMER VALUES (99, 0, 'testName', 100)");
//create customer debit

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2012 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.
@@ -28,8 +28,9 @@ import org.junit.runner.RunWith;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -39,7 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration(locations = {"/data-source-context.xml"})
public class JdbcTradeWriterTests {
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
private JdbcTradeDao writer;
@@ -65,7 +66,7 @@ public class JdbcTradeWriterTests {
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() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2012 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.
@@ -32,6 +32,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -50,9 +51,9 @@ public class TwoJobInstancesPagingFunctionalTests {
@Autowired
private Job job;
private JdbcTemplate jdbcTemplate;
private JdbcOperations jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);