ResourceLocatorItemStreamReader extends AbstractResourceLocatorItemStream<S, T> implements ItemStreamReader<T>, ResourceAwareItemStreamReader<T> { @Override public T read() throws Exception { T item = null; while (item == null) { if (!opened && resourceContext.containsResource()) { delegate.open(resourceContext.getExecutionContext()); opened = true; } if (opened) { item = delegate.read(); resourceLocator.onRead(item, resourceContext); } if (resourceContext.hasChanged()) { delegate.close(); delegate.update(resourceContext.getExecutionContext()); delegate.setResource(resourceContext.consumeResource()); opened = false; } else { break; } } return item; }  @Override T read();  }
@Test(timeout = 1000L) public void testReadWOResourceOnOpen() throws Exception { ExecutionContext executionContext = new ExecutionContext(); delegator.setResourceLocator(new CountingItemResourceLocator<>()); delegator.open(executionContext); delegator.read(); delegator.update(executionContext); Assert.assertNull(delegate.getResource()); }
@Test(timeout = 1000L) public void testReadWResourceOnOpen() throws Exception { ExecutionContext executionContext = new ExecutionContext(); delegator.setResourceLocator(new CountingItemResourceLocator<String>() { @Override public void onOpen(ResourceContext context) throws ItemStreamException { context.changeResource(new ClassPathResource(EXCEL_FILE_NAME2)); } }); delegator.open(executionContext); Resource onOpen = delegate.getResource(); Assert.assertEquals(EXCEL_FILE_NAME2, onOpen.getFilename()); delegator.read(); delegator.update(executionContext); Resource onRead = delegate.getResource(); Assert.assertEquals(onOpen, onRead); }
@Test(timeout = 1000L) public void testReadSetResourceBeforeOnOpen() throws Exception { ExecutionContext executionContext = new ExecutionContext(); delegator.setResourceLocator(new CountingItemResourceLocator<>()); delegator.open(executionContext); delegator.setResource(new ClassPathResource(EXCEL_FILE_NAME)); delegator.open(executionContext); Resource onOpen = delegate.getResource(); Assert.assertEquals(EXCEL_FILE_NAME, onOpen.getFilename()); delegator.read(); delegator.update(executionContext); Resource onRead = delegate.getResource(); Assert.assertEquals(onOpen, onRead); }
@Test(timeout = 1000L) public void testReadWResourceOnRead() throws Exception { ExecutionContext executionContext = new ExecutionContext(); delegator.setResourceLocator(new CountingItemResourceLocator<String>() { @Override public void onRead(String item, ResourceContext context) throws Exception { context.changeResource(new FileSystemResource(item)); } }); delegator.open(executionContext); delegator.setResource(new ClassPathResource(EXCEL_FILE_NAME)); delegator.open(executionContext); delegator.read(); delegator.read(); delegator.update(executionContext); Assert.assertEquals("a", delegate.getResource().getFilename()); }