BATCH-1306:
*FieldExtractorLineAggregator now replaces nulls with empty strings before calling doAggregate(). *PassthroughFieldExtractor no longer replaces nulls.
This commit is contained in:
@@ -15,8 +15,9 @@
|
||||
*/
|
||||
package org.springframework.batch.item.file.transform;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,19 @@ import org.junit.Test;
|
||||
*/
|
||||
public class DelimitedLineAggregatorTests {
|
||||
|
||||
private DelimitedLineAggregator<String[]> aggregator = new DelimitedLineAggregator<String[]>();
|
||||
private static DelimitedLineAggregator<String[]> aggregator;
|
||||
|
||||
private FieldExtractor<String[]> defaultFieldExtractor = new FieldExtractor<String[]>() {
|
||||
public Object[] extract(String[] item) {
|
||||
return item;
|
||||
}
|
||||
};
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
aggregator = new DelimitedLineAggregator<String[]>();
|
||||
aggregator.setFieldExtractor(defaultFieldExtractor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDelimiter() {
|
||||
@@ -38,4 +51,8 @@ public class DelimitedLineAggregatorTests {
|
||||
assertEquals("foo,bar", aggregator.aggregate(new String[] { "foo", "bar" }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAggregateWithNull() {
|
||||
assertEquals("foo,,bar", aggregator.aggregate(new String[] { "foo", null, "bar" }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.batch.item.file.transform;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,19 @@ import org.junit.Test;
|
||||
public class FormatterLineAggregatorTests {
|
||||
|
||||
// object under test
|
||||
private FormatterLineAggregator<String[]> aggregator = new FormatterLineAggregator<String[]>();
|
||||
private FormatterLineAggregator<String[]> aggregator;
|
||||
|
||||
private FieldExtractor<String[]> defaultFieldExtractor = new FieldExtractor<String[]>() {
|
||||
public Object[] extract(String[] item) {
|
||||
return item;
|
||||
}
|
||||
};
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
aggregator = new FormatterLineAggregator<String[]>();
|
||||
aggregator.setFieldExtractor(defaultFieldExtractor);
|
||||
}
|
||||
|
||||
/**
|
||||
* If no ranges are specified, IllegalArgumentException is thrown
|
||||
@@ -110,17 +123,18 @@ public class FormatterLineAggregatorTests {
|
||||
aggregator.setMaximumLength(25);
|
||||
|
||||
aggregator.setFieldExtractor(new FieldExtractor<String[]>() {
|
||||
private int[] widths = new int[] {13,12};
|
||||
private int[] widths = new int[] { 13, 12 };
|
||||
|
||||
public Object[] extract(String[] item) {
|
||||
String[] strings = new String[item.length];
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
strings[i] = item[i];
|
||||
if (item[i].length()<widths[i]) {
|
||||
if (item[i].length() < widths[i]) {
|
||||
StringBuffer buffer = new StringBuffer(strings[i]);
|
||||
for (int j = 0; j<(widths[i]-item[i].length()+1)/2; j++) {
|
||||
for (int j = 0; j < (widths[i] - item[i].length() + 1) / 2; j++) {
|
||||
buffer.append(" ");
|
||||
}
|
||||
strings[i] = buffer.toString();
|
||||
strings[i] = buffer.toString();
|
||||
}
|
||||
}
|
||||
return strings;
|
||||
@@ -143,17 +157,18 @@ public class FormatterLineAggregatorTests {
|
||||
aggregator.setMaximumLength(24);
|
||||
|
||||
aggregator.setFieldExtractor(new FieldExtractor<String[]>() {
|
||||
private int[] widths = new int[] {13,11};
|
||||
private int[] widths = new int[] { 13, 11 };
|
||||
|
||||
public Object[] extract(String[] item) {
|
||||
String[] strings = new String[item.length];
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
strings[i] = item[i];
|
||||
if (item[i].length()<widths[i]) {
|
||||
if (item[i].length() < widths[i]) {
|
||||
StringBuffer buffer = new StringBuffer(strings[i]);
|
||||
for (int j = 0; j<widths[i]-item[i].length(); j++) {
|
||||
for (int j = 0; j < widths[i] - item[i].length(); j++) {
|
||||
buffer.append(".");
|
||||
}
|
||||
strings[i] = buffer.toString();
|
||||
strings[i] = buffer.toString();
|
||||
}
|
||||
}
|
||||
return strings;
|
||||
@@ -181,8 +196,9 @@ public class FormatterLineAggregatorTests {
|
||||
*/
|
||||
@Test
|
||||
public void testAggregateNullArgument() {
|
||||
String[] args = { null };
|
||||
aggregator.setFormat("%3s");
|
||||
assertEquals(" ", aggregator.aggregate(args));
|
||||
String[] args = { "foo", null, "bar" };
|
||||
aggregator.setFormat("%3s%3s%3s");
|
||||
assertEquals("foo bar", aggregator.aggregate(args));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class PassThroughFieldExtractorTests {
|
||||
public void testExtractArray() {
|
||||
PassThroughFieldExtractor<String[]> extractor = new PassThroughFieldExtractor<String[]>();
|
||||
Object[] result = extractor.extract(new String[] { "a", "b", null, "d" });
|
||||
assertTrue(Arrays.equals(new Object[] { "a", "b", "", "d" }, result));
|
||||
assertTrue(Arrays.equals(new Object[] { "a", "b", null, "d" }, result));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -53,6 +53,6 @@ public class PassThroughFieldExtractorTests {
|
||||
public void testExtractCollection() {
|
||||
PassThroughFieldExtractor<List<String>> extractor = new PassThroughFieldExtractor<List<String>>();
|
||||
Object[] result = extractor.extract(Arrays.asList("a", "b", null, "d"));
|
||||
assertTrue(Arrays.equals(new Object[] { "a", "b", "", "d" }, result));
|
||||
assertTrue(Arrays.equals(new Object[] { "a", "b", null, "d" }, result));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user