11package org .javaee7 .batch .sample .chunk .mapper ;
22
3+ import static java .lang .Thread .sleep ;
4+ import static javax .batch .runtime .BatchRuntime .getJobOperator ;
5+ import static javax .batch .runtime .BatchStatus .COMPLETED ;
6+ import static javax .batch .runtime .Metric .MetricType .COMMIT_COUNT ;
7+ import static javax .batch .runtime .Metric .MetricType .READ_COUNT ;
8+ import static javax .batch .runtime .Metric .MetricType .WRITE_COUNT ;
9+ import static org .javaee7 .batch .sample .chunk .mapper .MyItemReader .totalReaders ;
10+ import static org .javaee7 .util .BatchTestHelper .keepTestAlive ;
11+ import static org .junit .Assert .assertEquals ;
12+
13+ import java .util .List ;
14+ import java .util .Map ;
15+ import java .util .Properties ;
16+
17+ import javax .batch .operations .JobOperator ;
18+ import javax .batch .runtime .JobExecution ;
19+ import javax .batch .runtime .Metric ;
20+ import javax .batch .runtime .StepExecution ;
21+
322import org .javaee7 .util .BatchTestHelper ;
423import org .jboss .arquillian .container .test .api .Deployment ;
524import org .jboss .arquillian .junit .Arquillian ;
1029import org .junit .Test ;
1130import org .junit .runner .RunWith ;
1231
13- import javax .batch .operations .JobOperator ;
14- import javax .batch .runtime .*;
15- import java .util .List ;
16- import java .util .Map ;
17- import java .util .Properties ;
18-
19- import static javax .batch .runtime .BatchRuntime .getJobOperator ;
20- import static javax .batch .runtime .BatchStatus .COMPLETED ;
21- import static org .javaee7 .util .BatchTestHelper .keepTestAlive ;
22- import static org .junit .Assert .assertEquals ;
23-
2432/**
2533 * The Batch specification provides a Chunk Oriented processing style. This style is defined by enclosing into a
2634 * transaction a set of reads, process and write operations via +javax.batch.api.chunk.ItemReader+,
@@ -74,7 +82,9 @@ public static WebArchive createDeployment() {
7482 .addPackage ("org.javaee7.batch.sample.chunk.mapper" )
7583 .addAsWebInfResource (EmptyAsset .INSTANCE , ArchivePaths .create ("beans.xml" ))
7684 .addAsResource ("META-INF/batch-jobs/myJob.xml" );
85+
7786 System .out .println (war .toString (true ));
87+
7888 return war ;
7989 }
8090
@@ -93,40 +103,38 @@ public void testBatchChunkMapper() throws Exception {
93103 JobOperator jobOperator = null ;
94104 Long executionId = null ;
95105 JobExecution jobExecution = null ;
96- for (int i = 0 ; i <3 ; i ++) {
97- jobOperator = getJobOperator ();
98- executionId = jobOperator .start ("myJob" , new Properties ());
99- jobExecution = jobOperator .getJobExecution (executionId );
100-
101- jobExecution = keepTestAlive (jobExecution );
106+ jobOperator = getJobOperator ();
107+ executionId = jobOperator .start ("myJob" , new Properties ());
108+ jobExecution = jobOperator .getJobExecution (executionId );
109+
110+ jobExecution = keepTestAlive (jobExecution );
102111
103- if (COMPLETED .equals (jobExecution .getBatchStatus ())) {
104- break ;
105- }
106-
107- System .out .println ("Execution did not complete, trying again" );
108- }
112+ sleep (1000 );
109113
110114 List <StepExecution > stepExecutions = jobOperator .getStepExecutions (executionId );
111115 for (StepExecution stepExecution : stepExecutions ) {
112116 if (stepExecution .getStepName ().equals ("myStep" )) {
113117 Map <Metric .MetricType , Long > metricsMap = BatchTestHelper .getMetricsMap (stepExecution .getMetrics ());
114118
115119 // <1> The read count should be 20 elements. Check +MyItemReader+.
116- assertEquals (20L , metricsMap .get (Metric .MetricType .READ_COUNT ).longValue ());
120+ assertEquals (20L , metricsMap .get (READ_COUNT ).longValue ());
121+
117122 // <2> The write count should be 10. Only half of the elements read are processed to be written.
118- assertEquals (10L , metricsMap .get (Metric .MetricType .WRITE_COUNT ).longValue ());
123+ assertEquals (10L , metricsMap .get (WRITE_COUNT ).longValue ());
124+
119125 // Number of elements by the item count value on myJob.xml, plus an additional transaction for the
120126 // remaining elements by each partition.
121127 long commitCount = (10L / 3 + (10 % 3 > 0 ? 1 : 0 )) * 2 ;
128+
122129 // <3> The commit count should be 8. Checkpoint is on every 3rd read, 4 commits for read elements and 2 partitions.
123- assertEquals (commitCount , metricsMap .get (Metric . MetricType . COMMIT_COUNT ).longValue ());
130+ assertEquals (commitCount , metricsMap .get (COMMIT_COUNT ).longValue ());
124131 }
125132 }
126133
127134 // <4> Make sure that all the partitions were created.
128- assertEquals (2L , MyItemReader .totalReaders );
135+ assertEquals (2L , totalReaders );
136+
129137 // <5> Job should be completed.
130- assertEquals (BatchStatus . COMPLETED , jobExecution .getBatchStatus ());
138+ assertEquals (COMPLETED , jobExecution .getBatchStatus ());
131139 }
132140}
0 commit comments