Example usage for org.springframework.util StringUtils commaDelimitedListToStringArray

List of usage examples for org.springframework.util StringUtils commaDelimitedListToStringArray

Introduction

In this page you can find the example usage for org.springframework.util StringUtils commaDelimitedListToStringArray.

Prototype

public static String[] commaDelimitedListToStringArray(@Nullable String str) 

Source Link

Document

Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.

Usage

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check to make sure that ItemStreamException can be skipped. (see
 * BATCH-915)// ww  w  .j  av a 2 s  .c  o m
 */
@Test
public void testReadSkipItemStreamException() throws Exception {
    reader.setFailures("2");
    reader.setExceptionType(ItemStreamException.class);

    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(ItemStreamException.class, true);
    factory.setSkippableExceptionClasses(map);

    Step step = factory.getObject();

    step.execute(stepExecution);

    assertEquals(1, stepExecution.getSkipCount());
    assertEquals(1, stepExecution.getReadSkipCount());
    assertEquals(4, stepExecution.getReadCount());
    assertEquals(0, stepExecution.getWriteSkipCount());
    assertEquals(0, stepExecution.getRollbackCount());

    // writer did not skip "2" as it never made it to writer, only "4" did
    assertTrue(reader.getRead().contains("4"));
    assertFalse(reader.getRead().contains("2"));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,3,4,5"));
    assertEquals(expectedOutput, writer.getWritten());

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *//*from ww w  . j  ava 2s  .  c  om*/
@Test
public void testProcessSkip() throws Exception {
    processor.setFailures("4");
    writer.setFailures("4");

    Step step = factory.getObject();

    step.execute(stepExecution);

    assertEquals(1, stepExecution.getSkipCount());
    assertEquals(0, stepExecution.getReadSkipCount());
    assertEquals(5, stepExecution.getReadCount());
    assertEquals(1, stepExecution.getProcessSkipCount());
    assertEquals(1, stepExecution.getRollbackCount());

    // writer skips "4"
    assertTrue(reader.getRead().contains("4"));
    assertFalse(writer.getWritten().contains("4"));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
    assertEquals(expectedOutput, writer.getWritten());

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

@Test
public void testProcessFilter() throws Exception {
    processor.setFailures("4");
    processor.setFilter(true);//from  w w  w . ja v a 2  s  . co  m
    ItemProcessListenerStub<String, String> listenerStub = new ItemProcessListenerStub<String, String>();
    factory.setListeners(new StepListener[] { listenerStub });
    Step step = factory.getObject();

    step.execute(stepExecution);

    assertEquals(0, stepExecution.getSkipCount());
    assertEquals(0, stepExecution.getReadSkipCount());
    assertEquals(5, stepExecution.getReadCount());
    assertEquals(1, stepExecution.getFilterCount());
    assertEquals(0, stepExecution.getRollbackCount());
    assertTrue(listenerStub.isFilterEncountered());

    // writer skips "4"
    assertTrue(reader.getRead().contains("4"));
    assertFalse(writer.getWritten().contains("4"));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
    assertEquals(expectedOutput, writer.getWritten());

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *///from   w  w w  .  j ava2  s. co m
@Test
public void testWriteSkip() throws Exception {
    writer.setFailures("4");

    Step step = factory.getObject();

    step.execute(stepExecution);

    assertEquals(1, stepExecution.getSkipCount());
    assertEquals(0, stepExecution.getReadSkipCount());
    assertEquals(5, stepExecution.getReadCount());
    assertEquals(1, stepExecution.getWriteSkipCount());
    assertEquals(2, stepExecution.getRollbackCount());

    // writer skips "4"
    assertTrue(reader.getRead().contains("4"));
    assertFalse(writer.getCommitted().contains("4"));

    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5"));
    assertEquals(expectedOutput, writer.getCommitted());

    assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *//*from www.  j a  va  2s  . com*/
@Test
public void testSkipOverLimit() throws Exception {
    reader.setFailures("2");
    writer.setFailures("4");

    factory.setSkipLimit(1);

    Step step = factory.getObject();

    step.execute(stepExecution);

    assertEquals(1, stepExecution.getSkipCount());

    // writer did not skip "2" as it never made it to writer, only "4" did
    assertTrue(reader.getRead().contains("4"));
    assertFalse(writer.getCommitted().contains("4"));

    // failure on "4" tripped the skip limit so we never got to "5"
    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,3"));
    assertEquals(expectedOutput, writer.getCommitted());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *///from w  ww .  ja  v a  2s  . c  o m
@SuppressWarnings("unchecked")
@Test
public void testSkipOverLimitOnRead() throws Exception {
    reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
    reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));

    writer.setFailures("4");

    factory.setSkipLimit(3);
    factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());

    assertEquals(3, stepExecution.getSkipCount());
    assertEquals(2, stepExecution.getReadSkipCount());
    assertEquals(1, stepExecution.getWriteSkipCount());
    assertEquals(2, stepExecution.getReadCount());

    // writer did not skip "2" as it never made it to writer, only "4" did
    assertFalse(reader.getRead().contains("2"));
    assertTrue(reader.getRead().contains("4"));

    // only "1" was ever committed
    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1"));
    assertEquals(expectedOutput, writer.getCommitted());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *//*from w w w .j  a v a  2s .  c  o m*/
@SuppressWarnings("unchecked")
@Test
public void testSkipListenerFailsOnRead() throws Exception {
    reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
    reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));

    writer.setFailures("4");

    factory.setSkipLimit(3);
    factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
        @Override
        public void onSkipInRead(Throwable t) {
            throw new RuntimeException("oops");
        }
    } });
    factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
    assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage());

    // listeners are called only once chunk is about to commit, so
    // listener failure does not affect other statistics
    assertEquals(2, stepExecution.getReadSkipCount());
    // but we didn't get as far as the write skip in the scan:
    assertEquals(0, stepExecution.getWriteSkipCount());
    assertEquals(2, stepExecution.getSkipCount());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *///from   w w  w . j av a2  s  .co m
