OPEN - issue BATCH-371: FlatFileItemWriter no longer uses LineAggregator

http://jira.springframework.org/browse/BATCH-371

Moved existing default transformer out to a top level class, and introduce lineAggregatorItemTransformer as an option.  I still don't see a good way to get a String[] in to it reliably.
This commit is contained in:
dsyer
2008-02-19 21:40:44 +00:00
parent 10d4632dcb
commit 91a9e94590
5 changed files with 207 additions and 124 deletions

View File

@@ -157,77 +157,11 @@ public class FlatFileItemWriterTests extends TestCase {
assertEquals("FOO:" + data.toString(), lineFromFile);
}
/**
* Regular usage of <code>write(String)</code> method
* @throws Exception
*/
public void testWriteWithConverterAndInfiniteLoopInCollection() throws Exception {
inputSource.setTransformer(new ItemTransformer() {
public Object transform(Object input) {
return "FOO:" + input;
}
});
Object data = new Object();
inputSource.write(new Object[] { data, data });
inputSource.close();
String lineFromFile = readLine();
assertEquals("FOO:" + data.toString(), lineFromFile);
lineFromFile = readLine();
assertEquals("FOO:" + data.toString(), lineFromFile);
}
/**
* Regular usage of <code>write(String)</code> method
* @throws Exception
*/
public void testWriteWithConverterAndInfiniteLoopInConvertedCollection() throws Exception {
inputSource.setTransformer(new ItemTransformer() {
boolean converted = false;
public Object transform(Object input) {
if (converted) {
return input;
}
converted = true;
return new Object[] { input, input };
}
});
Object data = new Object();
try {
inputSource.write(data);
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
// expected
assertTrue("Wrong message: " + e, e.getMessage().toLowerCase().indexOf("infinite") >= 0);
}
inputSource.close();
String lineFromFile = readLine();
assertNull(lineFromFile);
}
/**
* Regular usage of <code>write(String)</code> method
* @throws Exception
*/
public void testWriteWithConverterAndString() throws Exception {
inputSource.setTransformer(new ItemTransformer() {
public Object transform(Object input) {
return "FOO:" + input;
}
});
inputSource.write(Collections.singleton(TEST_STRING));
inputSource.close();
String lineFromFile = readLine();
// converter not used if input is String
assertEquals(TEST_STRING, lineFromFile);
}
/**
* Regular usage of <code>write(String)</code> method
* @throws Exception
*/
public void testWriteWithConverterAndCollectionOfString() throws Exception {
inputSource.setTransformer(new ItemTransformer() {
public Object transform(Object input) {
return "FOO:" + input;
@@ -236,8 +170,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.write(TEST_STRING);
inputSource.close();
String lineFromFile = readLine();
// converter not used if input is String
assertEquals(TEST_STRING, lineFromFile);
assertEquals("FOO:"+TEST_STRING, lineFromFile);
}
/**

View File

@@ -0,0 +1,64 @@
/*
* 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.io.file.transform;
import junit.framework.TestCase;
/**
* @author Dave Syer
*
*/
public class LineAggregatorItemTransformerTests extends TestCase {
private LineAggregatorItemTransformer transformer = new LineAggregatorItemTransformer();
/**
* Test method for {@link org.springframework.batch.io.file.transform.LineAggregatorItemTransformer#setAggregator(org.springframework.batch.io.file.transform.LineAggregator)}.
* @throws Exception
*/
public void testSetAggregator() throws Exception {
transformer.setAggregator(new LineAggregator() {
public String aggregate(String[] args) {
return "foo";
}
});
String value = (String) transformer.transform(new String[] {"a", "b"});
assertEquals("foo", value);
}
/**
* Test method for {@link org.springframework.batch.io.file.transform.LineAggregatorItemTransformer#transform(java.lang.Object)}.
* @throws Exception
*/
public void testTransform() throws Exception {
String value = (String) transformer.transform(new String[] {"a", "b"});
assertTrue("Wrong value: "+value, value.startsWith("a,b"));
}
/**
* Test method for {@link org.springframework.batch.io.file.transform.LineAggregatorItemTransformer#transform(java.lang.Object)}.
* @throws Exception
*/
public void testTransformWrongType() throws Exception {
try {
transformer.transform("foo");
fail("Expected ClassCastException");
} catch (ClassCastException e) {
// Expected
}
}
}