/** * 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; }
@Test public void testGetCurrentResourceBeforeRead() throws Exception { tested.open(ctx); assertNull("There is no 'current' resource before read is called", tested.getCurrentResource()); }
/** * Store the current resource index and position in the resource. */ @Override public void update(ExecutionContext executionContext) throws ItemStreamException { super.update(executionContext); if (saveState) { executionContext.putInt(getExecutionContextKey(RESOURCE_KEY), currentResource); delegate.update(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); }
@Test public void testRestartWhenStateNotSaved() throws Exception { tested.setSaveState(false); tested.open(ctx); assertEquals("1", tested.read()); tested.update(ctx); assertEquals("2", tested.read()); assertEquals("3", tested.read()); tested.close(); tested.open(ctx); assertEquals("1", tested.read()); }
@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; }
/** * Empty resource list is OK. */ @Test public void testNoResourcesFound() throws Exception { tested.setResources(new Resource[] {}); 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 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 }); }
@Bean public MultiResourceItemReader<Customer> multiResourceItemReader() { MultiResourceItemReader<Customer> reader = new MultiResourceItemReader<>(); reader.setDelegate(customerItemReader()); reader.setResources(inputFiles); 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()); } }
@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]); }
@Test public void testGetCurrentResource() throws Exception { tested.open(ctx); assertEquals("1", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("2", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("3", tested.read()); assertSame(r1, tested.getCurrentResource()); assertEquals("4", tested.read()); assertSame(r2, tested.getCurrentResource()); assertEquals("5", tested.read()); assertSame(r2, tested.getCurrentResource()); assertEquals("6", tested.read()); assertSame(r4, tested.getCurrentResource()); assertEquals("7", tested.read()); assertSame(r5, tested.getCurrentResource()); assertEquals("8", tested.read()); assertSame(r5, tested.getCurrentResource()); assertEquals(null, tested.read()); assertSame(null, tested.getCurrentResource()); tested.close(); }
private T readFromDelegate() throws Exception { T item = delegate.read(); if(item instanceof ResourceAware){ ((ResourceAware) item).setResource(getCurrentResource()); } return item; }
/** * Reads the next item, jumping to next resource if necessary. */ @Override public T read() throws Exception, UnexpectedInputException, ParseException { if (noInput) { return null; } // If there is no resource, then this is the first item, set the current // resource to 0 and open the first delegate. if (currentResource == -1) { currentResource = 0; delegate.setResource(resources[currentResource]); delegate.open(new ExecutionContext()); } return readNextItem(); }
/** * Use the delegate to read the next item, jump to next resource if current one is exhausted. Items are appended to * the buffer. * * @return next item from input */ private T readNextItem() throws Exception { T item = readFromDelegate(); while (item == null) { currentResource++; if (currentResource >= resources.length) { return null; } delegate.close(); delegate.setResource(resources[currentResource]); delegate.open(new ExecutionContext()); item = readFromDelegate(); } return item; }
@Override protected ItemReader<Foo> getItemReader() throws Exception { MultiResourceItemReader<Foo> multiReader = new MultiResourceItemReader<>(); Resource r4 = new ByteArrayResource("<foos> <foo value=\"4\"/> <foo value=\"5\"/> </foos>".getBytes()); multiReader.setDelegate(reader); multiReader.setResources(new Resource[] { r1, r2, r3, r4 }); multiReader.setSaveState(true); multiReader.setComparator(new Comparator<Resource>() { @Override public int compare(Resource arg0, Resource arg1) {
/** * 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(); }
/** * 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 }); }
/** * 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); }