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

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

Introduction

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

Prototype

public void setDelegate(ItemReader<T> delegate) 

Source Link

Document

The item reader to use as a delegate.

Usage

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

/**
 * Test should read succesfully.// w ww  .  j  a  v  a  2s . c o m
 *
 * @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.//  ww  w . j a v  a 2s. 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();
    }
}