Example usage for org.springframework.batch.item ItemStreamReader open

List of usage examples for org.springframework.batch.item ItemStreamReader open

Introduction

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

Prototype

void open(ExecutionContext executionContext) throws ItemStreamException;

Source Link

Document

Open the stream for the provided ExecutionContext .

Usage

From source file:com.create.batch.TicketReaderFactoryTest.java

@Test(expected = FlatFileParseException.class)
public void testCreateReaderAndFailToRead() throws Exception {
    // given/*  w  w  w .  ja  v a2  s .c  om*/
    final ExecutionContext executionContext = mock(ExecutionContext.class);

    // when
    final ItemStreamReader<Ticket> reader = factory.createReader(new ClassPathResource("tickets-fail.csv"));

    try {
        reader.open(executionContext);
        System.out.println(reader.read());
    } finally {
        reader.close();
    }

    // then
}

From source file:com.create.batch.TicketReaderFactoryTest.java

@Test
public void testCreateReaderAndReadCorrectData() throws Exception {
    // given/*from w w w.jav  a 2  s. c om*/
    final ExecutionContext executionContext = mock(ExecutionContext.class);
    final LocalDate date = LocalDate.of(2015, 12, 20);

    // when
    final ItemStreamReader<Ticket> reader = factory.createReader(new ClassPathResource("tickets.csv"));
    final Ticket ticket;

    try {
        reader.open(executionContext);
        ticket = reader.read();
    } finally {
        reader.close();
    }

    // then
    assertThat(ticket, notNullValue());
    assertThat(ticket.getTag(), equalTo("Ticket_0"));
    assertThat(ticket.getDate(), equalTo(Date.valueOf(date)));
    assertThat(ticket.getContent(), equalTo("Test ticket"));
}

From source file:org.beanio.spring.SpringTest.java

/**
 * Test shared stream factory configuration for flat file reader.
 *///from  w  w w.java  2 s .c om
@Test
@SuppressWarnings("unchecked")
public void testItemReaderWithSharedStreamFactory() throws Exception {
    ItemStreamReader<Map<String, Object>> reader = (ItemStreamReader<Map<String, Object>>) context
            .getBean("itemReader-sharedFactory");
    assertNotNull(reader);

    try {
        reader.open(new ExecutionContext());
    } finally {
        reader.close();
    }
}

From source file:org.beanio.spring.SpringTest.java

/**
 * Test standalone flat file reader configuration.
 *//*from w  w  w  . j a  v  a  2 s .com*/
@Test
@SuppressWarnings("unchecked")
public void testItemReader() throws Exception {
    ItemStreamReader<Map<String, Object>> reader = (ItemStreamReader<Map<String, Object>>) context
            .getBean("itemReader-standalone");
    assertNotNull(reader);

    try {
        reader.open(new ExecutionContext());

        Map<String, Object> map = reader.read();
        assertNotNull(map);
        assertEquals(new Integer(1), map.get("id"));
        assertEquals("John", map.get("name"));

    } finally {
        reader.close();
    }
}

From source file:org.beanio.spring.SpringTest.java

@Test
@SuppressWarnings("unchecked")
public void testRestartItemReader() throws Exception {
    ItemStreamReader<Map<String, Object>> reader = (ItemStreamReader<Map<String, Object>>) context
            .getBean("itemReader-restart");
    assertNotNull(reader);//www .j  ava2  s . co  m

    try {
        ExecutionContext executionContext = new ExecutionContext();
        executionContext.put("BeanIOFlatFileItemReader.read.count", new Integer(2));

        reader.open(executionContext);

        Map<String, Object> map = reader.read();
        assertNotNull(map);
        assertEquals(new Integer(3), map.get("id"));
        assertEquals("Joe", map.get("name"));
    } finally {
        reader.close();
    }
}

From source file:org.springframework.batch.core.scope.StepScopePerformanceTests.java

private int doTest(String name, String test) throws Exception {
    @SuppressWarnings("unchecked")
    ItemStreamReader<String> reader = (ItemStreamReader<String>) applicationContext.getBean(name);
    reader.open(new ExecutionContext());
    StopWatch stopWatch = new StopWatch(test);
    stopWatch.start();/* ww  w  .ja v  a2s.  c  o  m*/
    int count = 0;
    while (reader.read() != null) {
        // do nothing
        count++;
    }
    stopWatch.stop();
    reader.close();
    logger.info(stopWatch.shortSummary());
    return count;
}