RESOLVED - issue BATCH-1417: Error in FlatFileItemReader when RecordSeparatorPolicy.preProcess or readLine returns null
Throw FlatFileParseException if file ends in the middle of a record.
This commit is contained in:
@@ -180,7 +180,11 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
|
||||
String record = line;
|
||||
if (line != null) {
|
||||
while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) {
|
||||
record = recordSeparatorPolicy.preProcess(record) + (line = readLine());
|
||||
line = readLine();
|
||||
if (line==null) {
|
||||
throw new FlatFileParseException("Unexpected end of file before record complete", record, lineCount);
|
||||
}
|
||||
record = recordSeparatorPolicy.preProcess(record) + line;
|
||||
}
|
||||
}
|
||||
String logicalLine = recordSeparatorPolicy.postProcess(record);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.springframework.batch.item.file;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -70,6 +73,43 @@ public class FlatFileItemReaderTests {
|
||||
assertEquals("testLine5testLine6", reader.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomRecordSeparatorPolicyEndOfFile() throws Exception {
|
||||
|
||||
reader.setRecordSeparatorPolicy(new RecordSeparatorPolicy() {
|
||||
// 1 record = 2 lines
|
||||
boolean pair = true;
|
||||
|
||||
public boolean isEndOfRecord(String line) {
|
||||
pair = !pair;
|
||||
return pair;
|
||||
}
|
||||
|
||||
public String postProcess(String record) {
|
||||
return record;
|
||||
}
|
||||
|
||||
public String preProcess(String record) {
|
||||
return record;
|
||||
}
|
||||
});
|
||||
|
||||
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\n"));
|
||||
reader.open(executionContext);
|
||||
|
||||
assertEquals("testLine1testLine2", reader.read());
|
||||
|
||||
try {
|
||||
reader.read();
|
||||
fail("Expected Exception");
|
||||
} catch (FlatFileParseException e) {
|
||||
// File ends in the middle of a record
|
||||
assertEquals(3, e.getLineNumber());
|
||||
assertEquals("testLine3", e.getInput());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestartWithSkippedLines() throws Exception {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user