@Override public void open(ExecutionContext executionContext) throws ItemStreamException { this.delegate.open(executionContext); }
@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 testDirectoryResource() throws Exception { FileSystemResource resource = new FileSystemResource("build/data"); resource.getFile().mkdirs(); assertTrue(resource.getFile().isDirectory()); reader.setResource(resource); reader.afterPropertiesSet(); reader.setStrict(false); reader.open(executionContext); assertNull(reader.read()); }
@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(); }
/** * In strict mode, resource must exist at the time reader is opened. */ @Test(expected = ItemStreamException.class) public void testStrictness() throws Exception { Resource resource = new NonExistentResource(); reader.setResource(resource); reader.setStrict(true); reader.afterPropertiesSet(); reader.open(executionContext); }
@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 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 testStrict() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(new FileSystemResource("this/file/does/not/exist")) .delimited() .names("first", "second", "third") .targetType(Foo.class) .strict(false) .build(); reader.open(new ExecutionContext()); assertNull(reader.read()); }
@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 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 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 testPrototypeBean() throws Exception { BeanFactory factory = new AnnotationConfigApplicationContext(Beans.class); FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3")) .delimited() .names("first", "second", "third") .prototypeBeanName("foo") .beanFactory(factory) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testSimpleDelimited() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3")) .delimited() .names("first", "second", "third") .targetType(Foo.class) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testSimpleDelimitedWithWhitespaceCharacter() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1 2 3")) .delimited() .delimiter(" ") .names("first", "second", "third") .targetType(Foo.class) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testSimpleDelimitedWithTabCharacter() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1\t2\t3")) .delimited() .delimiter(DelimitedLineTokenizer.DELIMITER_TAB) .names("first", "second", "third") .targetType(Foo.class) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testBeanWrapperFieldSetMapperStrict() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3")) .delimited() .names("setFirst", "setSecond", "setThird") .targetType(Foo.class) .beanMapperStrict(true) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testDelimitedIncludedFields() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3")) .delimited() .includedFields(0, 2) .addIncludedField(1) .names("first", "second", "third") .targetType(Foo.class) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }
@Test public void testSimpleFixedLength() throws Exception { FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1 2 3")) .fixedLength() .columns(new Range(1, 3), new Range(4, 6), new Range(7)) .names("first", "second", "third") .targetType(Foo.class) .build(); reader.open(new ExecutionContext()); Foo item = reader.read(); assertEquals(1, item.getFirst()); assertEquals(2, item.getSecond()); assertEquals("3", item.getThird()); assertNull(reader.read()); }