[BATCH-383] Fixes to make Spring Batch 1.4 runtime compliant.

This commit is contained in:
nebhale
2008-03-11 13:49:17 +00:00
parent 32e25b081c
commit 9f7cc87c0d
40 changed files with 500 additions and 564 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
/**
* Tests for {@link FlatFileItemReader} - skip and restart functionality.
@@ -40,7 +41,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
// common value used for writing to a file
private String TEST_STRING = "FlatFileInputTemplate-TestData";
private ExecutionContext executionContext;
// simple stub instead of a realistic tokenizer
@@ -57,8 +58,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
};
/**
* Create inputFile, inject mock/stub dependencies for tested object,
* initialize the tested object
* Create inputFile, inject mock/stub dependencies for tested object, initialize the tested object
*/
protected void setUp() throws Exception {
@@ -83,6 +83,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
/**
* Test skip and skipRollback functionality
*
* @throws IOException
*/
public void testSkip() throws Exception {
@@ -118,6 +119,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
/**
* Test skip and skipRollback functionality
*
* @throws IOException
*/
public void testSkipFirstChunk() throws Exception {
@@ -160,8 +162,8 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
// get restart data
reader.update(executionContext);
assertEquals(4, executionContext.getLong(
FlatFileItemReader.class.getSimpleName() + ".lines.read.count"));
assertEquals(4, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class)
+ ".lines.read.count"));
// close input
reader.close(executionContext);
@@ -175,7 +177,8 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
assertEquals("[testLine6]", reader.read().toString());
reader.update(executionContext);
assertEquals(6, executionContext.getLong(FlatFileItemReader.class.getSimpleName() + ".lines.read.count"));
assertEquals(6, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class)
+ ".lines.read.count"));
}
}

View File

