Example usage for org.springframework.batch.item.support SingleItemPeekableItemReader peek

List of usage examples for org.springframework.batch.item.support SingleItemPeekableItemReader peek

Introduction

In this page you can find the example usage for org.springframework.batch.item.support SingleItemPeekableItemReader peek.

Prototype

@Override
public T peek() throws Exception, UnexpectedInputException, ParseException 

Source Link

Document

Peek at the next item, ensuring that if the delegate is an ItemStream the state is stored for the next call to #update(ExecutionContext) .

Usage

From source file:de.langmi.spring.batch.examples.readers.file.peekable.SimplePeekableItemReaderTest.java

/**
 * Test should read succesfully./*from  w  w  w . j a va2  s .  c o  m*/
 *
 * @throws Exception 
 */
@Test
public void testSuccessfulPeekAhead() throws Exception {
    // init delegate
    delegateReader.setLineMapper(new PassThroughLineMapper());
    delegateReader.setResource(new FileSystemResource(INPUT_FILE));
    // init peekable
    SingleItemPeekableItemReader<String> peekable = new SingleItemPeekableItemReader<String>();
    peekable.setDelegate(delegateReader);
    // open, provide "mock" ExecutionContext
    peekable.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext());
    // read
    try {
        int count = 0;
        String line;
        while ((line = peekable.read()) != null) {
            assertEquals(String.valueOf(count), line);
            // test for peek
            String lineAhead = peekable.peek();
            if (count + 1 < EXPECTED_COUNT) {
                assertEquals(String.valueOf(count + 1), lineAhead);
            } else {
                assertNull(lineAhead);
            }
            count++;
        }
        assertEquals(EXPECTED_COUNT, count);
    } catch (Exception e) {
        throw e;
    } finally {
        peekable.close();
    }
}