From ad84e6fb8315cf4a15ee80732d69829d96409ce9 Mon Sep 17 00:00:00 2001 From: robokaso Date: Wed, 10 Dec 2008 14:45:45 +0000 Subject: [PATCH] IN PROGRESS - BATCH-961: integration tests for JobOperator added start-stop-resume-stop testcase --- .../GracefulShutdownFunctionalTests.java | 1 + .../sample/JobOperatorFunctionalTests.java | 79 +++++++++++++++++++ .../JobOperatorFunctionalTests-context.xml | 10 +++ 3 files changed, 90 insertions(+) create mode 100644 spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java create mode 100644 spring-batch-samples/src/test/resources/org/springframework/batch/sample/JobOperatorFunctionalTests-context.xml diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java index bfcafb076..cfcc32889 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java @@ -41,6 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class GracefulShutdownFunctionalTests extends AbstractBatchLauncherTests { @Test + @Override public void testLaunchJob() throws Exception { final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis()) diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java new file mode 100644 index 000000000..bf7492ee7 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/JobOperatorFunctionalTests.java @@ -0,0 +1,79 @@ +package org.springframework.batch.sample; + +import static org.junit.Assert.*; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import static org.springframework.batch.core.BatchStatus.*; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.configuration.ListableJobRegistry; +import org.springframework.batch.core.configuration.support.ReferenceJobFactory; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.JobOperator; +import org.springframework.batch.core.launch.NoSuchJobExecutionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration() +public class JobOperatorFunctionalTests { + + private static final Log logger = LogFactory.getLog(JobOperatorFunctionalTests.class); + + @Autowired + JobOperator tested; + + @Autowired + JobLauncher launcher; + + @Autowired + Job job; + + @Autowired + ListableJobRegistry jobRegistry; + + @Before + public void setUp() throws Exception { + jobRegistry.register(new ReferenceJobFactory(job)); + } + + @Test + public void testStartStopResumeJob() throws Exception { + + String params = new JobParametersBuilder().addLong("jobOperatorTestParam", 7L).toJobParameters().toString(); + long executionId = tested.start(job.getName(), params); + stopAndCheckStatus(executionId); + + long resumedExecutionId = tested.resume(executionId); + stopAndCheckStatus(resumedExecutionId); + + } + + /** + * @param executionId id of running job execution + */ + private void stopAndCheckStatus(long executionId) throws InterruptedException, NoSuchJobExecutionException { + Thread.sleep(1000); + + assertTrue(tested.getSummary(executionId).contains(STARTED.toString())); + + tested.stop(executionId); + + int count = 0; + while (!tested.getSummary(executionId).contains(STOPPED.toString()) && count <= 10) { + logger.info("Checking for end time in JobExecution: count=" + count); + Thread.sleep(100); + count++; + } + + String summary = tested.getSummary(executionId); + assertFalse("Timed out waiting for job to end.", summary.contains(STARTED.toString())); + assertTrue(summary.contains(STOPPED.toString())); + } + +} diff --git a/spring-batch-samples/src/test/resources/org/springframework/batch/sample/JobOperatorFunctionalTests-context.xml b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/JobOperatorFunctionalTests-context.xml new file mode 100644 index 000000000..93b921438 --- /dev/null +++ b/spring-batch-samples/src/test/resources/org/springframework/batch/sample/JobOperatorFunctionalTests-context.xml @@ -0,0 +1,10 @@ + + + + + + +