@@ -36,7 +36,7 @@ import org.springframework.core.io.Resource;
/**
* Tests for {@link FlatFileItemReader} - the fundamental item reading functionality.
*
*
* @see FlatFileItemReaderAdvancedTests
* @author Dave Syer
*/
@@ -48,7 +48,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
// common value used for writing to a file
private String TEST_STRING = "FlatFileInputTemplate-TestData";
private String TEST_OUTPUT = "[FlatFileInputTemplate-TestData]";
private ExecutionContext executionContext;
// simple stub instead of a realistic tokenizer
@@ -58,15 +58,14 @@ public class FlatFileItemReaderBasicTests extends TestCase {
}
};
private FieldSetMapper fieldSetMapper = new FieldSetMapper(){
private FieldSetMapper fieldSetMapper = new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
return fs;
}
};
/**
* Create inputFile, inject mock/stub dependencies for tested object,
* initialize the tested object
* Create inputFile, inject mock/stub dependencies for tested object, initialize the tested object
*/
protected void setUp() throws Exception {
@@ -74,7 +73,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setLineTokenizer(tokenizer);
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.afterPropertiesSet();
executionContext = new ExecutionContext();
}
@@ -126,7 +125,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
}
public void testReadWithMapperError() throws Exception {
itemReader.setFieldSetMapper(new FieldSetMapper(){
itemReader.setFieldSetMapper(new FieldSetMapper() {
public Object mapLine(FieldSet fs) {
throw new RuntimeException("foo");
}
@@ -150,7 +149,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.read();
fail("Expected ReaderNotOpenException");
} catch (ReaderNotOpenException e) {
assertTrue(e.getMessage().contains("open"));
assertTrue(contains(e.getMessage(), "open"));
}
}
@@ -169,8 +168,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
try {
itemReader.afterPropertiesSet();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
}
@@ -196,8 +194,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
try {
itemReader.open(executionContext);
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
}
@@ -209,8 +206,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
try {
itemReader.open(executionContext);
fail("Expected BatchEnvironmentException");
}
catch (ItemStreamException e) {
} catch (ItemStreamException e) {
// expected
assertEquals("foo", e.getCause().getMessage());
}
@@ -227,8 +223,8 @@ public class FlatFileItemReaderBasicTests extends TestCase {
}
public void testComments() throws Exception {
itemReader.setResource(getInputResource("% Comment\n"+TEST_STRING));
itemReader.setComments(new String[] {"%"});
itemReader.setResource(getInputResource("% Comment\n" + TEST_STRING));
itemReader.setComments(new String[] { "%" });
testRead();
}
@@ -237,7 +233,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
*/
public void testColumnNamesInHeader() throws Exception {
final String INPUT = "name1|name2\nvalue1|value2\nvalue3|value4";
itemReader = new FlatFileItemReader();
itemReader.setResource(getInputResource(INPUT));
itemReader.setLineTokenizer(new DelimitedLineTokenizer('|'));
@@ -245,11 +241,11 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setFirstLineIsHeader(true);
itemReader.afterPropertiesSet();
itemReader.open(executionContext);
FieldSet fs = (FieldSet) itemReader.read();
assertEquals("value1", fs.readString("name1"));
assertEquals("value2", fs.readString("name2"));
fs = (FieldSet) itemReader.read();
assertEquals("value3", fs.readString("name1"));
assertEquals("value4", fs.readString("name2"));
@@ -260,7 +256,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
*/
public void testLinesToSkip() throws Exception {
final String INPUT = "foo bar spam\none two\nthree four";
itemReader = new FlatFileItemReader();
itemReader.setResource(getInputResource(INPUT));
itemReader.setLineTokenizer(new DelimitedLineTokenizer(' '));
@@ -268,62 +264,66 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setLinesToSkip(1);
itemReader.afterPropertiesSet();
itemReader.open(executionContext);
FieldSet fs = (FieldSet) itemReader.read();
assertEquals("one", fs.readString(0));
assertEquals("two", fs.readString(1));
fs = (FieldSet) itemReader.read();
assertEquals("three", fs.readString(0));
assertEquals("four", fs.readString(1));
}
public void testNonExistantResource() throws Exception{
public void testNonExistantResource() throws Exception {
Resource resource = new NonExistentResource();
FlatFileItemReader testReader = new FlatFileItemReader();
testReader.setResource(resource);
testReader.setLineTokenizer(tokenizer);
testReader.setFieldSetMapper(fieldSetMapper);
testReader.setResource(resource);
//afterPropertiesSet should only throw an exception if the Resource is null
// afterPropertiesSet should only throw an exception if the Resource is null
testReader.afterPropertiesSet();
try{
try {
testReader.open(executionContext);
fail();
}catch(IllegalStateException ex){
//expected
} catch (IllegalStateException ex) {
// expected
}
}
public void testRuntimeFileCreation() throws Exception{
public void testRuntimeFileCreation() throws Exception {
Resource resource = new NonExistentResource();
FlatFileItemReader testReader = new FlatFileItemReader();
testReader.setResource(resource);
testReader.setLineTokenizer(tokenizer);
testReader.setFieldSetMapper(fieldSetMapper);
testReader.setResource(resource);
//afterPropertiesSet should only throw an exception if the Resource is null
// afterPropertiesSet should only throw an exception if the Resource is null
testReader.afterPropertiesSet();
//replace the resource to simulate runtime resource creation
// replace the resource to simulate runtime resource creation
testReader.setResource(getInputResource(TEST_STRING));
testReader.open(executionContext);
assertEquals(TEST_OUTPUT, testReader.read().toString());
}
private class NonExistentResource extends AbstractResource{
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}
private class NonExistentResource extends AbstractResource {
public NonExistentResource() {
}
public boolean exists() {
return false;
}

View File

@@ -30,11 +30,11 @@ import org.springframework.batch.item.file.mapping.FieldSetCreator;
import org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
/**
* Tests of regular usage for {@link FlatFileItemWriter} Exception cases will be
* in separate TestCase classes with different <code>setUp</code> and
* <code>tearDown</code> methods
* Tests of regular usage for {@link FlatFileItemWriter} Exception cases will be in separate TestCase classes with
* different <code>setUp</code> and <code>tearDown</code> methods
*
* @author robert.kasanicky
* @author Dave Syer
@@ -53,12 +53,11 @@ public class FlatFileItemWriterTests extends TestCase {
// reads the output file to check the result
private BufferedReader reader;
private ExecutionContext executionContext;
/**
* Create temporary output file, define mock behaviour, set dependencies and
* initialize the object under test
* Create temporary output file, define mock behaviour, set dependencies and initialize the object under test
*/
protected void setUp() throws Exception {
@@ -87,9 +86,8 @@ public class FlatFileItemWriterTests extends TestCase {
}
/*
* Read a line from the output file, if the reader has not been created,
* recreate. This method is only necessary because running the tests in a
* UNIX environment locks the file if it's open for writing.
* Read a line from the output file, if the reader has not been created, recreate. This method is only necessary
* because running the tests in a UNIX environment locks the file if it's open for writing.
*/
private String readLine() throws IOException {
@@ -102,6 +100,7 @@ public class FlatFileItemWriterTests extends TestCase {
/**
* Regular usage of <code>write(String)</code> method
*
* @throws Exception
*/
public void testWriteString() throws Exception {
@@ -115,6 +114,7 @@ public class FlatFileItemWriterTests extends TestCase {
/**
* Regular usage of <code>write(String)</code> method
*
* @throws Exception
*/
public void testWriteWithConverter() throws Exception {
@@ -133,6 +133,7 @@ public class FlatFileItemWriterTests extends TestCase {
/**
* Regular usage of <code>write(String)</code> method
*
* @throws Exception
*/
public void testWriteWithConverterAndInfiniteLoop() throws Exception {
@@ -151,6 +152,7 @@ public class FlatFileItemWriterTests extends TestCase {
/**
* Regular usage of <code>write(String)</code> method
*
* @throws Exception
*/
public void testWriteWithConverterAndString() throws Exception {
@@ -167,6 +169,7 @@ public class FlatFileItemWriterTests extends TestCase {
/**
* Regular usage of <code>write(String[], LineDescriptor)</code> method
*
* @throws Exception
*/
public void testWriteRecord() throws Exception {
@@ -245,7 +248,7 @@ public class FlatFileItemWriterTests extends TestCase {
}
// 3 lines were written to the file after restart
assertEquals(3, executionContext.getLong(FlatFileItemWriter.class.getSimpleName() + ".written"));
assertEquals(3, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
}
@@ -254,8 +257,7 @@ public class FlatFileItemWriterTests extends TestCase {
try {
inputSource.afterPropertiesSet();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
}
@@ -269,7 +271,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.update(executionContext);
assertNotNull(executionContext);
assertEquals(3, executionContext.entrySet().size());
assertEquals(0, executionContext.getLong(FlatFileItemWriter.class.getSimpleName() + ".current.count"));
assertEquals(0, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".current.count"));
}
private void commit() throws Exception {

View File

@@ -21,6 +21,7 @@ import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.util.ClassUtils;
/**
* Tests for {@link StaxEventItemReader}.
@@ -38,7 +39,7 @@ public class StaxEventItemReaderTests extends TestCase {
private EventReaderDeserializer deserializer = new MockFragmentDeserializer();
private static final String FRAGMENT_ROOT_ELEMENT = "fragment";
private ExecutionContext executionContext;
protected void setUp() throws Exception {
@@ -55,8 +56,7 @@ public class StaxEventItemReaderTests extends TestCase {
try {
source.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected;
}
@@ -65,8 +65,7 @@ public class StaxEventItemReaderTests extends TestCase {
try {
source.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
@@ -75,15 +74,14 @@ public class StaxEventItemReaderTests extends TestCase {
try {
source.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
}
/**
* Regular usage scenario. ItemReader should pass XML fragments to
* deserializer wrapped with StartDocument and EndDocument events.
* Regular usage scenario. ItemReader should pass XML fragments to deserializer wrapped with StartDocument and
* EndDocument events.
*/
public void testFragmentWrapping() throws Exception {
source.afterPropertiesSet();
@@ -120,7 +118,7 @@ public class StaxEventItemReaderTests extends TestCase {
source.read();
source.update(executionContext);
System.out.println(executionContext);
assertEquals(1, executionContext.getLong(StaxEventItemReader.class.getSimpleName() + ".read.count"));
assertEquals(1, executionContext.getLong(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count"));
List expectedAfterRestart = (List) source.read();
source = createNewInputSouce();
@@ -130,20 +128,18 @@ public class StaxEventItemReaderTests extends TestCase {
}
/**
* Restore point must not exceed end of file, input source must not be
* already initialised when restoring.
* Restore point must not exceed end of file, input source must not be already initialised when restoring.
*/
public void testInvalidRestore() {
ExecutionContext context = new ExecutionContext();
context.putLong(StaxEventItemReader.class.getSimpleName() + ".read.count", 100000);
context.putLong(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count", 100000);
try {
source.open(context);
fail("Expected StreamException");
}
catch (ItemStreamException e) {
} catch (ItemStreamException e) {
// expected
String message = e.getMessage();
assertTrue("Wrong message: "+message, message.contains("must be before"));
assertTrue("Wrong message: " + message, contains(message, "must be before"));
}
}
@@ -151,6 +147,7 @@ public class StaxEventItemReaderTests extends TestCase {
source.close(executionContext);
source.update(executionContext);
}
/**
* Skipping marked records after rollback.
*/
@@ -184,8 +181,7 @@ public class StaxEventItemReaderTests extends TestCase {
source.setFragmentDeserializer(new ExceptionFragmentDeserializer());
try {
source.read();
}
catch (Exception expected) {
} catch (Exception expected) {
source.reset();
}
source.setFragmentDeserializer(deserializer);
@@ -194,14 +190,13 @@ public class StaxEventItemReaderTests extends TestCase {
}
/**
* Statistics return the current record count. Calling read after end of
* input does not increase the counter.
* Statistics return the current record count. Calling read after end of input does not increase the counter.
*/
public void testExecutionContext() {
final int NUMBER_OF_RECORDS = 2;
source.open(executionContext);
source.update(executionContext);
for (int i = 0; i < NUMBER_OF_RECORDS; i++) {
long recordCount = extractRecordCount();
assertEquals(i, recordCount);
@@ -215,7 +210,7 @@ public class StaxEventItemReaderTests extends TestCase {
}
private long extractRecordCount() {
return executionContext.getLong(StaxEventItemReader.class.getSimpleName() + ".read.count");
return executionContext.getLong(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count");
}
public void testCloseWithoutOpen() throws Exception {
@@ -243,8 +238,7 @@ public class StaxEventItemReaderTests extends TestCase {
try {
item = newSource.read();
fail("Expected ReaderNotOpenException");
}
catch (ReaderNotOpenException e) {
} catch (ReaderNotOpenException e) {
// expected
}
}
@@ -267,8 +261,7 @@ public class StaxEventItemReaderTests extends TestCase {
try {
source.open(executionContext);
}
catch (DataAccessResourceFailureException ex) {
} catch (DataAccessResourceFailureException ex) {
assertTrue(ex.getCause() instanceof IOException);
}
@@ -282,8 +275,7 @@ public class StaxEventItemReaderTests extends TestCase {
try {
source.open(executionContext);
fail();
}
catch (IllegalStateException ex) {
} catch (IllegalStateException ex) {
// expected
}
}
@@ -312,15 +304,14 @@ public class StaxEventItemReaderTests extends TestCase {
}
/**
* A simple XMLEvent deserializer mock - check for the start and end
* document events for the fragment root & end tags + skips the fragment
* contents.
* A simple XMLEvent deserializer mock - check for the start and end document events for the fragment root & end
* tags + skips the fragment contents.
*/
private static class MockFragmentDeserializer implements EventReaderDeserializer {
/**
* A simple mapFragment implementation checking the
* StaxEventReaderItemReader basic read functionality.
* A simple mapFragment implementation checking the StaxEventReaderItemReader basic read functionality.
*
* @param eventReader
* @return list of the events from fragment body
*/
@@ -348,8 +339,7 @@ public class StaxEventItemReaderTests extends TestCase {
XMLEvent event4 = eventReader.nextEvent();
assertTrue(event4.isEndDocument());
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException("Error occured in FragmentDeserializer", e);
}
return fragmentContent;
@@ -364,7 +354,7 @@ public class StaxEventItemReaderTests extends TestCase {
do {
eventInsideFragment = eventReader.peek();
if (eventInsideFragment instanceof EndElement
&& ((EndElement) eventInsideFragment).getName().getLocalPart().equals(FRAGMENT_ROOT_ELEMENT)) {
&& ((EndElement) eventInsideFragment).getName().getLocalPart().equals(FRAGMENT_ROOT_ELEMENT)) {
break;
}
events.add(eventReader.nextEvent());
@@ -375,6 +365,10 @@ public class StaxEventItemReaderTests extends TestCase {
}
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}
/**
* Moves cursor inside the fragment body and causes rollback.
*/

View File

@@ -18,6 +18,7 @@ import org.springframework.core.io.Resource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.xml.transform.StaxResult;
/**
@@ -30,7 +31,7 @@ public class StaxEventItemWriterTests extends TestCase {
// output file
private Resource resource;
private ExecutionContext executionContext;
// test item for writing to output
@@ -40,7 +41,7 @@ public class StaxEventItemWriterTests extends TestCase {
}
};
private static final String TEST_STRING = StaxEventItemWriter.class.getSimpleName() + "-testString";
private static final String TEST_STRING = ClassUtils.getShortName(StaxEventItemWriter.class) + "-testString";
private static final int NOT_FOUND = -1;
@@ -62,20 +63,20 @@ public class StaxEventItemWriterTests extends TestCase {
// see asserts in the marshaller
writer.write(item);
assertFalse(marshaller.wasCalled);
writer.flush();
assertTrue(marshaller.wasCalled);
}
public void testClear() throws Exception {
writer.open(executionContext);
writer.write(item);
writer.write(item);
writer.clear();
//writer.write(item);
// writer.write(item);
writer.flush();
assertFalse(outputFileContent().contains(TEST_STRING));
assertFalse(contains(outputFileContent(), TEST_STRING));
}
/**
@@ -97,7 +98,7 @@ public class StaxEventItemWriterTests extends TestCase {
writer.write(item);
assertEquals("", outputFileContent());
writer.flush();
assertTrue(outputFileContent().contains(TEST_STRING));
assertTrue(contains(outputFileContent(), TEST_STRING));
}
/**
@@ -116,7 +117,7 @@ public class StaxEventItemWriterTests extends TestCase {
writer.open(executionContext);
writer.write(item);
writer.close(executionContext);
// check the output is concatenation of 'before restart' and 'after
// restart' writes.
String outputFile = outputFileContent();
@@ -139,7 +140,8 @@ public class StaxEventItemWriterTests extends TestCase {
for (int i = 1; i <= NUMBER_OF_RECORDS; i++) {
writer.write(item);
writer.update(executionContext);
long writeStatistics = executionContext.getLong(StaxEventItemWriter.class.getSimpleName() + ".record.count");
long writeStatistics = executionContext.getLong(ClassUtils.getShortName(StaxEventItemWriter.class)
+ ".record.count");
assertEquals(i, writeStatistics);
}
@@ -168,9 +170,9 @@ public class StaxEventItemWriterTests extends TestCase {
* Checks the received parameters.
*/
private class InputCheckMarshaller implements Marshaller {
boolean wasCalled = false;
public void marshal(Object graph, Result result) {
wasCalled = true;
assertTrue(result instanceof StaxResult);
@@ -192,8 +194,7 @@ public class StaxEventItemWriterTests extends TestCase {
StaxResult staxResult = (StaxResult) result;
try {
staxResult.getXMLEventWriter().add(XMLEventFactory.newInstance().createComment(graph.toString()));
}
catch (XMLStreamException e) {
} catch (XMLStreamException e) {
throw new RuntimeException("Exception while writing to output file", e);
}
}
@@ -228,7 +229,11 @@ public class StaxEventItemWriterTests extends TestCase {
source.setSaveState(true);
source.afterPropertiesSet();
return source;
}
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}
}

View File

@@ -107,7 +107,6 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().toLowerCase().indexOf("unclassified")>=0);
assertEquals("Foo", e.getCause().getMessage());
}
}