Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39bb979

Browse files
committed
BATCH-2233: Moved JsrTestUtils to a location that is actually published with the release
1 parent 513e47f commit 39bb979

File tree

15 files changed

+247
-137
lines changed

15 files changed

+247
-137
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ project('spring-batch-test') {
447447
testCompile "org.hsqldb:hsqldb:$hsqldbVersion"
448448

449449
optional "org.aspectj:aspectjrt:$aspectjVersion"
450+
optional "javax.batch:javax.batch-api:$javaxBatchApiVersion"
450451
}
451452
}
452453

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.jsr;
17+
18+
import javax.batch.operations.JobOperator;
19+
import javax.batch.runtime.BatchRuntime;
20+
import javax.batch.runtime.BatchStatus;
21+
import javax.batch.runtime.JobExecution;
22+
import javax.batch.runtime.Metric;
23+
import javax.batch.runtime.StepExecution;
24+
import java.util.Date;
25+
import java.util.Properties;
26+
import java.util.concurrent.TimeoutException;
27+
28+
/**
29+
* @author mminella
30+
*/
31+
public abstract class AbstractJsrTestCase {
32+
33+
private static JobOperator operator;
34+
35+
static {
36+
operator = BatchRuntime.getJobOperator();
37+
}
38+
39+
/**
40+
* Executes a job and waits for it's status to be any of {@link javax.batch.runtime.BatchStatus#STOPPED},
41+
* {@link javax.batch.runtime.BatchStatus#COMPLETED}, or {@link javax.batch.runtime.BatchStatus#FAILED}. If the job does not
42+
* reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is
43+
* thrown.
44+
*
45+
* @param jobName
46+
* @param properties
47+
* @param timeout
48+
* @return the {@link javax.batch.runtime.JobExecution} for the final state of the job
49+
* @throws java.util.concurrent.TimeoutException if the timeout occurs
50+
*/
51+
public static JobExecution runJob(String jobName, Properties properties, long timeout) throws TimeoutException {
52+
long executionId = operator.start(jobName, properties);
53+
JobExecution execution = operator.getJobExecution(executionId);
54+
55+
Date curDate = new Date();
56+
BatchStatus curBatchStatus = execution.getBatchStatus();
57+
58+
while(true) {
59+
if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) {
60+
break;
61+
}
62+
63+
if(new Date().getTime() - curDate.getTime() > timeout) {
64+
throw new TimeoutException("Job processing did not complete in time");
65+
}
66+
67+
execution = operator.getJobExecution(executionId);
68+
curBatchStatus = execution.getBatchStatus();
69+
}
70+
return execution;
71+
}
72+
73+
/**
74+
* Restarts a job and waits for it's status to be any of {@link BatchStatus#STOPPED},
75+
* {@link BatchStatus#COMPLETED}, or {@link BatchStatus#FAILED}. If the job does not
76+
* reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is
77+
* thrown.
78+
*
79+
* @param executionId
80+
* @param properties
81+
* @param timeout
82+
* @return the {@link JobExecution} for the final state of the job
83+
* @throws java.util.concurrent.TimeoutException if the timeout occurs
84+
*/
85+
public static JobExecution restartJob(long executionId, Properties properties, long timeout) throws TimeoutException {
86+
long restartId = operator.restart(executionId, properties);
87+
JobExecution execution = operator.getJobExecution(restartId);
88+
89+
Date curDate = new Date();
90+
BatchStatus curBatchStatus = execution.getBatchStatus();
91+
92+
while(true) {
93+
if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) {
94+
break;
95+
}
96+
97+
if(new Date().getTime() - curDate.getTime() > timeout) {
98+
throw new TimeoutException("Job processing did not complete in time");
99+
}
100+
101+
execution = operator.getJobExecution(restartId);
102+
curBatchStatus = execution.getBatchStatus();
103+
}
104+
return execution;
105+
}
106+
107+
public static Metric getMetric(StepExecution stepExecution, Metric.MetricType type) {
108+
Metric[] metrics = stepExecution.getMetrics();
109+
110+
for (Metric metric : metrics) {
111+
if(metric.getType() == type) {
112+
return metric;
113+
}
114+
}
115+
116+
return null;
117+
}
118+
119+
120+
}

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/ExceptionHandlingParsingTests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

