Skip to content

Commit a333c16

Browse files
committed
Add JobParametersIncrementer implementation based on a DataFieldMaxValueIncrementer
Issue #1521
1 parent d4133de commit a333c16

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.launch.support;
17+
18+
import org.springframework.batch.core.JobParameters;
19+
import org.springframework.batch.core.JobParametersBuilder;
20+
import org.springframework.batch.core.JobParametersIncrementer;
21+
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* This incrementer uses a {@link DataFieldMaxValueIncrementer} to generate
26+
* the sequence of values to use as job instance discriminator.
27+
*
28+
* @author Gregory D. Hopkins
29+
* @author Mahmoud Ben Hassine
30+
*/
31+
public class DataFieldMaxValueJobParametersIncrementer implements JobParametersIncrementer {
32+
33+
/**
34+
* Default key used as a job parameter.
35+
*/
36+
public static final String DEFAULT_KEY = "run.id";
37+
38+
private String key = DEFAULT_KEY;
39+
private DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer;
40+
41+
/**
42+
* Create a new {@link DataFieldMaxValueJobParametersIncrementer}.
43+
*
44+
* @param dataFieldMaxValueIncrementer the incrementer to use to generate
45+
* the sequence of values. Must not be {@code null}.
46+
*/
47+
public DataFieldMaxValueJobParametersIncrementer(DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer) {
48+
Assert.notNull(dataFieldMaxValueIncrementer, "dataFieldMaxValueIncrementer must not be null");
49+
this.dataFieldMaxValueIncrementer = dataFieldMaxValueIncrementer;
50+
}
51+
52+
@Override
53+
public JobParameters getNext(JobParameters jobParameters) {
54+
return new JobParametersBuilder(jobParameters == null ? new JobParameters() : jobParameters)
55+
.addLong(this.key, this.dataFieldMaxValueIncrementer.nextLongValue())
56+
.toJobParameters();
57+
}
58+
59+
/**
60+
* Get the key. Defaults to {@link #DEFAULT_KEY}.
61+
*
62+
* @return the key
63+
*/
64+
public String getKey() {
65+
return this.key;
66+
}
67+
68+
/**
69+
* The name of the key to use as a job parameter. Defaults to {@link #DEFAULT_KEY}.
70+
* Must not be {@code null} or empty.
71+
*
72+
* @param key the key to set
73+
*/
74+
public void setKey(String key) {
75+
Assert.hasText(key, "key must not be null or empty");
76+
this.key = key;
77+
}
78+
79+
/**
80+
* Get the incrementer.
81+
*
82+
* @return the incrementer
83+
*/
84+
public DataFieldMaxValueIncrementer getDataFieldMaxValueIncrementer() {
85+
return this.dataFieldMaxValueIncrementer;
86+
}
87+
88+
/**
89+
* The incrementer to generate the sequence of values. Must not be {@code null}.
90+
*
91+
* @param dataFieldMaxValueIncrementer the incrementer to generate the sequence of values
92+
*/
93+
public void setDataFieldMaxValueIncrementer(DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer) {
94+
Assert.notNull(dataFieldMaxValueIncrementer, "dataFieldMaxValueIncrementer must not be null");
95+
this.dataFieldMaxValueIncrementer = dataFieldMaxValueIncrementer;
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.launch.support;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import org.springframework.batch.core.JobParameters;
22+
import org.springframework.batch.core.JobParametersBuilder;
23+
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
24+
25+
import static org.junit.Assert.fail;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
28+
29+
/**
30+
* @author Mahmoud Ben Hassine
31+
*/
32+
public class DataFieldMaxValueJobParametersIncrementerTests {
33+
34+
private final DataFieldMaxValueIncrementer incrementer = mock(DataFieldMaxValueIncrementer.class);
35+
36+
@Test
37+
public void testInvalidKey() {
38+
DataFieldMaxValueJobParametersIncrementer jobParametersIncrementer
39+
= new DataFieldMaxValueJobParametersIncrementer(this.incrementer);
40+
try {
41+
jobParametersIncrementer.setKey("");
42+
fail("Must fail if the key is empty");
43+
}
44+
catch (IllegalArgumentException exception) {
45+
Assert.assertEquals("key must not be null or empty", exception.getMessage());
46+
}
47+
}
48+
49+
@Test
50+
public void testInvalidDataFieldMaxValueIncrementer() {
51+
try {
52+
new DataFieldMaxValueJobParametersIncrementer(null);
53+
fail("Must fail if the incrementer is null");
54+
}
55+
catch (IllegalArgumentException exception) {
56+
Assert.assertEquals("dataFieldMaxValueIncrementer must not be null", exception.getMessage());
57+
}
58+
}
59+
60+
@Test
61+
public void testGetNext() {
62+
// given
63+
JobParameters jobParameters = new JobParameters();
64+
when(this.incrementer.nextLongValue()).thenReturn(10L);
65+
DataFieldMaxValueJobParametersIncrementer jobParametersIncrementer
66+
= new DataFieldMaxValueJobParametersIncrementer(this.incrementer);
67+
68+
// when
69+
JobParameters nextParameters = jobParametersIncrementer.getNext(jobParameters);
70+
71+
// then
72+
Long runId = nextParameters.getLong("run.id");
73+
Assert.assertEquals(new Long(10) ,runId);
74+
}
75+
76+
@Test
77+
public void testGetNextAppend() {
78+
// given
79+
JobParameters jobParameters = new JobParametersBuilder()
80+
.addString("foo", "bar")
81+
.toJobParameters();
82+
when(this.incrementer.nextLongValue()).thenReturn(10L);
83+
DataFieldMaxValueJobParametersIncrementer jobParametersIncrementer
84+
= new DataFieldMaxValueJobParametersIncrementer(this.incrementer);
85+
86+
// when
87+
JobParameters nextParameters = jobParametersIncrementer.getNext(jobParameters);
88+
89+
// then
90+
Long runId = nextParameters.getLong("run.id");
91+
String foo = nextParameters.getString("foo");
92+
Assert.assertEquals(new Long(10) ,runId);
93+
Assert.assertEquals("bar" ,foo);
94+
}
95+
96+
@Test
97+
public void testGetNextOverride() {
98+
// given
99+
JobParameters jobParameters = new JobParametersBuilder()
100+
.addLong("run.id", 1L)
101+
.toJobParameters();
102+
when(this.incrementer.nextLongValue()).thenReturn(10L);
103+
DataFieldMaxValueJobParametersIncrementer jobParametersIncrementer
104+
= new DataFieldMaxValueJobParametersIncrementer(this.incrementer);
105+
106+
// when
107+
JobParameters nextParameters = jobParametersIncrementer.getNext(jobParameters);
108+
109+
// then
110+
Long runId = nextParameters.getLong("run.id");
111+
Assert.assertEquals(new Long(10) ,runId);
112+
}
113+
}

0 commit comments

Comments
 (0)