Fix JsonFileItemWriter for multiple write's

Resolves BATCH-2749
This commit is contained in:
Evgenii Strepetov
2018-09-05 17:55:06 -07:00
committed by Michael Minella
parent c8a0a7d407
commit 7a3248cabf
4 changed files with 37 additions and 3 deletions

View File

@@ -59,6 +59,8 @@ public abstract class JsonFileItemWriterFunctionalTests {
private ExecutionContext executionContext;
private Trade trade1 = new Trade("123", 5, new BigDecimal("10.5"), "foo");
private Trade trade2 = new Trade("456", 10, new BigDecimal("20.5"), "bar");
private Trade trade3 = new Trade("789", 15, new BigDecimal("30.5"), "foobar");
private Trade trade4 = new Trade("987", 20, new BigDecimal("40.5"), "barfoo");
protected abstract JsonObjectMarshaller<Trade> getJsonObjectMarshaller();
protected abstract JsonObjectMarshaller<Trade> getJsonObjectMarshallerWithPrettyPrint();
@@ -93,6 +95,20 @@ public abstract class JsonFileItemWriterFunctionalTests {
this.resource.getFile());
}
@Test
public void testJsonWritingWithMultipleWrite() throws Exception {
// when
this.writer.open(this.executionContext);
this.writer.write(this.items);
this.writer.write(Arrays.asList(trade3, trade4));
this.writer.close();
// then
assertFileEquals(
new File(EXPECTED_FILE_DIRECTORY + "expected-trades-with-multiple-writes.json"),
this.resource.getFile());
}
@Test
public void testJsonWritingWithPrettyPrinting() throws Exception {
// given

View File

@@ -0,0 +1,6 @@
[
{"isin":"123","quantity":5,"price":10.5,"customer":"foo"},
{"isin":"456","quantity":10,"price":20.5,"customer":"bar"},
{"isin":"789","quantity":15,"price":30.5,"customer":"foobar"},
{"isin":"987","quantity":20,"price":40.5,"customer":"barfoo"}
]

View File

@@ -10,5 +10,17 @@
"quantity": 2,
"price": 1.4,
"customer": "bar"
},
{
"isin": "789",
"quantity": 3,
"price": 1.6,
"customer": "foobar"
},
{
"isin": "100",
"quantity": 4,
"price": 1.8,
"customer": "barfoo"
}
]

View File

@@ -95,10 +95,10 @@ public class JsonFileItemWriter<T> extends AbstractFileItemWriter<T> {
public String doWrite(List<? extends T> items) {
StringBuilder lines = new StringBuilder();
Iterator<? extends T> iterator = items.iterator();
if (!items.isEmpty() && state.getLinesWritten() > 0) {
lines.append(JSON_OBJECT_SEPARATOR).append(this.lineSeparator);
}
while (iterator.hasNext()) {
if (iterator.hasNext() && state.getLinesWritten() > 0) {
lines.append(JSON_OBJECT_SEPARATOR).append(this.lineSeparator);
}
T item = iterator.next();
lines.append(' ').append(this.jsonObjectMarshaller.marshal(item));
if (iterator.hasNext()) {