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

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

Introduction

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

Prototype

void close() throws ItemStreamException;

Source Link

Document

If any resources are needed for the stream to operate they need to be destroyed here.

Usage

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

@Test(expected = FlatFileParseException.class)
public void testCreateReaderAndFailToRead() throws Exception {
    // given/*from ww  w. j a  va2s  .  co  m*/
    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 www  . j av a  2s  .co  m
    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 . j a  v a  2  s. c o m
@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  www. j a  v a2 s . c om*/
@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);/* w ww  .  j  a v  a 2s  . com*/

    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();//from w  ww  .  jav a  2  s .c  o  m
    int count = 0;
    while (reader.read() != null) {
        // do nothing
        count++;
    }
    stopWatch.stop();
    reader.close();
    logger.info(stopWatch.shortSummary());
    return count;
}