|
| 1 | +package org.javaee7.batch.multiple.steps; |
| 2 | + |
| 3 | +import org.javaee7.util.BatchTestHelper; |
| 4 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 5 | +import org.jboss.arquillian.junit.Arquillian; |
| 6 | +import org.jboss.shrinkwrap.api.ArchivePaths; |
| 7 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 8 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 9 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +import javax.batch.operations.JobOperator; |
| 14 | +import javax.batch.runtime.*; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertArrayEquals; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Roberto Cortez |
| 25 | + */ |
| 26 | +@RunWith(Arquillian.class) |
| 27 | +public class BatchMultipleStepsTest { |
| 28 | + @Deployment |
| 29 | + public static WebArchive createDeployment() { |
| 30 | + WebArchive war = ShrinkWrap.create(WebArchive.class) |
| 31 | + .addClass(BatchTestHelper.class) |
| 32 | + .addPackage("org.javaee7.batch.multiple.steps") |
| 33 | + .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml")) |
| 34 | + .addAsResource("META-INF/batch-jobs/myJob.xml"); |
| 35 | + System.out.println(war.toString(true)); |
| 36 | + return war; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testBatchMultipleSteps() throws Exception { |
| 41 | + JobOperator jobOperator = BatchRuntime.getJobOperator(); |
| 42 | + Long executionId = jobOperator.start("myJob", new Properties()); |
| 43 | + JobExecution jobExecution = jobOperator.getJobExecution(executionId); |
| 44 | + |
| 45 | + BatchTestHelper.keepTestAlive(jobExecution); |
| 46 | + |
| 47 | + List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId); |
| 48 | + List<String> executedSteps = new ArrayList<>(); |
| 49 | + for (StepExecution stepExecution : stepExecutions) { |
| 50 | + executedSteps.add(stepExecution.getStepName()); |
| 51 | + |
| 52 | + if (stepExecution.getStepName().equals("step1")) { |
| 53 | + Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics()); |
| 54 | + System.out.println(metricsMap); |
| 55 | + assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue()); |
| 56 | + assertEquals(10L / 2, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue()); |
| 57 | + assertEquals(10L / 3 + (10L % 3 > 0 ? 1 : 0), metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue()); |
| 58 | + } |
| 59 | + } |
| 60 | + assertEquals(2, stepExecutions.size()); |
| 61 | + assertArrayEquals(new String[]{"step1", "step2"}, executedSteps.toArray()); |
| 62 | + assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus()); |
| 63 | + } |
| 64 | +} |
0 commit comments