/** * Test method for {@link org.springframework.batch.item.support.SingleItemPeekableItemReader#close()}. */ @Test public void testCloseAndOpenNoPeek() throws Exception { reader.setDelegate(new CountingListItemReader<>(Arrays.asList("a", "b"))); assertEquals("a", reader.read()); ExecutionContext executionContext = new ExecutionContext(); reader.update(executionContext); reader.close(); reader.open(executionContext); assertEquals("b", reader.read()); }
/** * Returns a fully constructed {@link SingleItemPeekableItemReader}. * * @return a new {@link SingleItemPeekableItemReader} */ public SingleItemPeekableItemReader<T> build() { Assert.notNull(this.delegate, "A delegate is required"); SingleItemPeekableItemReader<T> reader = new SingleItemPeekableItemReader<>(); reader.setDelegate(this.delegate); return reader; } }
/** * Peek at the next item, ensuring that if the delegate is an * {@link ItemStream} the state is stored for the next call to * {@link #update(ExecutionContext)}. * * @return the next item (or null if there is none). * * @see PeekableItemReader#peek() */ @Override public T peek() throws Exception, UnexpectedInputException, ParseException { if (next == null) { updateDelegate(executionContext); next = delegate.read(); } return next; }
/** * Test method for {@link org.springframework.batch.item.support.SingleItemPeekableItemReader#peek()}. */ @Test public void testPeek() throws Exception { reader.setDelegate(new CountingListItemReader<>(Arrays.asList("a", "b"))); assertEquals("a", reader.peek()); assertEquals("a", reader.read()); assertEquals("b", reader.read()); assertEquals(null, reader.peek()); assertEquals(null, reader.read()); }
/** * Test method for {@link org.springframework.batch.item.support.SingleItemPeekableItemReader#read()}. */ @Test public void testRead() throws Exception { reader.setDelegate(new CountingListItemReader<>(Arrays.asList("a", "b"))); assertEquals("a", reader.read()); assertEquals("b", reader.read()); assertEquals(null, reader.read()); }
/** * Test method to validate builder creates a * {@link org.springframework.batch.item.support.SingleItemPeekableItemReader#peek()} * with expected peek and read behavior. */ @Test public void testPeek() throws Exception { SingleItemPeekableItemReader<String> reader = new SingleItemPeekableItemReaderBuilder<String>() .delegate( new ListItemReader<>(Arrays.asList("a", "b"))) .build(); assertEquals("a", reader.peek()); assertEquals("a", reader.read()); assertEquals("b", reader.read()); assertEquals(null, reader.peek()); assertEquals(null, reader.read()); }
@Test public void testCloseAndOpenWithPeekAndRead() throws Exception { ExecutionContext executionContext = new ExecutionContext(); reader.setDelegate(new CountingListItemReader<>(Arrays.asList("a", "b", "c"))); assertEquals("a", reader.read()); assertEquals("b", reader.peek()); reader.update(executionContext); reader.close(); reader.open(executionContext); assertEquals("b", reader.read()); assertEquals("c", reader.peek()); reader.update(executionContext); reader.close(); reader.open(executionContext); assertEquals("c", reader.read()); }
/** * Returns a fully constructed {@link SingleItemPeekableItemReader}. * * @return a new {@link SingleItemPeekableItemReader} */ public SingleItemPeekableItemReader<T> build() { Assert.notNull(this.delegate, "A delegate is required"); SingleItemPeekableItemReader<T> reader = new SingleItemPeekableItemReader<>(); reader.setDelegate(this.delegate); return reader; } }
/** * If there is a cached peek, then retrieve the execution context state from * that point. If there is no peek cached, then call directly to the * delegate. * * @param executionContext the current context * @throws ItemStreamException if there is a problem * @see ItemStream#update(ExecutionContext) */ @Override public void update(ExecutionContext executionContext) throws ItemStreamException { if (next != null) { // Get the last state from the delegate instead of using // current value. for (Entry<String, Object> entry : this.executionContext.entrySet()) { executionContext.put(entry.getKey(), entry.getValue()); } return; } updateDelegate(executionContext); }
/** * Test method for {@link org.springframework.batch.item.support.SingleItemPeekableItemReader#close()}. */ @Test public void testCloseAndOpenWithPeek() throws Exception { reader.setDelegate(new CountingListItemReader<>(Arrays.asList("a", "b", "c"))); assertEquals("a", reader.read()); assertEquals("b", reader.peek()); ExecutionContext executionContext = new ExecutionContext(); reader.update(executionContext); reader.close(); reader.open(executionContext); assertEquals("b", reader.read()); }
/** * Peek at the next item, ensuring that if the delegate is an * {@link ItemStream} the state is stored for the next call to * {@link #update(ExecutionContext)}. * * @return the next item (or null if there is none). * * @see PeekableItemReader#peek() */ @Override public T peek() throws Exception, UnexpectedInputException, ParseException { if (next == null) { updateDelegate(executionContext); next = delegate.read(); } return next; }
/** * If there is a cached peek, then retrieve the execution context state from * that point. If there is no peek cached, then call directly to the * delegate. * * @param executionContext the current context * @throws ItemStreamException if there is a problem * @see ItemStream#update(ExecutionContext) */ @Override public void update(ExecutionContext executionContext) throws ItemStreamException { if (next != null) { // Get the last state from the delegate instead of using // current value. for (Entry<String, Object> entry : this.executionContext.entrySet()) { executionContext.put(entry.getKey(), entry.getValue()); } return; } updateDelegate(executionContext); }