/** * Builds the {@link MultiResourceItemReader}. * * @return a {@link MultiResourceItemReader} */ public MultiResourceItemReader<T> build() { Assert.notNull(this.resources, "resources array is required."); Assert.notNull(this.delegate, "delegate is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } MultiResourceItemReader<T> reader = new MultiResourceItemReader<>(); reader.setResources(this.resources); reader.setDelegate(this.delegate); reader.setSaveState(this.saveState); reader.setStrict(this.strict); if (comparator != null) { reader.setComparator(this.comparator); } if (StringUtils.hasText(this.name)) { reader.setName(this.name); } return reader; }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { MultiResourceItemReader<Foo> multiReader = (MultiResourceItemReader<Foo>) tested; multiReader.close(); multiReader.setResources(new Resource[] { new ByteArrayResource("" .getBytes()) }); multiReader.open(new ExecutionContext()); }
/** * No resources to read should result in error in strict mode. */ @Test(expected = IllegalStateException.class) public void testStrictModeEnabled() throws Exception { tested.setResources(new Resource[] {}); tested.setStrict(true); tested.open(ctx); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { MultiResourceItemReader<Foo> multiReader = (MultiResourceItemReader<Foo>) tested; multiReader.close(); multiReader.setResources(new Resource[] { new ByteArrayResource("".getBytes()) }); multiReader.open(new ExecutionContext()); } }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { MultiResourceItemReader<Foo> multiReader = (MultiResourceItemReader<Foo>) tested; multiReader.close(); multiReader.setResources(new Resource[] { new ByteArrayResource("<foos />" .getBytes()) }); multiReader.open(new ExecutionContext()); }
/** * No resources to read is OK when strict=false. */ @Test public void testStrictModeDisabled() throws Exception { tested.setResources(new Resource[] {}); tested.setStrict(false); tested.open(ctx); assertTrue("empty input doesn't cause an error", true); }
/** * Missing resource is OK. */ @Test public void testNonExistentResources() throws Exception { tested.setResources(new Resource[] { new FileSystemResource("no/such/file.txt") }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.close(); }
/** * Directory resource behaves as if it was empty. */ @Test public void testDirectoryResources() throws Exception { FileSystemResource resource = new FileSystemResource("build/data"); resource.getFile().mkdirs(); assertTrue(resource.getFile().isDirectory()); tested.setResources(new Resource[] { resource }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.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 }); }
/** * Setup the tested reader to read from the test resources. */ @Before public void setUp() throws Exception { itemReader.setLineMapper(new PassThroughLineMapper()); 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 }); }
/** * Empty resource list is OK. */ @Test public void testNoResourcesFound() throws Exception { tested.setResources(new Resource[] {}); tested.open(new ExecutionContext()); assertNull(tested.read()); tested.close(); }
@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; }
@Test public void testFirstResourceThrowsExceptionOnRead() throws Exception { Resource badResource = new AbstractResource() { @Override public InputStream getInputStream() throws IOException { throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { badResource, r2, r3, r4, r5 }); tested.open(ctx); try { assertEquals("1", tested.read()); fail(); } catch (ItemStreamException ex) { // a try/catch was used to ensure the exception was thrown when reading // the 1st item, rather than on open } }
/** * Resources are ordered according to injected comparator. */ @Test public void testResourceOrderingWithCustomComparator() { Resource r1 = new ByteArrayResource("".getBytes(), "b"); Resource r2 = new ByteArrayResource("".getBytes(), "a"); Resource r3 = new ByteArrayResource("".getBytes(), "c"); Resource[] resources = new Resource[] { r1, r2, r3 }; Comparator<Resource> comp = new Comparator<Resource>() { /** * Reversed ordering by filename. */ @Override public int compare(Resource o1, Resource o2) { Resource r1 = o1; Resource r2 = o2; return -r1.getDescription().compareTo(r2.getDescription()); } }; tested.setComparator(comp); tested.setResources(resources); tested.open(ctx); resources = (Resource[]) ReflectionTestUtils.getField(tested, "resources"); assertSame(r3, resources[0]); assertSame(r1, resources[1]); assertSame(r2, resources[2]); }
multiReader.setResources(new Resource[] { r1, r2, r3, r4 }); multiReader.setSaveState(true); multiReader.setComparator(new Comparator<Resource>() {
@Test public void testBadIOInput() throws Exception { Resource badResource = new AbstractResource() { @Override public boolean exists() { // Looks good ... return true; } @Override public InputStream getInputStream() throws IOException { // ... but fails during read throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { badResource, r2, r3, r4, r5 }); tested.open(ctx); try { assertEquals("1", tested.read()); fail(); } catch (ItemStreamException ex) { // expected } // Now check the next read gets the next resource assertEquals("4", tested.read()); }
/** * Test {@link org.springframework.batch.item.ItemStream} lifecycle symmetry */ @Test public void testNonExistentResourcesItemStreamLifecycle() throws Exception { ItemStreamReaderImpl delegate = new ItemStreamReaderImpl(); tested.setDelegate(delegate); tested.setResources(new Resource[] { }); itemReader.setStrict(false); tested.open(new ExecutionContext()); assertNull(tested.read()); assertFalse(delegate.openCalled); assertFalse(delegate.closeCalled); assertFalse(delegate.updateCalled); tested.close(); }
@Test public void testMiddleResourceThrowsException() throws Exception { Resource badResource = new AbstractResource() { @Override public InputStream getInputStream() throws IOException { throw new RuntimeException(); } @Override public String getDescription() { return null; } }; tested.setResources(new Resource[] { r1, badResource, r3, r4, r5 }); tested.open(ctx); assertEquals("1", tested.read()); assertEquals("2", tested.read()); assertEquals("3", tested.read()); try { assertEquals("4", tested.read()); fail(); } catch (ItemStreamException ex) { // a try/catch was used to ensure the exception was thrown when reading // the 4th item, rather than on open } }
@Bean public MultiResourceItemReader<Customer> multiResourceItemReader() { MultiResourceItemReader<Customer> reader = new MultiResourceItemReader<>(); reader.setDelegate(customerItemReader()); reader.setResources(inputFiles); return reader; }
/** * Builds the {@link MultiResourceItemReader}. * * @return a {@link MultiResourceItemReader} */ public MultiResourceItemReader<T> build() { Assert.notNull(this.resources, "resources array is required."); Assert.notNull(this.delegate, "delegate is required."); if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } MultiResourceItemReader<T> reader = new MultiResourceItemReader<>(); reader.setResources(this.resources); reader.setDelegate(this.delegate); reader.setSaveState(this.saveState); reader.setStrict(this.strict); if (comparator != null) { reader.setComparator(this.comparator); } if (StringUtils.hasText(this.name)) { reader.setName(this.name); } return reader; }