Skip to content

Commit df6904b

Browse files
committed
Merge pull request #179 from radcortez/master
More tests for batch, chunk-optional-processor and chunk-partition
2 parents 1efc18b + 719fee2 commit df6904b

File tree

6 files changed

+147
-14
lines changed

6 files changed

+147
-14
lines changed

batch/chunk-optional-processor/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
<version>1.0-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
10-
11-
<groupId>org.javaee7.batch</groupId>
10+
1211
<artifactId>chunk-optional-processor</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1412
<packaging>war</packaging>
1513

1614
<name>${project.artifactId}</name>
1715

16+
<dependencies>
17+
<dependency>
18+
<groupId>org.javaee7</groupId>
19+
<artifactId>util-samples</artifactId>
20+
</dependency>
21+
</dependencies>
1822
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.javaee7.batch.chunk.optional.processor;
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 BatchChunkOptionalProcessorTest {
26+
@Deployment
27+
public static WebArchive createDeployment() {
28+
WebArchive war = ShrinkWrap.create(WebArchive.class)
29+
.addClass(BatchTestHelper.class)
30+
.addPackage("org.javaee7.batch.chunk.optional.processor")
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 testBatchChunkOptionalProcessor() 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+
assertEquals(10L, metricsMap.get(Metric.MetricType.READ_COUNT).longValue());
50+
assertEquals(10L, metricsMap.get(Metric.MetricType.WRITE_COUNT).longValue());
51+
assertEquals(10L / 3 + 10 % 3, metricsMap.get(Metric.MetricType.COMMIT_COUNT).longValue());
52+
}
53+
}
54+
55+
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
56+
}
57+
}

batch/chunk-partition/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

11-
<groupId>org.javaee7.batch</groupId>
1211
<artifactId>chunk-partition</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1412
<packaging>war</packaging>
1513

1614
<name>${project.artifactId}</name>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.javaee7</groupId>
19+
<artifactId>util-samples</artifactId>
20+
</dependency>
21+
</dependencies>
1722
</project>

batch/chunk-partition/src/main/java/org/javaee7/batch/sample/chunk/partition/MyItemProcessor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@
4949
*/
5050
@Named
5151
public class MyItemProcessor implements ItemProcessor {
52+
public static int totalProcessors = 0;
53+
private int processorId;
5254

5355
@Inject
5456
JobContext context;
5557

58+
public MyItemProcessor() {
59+
processorId = ++totalProcessors;
60+
}
61+
5662
@Override
5763
public MyOutputRecord processItem(Object t) {
58-
System.out.format("processItem (%d): %s", context.getExecutionId(), t);
64+
System.out.format("processItem (%d): %s\n", processorId, t);
5965

6066
return (((MyInputRecord) t).getId() % 2 == 0) ? null : new MyOutputRecord(((MyInputRecord) t).getId() * 2);
6167
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@
3939
*/
4040
package org.javaee7.batch.sample.chunk.partition;
4141

42-
import java.io.Serializable;
43-
import java.util.StringTokenizer;
4442
import javax.batch.api.BatchProperty;
4543
import javax.batch.api.chunk.AbstractItemReader;
46-
import javax.batch.runtime.context.JobContext;
4744
import javax.inject.Inject;
4845
import javax.inject.Named;
46+
import java.io.Serializable;
47+
import java.util.StringTokenizer;
4948

5049
/**
5150
* @author Arun Gupta
5251
*/
5352
@Named
5453
public class MyItemReader extends AbstractItemReader {
54+
public static int totalReaders = 0;
55+
private int readerId;
5556

5657
private StringTokenizer tokens;
5758

@@ -62,10 +63,7 @@ public class MyItemReader extends AbstractItemReader {
6263
@Inject
6364
@BatchProperty(name = "end")
6465
private String endProp;
65-
66-
@Inject
67-
JobContext context;
68-
66+
6967
@Override
7068
public void open(Serializable e) {
7169
int start = new Integer(startProp);
@@ -76,14 +74,16 @@ public void open(Serializable e) {
7674
if (i < end)
7775
builder.append(",");
7876
}
77+
78+
readerId = ++totalReaders;
7979
tokens = new StringTokenizer(builder.toString(), ",");
8080
}
8181

8282
@Override
8383
public MyInputRecord readItem() {
8484
if (tokens.hasMoreTokens()) {
8585
int token = Integer.valueOf(tokens.nextToken());
86-
System.out.format("readItem (%d): %d", context.getExecutionId(), token);
86+
System.out.format("readItem (%d): %d\n", readerId, token);
8787
return new MyInputRecord(token);
8888
}
8989
return null;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.javaee7.batch.sample.chunk.partition;
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 BatchChunkPartitionTest {
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.partition")
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 testBatchChunkPartition() 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(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
60+
}
61+
}

0 commit comments

Comments
 (0)