IN PROGRESS - BATCH-971: add database writers to iosample
added processing phase to iosample included jdbcBatch and hibernate into iosample and removed the redundant original samples
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Test for HibernateJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration()
|
||||
public class HibernateJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Test for JdbcBatchItemWriterJob - checks that customer credit has been updated to expected value.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class JdbcBatchJobFunctionalTests extends AbstractCustomerCreditIncreaseTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* Base class for IoSample tests that increase input customer credit by fixed
|
||||
* amount. Assumes inputs and outputs are in the same format and uses the job's
|
||||
* {@link ItemReader} to parse the outputs.
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractIoSampleTests extends AbstractJobTests {
|
||||
|
||||
@Autowired
|
||||
private ItemReader<CustomerCredit> reader;
|
||||
|
||||
/**
|
||||
* Check the resulting credits correspond to inputs increased by fixed
|
||||
* amount.
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateCredit() throws Exception {
|
||||
|
||||
open(reader);
|
||||
List<CustomerCredit> inputs = getCredits(reader);
|
||||
close(reader);
|
||||
|
||||
JobExecution jobExecution = this.launchJob();
|
||||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
|
||||
|
||||
pointReaderToOutput(reader);
|
||||
open(reader);
|
||||
List<CustomerCredit> outputs = getCredits(reader);
|
||||
close(reader);
|
||||
|
||||
assertEquals(inputs.size(), outputs.size());
|
||||
int itemCount = inputs.size();
|
||||
assertTrue(itemCount > 0);
|
||||
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
|
||||
outputs.get(i).getCredit().intValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the reader to read outputs (if necessary). Required for
|
||||
* file-to-file jobs jobs, usually no-op for database jobs where inputs are
|
||||
* updated (rather than outputs created).
|
||||
*/
|
||||
protected abstract void pointReaderToOutput(ItemReader<CustomerCredit> reader);
|
||||
|
||||
/**
|
||||
* Read all credits using the provided reader.
|
||||
*/
|
||||
private List<CustomerCredit> getCredits(ItemReader<CustomerCredit> reader) throws Exception {
|
||||
CustomerCredit credit;
|
||||
List<CustomerCredit> result = new ArrayList<CustomerCredit>();
|
||||
while ((credit = reader.read()) != null) {
|
||||
result.add(credit);
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the reader if applicable.
|
||||
*/
|
||||
private void open(ItemReader<?> reader) {
|
||||
if (reader instanceof ItemStream) {
|
||||
((ItemStream) reader).open(new ExecutionContext());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the reader if applicable.
|
||||
*/
|
||||
private void close(ItemReader<?> reader) {
|
||||
if (reader instanceof ItemStream) {
|
||||
((ItemStream) reader).close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,11 +16,12 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.batch.test.AssertFile;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -31,17 +32,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/delimited.xml" })
|
||||
public class DelimitedFunctionalTests extends AbstractJobTests {
|
||||
public class DelimitedFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/delimitedOutput.csv";
|
||||
private static final String INPUT_FILE = "src/main/resources/data/iosample/input/delimited.csv";
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
this.launchJob();
|
||||
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
FlatFileItemReader<CustomerCredit> fileReader = (FlatFileItemReader<CustomerCredit>) reader;
|
||||
fileReader.setResource(outputResource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,28 +16,26 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.batch.test.AssertFile;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/fixedLength.xml" })
|
||||
public class FixedLengthFunctionalTests extends AbstractJobTests {
|
||||
public class FixedLengthFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/fixedLengthOutput.txt";
|
||||
private static final String INPUT_FILE = "src/main/resources/data/iosample/input/fixedLength.txt";
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
this.launchJob();
|
||||
AssertFile.assertFileEquals(new FileSystemResource(OUTPUT_FILE), new FileSystemResource(INPUT_FILE));
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
FlatFileItemReader<?> fileReader = (FlatFileItemReader<?>) reader;
|
||||
fileReader.setResource(outputResource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/hibernate-context.xml",
|
||||
"/jobs/ioSampleJob.xml", "/jobs/iosample/hibernate.xml" })
|
||||
public class HibernateFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,18 +16,11 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
@@ -36,28 +29,11 @@ import org.springframework.test.jdbc.SimpleJdbcTestUtils;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/jdbcCursor.xml" })
|
||||
public class JdbcCursorFunctionalTests extends AbstractJobTests {
|
||||
public class JdbcCursorFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
this.simpleJdbcTemplate.update("delete from TRADE");
|
||||
|
||||
this.launchJob();
|
||||
|
||||
assertEquals(4, SimpleJdbcTestUtils.countRowsInTable(simpleJdbcTemplate, "TRADE"));
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
String sql = "select count(*) from TRADE where CUSTOMER = ?";
|
||||
assertEquals(1, simpleJdbcTemplate.queryForInt(sql, "customer" + i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/multiLine.xml" })
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/iosample/multiLine.xml" })
|
||||
public class MultiLineFunctionalTests extends AbstractJobTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/multiLineOutput.txt";
|
||||
|
||||
private static final String INPUT_FILE = "src/main/resources/data/iosample/input/multiLine.txt";
|
||||
|
||||
/**
|
||||
|
||||
@@ -29,11 +29,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/multiRecordType.xml" })
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/iosample/multiRecordType.xml" })
|
||||
public class MultiRecordTypeFunctionalTests extends AbstractJobTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/multiRecordTypeOutput.txt";
|
||||
|
||||
private static final String INPUT_FILE = "src/main/resources/data/iosample/input/multiRecordType.txt";
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,11 +16,12 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.batch.test.AssertFile;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.MultiResourceItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -31,18 +32,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/multiResource.xml" })
|
||||
public class MultiResourceFunctionalTests extends AbstractJobTests {
|
||||
public class MultiResourceFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
this.launchJob();
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
MultiResourceItemReader<?> multiReader = (MultiResourceItemReader<?>) reader;
|
||||
multiReader.setResources(new Resource[] {
|
||||
new FileSystemResource("target/test-outputs/multiResourceOutput.csv.1"),
|
||||
new FileSystemResource("target/test-outputs/multiResourceOutput.csv.2") });
|
||||
|
||||
AssertFile.assertFileEquals(new FileSystemResource("target/test-outputs/multiResourceOutput.csv.1"),
|
||||
new FileSystemResource("src/main/resources/data/iosample/input/delimited.csv"));
|
||||
AssertFile.assertFileEquals(new FileSystemResource("target/test-outputs/multiResourceOutput.csv.2"),
|
||||
new FileSystemResource("src/main/resources/data/iosample/input/delimited2.csv"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,12 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import java.io.FileReader;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.test.AbstractJobTests;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.xml.StaxEventItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -33,18 +32,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/ioSampleJob.xml",
|
||||
"/jobs/iosample/xml.xml" })
|
||||
public class XmlFunctionalTests extends AbstractJobTests {
|
||||
public class XmlFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
private static final String OUTPUT_FILE = "target/test-outputs/output.xml";
|
||||
private static final String INPUT_FILE = "src/main/resources/data/iosample/input/input.xml";
|
||||
|
||||
/**
|
||||
* Output should be the same as input
|
||||
*/
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
this.launchJob();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
XMLAssert.assertXMLEqual(new FileReader(INPUT_FILE), new FileReader(OUTPUT_FILE));
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
StaxEventItemReader<?> xmlReader = (StaxEventItemReader<?>) reader;
|
||||
xmlReader.setResource(outputResource);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/hibernate-context.xml" />
|
||||
<import resource="classpath:/jobs/hibernateJob.xml" />
|
||||
|
||||
</beans>
|
||||
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/jobs/batchUpdateJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user