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