13
13
import java .util .Map ;
14
14
import java .util .Set ;
15
15
import java .util .function .Function ;
16
- import org .junit .jupiter .api .BeforeEach ;
16
+
17
+ import org .junit .jupiter .api .BeforeAll ;
17
18
import org .junit .jupiter .api .Test ;
19
+ import org .junit .jupiter .api .TestInstance ;
18
20
import org .junit .jupiter .api .extension .ExtendWith ;
19
21
import org .springframework .beans .factory .annotation .Autowired ;
20
22
import org .springframework .data .jpa .datatables .Config ;
29
31
30
32
@ ExtendWith (SpringExtension .class )
31
33
@ ContextConfiguration (classes = Config .class )
34
+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
32
35
public class EmployeeRepositoryTest {
33
- protected DataTablesInput input ;
34
-
35
36
@ Autowired
36
37
private EmployeeRepository employeeRepository ;
37
38
@@ -43,16 +44,14 @@ protected DataTablesOutput<EmployeeDto> getOutput(DataTablesInput input, Functio
43
44
return employeeRepository .findAll (input , converter );
44
45
}
45
46
46
- @ BeforeEach
47
+ @ BeforeAll
47
48
public void init () {
48
- employeeRepository .deleteAll ();
49
49
employeeRepository .saveAll (Employee .ALL );
50
- input = getBasicInput ();
51
50
}
52
51
53
52
@ Test
54
53
void basic () {
55
- DataTablesOutput <Employee > output = getOutput (input );
54
+ DataTablesOutput <Employee > output = getOutput (createInput () );
56
55
assertThat (output .getDraw ()).isEqualTo (1 );
57
56
assertThat (output .getError ()).isNull ();
58
57
assertThat (output .getRecordsFiltered ()).isEqualTo (Employee .ALL .size ());
@@ -62,6 +61,7 @@ void basic() {
62
61
63
62
@ Test
64
63
void paginated () {
64
+ DataTablesInput input = createInput ();
65
65
input .setDraw (2 );
66
66
input .setLength (5 );
67
67
input .setStart (5 );
@@ -75,6 +75,8 @@ void paginated() {
75
75
76
76
@ Test
77
77
void sortAscending () {
78
+ DataTablesInput input = createInput ();
79
+
78
80
input .addOrder ("age" , true );
79
81
80
82
DataTablesOutput <Employee > output = getOutput (input );
@@ -83,6 +85,8 @@ void sortAscending() {
83
85
84
86
@ Test
85
87
void sortDescending () {
88
+ DataTablesInput input = createInput ();
89
+
86
90
input .addOrder ("age" , false );
87
91
88
92
DataTablesOutput <Employee > output = getOutput (input );
@@ -91,6 +95,8 @@ void sortDescending() {
91
95
92
96
@ Test
93
97
void globalFilter () {
98
+ DataTablesInput input = createInput ();
99
+
94
100
input .getSearch ().setValue ("William" );
95
101
96
102
DataTablesOutput <Employee > output = getOutput (input );
@@ -99,6 +105,8 @@ void globalFilter() {
99
105
100
106
@ Test
101
107
void globalFilterWithMultiplePages () {
108
+ DataTablesInput input = createInput ();
109
+
102
110
input .getSearch ().setValue ("e" );
103
111
input .setLength (1 );
104
112
@@ -110,6 +118,8 @@ void globalFilterWithMultiplePages() {
110
118
111
119
@ Test
112
120
void globalFilterIgnoreCaseIgnoreSpace () {
121
+ DataTablesInput input = createInput ();
122
+
113
123
input .getSearch ().setValue (" aMoS " );
114
124
115
125
DataTablesOutput <Employee > output = getOutput (input );
@@ -118,6 +128,8 @@ void globalFilterIgnoreCaseIgnoreSpace() {
118
128
119
129
@ Test
120
130
void columnFilter () {
131
+ DataTablesInput input = createInput ();
132
+
121
133
input .getColumn ("lastName" ).setSearchValue (" AmOs " );
122
134
123
135
DataTablesOutput <Employee > output = getOutput (input );
@@ -126,6 +138,8 @@ void columnFilter() {
126
138
127
139
@ Test
128
140
void multipleColumnFilters () {
141
+ DataTablesInput input = createInput ();
142
+
129
143
input .getColumn ("age" ).setSearchValue ("28" );
130
144
input .getColumn ("position" ).setSearchValue ("Software" );
131
145
@@ -135,6 +149,8 @@ void multipleColumnFilters() {
135
149
136
150
@ Test
137
151
void columnFilterWithMultipleCases () {
152
+ DataTablesInput input = createInput ();
153
+
138
154
input .getColumn ("position" ).setSearchValue ("Accountant+Junior Technical Author" );
139
155
140
156
DataTablesOutput <Employee > output = getOutput (input );
@@ -144,6 +160,8 @@ void columnFilterWithMultipleCases() {
144
160
145
161
@ Test
146
162
void columnFilterWithNoCase () {
163
+ DataTablesInput input = createInput ();
164
+
147
165
input .getColumn ("position" ).setSearchValue ("+" );
148
166
149
167
DataTablesOutput <Employee > output = getOutput (input );
@@ -152,6 +170,8 @@ void columnFilterWithNoCase() {
152
170
153
171
@ Test
154
172
void zeroLength () {
173
+ DataTablesInput input = createInput ();
174
+
155
175
input .setLength (0 );
156
176
157
177
DataTablesOutput <Employee > output = getOutput (input );
@@ -161,6 +181,8 @@ void zeroLength() {
161
181
162
182
@ Test
163
183
void negativeLength () {
184
+ DataTablesInput input = createInput ();
185
+
164
186
input .setLength (-1 );
165
187
166
188
DataTablesOutput <Employee > output = getOutput (input );
@@ -170,6 +192,8 @@ void negativeLength() {
170
192
171
193
@ Test
172
194
void multipleColumnFiltersOnManyToOneRelationship () {
195
+ DataTablesInput input = createInput ();
196
+
173
197
input .getColumn ("office.city" ).setSearchValue ("new york" );
174
198
input .getColumn ("office.country" ).setSearchValue ("USA" );
175
199
@@ -180,6 +204,8 @@ void multipleColumnFiltersOnManyToOneRelationship() {
180
204
181
205
@ Test
182
206
void withConverter () {
207
+ DataTablesInput input = createInput ();
208
+
183
209
input .getColumn ("firstName" ).setSearchValue ("airi" );
184
210
185
211
DataTablesOutput <EmployeeDto > output = getOutput (input , employee ->
@@ -189,14 +215,14 @@ void withConverter() {
189
215
190
216
@ Test
191
217
protected void withAnAdditionalSpecification () {
192
- DataTablesOutput <Employee > output = employeeRepository .findAll (input , new SoftwareEngineersOnly <>());
218
+ DataTablesOutput <Employee > output = employeeRepository .findAll (createInput () , new SoftwareEngineersOnly <>());
193
219
assertThat (output .getRecordsFiltered ()).isEqualTo (2 );
194
220
assertThat (output .getRecordsTotal ()).isEqualTo (Employee .ALL .size ());
195
221
}
196
222
197
223
@ Test
198
224
protected void withAPreFilteringSpecification () {
199
- DataTablesOutput <Employee > output = employeeRepository .findAll (input , null , new SoftwareEngineersOnly <>());
225
+ DataTablesOutput <Employee > output = employeeRepository .findAll (createInput () , null , new SoftwareEngineersOnly <>());
200
226
assertThat (output .getRecordsFiltered ()).isEqualTo (2 );
201
227
assertThat (output .getRecordsTotal ()).isEqualTo (2 );
202
228
}
@@ -210,6 +236,8 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
210
236
211
237
@ Test
212
238
void columnFilterWithNull () {
239
+ DataTablesInput input = createInput ();
240
+
213
241
input .getColumn ("comment" ).setSearchValue ("NULL" );
214
242
215
243
DataTablesOutput <Employee > output = getOutput (input );
@@ -218,6 +246,8 @@ void columnFilterWithNull() {
218
246
219
247
@ Test
220
248
void columnFilterWithNullEscaped () {
249
+ DataTablesInput input = createInput ();
250
+
221
251
input .getColumn ("comment" ).setSearchValue ("\\ NULL" );
222
252
223
253
DataTablesOutput <Employee > output = getOutput (input );
@@ -226,6 +256,8 @@ void columnFilterWithNullEscaped() {
226
256
227
257
@ Test
228
258
void columnFilterWithEscapeCharacters () {
259
+ DataTablesInput input = createInput ();
260
+
229
261
input .getColumn ("comment" ).setSearchValue ("foo~" );
230
262
DataTablesOutput <Employee > output = getOutput (input );
231
263
assertThat (output .getData ()).containsOnly (Employee .ASHTON_COX );
@@ -241,6 +273,8 @@ void columnFilterWithEscapeCharacters() {
241
273
242
274
@ Test
243
275
void columnFilterWithValueOrNull () {
276
+ DataTablesInput input = createInput ();
277
+
244
278
input .getColumn ("comment" ).setSearchValue ("@foo@@+NULL" );
245
279
246
280
DataTablesOutput <Employee > output = getOutput (input );
@@ -249,6 +283,8 @@ void columnFilterWithValueOrNull() {
249
283
250
284
@ Test
251
285
void columnFilterBoolean () {
286
+ DataTablesInput input = createInput ();
287
+
252
288
input .getColumn ("isWorkingRemotely" ).setSearchValue ("true" );
253
289
254
290
DataTablesOutput <Employee > output = getOutput (input );
@@ -257,6 +293,8 @@ void columnFilterBoolean() {
257
293
258
294
@ Test
259
295
void columnFilterBooleanBothCases () {
296
+ DataTablesInput input = createInput ();
297
+
260
298
input .getColumn ("isWorkingRemotely" ).setSearchValue ("true+false" );
261
299
262
300
DataTablesOutput <Employee > output = getOutput (input );
@@ -265,6 +303,8 @@ void columnFilterBooleanBothCases() {
265
303
266
304
@ Test
267
305
protected void unknownColumn () {
306
+ DataTablesInput input = createInput ();
307
+
268
308
input .addColumn ("unknown" , true , true , "test" );
269
309
270
310
DataTablesOutput <Employee > output = getOutput (input );
@@ -273,6 +313,8 @@ protected void unknownColumn() {
273
313
274
314
@ Test
275
315
void withSearchPanes () {
316
+ DataTablesInput input = createInput ();
317
+
276
318
Map <String , Set <String >> searchPanes = new HashMap <>();
277
319
searchPanes .put ("position" , new HashSet <>(asList ("Software Engineer" , "Integration Specialist" )));
278
320
searchPanes .put ("age" , emptySet ());
@@ -296,6 +338,8 @@ void withSearchPanes() {
296
338
297
339
@ Test
298
340
void withSearchPanesAndAPreFilteringSpecification () {
341
+ DataTablesInput input = createInput ();
342
+
299
343
Map <String , Set <String >> searchPanes = new HashMap <>();
300
344
searchPanes .put ("position" , new HashSet <>(asList ("Software Engineer" , "Integration Specialist" )));
301
345
searchPanes .put ("age" , emptySet ());
@@ -315,7 +359,7 @@ void withSearchPanesAndAPreFilteringSpecification() {
315
359
);
316
360
}
317
361
318
- private static DataTablesInput getBasicInput () {
362
+ protected static DataTablesInput createInput () {
319
363
DataTablesInput input = new DataTablesInput ();
320
364
input .addColumn ("id" , true , true , "" );
321
365
input .addColumn ("firstName" , true , true , "" );
0 commit comments