@Before public void setUp() { reader.setResource(inputResource1); reader.setLineMapper(new PassThroughLineMapper()); itemReader.setResource(inputResource2); itemReader.setLineMapper(new ItemLineMapper()); }
@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()); }
itemReader.setResource(resource);
@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()); }
reader.setResource(resource);
reader.setResource(this.resource);
@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()); }
@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; }
/** * 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 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 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")); }
@Override public void setResource(Resource resource){ //No longer using MultiResource Reader this.itemResource = resource; super.setResource(resource); }
@Bean public FlatFileItemReader<Partner> reader(){ FlatFileItemReader<Partner> itemReader = new FlatFileItemReader<Partner>(); itemReader.setLineMapper(lineMapper()); itemReader.setResource(new ClassPathResource("partner-import.csv")); return itemReader; }
@Bean public FlatFileItemReader<Partner> reader(){ FlatFileItemReader<Partner> itemReader = new FlatFileItemReader<Partner>(); itemReader.setLineMapper(lineMapper()); itemReader.setResource(new ClassPathResource("partner-import.csv")); return itemReader; }
@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; }