1313import java .util .Map ;
1414import java .util .Set ;
1515import java .util .function .Function ;
16- import org .junit .jupiter .api .BeforeEach ;
16+
17+ import org .junit .jupiter .api .BeforeAll ;
1718import org .junit .jupiter .api .Test ;
19+ import org .junit .jupiter .api .TestInstance ;
1820import org .junit .jupiter .api .extension .ExtendWith ;
1921import org .springframework .beans .factory .annotation .Autowired ;
2022import org .springframework .data .jpa .datatables .Config ;
2931
3032@ ExtendWith (SpringExtension .class )
3133@ ContextConfiguration (classes = Config .class )
34+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
3235public class EmployeeRepositoryTest {
33- protected DataTablesInput input ;
34-
3536 @ Autowired
3637 private EmployeeRepository employeeRepository ;
3738
@@ -43,16 +44,14 @@ protected DataTablesOutput<EmployeeDto> getOutput(DataTablesInput input, Functio
4344 return employeeRepository .findAll (input , converter );
4445 }
4546
46- @ BeforeEach
47+ @ BeforeAll
4748 public void init () {
48- employeeRepository .deleteAll ();
4949 employeeRepository .saveAll (Employee .ALL );
50- input = getBasicInput ();
5150 }
5251
5352 @ Test
5453 void basic () {
55- DataTablesOutput <Employee > output = getOutput (input );
54+ DataTablesOutput <Employee > output = getOutput (createInput () );
5655 assertThat (output .getDraw ()).isEqualTo (1 );
5756 assertThat (output .getError ()).isNull ();
5857 assertThat (output .getRecordsFiltered ()).isEqualTo (Employee .ALL .size ());
@@ -62,6 +61,7 @@ void basic() {
6261
6362 @ Test
6463 void paginated () {
64+ DataTablesInput input = createInput ();
6565 input .setDraw (2 );
6666 input .setLength (5 );
6767 input .setStart (5 );
@@ -75,6 +75,8 @@ void paginated() {
7575
7676 @ Test
7777 void sortAscending () {
78+ DataTablesInput input = createInput ();
79+
7880 input .addOrder ("age" , true );
7981
8082 DataTablesOutput <Employee > output = getOutput (input );
@@ -83,6 +85,8 @@ void sortAscending() {
8385
8486 @ Test
8587 void sortDescending () {
88+ DataTablesInput input = createInput ();
89+
8690 input .addOrder ("age" , false );
8791
8892 DataTablesOutput <Employee > output = getOutput (input );
@@ -91,6 +95,8 @@ void sortDescending() {
9195
9296 @ Test
9397 void globalFilter () {
98+ DataTablesInput input = createInput ();
99+
94100 input .getSearch ().setValue ("William" );
95101
96102 DataTablesOutput <Employee > output = getOutput (input );
@@ -99,6 +105,8 @@ void globalFilter() {
99105
100106 @ Test
101107 void globalFilterWithMultiplePages () {
108+ DataTablesInput input = createInput ();
109+
102110 input .getSearch ().setValue ("e" );
103111 input .setLength (1 );
104112
@@ -110,6 +118,8 @@ void globalFilterWithMultiplePages() {
110118
111119 @ Test
112120 void globalFilterIgnoreCaseIgnoreSpace () {
121+ DataTablesInput input = createInput ();
122+
113123 input .getSearch ().setValue (" aMoS " );
114124
115125 DataTablesOutput <Employee > output = getOutput (input );
@@ -118,6 +128,8 @@ void globalFilterIgnoreCaseIgnoreSpace() {
118128
119129 @ Test
120130 void columnFilter () {
131+ DataTablesInput input = createInput ();
132+
121133 input .getColumn ("lastName" ).setSearchValue (" AmOs " );
122134
123135 DataTablesOutput <Employee > output = getOutput (input );
@@ -126,6 +138,8 @@ void columnFilter() {
126138
127139 @ Test
128140 void multipleColumnFilters () {
141+ DataTablesInput input = createInput ();
142+
129143 input .getColumn ("age" ).setSearchValue ("28" );
130144 input .getColumn ("position" ).setSearchValue ("Software" );
131145
@@ -135,6 +149,8 @@ void multipleColumnFilters() {
135149
136150 @ Test
137151 void columnFilterWithMultipleCases () {
152+ DataTablesInput input = createInput ();
153+
138154 input .getColumn ("position" ).setSearchValue ("Accountant+Junior Technical Author" );
139155
140156 DataTablesOutput <Employee > output = getOutput (input );
@@ -144,6 +160,8 @@ void columnFilterWithMultipleCases() {
144160
145161 @ Test
146162 void columnFilterWithNoCase () {
163+ DataTablesInput input = createInput ();
164+
147165 input .getColumn ("position" ).setSearchValue ("+" );
148166
149167 DataTablesOutput <Employee > output = getOutput (input );
@@ -152,6 +170,8 @@ void columnFilterWithNoCase() {
152170
153171 @ Test
154172 void zeroLength () {
173+ DataTablesInput input = createInput ();
174+
155175 input .setLength (0 );
156176
157177 DataTablesOutput <Employee > output = getOutput (input );
@@ -161,6 +181,8 @@ void zeroLength() {
161181
162182 @ Test
163183 void negativeLength () {
184+ DataTablesInput input = createInput ();
185+
164186 input .setLength (-1 );
165187
166188 DataTablesOutput <Employee > output = getOutput (input );
@@ -170,6 +192,8 @@ void negativeLength() {
170192
171193 @ Test
172194 void multipleColumnFiltersOnManyToOneRelationship () {
195+ DataTablesInput input = createInput ();
196+
173197 input .getColumn ("office.city" ).setSearchValue ("new york" );
174198 input .getColumn ("office.country" ).setSearchValue ("USA" );
175199
@@ -180,6 +204,8 @@ void multipleColumnFiltersOnManyToOneRelationship() {
180204
181205 @ Test
182206 void withConverter () {
207+ DataTablesInput input = createInput ();
208+
183209 input .getColumn ("firstName" ).setSearchValue ("airi" );
184210
185211 DataTablesOutput <EmployeeDto > output = getOutput (input , employee ->
@@ -189,14 +215,14 @@ void withConverter() {
189215
190216 @ Test
191217 protected void withAnAdditionalSpecification () {
192- DataTablesOutput <Employee > output = employeeRepository .findAll (input , new SoftwareEngineersOnly <>());
218+ DataTablesOutput <Employee > output = employeeRepository .findAll (createInput () , new SoftwareEngineersOnly <>());
193219 assertThat (output .getRecordsFiltered ()).isEqualTo (2 );
194220 assertThat (output .getRecordsTotal ()).isEqualTo (Employee .ALL .size ());
195221 }
196222
197223 @ Test
198224 protected void withAPreFilteringSpecification () {
199- DataTablesOutput <Employee > output = employeeRepository .findAll (input , null , new SoftwareEngineersOnly <>());
225+ DataTablesOutput <Employee > output = employeeRepository .findAll (createInput () , null , new SoftwareEngineersOnly <>());
200226 assertThat (output .getRecordsFiltered ()).isEqualTo (2 );
201227 assertThat (output .getRecordsTotal ()).isEqualTo (2 );
202228 }
@@ -210,6 +236,8 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
210236
211237 @ Test
212238 void columnFilterWithNull () {
239+ DataTablesInput input = createInput ();
240+
213241 input .getColumn ("comment" ).setSearchValue ("NULL" );
214242
215243 DataTablesOutput <Employee > output = getOutput (input );
@@ -218,6 +246,8 @@ void columnFilterWithNull() {
218246
219247 @ Test
220248 void columnFilterWithNullEscaped () {
249+ DataTablesInput input = createInput ();
250+
221251 input .getColumn ("comment" ).setSearchValue ("\\ NULL" );
222252
223253 DataTablesOutput <Employee > output = getOutput (input );
@@ -226,6 +256,8 @@ void columnFilterWithNullEscaped() {
226256
227257 @ Test
228258 void columnFilterWithEscapeCharacters () {
259+ DataTablesInput input = createInput ();
260+
229261 input .getColumn ("comment" ).setSearchValue ("foo~" );
230262 DataTablesOutput <Employee > output = getOutput (input );
231263 assertThat (output .getData ()).containsOnly (Employee .ASHTON_COX );
@@ -241,6 +273,8 @@ void columnFilterWithEscapeCharacters() {
241273
242274 @ Test
243275 void columnFilterWithValueOrNull () {
276+ DataTablesInput input = createInput ();
277+
244278 input .getColumn ("comment" ).setSearchValue ("@foo@@+NULL" );
245279
246280 DataTablesOutput <Employee > output = getOutput (input );
@@ -249,6 +283,8 @@ void columnFilterWithValueOrNull() {
249283
250284 @ Test
251285 void columnFilterBoolean () {
286+ DataTablesInput input = createInput ();
287+
252288 input .getColumn ("isWorkingRemotely" ).setSearchValue ("true" );
253289
254290 DataTablesOutput <Employee > output = getOutput (input );
@@ -257,6 +293,8 @@ void columnFilterBoolean() {
257293
258294 @ Test
259295 void columnFilterBooleanBothCases () {
296+ DataTablesInput input = createInput ();
297+
260298 input .getColumn ("isWorkingRemotely" ).setSearchValue ("true+false" );
261299
262300 DataTablesOutput <Employee > output = getOutput (input );
@@ -265,6 +303,8 @@ void columnFilterBooleanBothCases() {
265303
266304 @ Test
267305 protected void unknownColumn () {
306+ DataTablesInput input = createInput ();
307+
268308 input .addColumn ("unknown" , true , true , "test" );
269309
270310 DataTablesOutput <Employee > output = getOutput (input );
@@ -273,6 +313,8 @@ protected void unknownColumn() {
273313
274314 @ Test
275315 void withSearchPanes () {
316+ DataTablesInput input = createInput ();
317+
276318 Map <String , Set <String >> searchPanes = new HashMap <>();
277319 searchPanes .put ("position" , new HashSet <>(asList ("Software Engineer" , "Integration Specialist" )));
278320 searchPanes .put ("age" , emptySet ());
@@ -296,6 +338,8 @@ void withSearchPanes() {
296338
297339 @ Test
298340 void withSearchPanesAndAPreFilteringSpecification () {
341+ DataTablesInput input = createInput ();
342+
299343 Map <String , Set <String >> searchPanes = new HashMap <>();
300344 searchPanes .put ("position" , new HashSet <>(asList ("Software Engineer" , "Integration Specialist" )));
301345 searchPanes .put ("age" , emptySet ());
@@ -315,7 +359,7 @@ void withSearchPanesAndAPreFilteringSpecification() {
315359 );
316360 }
317361
318- private static DataTablesInput getBasicInput () {
362+ protected static DataTablesInput createInput () {
319363 DataTablesInput input = new DataTablesInput ();
320364 input .addColumn ("id" , true , true , "" );
321365 input .addColumn ("firstName" , true , true , "" );
0 commit comments