diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java index 609a081d2..fb14e3076 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JsrSplitParsingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,14 +27,6 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.task.SimpleAsyncTaskExecutor; -import javax.batch.api.AbstractBatchlet; -import javax.batch.runtime.BatchRuntime; -import javax.batch.runtime.StepExecution; -import javax.batch.runtime.context.JobContext; -import javax.inject.Inject; -import java.util.List; - -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -43,16 +35,6 @@ public class JsrSplitParsingTests extends AbstractJsrTestCase { @Rule public ExpectedException expectedException = ExpectedException.none(); - @Test - public void test() throws Exception { - javax.batch.runtime.JobExecution execution = runJob("JsrSplitParsingTests-context", null, 10000L); - assertEquals(javax.batch.runtime.BatchStatus.COMPLETED, execution.getBatchStatus()); - assertEquals("COMPLETED", execution.getExitStatus()); - - List stepExecutions = BatchRuntime.getJobOperator().getStepExecutions(execution.getExecutionId()); - assertEquals(5, stepExecutions.size()); - } - @Test public void testOneFlowInSplit() { try { @@ -86,15 +68,4 @@ public class JsrSplitParsingTests extends AbstractJsrTestCase { context.close(); } - public static class ExitStatusSettingBatchlet extends AbstractBatchlet { - - @Inject - JobContext jobContext; - - @Override - public String process() throws Exception { - jobContext.setExitStatus("Should be ignored"); - return null; - } - } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/DecisionStepTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/DecisionStepTests.java index 2b4b44948..8a8903283 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/DecisionStepTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/DecisionStepTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,6 @@ import javax.batch.runtime.StepExecution; import org.junit.Test; import org.springframework.batch.core.jsr.AbstractJsrTestCase; -import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.Assert; import static org.junit.Assert.assertEquals; @@ -82,15 +81,6 @@ public class DecisionStepTests extends AbstractJsrTestCase { assertEquals(3, BatchRuntime.getJobOperator().getStepExecutions(execution.getExecutionId()).size()); } - @Test - public void testDecisionAfterSplit() throws Exception { - JobExecution execution = runJob("DecisionStepTests-decisionAfterSplit-context", new Properties(), 10000L); - org.springframework.batch.core.JobExecution jobExecution = (org.springframework.batch.core.JobExecution) ReflectionTestUtils.getField(execution, "execution"); - assertEquals(String.format("Received a %s because of %s", execution.getBatchStatus(), jobExecution.getExitStatus().getExitDescription()), BatchStatus.COMPLETED, execution.getBatchStatus()); - assertEquals(4, BatchRuntime.getJobOperator().getStepExecutions(execution.getExecutionId()).size()); - assertEquals(2, StepExecutionCountingDecider.previousStepCount); - } - @Test public void testDecisionRestart() throws Exception { JobExecution execution = runJob("DecisionStepTests-restart-context", new Properties(), 10000L); @@ -130,17 +120,6 @@ public class DecisionStepTests extends AbstractJsrTestCase { } } - public static class StepExecutionCountingDecider implements Decider { - - static int previousStepCount = 0; - - @Override - public String decide(StepExecution[] executions) throws Exception { - previousStepCount = executions.length; - return "next"; - } - } - public static class NextDecider implements Decider { @Override diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/SplitTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/SplitTests.java new file mode 100644 index 000000000..5e917de0f --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/step/SplitTests.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.jsr.step; + +import java.time.Duration; +import java.time.Instant; +import java.util.EnumSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import javax.batch.api.AbstractBatchlet; +import javax.batch.api.Decider; +import javax.batch.operations.JobOperator; +import javax.batch.runtime.BatchRuntime; +import javax.batch.runtime.BatchStatus; +import javax.batch.runtime.JobExecution; +import javax.batch.runtime.StepExecution; +import javax.batch.runtime.context.JobContext; +import javax.inject.Inject; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Mahmoud Ben Hassine + */ +public class SplitTests { + + private static final Set END_STATUSES = + EnumSet.of(BatchStatus.COMPLETED, BatchStatus.FAILED, BatchStatus.STOPPED); + private final JobOperator jobOperator = BatchRuntime.getJobOperator(); + + @Test + public void testSplit() { + // given + String jobXMLName = "SplitTests-testSplit-context"; + Properties jobParameters = new Properties(); + + // when + long executionId = jobOperator.start(jobXMLName, jobParameters); + waitFor(executionId, 10); + JobExecution jobExecution = jobOperator.getJobExecution(executionId); + List stepExecutions = jobOperator.getStepExecutions(executionId); + + // then + assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus()); + assertEquals("COMPLETED", jobExecution.getExitStatus()); + assertEquals(5, stepExecutions.size()); + } + + @Test + public void testDecisionAfterSplit() { + // given + String jobXMLName = "SplitTests-testDecisionAfterSplit-context"; + Properties jobParameters = new Properties(); + + // when + long executionId = jobOperator.start(jobXMLName, jobParameters); + waitFor(executionId, 10); + JobExecution jobExecution = jobOperator.getJobExecution(executionId); + + // then + assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus()); + assertEquals(4, jobOperator.getStepExecutions(executionId).size()); + assertEquals(2, StepExecutionCountingDecider.previousStepCount); + } + + private void waitFor(long executionId, int timeoutInSeconds) { + Instant startTime = Instant.now(); + while (!END_STATUSES.contains(jobOperator.getJobExecution(executionId).getBatchStatus())) { + if ((Duration.between(Instant.now(), startTime).getSeconds() > timeoutInSeconds)) { + fail("Job processing did not complete in time"); + } + } + } + + public static class StepExecutionCountingDecider implements Decider { + + static int previousStepCount = 0; + + @Override + public String decide(StepExecution[] executions) { + previousStepCount = executions.length; + return "next"; + } + } + + public static class ExitStatusSettingBatchlet extends AbstractBatchlet { + + @Inject + JobContext jobContext; + + @Override + public String process() throws Exception { + jobContext.setExitStatus("Should be ignored"); + return null; + } + } + +} diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/DecisionStepTests-decisionAfterSplit-context.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testDecisionAfterSplit-context.xml similarity index 95% rename from spring-batch-core/src/test/resources/META-INF/batch-jobs/DecisionStepTests-decisionAfterSplit-context.xml rename to spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testDecisionAfterSplit-context.xml index 27c79a25f..487a762ef 100644 --- a/spring-batch-core/src/test/resources/META-INF/batch-jobs/DecisionStepTests-decisionAfterSplit-context.xml +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testDecisionAfterSplit-context.xml @@ -25,7 +25,7 @@ - + diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/JsrSplitParsingTests-context.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testSplit-context.xml similarity index 89% rename from spring-batch-core/src/test/resources/META-INF/batch-jobs/JsrSplitParsingTests-context.xml rename to spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testSplit-context.xml index f43b1597c..31591ac19 100644 --- a/spring-batch-core/src/test/resources/META-INF/batch-jobs/JsrSplitParsingTests-context.xml +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitTests-testSplit-context.xml @@ -9,17 +9,17 @@ - + - + - +