@Override public void close() throws ItemStreamException { this.delegate.close(); }
@Test public void testCurrentItemCountFromContext() throws Exception { reader.setCurrentItemCount(2); executionContext.putInt(reader.getClass().getSimpleName() + ".read.count", 3); reader.open(executionContext); // read some records assertEquals("testLine4", reader.read()); // close input reader.close(); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { FlatFileItemReader<Foo> reader = (FlatFileItemReader<Foo>) tested; reader.close(); reader.setResource(new ByteArrayResource("".getBytes())); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Test public void testMaxItemCountFromContext() throws Exception { reader.setMaxItemCount(2); executionContext.putInt(reader.getClass().getSimpleName() + ".read.count.max", Integer.MAX_VALUE); reader.open(executionContext); // read some records reader.read(); reader.read(); assertNotNull(reader.read()); // close input reader.close(); }
@Test public void testMaxAndCurrentItemCount() throws Exception { reader.setMaxItemCount(2); reader.setCurrentItemCount(2); reader.open(executionContext); // read some records assertNull(reader.read()); // close input reader.close(); }
@Test public void testNonExistentResource() throws Exception { Resource resource = new NonExistentResource(); reader.setResource(resource); // afterPropertiesSet should only throw an exception if the Resource is // null reader.afterPropertiesSet(); reader.setStrict(false); reader.open(executionContext); assertNull(reader.read()); reader.close(); }
@Test public void testOpenBadIOInput() throws Exception { reader.setResource(new AbstractResource() { @Override public String getDescription() { return null; } @Override public InputStream getInputStream() throws IOException { throw new IOException(); } @Override public boolean exists() { return true; } }); try { reader.open(executionContext); fail(); } catch (ItemStreamException ex) { // expected } // read() should then return a null assertNull(reader.read()); reader.close(); }
@Test public void testCurrentItemCount() throws Exception { reader.setCurrentItemCount(2); reader.open(executionContext); // read some records reader.read(); reader.read(); // get restart data reader.update(executionContext); assertEquals(4, executionContext.getInt(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); // close input reader.close(); }
@Test public void testMaxItemCount() throws Exception { reader.setMaxItemCount(2); reader.open(executionContext); // read some records reader.read(); reader.read(); // get restart data reader.update(executionContext); assertNull(reader.read()); assertEquals(2, executionContext.getInt(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); // close input reader.close(); }
@Test public void testRestartWithCustomRecordSeparatorPolicy() throws Exception { reader.setRecordSeparatorPolicy(new RecordSeparatorPolicy() { // 1 record = 2 lines boolean pair = true; @Override public boolean isEndOfRecord(String line) { pair = !pair; return pair; } @Override public String postProcess(String record) { return record; } @Override public String preProcess(String record) { return record; } }); reader.open(executionContext); assertEquals("testLine1testLine2", reader.read()); assertEquals("testLine3testLine4", reader.read()); reader.update(executionContext); reader.close(); reader.open(executionContext); assertEquals("testLine5testLine6", reader.read()); }
@Test public void testItemCountAware() throws Exception { itemReader.open(executionContext); Item item1 = itemReader.read(); assertEquals("testLine1", item1.getValue()); assertEquals(1, item1.getItemCount()); Item item2 = itemReader.read(); assertEquals("testLine2", item2.getValue()); assertEquals(2, item2.getItemCount()); itemReader.update(executionContext); itemReader.close(); itemReader.open(executionContext); Item item3 = itemReader.read(); assertEquals("testLine3", item3.getValue()); assertEquals(3, item3.getItemCount()); }
@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")); }
assertEquals(2, item2.getItemCount()); itemReader.update(executionContext); itemReader.close();