/** * Fatal exception should cause immediate termination provided the exception * is not skippable (note the fatal exception is also classified as * rollback). */ @Test public void testFatalException() throws Exception { reader.setFailures("2"); Map<Class<? extends Throwable>, Boolean> map = new HashMap<>(); map.put(SkippableException.class, true); map.put(SkippableRuntimeException.class, true); map.put(FatalRuntimeException.class, false); factory.setSkippableExceptionClasses(map); factory.setItemWriter(new ItemWriter<String>() { @Override public void write(List<? extends String> items) { throw new FatalRuntimeException("Ouch!"); } }); Step step = factory.getObject(); step.execute(stepExecution); String message = stepExecution.getFailureExceptions().get(0).getCause().getMessage(); assertEquals("Wrong message: ", "Ouch!", message); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); }
@Test public void testAutoRegisterItemListeners() throws Exception { reader.setFailures("2");
/** * Check items causing errors are skipped as expected. */ @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())); }
/** * Check items causing errors are skipped as expected. */ @Test public void testSkipOverLimitOnReadWithListener() throws Exception { reader.setFailures("1", "3", "5"); writer.setFailures(); final List<Throwable> listenerCalls = new ArrayList<>(); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInRead(Throwable t) { listenerCalls.add(t); } } }); factory.setCommitInterval(2); factory.setSkipLimit(2); Step step = factory.getObject(); step.execute(stepExecution); // 1,3 skipped inside a committed chunk. 5 tripped the skip // limit but it was skipped in a chunk that rolled back, so // it will re-appear on a restart and the listener is not called. assertEquals(2, listenerCalls.size()); assertEquals(2, stepExecution.getReadSkipCount()); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); }
/** * Check items causing errors are skipped as expected. */ @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())); }
/** * Check items causing errors are skipped as expected. */ @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())); }
@SuppressWarnings("unchecked") @Test public void testDefaultSkipPolicy() throws Exception { reader.setItems("a", "b", "c"); reader.setFailures("b"); factory.setSkippableExceptionClasses(getExceptionMap(Exception.class)); factory.setSkipLimit(1); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(1, stepExecution.getSkipCount()); assertEquals("[a, c]", reader.getRead().toString()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); }
/** * Check items causing errors are skipped as expected. */ @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())); }
/** * Check to make sure that ItemStreamException can be skipped. (see * BATCH-915) */ @Test public void testReadSkipItemStreamException() throws Exception { reader.setFailures("2"); reader.setExceptionType(ItemStreamException.class); Map<Class<? extends Throwable>, Boolean> map = new HashMap<>(); 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())); }
/** * Check items causing errors are skipped as expected. */ @SuppressWarnings("unchecked") @Test public void testSkipOverLimitOnReadWithAllSkipsAtEnd() throws Exception { reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15")); reader.setFailures(StringUtils.commaDelimitedListToStringArray("6,12,13,14,15")); writer.setFailures("4"); factory.setCommitInterval(5); factory.setSkipLimit(3); factory.setSkippableExceptionClasses(getExceptionMap(Exception.class)); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals("bad skip count", 3, stepExecution.getSkipCount()); assertEquals("bad read skip count", 2, stepExecution.getReadSkipCount()); assertEquals("bad write skip count", 1, stepExecution.getWriteSkipCount()); // writer did not skip "6" as it never made it to writer, only "4" did assertFalse(reader.getRead().contains("6")); assertTrue(reader.getRead().contains("4")); // only "1" was ever committed List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,2,3,5,7,8,9,10,11")); assertEquals(expectedOutput, writer.getCommitted()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); }
/** * Check items causing errors are skipped as expected. */ @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())); }
/** * Check items causing errors are skipped as expected. */ @Test public void testReadSkip() throws Exception { reader.setFailures("2"); 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())); }
/** * Scenario: Exception in reader that should not cause rollback */ @Test public void testReaderDefaultNoRollbackOnCheckedException() throws Exception { reader.setItems("1", "2", "3", "4"); reader.setFailures("2", "3"); reader.setExceptionType(SkippableException.class); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertEquals(2, stepExecution.getSkipCount()); assertEquals(0, stepExecution.getRollbackCount()); }
/** * Check items causing errors are skipped as expected. */ @Test public void testReadSkipWithPolicyExceptionInReader() throws Exception { // Should be ignored factory.setSkipLimit(0); factory.setSkipPolicy(new SkipPolicy() { @Override public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException { throw new RuntimeException("Planned exception in SkipPolicy"); } }); reader.setFailures("2"); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(0, stepExecution.getReadSkipCount()); assertEquals(1, stepExecution.getReadCount()); }
/** * Non-skippable (and non-fatal) exception causes failure immediately. * * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testNonSkippableExceptionOnRead() throws Exception { reader.setFailures("2"); // nothing is skippable factory.setSkippableExceptionClasses(getExceptionMap(NonExistentException.class)); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); assertTrue(stepExecution.getExitStatus().getExitDescription().contains("Non-skippable exception during read")); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); }
/** * Scenario: Exception in reader that should not cause rollback */ @SuppressWarnings("unchecked") @Test public void testReaderAttributesOverrideSkippableNoRollback() throws Exception { reader.setFailures("2", "3"); reader.setItems("1", "2", "3", "4"); reader.setExceptionType(SkippableException.class); // No skips by default factory.setSkippableExceptionClasses(getExceptionMap(RuntimeException.class)); // But this one is explicit in the tx-attrs so it should be skipped factory.setNoRollbackExceptionClasses(getExceptionList(SkippableException.class)); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertEquals(0, stepExecution.getSkipCount()); assertEquals(0, stepExecution.getRollbackCount()); }