1919import static org .junit .Assert .assertEquals ;
2020
2121/**
22+ * The Batch specification provides a Chunk Oriented processing style. This style is defined by enclosing into a
23+ * transaction a set of reads, process and write operations via +javax.batch.api.chunk.ItemReader+,
24+ * +javax.batch.api.chunk.ItemProcessor+ and +javax.batch.api.chunk.ItemWriter+. Items are read one at a time, processed
25+ * and aggregated. The transaction is then committed when the defined +checkpoint-policy+ is triggered.
26+ *
27+ * include::myJob.xml[]
28+ *
29+ * A very simple job is defined in the +myJob.xml+ file. Just a single step with a reader, a processor and writer.
30+ *
2231 * @author Roberto Cortez
2332 */
2433@ RunWith (Arquillian .class )
2534public class ChunkSimpleTest {
35+ /**
36+ * We're just going to deploy the application as a +web archive+. Note the inclusion of the following files:
37+ *
38+ * [source,file]
39+ * ----
40+ * /META-INF/batch-jobs/myjob.xml
41+ * ----
42+ *
43+ * The +myjob.xml+ file is needed for running the batch definition.
44+ */
2645 @ Deployment
2746 public static WebArchive createDeployment () {
2847 WebArchive war = ShrinkWrap .create (WebArchive .class )
@@ -34,6 +53,15 @@ public static WebArchive createDeployment() {
3453 return war ;
3554 }
3655
56+ /**
57+ * In the test, we're just going to invoke the batch execution and wait for completion. To validate the test
58+ * expected behaviour we need to query the +Metric[]+ object available in the step execution.
59+ *
60+ * The batch process itself will read and process 10 elements from numbers 1 to 10, but only write the odd
61+ * elements. Commits are executed after 3 elements are read.
62+ *
63+ * @throws Exception an exception if the batch could not complete successfully.
64+ */
3765 @ Test
3866 public void testChunkSimple () throws Exception {
3967 JobOperator jobOperator = BatchRuntime .getJobOperator ();
@@ -46,12 +74,18 @@ public void testChunkSimple() throws Exception {
4674 for (StepExecution stepExecution : stepExecutions ) {
4775 if (stepExecution .getStepName ().equals ("myStep" )) {
4876 Map <Metric .MetricType , Long > metricsMap = BatchTestHelper .getMetricsMap (stepExecution .getMetrics ());
77+
78+ // <1> The read count should be 10 elements. Check +MyItemReader+.
4979 assertEquals (10L , metricsMap .get (Metric .MetricType .READ_COUNT ).longValue ());
80+ // <2> The write count should be 5. Only half of the elements read are processed to be written.
5081 assertEquals (10L / 2L , metricsMap .get (Metric .MetricType .WRITE_COUNT ).longValue ());
51- assertEquals (10L / 3 + (10L % 3 > 0 ? 1 : 0 ), metricsMap .get (Metric .MetricType .COMMIT_COUNT ).longValue ());
82+ // <3> The commit count should be 4. Checkpoint is on every 3rd read, 4 commits for read elements.
83+ assertEquals (10L / 3 + (10L % 3 > 0 ? 1 : 0 ),
84+ metricsMap .get (Metric .MetricType .COMMIT_COUNT ).longValue ());
5285 }
5386 }
5487
88+ // <4> Job should be completed.
5589 assertEquals (jobExecution .getBatchStatus (), BatchStatus .COMPLETED );
5690 }
5791}
0 commit comments