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

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

Introduction

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

Prototype

@Override
public void open(ExecutionContext executionContext) throws ItemStreamException 

Source Link

Document

If the delegate is an ItemStream , just pass the call on, otherwise reset the peek cache.

Usage

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

/**
 * Test should read succesfully./*from  ww w.  j  a  v a  2s .  c  om*/
 *
 * @throws Exception 
 */
@Test
public void testSuccessfulReading() 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);
            count++;
        }
        assertEquals(EXPECTED_COUNT, count);
    } catch (Exception e) {
        throw e;
    } finally {
        peekable.close();
    }
}

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

/**
 * Test should read succesfully./*from  w  w w .j  a v a  2  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();
    }
}