Skip to content

Commit 48a5c90

Browse files
committed
Refactored common methods used by batch tests to project batch-test.
Added test for chuck-simple project.
1 parent 3ebb1ea commit 48a5c90

File tree

5 files changed

+155
-26
lines changed

5 files changed

+155
-26
lines changed

batch/batch-test/pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<artifactId>javaee7-samples</artifactId>
8+
<groupId>org.javaee7</groupId>
9+
<version>1.0-SNAPSHOT</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<artifactId>batch-test</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.javaee7.batch.test;
2+
3+
import javax.batch.runtime.BatchStatus;
4+
import javax.batch.runtime.JobExecution;
5+
import javax.batch.runtime.Metric;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* @author Roberto Cortez
11+
*/
12+
public final class BatchTestHelper {
13+
private static final int MAX_TRIES = 10;
14+
private static final int THREAD_SLEEP = 100;
15+
16+
private BatchTestHelper() {
17+
throw new UnsupportedOperationException();
18+
}
19+
20+
/**
21+
* We need to keep the test running because JobOperator runs the batch job in an asynchronous way, so the
22+
* JobExecution can be properly updated with the running job status.
23+
*
24+
* @param jobExecution
25+
* the JobExecution of the job that is being runned on JobOperator.
26+
*
27+
* @throws InterruptedException thrown by Thread.sleep.
28+
*/
29+
public static void keepTestAlive(JobExecution jobExecution) throws InterruptedException {
30+
int maxTries = 0;
31+
while (!jobExecution.getBatchStatus().equals(BatchStatus.COMPLETED)) {
32+
if (maxTries < MAX_TRIES) {
33+
maxTries++;
34+
Thread.sleep(THREAD_SLEEP);
35+
} else {
36+
break;
37+
}
38+
}
39+
}
40+
41+
/**
42+
* Convert the Metric array contained in StepExecution to a key-value map for easy access to Metric parameters.
43+
*
44+
* @param metrics
45+
* a Metric array contained in StepExecution.
46+
*
47+
* @return a map view of the metrics array.
48+
*/
49+
public static Map<Metric.MetricType, Long> getMetricsMap(Metric[] metrics) {
50+
Map<Metric.MetricType, Long> metricsMap = new HashMap<>();
51+
for (Metric metric : metrics) {
52+
metricsMap.put(metric.getType(), metric.getValue());
53+
}
54+
return metricsMap;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.javaee7.batch.batchlet.simple;
22

3+
import org.javaee7.batch.test.BatchTestHelper;
34
import org.jboss.arquillian.container.test.api.Deployment;
45
import org.jboss.arquillian.junit.Arquillian;
56
import org.jboss.shrinkwrap.api.ArchivePaths;
@@ -21,13 +22,13 @@
2122
*/
2223
@RunWith(Arquillian.class)
2324
public class MyBatchletTest {
24-
2525
@Deployment
2626
public static WebArchive createDeployment() {
27-
WebArchive war = ShrinkWrap.create(WebArchive.class).
28-
addClass(MyBatchlet.class).
29-
addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml")).
30-
addAsResource("META-INF/batch-jobs/myJob.xml");
27+
WebArchive war = ShrinkWrap.create(WebArchive.class)
28+
.addClass(BatchTestHelper.class)
29+
.addClass(MyBatchlet.class)
30+
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
31+
.addAsResource("META-INF/batch-jobs/myJob.xml");
3132
System.out.println(war.toString(true));
3233
return war;
3334
}
@@ -38,27 +39,8 @@ public void testBatchletProcess() throws Exception {
3839
Long executionId = jobOperator.start("myJob", new Properties());
3940
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
4041

41-
keepTestAlive(jobExecution);
42+
BatchTestHelper.keepTestAlive(jobExecution);
4243

4344
Assert.assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
4445
}
45-
46-
/**
47-
* We need to keep the test running because JobOperator runs the batch job in an asynchronous way, so the
48-
* JobExecution can be properly updated with the running job status.
49-
*
50-
* @param jobExecution the JobExecution of the job that is being runned on JobOperator
51-
* @throws InterruptedException
52-
*/
53-
private void keepTestAlive(JobExecution jobExecution) throws InterruptedException {
54-
int maxTries = 0;
55-
while (!jobExecution.getBatchStatus().equals(BatchStatus.COMPLETED)) {
56-
if (maxTries < 10) {
57-
maxTries++;
58-
Thread.sleep(100);
59-
} else {
60-
break;
61-
}
62-
}
63-
}
6446
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.javaee7.batch.chunk.simple;
2+
3+
import org.javaee7.batch.test.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.Assert;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import javax.batch.operations.JobOperator;
15+
import javax.batch.runtime.BatchRuntime;
16+
import javax.batch.runtime.BatchStatus;
17+
import javax.batch.runtime.JobExecution;
18+
import javax.batch.runtime.Metric;
19+
import javax.batch.runtime.StepExecution;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Properties;
23+
24+
/**
25+
* @author Roberto Cortez
26+
*/
27+
@RunWith(Arquillian.class)
28+
public class ChunkSimpleTest {
29+
@Deployment
30+
public static WebArchive createDeployment() {
31+
WebArchive war = ShrinkWrap.create(WebArchive.class)
32+
.addClass(BatchTestHelper.class)
33+
.addClass(MyInputRecord.class)
34+
.addClass(MyItemProcessor.class)
35+
.addClass(MyItemReader.class)
36+
.addClass(MyItemWriter.class)
37+
.addClass(MyOutputRecord.class)
38+
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
39+
.addAsManifestResource("META-INF/batch-jobs/myJob.xml", "batch-jobs/myJob.xml");
40+
System.out.println(war.toString(true));
41+
return war;
42+
}
43+
44+
@Test
45+
public void testChunkSimple() throws Exception {
46+
JobOperator jobOperator = BatchRuntime.getJobOperator();
47+
Long executionId = jobOperator.start("myJob", new Properties());
48+
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
49+
50+
BatchTestHelper.keepTestAlive(jobExecution);
51+
52+
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
53+
for (StepExecution stepExecution : stepExecutions) {
54+
if (stepExecution.getStepName().equals("myStep")) {
55+
Map<Metric.MetricType,Long> metricsMap = BatchTestHelper.getMetricsMap(stepExecution.getMetrics());
56+
Assert.assertEquals((long) metricsMap.get(Metric.MetricType.READ_COUNT), 10L);
57+
Assert.assertEquals((long) metricsMap.get(Metric.MetricType.WRITE_COUNT), 10L/2L);
58+
Assert.assertEquals((long) metricsMap.get(Metric.MetricType.COMMIT_COUNT), 10L/3 + 10%3);
59+
}
60+
}
61+
62+
Assert.assertEquals(jobExecution.getBatchStatus(), BatchStatus.COMPLETED);
63+
}
64+
}

batch/pom.xml

+12-1
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</groupId>
@@ -15,6 +16,7 @@
1516
<name>Java EE 7 Batch Samples</name>
1617

1718
<modules>
19+
<module>batch-test</module>
1820
<module>batchlet-simple</module>
1921
<module>chunk-checkpoint</module>
2022
<module>chunk-csv-database</module>
@@ -31,4 +33,13 @@
3133
<module>chunk-simple-nobeans</module>
3234
</modules>
3335

36+
<dependencies>
37+
<dependency>
38+
<groupId>org.javaee7</groupId>
39+
<artifactId>batch-test</artifactId>
40+
<version>1.0-SNAPSHOT</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
3445
</project>

0 commit comments

Comments
 (0)