private void initializeTasklet() { tasklet = new SystemCommandTasklet(); tasklet.setEnvironmentParams(null); // inherit from parent process tasklet.setWorkingDirectory(null); // inherit from parent process tasklet.setSystemProcessExitCodeMapper(new SimpleSystemProcessExitCodeMapper()); tasklet.setTimeout(5000); // long enough timeout tasklet.setTerminationCheckInterval(500); tasklet.setCommand("invalid command, change value for successful execution"); tasklet.setInterruptOnCancel(true); tasklet.setTaskExecutor(new SimpleAsyncTaskExecutor()); }
@Test(expected = java.util.concurrent.ExecutionException.class) public void testExecuteException() throws Exception { String command = "non-sense-that-should-cause-exception-when-attempted-to-execute"; tasklet.setCommand(command); tasklet.afterPropertiesSet(); tasklet.execute(null, null); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); initializeTasklet(); tasklet.afterPropertiesSet(); tasklet.beforeStep(stepExecution); }
@Test public void testTimeoutNotSet() throws Exception { tasklet.setCommand("not-empty placeholder"); tasklet.setTimeout(0); try { tasklet.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } }
@Test public void testStopped() throws Exception { initializeTasklet(); tasklet.setJobExplorer(jobExplorer); tasklet.afterPropertiesSet(); tasklet.beforeStep(stepExecution); JobExecution stoppedJobExecution = new JobExecution(stepExecution.getJobExecution()); stoppedJobExecution.setStatus(BatchStatus.STOPPING); when(jobExplorer.getJobExecution(1L)).thenReturn(stepExecution.getJobExecution(), stepExecution.getJobExecution(), stoppedJobExecution); String command = isRunningOnWindows() ? "ping 127.0.0.1 -n 5" : "sleep 15"; tasklet.setCommand(command); tasklet.setTerminationCheckInterval(10); tasklet.afterPropertiesSet(); StepContribution contribution = stepExecution.createStepContribution(); StepContext stepContext = new StepContext(stepExecution); ChunkContext chunkContext = new ChunkContext(stepContext); tasklet.execute(contribution, chunkContext); assertEquals(ExitStatus.STOPPED.getExitCode(), contribution.getExitStatus().getExitCode()); }
@Test public void testExecuteTimeout() throws Exception { String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 3"; tasklet.setCommand(command); tasklet.setTimeout(10); tasklet.afterPropertiesSet(); log.info("Executing command: " + command); try { tasklet.execute(null, null); fail(); } catch (SystemCommandException e) { assertTrue(e.getMessage().contains("did not finish within the timeout")); } }
@Test public void testInterruption() throws Exception { String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 5"; tasklet.setCommand(command); tasklet.setTerminationCheckInterval(10); tasklet.afterPropertiesSet(); stepExecution.setTerminateOnly(); try { tasklet.execute(null, null); fail(); } catch (JobInterruptedException e) { System.out.println(e.getMessage()); assertTrue(e.getMessage().contains("Job interrupted while executing system command")); assertTrue(e.getMessage().contains(command)); } }
@Test public void testCommandNotSet() throws Exception { tasklet.setCommand(null); try { tasklet.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } tasklet.setCommand(""); try { tasklet.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } }
@Test public void testExecuteFailure() throws Exception { String command = getJavaCommand() + " org.springframework.batch.sample.tasklet.UnknownClass"; tasklet.setCommand(command); tasklet.setTimeout(200L); tasklet.afterPropertiesSet(); log.info("Executing command: " + command); try { StepContribution contribution = stepExecution.createStepContribution(); RepeatStatus exitStatus = tasklet.execute(contribution, null); assertEquals(RepeatStatus.FINISHED, exitStatus); assertEquals(ExitStatus.FAILED, contribution.getExitStatus()); } catch (RuntimeException e) { // on some platforms the system call does not return assertEquals("Execution of system command did not finish within the timeout", e.getMessage()); } }
@Test public void testExecute() throws Exception { String command = getJavaCommand() + " --version"; tasklet.setCommand(command); tasklet.afterPropertiesSet(); log.info("Executing command: " + command); RepeatStatus exitStatus = tasklet.execute(stepExecution.createStepContribution(), null); assertEquals(RepeatStatus.FINISHED, exitStatus); }