1818
import org.junit.Test;
19-
import org.springframework.batch.core.JobParametersBuilder;
20-
import org.springframework.batch.core.jsr.JsrTestUtils;
19+
import org.springframework.batch.core.jsr.AbstractJsrTestCase;
2120

2221
import javax.batch.api.BatchProperty;
2322
import javax.batch.api.chunk.ItemProcessor;
@@ -28,46 +27,45 @@
2827
import javax.batch.runtime.Metric;
2928
import javax.batch.runtime.StepExecution;
3029
import javax.inject.Inject;
31-
import java.util.ArrayList;
3230
import java.util.List;
3331
import java.util.Properties;
3432

3533
import static org.junit.Assert.assertEquals;
3634

37-
public class ExceptionHandlingParsingTests {
35+
public class ExceptionHandlingParsingTests extends AbstractJsrTestCase {
3836

3937
@Test
4038
public void testSkippable() throws Exception {
4139
JobOperator jobOperator = BatchRuntime.getJobOperator();
4240

4341
Properties jobParameters = new Properties();
4442
jobParameters.setProperty("run", "1");
45-
JobExecution execution1 = JsrTestUtils.runJob("ExceptionHandlingParsingTests-context", jobParameters, 10000l);
43+
JobExecution execution1 = runJob("ExceptionHandlingParsingTests-context", jobParameters, 10000l);
4644

4745
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(execution1.getExecutionId());
4846
assertEquals(BatchStatus.FAILED, execution1.getBatchStatus());
4947
assertEquals(1, stepExecutions.size());
50-
assertEquals(1, JsrTestUtils.getMetric(stepExecutions.get(0), Metric.MetricType.PROCESS_SKIP_COUNT).getValue());
48+
assertEquals(1, getMetric(stepExecutions.get(0), Metric.MetricType.PROCESS_SKIP_COUNT).getValue());
5149

5250
jobParameters = new Properties();
5351
jobParameters.setProperty("run", "2");
54-
JobExecution execution2 = JsrTestUtils.restartJob(execution1.getExecutionId(), jobParameters, 10000l);
52+
JobExecution execution2 = restartJob(execution1.getExecutionId(), jobParameters, 10000l);
5553
stepExecutions = jobOperator.getStepExecutions(execution2.getExecutionId());
5654
assertEquals(BatchStatus.FAILED, execution2.getBatchStatus());
5755
assertEquals(2, stepExecutions.size());
5856

5957
jobParameters = new Properties();
6058
jobParameters.setProperty("run", "3");
61-
JobExecution execution3 = JsrTestUtils.restartJob(execution2.getExecutionId(), jobParameters, 10000l);
59+
JobExecution execution3 = restartJob(execution2.getExecutionId(), jobParameters, 10000l);
6260
stepExecutions = jobOperator.getStepExecutions(execution3.getExecutionId());
6361
assertEquals(BatchStatus.COMPLETED, execution3.getBatchStatus());
6462
assertEquals(2, stepExecutions.size());
6563

66-
assertEquals(0, JsrTestUtils.getMetric(stepExecutions.get(1), Metric.MetricType.ROLLBACK_COUNT).getValue());
64+
assertEquals(0, getMetric(stepExecutions.get(1), Metric.MetricType.ROLLBACK_COUNT).getValue());
6765

6866
jobParameters = new Properties();
6967
jobParameters.setProperty("run", "4");
70-
JobExecution execution4 = JsrTestUtils.runJob("ExceptionHandlingParsingTests-context", jobParameters, 10000l);
68+
JobExecution execution4 = runJob("ExceptionHandlingParsingTests-context", jobParameters, 10000l);
7169
stepExecutions = jobOperator.getStepExecutions(execution4.getExecutionId());
7270
assertEquals(BatchStatus.COMPLETED, execution4.getBatchStatus());
7371
assertEquals(3, stepExecutions.size());

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/FlowParserTests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
*/
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertTrue;
20-
import static org.springframework.batch.core.jsr.JsrTestUtils.restartJob;
21-
import static org.springframework.batch.core.jsr.JsrTestUtils.runJob;
22-
23-
import java.util.List;
24-
import java.util.Properties;
18+
import org.junit.Test;
19+
import org.springframework.batch.core.ExitStatus;
20+
import org.springframework.batch.core.jsr.AbstractJsrTestCase;
2521

2622
import javax.batch.api.AbstractBatchlet;
2723
import javax.batch.operations.JobOperator;
@@ -30,9 +26,11 @@
3026
import javax.batch.runtime.StepExecution;
3127
import javax.batch.runtime.context.StepContext;
3228
import javax.inject.Inject;
29+
import java.util.List;
30+
import java.util.Properties;
3331

34-
import org.junit.Test;
35-
import org.springframework.batch.core.ExitStatus;
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertTrue;
3634

3735
/**
3836
* <p>
@@ -42,7 +40,7 @@
4240
* @author Chris Schaefer
4341
* @since 3.0
4442
*/
45-
public class FlowParserTests {
43+
public class FlowParserTests extends AbstractJsrTestCase {
4644
@Test
4745
public void testDuplicateTransitionPatternsAllowed() throws Exception {
4846
JobExecution stoppedExecution = runJob("FlowParserTests-context", new Properties(), 10000L);

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/ItemSkipParsingTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

1818
import org.junit.Test;
19-
import org.springframework.batch.core.jsr.JsrTestUtils;
19+
import org.springframework.batch.core.jsr.AbstractJsrTestCase;
2020
import org.springframework.batch.item.ItemProcessor;
2121
import org.springframework.batch.item.ItemReader;
2222
import org.springframework.batch.item.ItemWriter;
@@ -35,46 +35,46 @@
3535

3636
import static org.junit.Assert.assertEquals;
3737

38-
public class ItemSkipParsingTests {
38+
public class ItemSkipParsingTests extends AbstractJsrTestCase {
3939

4040
@Test
4141
public void test() throws Exception {
42-
javax.batch.runtime.JobExecution execution = JsrTestUtils.runJob("ItemSkipParsingTests-context", new Properties(), 10000l);
42+
javax.batch.runtime.JobExecution execution = runJob("ItemSkipParsingTests-context", new Properties(), 10000l);
4343
JobOperator jobOperator = BatchRuntime.getJobOperator();
4444

4545
assertEquals(BatchStatus.FAILED, execution.getBatchStatus());
4646
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(execution.getExecutionId());
47-
assertEquals(1, JsrTestUtils.getMetric(stepExecutions.get(0), Metric.MetricType.READ_SKIP_COUNT).getValue());
47+
assertEquals(1, getMetric(stepExecutions.get(0), Metric.MetricType.READ_SKIP_COUNT).getValue());
4848
assertEquals(1, TestSkipListener.readSkips);
4949
assertEquals(0, TestSkipListener.processSkips);
5050
assertEquals(0, TestSkipListener.writeSkips);
5151

5252
// Process skip and fail
53-
execution = JsrTestUtils.restartJob(execution.getExecutionId(), new Properties(), 10000l);
53+
execution = restartJob(execution.getExecutionId(), new Properties(), 10000l);
5454

5555
assertEquals(BatchStatus.FAILED, execution.getBatchStatus());
5656
stepExecutions = jobOperator.getStepExecutions(execution.getExecutionId());
57-
assertEquals(1, JsrTestUtils.getMetric(stepExecutions.get(0), Metric.MetricType.PROCESS_SKIP_COUNT).getValue());
57+
assertEquals(1, getMetric(stepExecutions.get(0), Metric.MetricType.PROCESS_SKIP_COUNT).getValue());
5858
assertEquals(0, TestSkipListener.readSkips);
5959
assertEquals(1, TestSkipListener.processSkips);
6060
assertEquals(0, TestSkipListener.writeSkips);
6161

6262
// Write skip and fail
63-
execution = JsrTestUtils.restartJob(execution.getExecutionId(), new Properties(), 10000l);
63+
execution = restartJob(execution.getExecutionId(), new Properties(), 10000l);
6464

6565
assertEquals(BatchStatus.FAILED, execution.getBatchStatus());
6666
stepExecutions = jobOperator.getStepExecutions(execution.getExecutionId());
67-
assertEquals(1, JsrTestUtils.getMetric(stepExecutions.get(0), Metric.MetricType.WRITE_SKIP_COUNT).getValue());
67+
assertEquals(1, getMetric(stepExecutions.get(0), Metric.MetricType.WRITE_SKIP_COUNT).getValue());
6868
assertEquals(0, TestSkipListener.readSkips);
6969
assertEquals(0, TestSkipListener.processSkips);
7070
assertEquals(1, TestSkipListener.writeSkips);
7171

7272
// Complete
73-
execution = JsrTestUtils.restartJob(execution.getExecutionId(), new Properties(), 10000l);
73+
execution = restartJob(execution.getExecutionId(), new Properties(), 10000l);
7474

7575
assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());
7676
stepExecutions = jobOperator.getStepExecutions(execution.getExecutionId());
77-
assertEquals(0, JsrTestUtils.getMetric(stepExecutions.get(0), Metric.MetricType.WRITE_SKIP_COUNT).getValue());
77+
assertEquals(0, getMetric(stepExecutions.get(0), Metric.MetricType.WRITE_SKIP_COUNT).getValue());
7878
assertEquals(0, TestSkipListener.readSkips);
7979
assertEquals(0, TestSkipListener.processSkips);
8080
assertEquals(0, TestSkipListener.writeSkips);

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/JobPropertyTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.batch.core.jsr.configuration.xml;
1717

18-
import java.io.Serializable;
19-
import java.util.List;
20-
import java.util.Properties;
18+
import org.junit.Test;
19+
import org.springframework.batch.core.StepContribution;
20+
import org.springframework.batch.core.jsr.AbstractJsrTestCase;
21+
import org.springframework.batch.core.scope.context.ChunkContext;
22+
import org.springframework.batch.core.step.tasklet.Tasklet;
23+
import org.springframework.batch.repeat.RepeatStatus;
2124

2225
import javax.batch.api.BatchProperty;
2326
import javax.batch.api.Batchlet;
@@ -31,13 +34,9 @@
3134
import javax.batch.runtime.JobExecution;
3235
import javax.batch.runtime.context.JobContext;
3336
import javax.inject.Inject;
34-
35-
import org.junit.Test;
36-
import org.springframework.batch.core.StepContribution;
37-
import org.springframework.batch.core.jsr.JsrTestUtils;
38-
import org.springframework.batch.core.scope.context.ChunkContext;
39-
import org.springframework.batch.core.step.tasklet.Tasklet;
40-
import org.springframework.batch.repeat.RepeatStatus;
37+
import java.io.Serializable;
38+
import java.util.List;
39+
import java.util.Properties;
4140

4241
import static junit.framework.Assert.assertEquals;
4342

@@ -49,15 +48,15 @@
4948
* @author Chris Schaefer
5049
* @since 3.0
5150
*/
52-
public class JobPropertyTests {
51+
public class JobPropertyTests extends AbstractJsrTestCase {
5352
@Test
5453
public void testJobPropertyConfiguration() throws Exception {
5554
Properties jobParameters = new Properties();
5655
jobParameters.setProperty("allow.start.if.complete", "true");
5756
jobParameters.setProperty("deciderName", "stepDecider");
5857
jobParameters.setProperty("deciderNumber", "1");
5958

60-
JobExecution jobExecution = JsrTestUtils.runJob("jsrJobPropertyTestsContext", jobParameters, 5000L);
59+
JobExecution jobExecution = runJob("jsrJobPropertyTestsContext", jobParameters, 5000L);
6160
assertEquals(BatchStatus.COMPLETED, jobExecution.getBatchStatus());
6261
}
6362

0 commit comments

Comments
 (0)