Skip to content

Commit 5c23c50

Browse files
committed
Merge pull request #141 from radcortez/master
Added test for chunk-checkpoint project.
2 parents 4305efd + f22f902 commit 5c23c50

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

batch/chunk-checkpoint/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>org.javaee7.batch</groupId>
@@ -8,10 +9,13 @@
89
<relativePath>../pom.xml</relativePath>
910
</parent>
1011

11-
<groupId>org.javaee7.batch</groupId>
1212
<artifactId>chunk-checkpoint</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1413
<packaging>war</packaging>
15-
<name>${project.artifactId}</name>
1614

15+
<dependencies>
16+
<dependency>
17+
<groupId>org.javaee7</groupId>
18+
<artifactId>util-samples</artifactId>
19+
</dependency>
20+
</dependencies>
1721
</project>

batch/chunk-checkpoint/src/main/java/org/javaee7/batch/chunk/checkpoint/MyCheckpointAlgorithm.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848
*/
4949
@Named
5050
public class MyCheckpointAlgorithm extends AbstractCheckpointAlgorithm {
51+
private static boolean checkpointAlgorithmExecuted;
5152

5253
@Override
5354
public boolean isReadyToCheckpoint() throws Exception {
54-
if (MyItemReader.COUNT % 5 == 0)
55-
return true;
56-
else
57-
return false;
55+
checkpointAlgorithmExecuted = true;
56+
return MyItemReader.COUNT % 5 == 0;
5857
}
5958

59+
public static boolean isCheckpointAlgorithmExecuted() {
60+
return checkpointAlgorithmExecuted;
61+
}
6062
}

batch/chunk-checkpoint/src/main/resources/META-INF/batch-jobs/myJob.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
4444
<step id="myStep" >
4545
<chunk item-count="3" checkpoint-policy="custom">
46-
<reader ref="myItemReader"></reader>
47-
<processor ref="myItemProcessor"></processor>
48-
<writer ref="myItemWriter"></writer>
46+
<reader ref="myItemReader"/>
47+
<processor ref="myItemProcessor"/>
48+
<writer ref="myItemWriter"/>
4949
<checkpoint-algorithm ref="myCheckpointAlgorithm"/>
5050
</chunk>
5151
</step>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.javaee7.batch.chunk.checkpoint;
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.BatchRuntime;
15+
import javax.batch.runtime.BatchStatus;
16+
import javax.batch.runtime.JobExecution;
17+
import javax.batch.runtime.Metric;
18+
import javax.batch.runtime.StepExecution;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Properties;
22+
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertTrue;
25+
26+
/**
27+
* @author Roberto Cortez
28+
*/
29+
@RunWith(Arquillian.class)
30+
public class BatchChunkCheckpointTest {
31+
32+
@Deployment
33+
public static WebArchive createDeployment() {
34+
WebArchive war = ShrinkWrap.create(WebArchive.class)
35+
.addClass(BatchTestHelper.class)
36+
.addPackage("org.javaee7.batch.chunk.checkpoint")
37+
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
38+
.addAsResource("META-INF/batch-jobs/myJob.xml");
39+
System.out.println(war.toString(true));
40+
return war;
41+
}
42+
43+
@Test
44+
public void testBatchChunkCheckpoint() throws Exception {
45+
JobOperator jobOperator = BatchRuntime.getJobOperator();
46+
Long executionId = jobOperator.start("myJob", new Properties());
47+
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
48+
49+
BatchTestHelper.keepTestAlive(jobExecution);
50+
51+
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
52+
for (StepExecution stepExecution : stepExecutions) {
53+
if (stepExecution.getStepName().equals("myStep")) {
54+
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
55+
56+
assertEquals(10L, (long) metricsMap.get(Metric.MetricType.READ_COUNT));
57+
assertEquals(10L / 2L, (long) metricsMap.get(Metric.MetricType.WRITE_COUNT));
58+
assertEquals(10L / 5L + 1, (long) metricsMap.get(Metric.MetricType.COMMIT_COUNT));
59+
}
60+
}
61+
62+
assertTrue(MyCheckpointAlgorithm.isCheckpointAlgorithmExecuted());
63+
assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
64+
}
65+
}

0 commit comments

Comments
 (0)