private String getJavaCommand() { String javaHome = System.getProperty("java.home"); String fileSeparator = System.getProperty("file.separator"); StringBuilder command = new StringBuilder(); command.append(javaHome); command.append(fileSeparator); command.append("bin"); command.append(fileSeparator); command.append("java"); if(isRunningOnWindows()) { command.append(".exe"); } return command.toString(); }
@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 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()); }