@Test public void testRuntimeFileCreation() throws Exception { Resource resource = new NonExistentResource(); reader.setResource(resource); // afterPropertiesSet should only throw an exception if the Resource is // null reader.afterPropertiesSet(); // replace the resource to simulate runtime resource creation reader.setResource(getInputResource(TEST_STRING)); reader.open(executionContext); assertEquals(TEST_STRING, reader.read()); }
@Test public void testCustomRecordSeparatorMultilineBlankLineAfterEnd() throws Exception { reader.setRecordSeparatorPolicy(new RecordSeparatorPolicy() { // 1 record = 2 lines boolean pair = true; @Override public boolean isEndOfRecord(String line) { if (StringUtils.hasText(line)) { pair = !pair; } return pair; } @Override public String postProcess(String record) { return StringUtils.hasText(record) ? record : null; } @Override public String preProcess(String record) { return record; } }); reader.setResource(getInputResource("testLine1\ntestLine2\n\n")); reader.open(executionContext); assertEquals("testLine1testLine2", reader.read()); assertEquals(null, reader.read()); }
@Test public void testCustomRecordSeparatorBlankLine() throws Exception { reader.setRecordSeparatorPolicy(new RecordSeparatorPolicy() { @Override public boolean isEndOfRecord(String line) { return StringUtils.hasText(line); } @Override public String postProcess(String record) { return StringUtils.hasText(record) ? record : null; } @Override public String preProcess(String record) { return record; } }); reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\n\n")); reader.open(executionContext); assertEquals("testLine1", reader.read()); assertEquals("testLine2", reader.read()); assertEquals("testLine3", reader.read()); assertEquals(null, reader.read()); }
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\n")); reader.open(executionContext);
@Test public void testRestartWithSkippedLines() throws Exception { reader.setLinesToSkip(2); reader.open(executionContext); // read some records reader.read(); reader.read(); // get restart data reader.update(executionContext); // read next two records reader.read(); reader.read(); assertEquals(2, executionContext.getInt(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); // close input reader.close(); reader.setResource(getInputResource("header\nignoreme\ntestLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6")); // init for restart reader.open(executionContext); // read remaining records assertEquals("testLine3", reader.read()); assertEquals("testLine4", reader.read()); reader.update(executionContext); assertEquals(4, executionContext.getInt(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); }