Skip to content

Commit fecc8f4

Browse files
committed
Merge pull request javaee-samples#177 from radcortez/master
Added test for chunk-mapper project.
2 parents 459846b + f583b5d commit fecc8f4

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

batch/chunk-mapper/pom.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>org.javaee7.batch</groupId>
@@ -8,11 +9,15 @@
89
<relativePath>../pom.xml</relativePath>
910
</parent>
1011

11-
<groupId>org.javaee7.batch</groupId>
1212
<artifactId>chunk-mapper</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1413
<packaging>war</packaging>
1514

1615
<name>${project.artifactId}</name>
1716

17+
<dependencies>
18+
<dependency>
19+
<groupId>org.javaee7</groupId>
20+
<artifactId>util-samples</artifactId>
21+
</dependency>
22+
</dependencies>
1823
</project>

batch/chunk-mapper/src/main/java/org/javaee7/batch/sample/chunk/mapper/MyItemReader.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@
4040
package org.javaee7.batch.sample.chunk.mapper;
4141

4242
import java.io.Serializable;
43-
import java.util.Properties;
4443
import java.util.StringTokenizer;
4544
import javax.batch.api.BatchProperty;
4645
import javax.batch.api.chunk.AbstractItemReader;
47-
import javax.batch.runtime.BatchRuntime;
4846
import javax.batch.runtime.context.JobContext;
4947
import javax.inject.Inject;
5048
import javax.inject.Named;
@@ -54,6 +52,8 @@
5452
*/
5553
@Named
5654
public class MyItemReader extends AbstractItemReader {
55+
public static int totalReaders = 0;
56+
private int readerId;
5757

5858
private StringTokenizer tokens;
5959

@@ -66,7 +66,7 @@ public class MyItemReader extends AbstractItemReader {
6666
private String endProp;
6767

6868
@Inject
69-
JobContext context;
69+
private JobContext context;
7070

7171
@Override
7272
public void open(Serializable e) {
@@ -81,14 +81,16 @@ public void open(Serializable e) {
8181
if (i < end)
8282
builder.append(",");
8383
}
84+
85+
readerId = ++totalReaders;
8486
tokens = new StringTokenizer(builder.toString(), ",");
8587
}
8688

8789
@Override
8890
public MyInputRecord readItem() {
8991
if (tokens.hasMoreTokens()) {
9092
int token = Integer.valueOf(tokens.nextToken());
91-
System.out.format("readItem (%d): %d", context.getExecutionId(), token);
93+
System.out.format("readItem (%d): %d\n", readerId, token);
9294
return new MyInputRecord(token);
9395
}
9496
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javaee7.batch.sample.chunk.mapper;
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.List;
16+
import java.util.Map;
17+
import java.util.Properties;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
/**
22+
* @author Roberto Cortez
23+
*/
24+
@RunWith(Arquillian.class)
25+
public class BatchChunkMapperTest {
26+
@Deployment
27+
public static WebArchive createDeployment() {
28+
WebArchive war = ShrinkWrap.create(WebArchive.class)
29+
.addClass(BatchTestHelper.class)
30+
.addPackage("org.javaee7.batch.sample.chunk.mapper")
31+
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
32+
.addAsResource("META-INF/batch-jobs/myJob.xml");
33+
System.out.println(war.toString(true));
34+
return war;
35+
}
36+
37+
@Test
38+
public void testBatchChunkMapper() throws Exception {
39+
JobOperator jobOperator = BatchRuntime.getJobOperator();
40+
Long executionId = jobOperator.start("myJob", new Properties());
41+
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
42+
43+
BatchTestHelper.keepTestAlive(jobExecution);
44+
45+
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
46+
for (StepExecution stepExecution : stepExecutions) {
47+
if (stepExecution.getStepName().equals("myStep")) {
48+
Map<Metric.MetricType, Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
49+
50+
assertEquals(20L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
51+
assertEquals(10L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
52+
// Number of elements by the item count value on myJob.xml, plus an additional transaction for the
53+
// remaining elements by each partition.
54+
long commitCount = 20L / 3 + (20 % 3 > 0 ? 1 : 0) * 2;
55+
assertEquals(commitCount, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
56+
}
57+
}
58+
59+
assertEquals(2L, MyItemReader.totalReaders);
60+
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
61+
}
62+
}

0 commit comments

Comments
 (0)