@Test public void testRead() throws Exception { ItemReader<Object> mock1 = delegates.get(0); ItemReader<Object> mock2 = delegates.get(1); EasyMock.expect(mock1.read()).andReturn("from 1st delegate"); EasyMock.expect(mock1.read()).andReturn("from 1st delegate"); EasyMock.expect(mock1.read()).andReturn(null); EasyMock.expect(mock2.read()).andReturn("from 2nd delegate"); EasyMock.expect(mock2.read()).andReturn(null); EasyMock.replay(mock1,mock2); CompositeItemReader<Object> composite = new CompositeItemReader<>(); composite.setDelegates(delegates); for (int i = 1; i <= 2; i++) { assertEquals("Read " + (i) + " should have returned an object", "from 1st delegate", composite.read()); } assertEquals("Read 3 should have returned an object", "from 2nd delegate", composite.read()); assertNull("There should be no more items to read", composite.read()); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { T item; if(currentDelegate == null || (item = currentDelegate.read()) == null) { logger.debug("Loading new item reader"); try { if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).close(); } currentDelegate = delegates.remove(0); logger.info("The current ItemReader is now " + currentDelegate); if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).open(new ExecutionContext()); } } catch (IndexOutOfBoundsException e) { logger.info("No more readers, returning null"); return null; } item = read(); } if (targetType == null) { targetType = (Class) item.getClass(); } else { if(!targetType.isInstance(item)) { logger.debug("Attempting to cast " + item + " to " + targetType); item = (T) conversionService.convert(item, targetType); } } logger.debug("Returning " + item); return item; }
CompositeItemReader implements ItemReader<T> { @SuppressWarnings({"rawtypes", "unchecked"}) @Override public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { T item; if(currentDelegate == null || (item = currentDelegate.read()) == null) { logger.debug("Loading new item reader"); try { if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).close(); } currentDelegate = delegates.remove(0); logger.info("The current ItemReader is now " + currentDelegate); if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).open(new ExecutionContext()); } } catch (IndexOutOfBoundsException e) { logger.info("No more readers, returning null"); return null; } item = read(); } if (targetType == null) { targetType = (Class) item.getClass(); } else { if(!targetType.isInstance(item)) { logger.debug("Attempting to cast " + item + " to " + targetType); item = (T) conversionService.convert(item, targetType); } } logger.debug("Returning " + item); return item; } }
CompositeItemReader implements ItemReader<T> { @SuppressWarnings({"rawtypes", "unchecked"}) @Override public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { T item; if(currentDelegate == null || (item = currentDelegate.read()) == null) { logger.debug("Loading new item reader"); try { if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).close(); } currentDelegate = delegates.remove(0); logger.info("The current ItemReader is now " + currentDelegate); if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).open(new ExecutionContext()); } } catch (IndexOutOfBoundsException e) { logger.info("No more readers, returning null"); return null; } item = read(); } if (targetType == null) { targetType = (Class) item.getClass(); } else { if(!targetType.isInstance(item)) { logger.debug("Attempting to cast " + item + " to " + targetType); item = (T) conversionService.convert(item, targetType); } } logger.debug("Returning " + item); return item; }  }
CompositeItemReader implements ItemReader<T> { @SuppressWarnings({"rawtypes", "unchecked"}) @Override public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { T item; if(currentDelegate == null || (item = currentDelegate.read()) == null) { logger.debug("Loading new item reader"); try { if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).close(); } currentDelegate = delegates.remove(0); logger.info("The current ItemReader is now " + currentDelegate); if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).open(new ExecutionContext()); } } catch (IndexOutOfBoundsException e) { logger.info("No more readers, returning null"); return null; } item = read(); } if (targetType == null) { targetType = (Class) item.getClass(); } else { if(!targetType.isInstance(item)) { logger.debug("Attempting to cast " + item + " to " + targetType); item = (T) conversionService.convert(item, targetType); } } logger.debug("Returning " + item); return item; }  List<ItemReader<T>> getDelegates(); void setDelegates(List<ItemReader<T>> delegates); @SuppressWarnings({ "rawtypes", "unchecked" }) void setTargetType(Class targetType); void setConversionService(ConversionService conversionService); @SuppressWarnings({"rawtypes", "unchecked"}) @Override T read(); }
CompositeItemReader implements ItemReader<T> { @SuppressWarnings({"rawtypes", "unchecked"}) @Override public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { T item; if(currentDelegate == null || (item = currentDelegate.read()) == null) { logger.debug("Loading new item reader"); try { if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).close(); } currentDelegate = delegates.remove(0); logger.info("The current ItemReader is now " + currentDelegate); if(currentDelegate != null && ItemStream.class.isAssignableFrom(currentDelegate.getClass())) { ((ItemStream) currentDelegate).open(new ExecutionContext()); } } catch (IndexOutOfBoundsException e) { logger.info("No more readers, returning null"); return null; } item = read(); } if (targetType == null) { targetType = (Class) item.getClass(); } else { if(!targetType.isInstance(item)) { logger.debug("Attempting to cast " + item + " to " + targetType); item = (T) conversionService.convert(item, targetType); } } logger.debug("Returning " + item); return item; }  List<ItemReader<T>> getDelegates(); void setDelegates(List<ItemReader<T>> delegates); @SuppressWarnings({ "rawtypes", "unchecked" }) void setTargetType(Class targetType); void setConversionService(ConversionService conversionService); @SuppressWarnings({"rawtypes", "unchecked"}) @Override T read();  }