scanner.close(); FlatFileItemReader<Store> itemReader = new FlatFileItemReader<Store>(); itemReader.setResource(resource); itemReader.setLineMapper(lineMapper); itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy()); itemReader.setLinesToSkip(1); itemReader.open(new ExecutionContext()); store = itemReader.read();
@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(); }
@Override protected ItemReader<Foo> getItemReader() throws Exception { FlatFileItemReader<Foo> tested = new FlatFileItemReader<>(); Resource resource = new ByteArrayResource(FOOS.getBytes()); tested.setResource(resource); tested.setLineMapper(new LineMapper<Foo>() { @Override public Foo mapLine(String line, int lineNumber) { Foo foo = new Foo(); foo.setValue(Integer.valueOf(line.trim())); return foo; } }); tested.setSaveState(true); tested.afterPropertiesSet(); return tested; }
@Bean public FlatFileItemReader<Partner> reader(){ FlatFileItemReader<Partner> itemReader = new FlatFileItemReader<Partner>(); itemReader.setLineMapper(lineMapper()); itemReader.setResource(new ClassPathResource("partner-import.csv")); return itemReader; }
@Bean ItemReader<StudentDTO> csvFileItemReader(Environment environment) { FlatFileItemReader<StudentDTO> csvFileReader = new FlatFileItemReader<>(); csvFileReader.setResource(new ClassPathResource(environment.getRequiredProperty(PROPERTY_CSV_SOURCE_FILE_PATH))); csvFileReader.setLinesToSkip(1); LineMapper<StudentDTO> studentLineMapper = createStudentLineMapper(); csvFileReader.setLineMapper(studentLineMapper); return csvFileReader; }
int validatorValue = this.tokenizerValidator.intValue(); FlatFileItemReader<T> reader = new FlatFileItemReader<>(); reader.setName(this.name); reader.setEncoding(this.encoding); reader.setResource(this.resource); reader.setLineMapper(this.lineMapper); reader.setLineMapper(lineMapper); reader.setLinesToSkip(this.linesToSkip); reader.setComments(this.comments.toArray(new String[this.comments.size()])); reader.setSkippedLinesCallback(this.skippedLinesCallback); reader.setRecordSeparatorPolicy(this.recordSeparatorPolicy); reader.setMaxItemCount(this.maxItemCount); reader.setCurrentItemCount(this.currentItemCount); reader.setSaveState(this.saveState); reader.setStrict(this.strict);
@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()); }
@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")); }
@Override protected ItemReader<Foo> getItemReader() throws Exception { MultiResourceItemReader<Foo> multiReader = new MultiResourceItemReader<>(); FlatFileItemReader<Foo> fileReader = new FlatFileItemReader<>(); fileReader.setLineMapper(new LineMapper<Foo>() { @Override public Foo mapLine(String line, int lineNumber) throws Exception { Foo foo = new Foo(); foo.setValue(Integer.valueOf(line)); return foo; } }); fileReader.setSaveState(true); multiReader.setDelegate(fileReader); Resource r1 = new ByteArrayResource("1\n2\n".getBytes()); Resource r2 = new ByteArrayResource("".getBytes()); Resource r3 = new ByteArrayResource("3\n".getBytes()); Resource r4 = new ByteArrayResource("4\n5\n".getBytes()); multiReader.setResources(new Resource[] { r1, r2, r3, r4 }); multiReader.setSaveState(true); multiReader.setComparator(new Comparator<Resource>() { @Override public int compare(Resource arg0, Resource arg1) { return 0; // preserve original ordering } }); return multiReader; }
@Before public void setUp() { reader.setResource(inputResource1); reader.setLineMapper(new PassThroughLineMapper()); itemReader.setResource(inputResource2); itemReader.setLineMapper(new ItemLineMapper()); }
@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 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(); }
/** * Setup the tested reader to read from the test resources. */ @Before public void setUp() throws Exception { itemReader.setLineMapper(new FooLineMapper()); tested.setDelegate(itemReader); tested.setComparator(new Comparator<Resource>() { @Override public int compare(Resource o1, Resource o2) { return 0; // do not change ordering } }); tested.setResources(new Resource[] { r1, r2, r3, r4, r5 }); }
fb.setItemReader(new DummyItemReader()); fb.setItemWriter(new DummyItemWriter()); fb.setStreams(new ItemStream[] {new FlatFileItemReader<>() }); fb.setCacheCapacity(5); fb.setIsReaderTransactionalQueue(true);
@Bean public FlatFileItemReader<Customer> customerItemReader() { FlatFileItemReader<Customer> reader = new FlatFileItemReader<>(); DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>(); DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); tokenizer.setNames(new String[] {"id", "firstName", "lastName", "birthdate"}); customerLineMapper.setLineTokenizer(tokenizer); customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper()); customerLineMapper.afterPropertiesSet(); reader.setLineMapper(customerLineMapper); return reader; }
reader.setLineMapper(exceptionLineMapper); reader.afterPropertiesSet(); reader.open(executionContext); assertNotNull(reader.read()); reader.read(); fail();
@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 testAdvancedDelimited() throws Exception { final List<String> skippedLines = new ArrayList<>(); FlatFileItemReader<Foo> reader = new FlatFileItemReaderBuilder<Foo>() .name("fooReader") .resource(getResource("1,2,3\n4,5,$1,2,3$\n@this is a comment\n6,7, 8")) .delimited() .quoteCharacter('$') .names("first", "second", "third") .targetType(Foo.class) .linesToSkip(1) .skippedLinesCallback(skippedLines::add) .addComment("@") .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); Foo item = reader.read(); assertEquals(4, item.getFirst()); assertEquals(5, item.getSecond()); assertEquals("1,2,3", item.getThird()); item = reader.read(); assertEquals(6, item.getFirst()); assertEquals(7, item.getSecond()); assertEquals("8", item.getThird()); reader.update(executionContext); assertNull(reader.read()); assertEquals("1,2,3", skippedLines.get(0)); assertEquals(1, skippedLines.size()); assertEquals(1, executionContext.size()); }
@Override public void setResource(Resource resource){ //No longer using MultiResource Reader this.itemResource = resource; super.setResource(resource); }