@SuppressWarnings("unchecked")
@Test
public void testSkipListenerFailsOnWrite() throws Exception {
    reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));

    writer.setFailures("4");

    factory.setSkipLimit(3);
    factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() {
        @Override
        public void onSkipInWrite(String item, Throwable t) {
            throw new RuntimeException("oops");
        }
    } });
    factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
    assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage());
    assertEquals(1, stepExecution.getSkipCount());
    assertEquals(0, stepExecution.getReadSkipCount());
    assertEquals(1, stepExecution.getWriteSkipCount());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *//*from  w w  w  .j a  v  a  2s.  c o  m*/
@Test
public void testSkipOnReadNotDoubleCounted() throws Exception {
    reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"));
    reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5"));

    writer.setFailures("4");

    factory.setSkipLimit(4);

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(4, stepExecution.getSkipCount());
    assertEquals(3, stepExecution.getReadSkipCount());
    assertEquals(1, stepExecution.getWriteSkipCount());

    // skipped 2,3,4,5
    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6"));
    assertEquals(expectedOutput, writer.getCommitted());

    // reader exceptions should not cause rollback, 1 writer exception
    // causes 2 rollbacks
    assertEquals(2, stepExecution.getRollbackCount());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java

/**
 * Check items causing errors are skipped as expected.
 *//* w  w w . ja  v  a 2 s .com*/
@Test
public void testSkipOnWriteNotDoubleCounted() throws Exception {
    reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7"));
    reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3"));

    writer.setFailures("4", "5");

    factory.setSkipLimit(4);
    factory.setCommitInterval(3); // includes all expected skips

    Step step = factory.getObject();

    step.execute(stepExecution);
    assertEquals(4, stepExecution.getSkipCount());
    assertEquals(2, stepExecution.getReadSkipCount());
    assertEquals(2, stepExecution.getWriteSkipCount());

    // skipped 2,3,4,5
    List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6,7"));
    assertEquals(expectedOutput, writer.getCommitted());
    assertStepExecutionsAreEqual(stepExecution,
            repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName()));
}