1
2
3
4 package de.powerstat.phplib.templateengine.intern.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.assertFalse;
11 import static org.junit.jupiter.api.Assertions.assertNotEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15
16 import java.util.List;
17
18 import org.junit.jupiter.api.Test;
19
20 import de.powerstat.phplib.templateengine.intern.VariableManager;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23
24
25
26
27 final class VariableManagerTests
28 {
29
30
31
32 private static final String CONSTRUCTION_FAILED = "Construction failed";
33
34
35
36
37 private static final String TESTVAR = "testvar";
38
39
40
41
42 private static final String TEST = "test";
43
44
45
46
47 private static final String VARIABLE_VALUE_NOT_AS_EXPECTED = "Variable value not as expected";
48
49
50
51
52 private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
53
54
55
56
57 private static final String RESULT_VALUES_NOT_AS_EXPECTED = "Result values not as expected";
58
59
60
61
62 private static final String BEFORE_SUBSTITUTE_AFTER = "before{substitute}after";
63
64
65
66
67 private static final String SUBSTITUTE = "substitute";
68
69
70
71
72 private static final String BEFORETESTAFTER = "beforetestafter";
73
74
75
76
77 private static final String TARGET = "target";
78
79
80
81
82 private static final String DEFAULT = "default";
83
84
85
86
87
88 VariableManagerTests()
89 {
90 super();
91 }
92
93
94
95
96
97 @Test
98 void testConstructor1()
99 {
100 final VariableManager vm1 = new VariableManager();
101 assertNotNull(vm1, CONSTRUCTION_FAILED);
102 }
103
104
105
106
107
108 @Test
109 void testConstructor2()
110 {
111 final VariableManager vm1 = new VariableManager();
112 final VariableManager vm2 = new VariableManager(vm1);
113 assertNotNull(vm2, CONSTRUCTION_FAILED);
114 }
115
116
117
118
119
120 @Test
121 void testSetVar1()
122 {
123 final VariableManager vm1 = new VariableManager();
124 vm1.setVar(TESTVAR, TEST);
125 assertEquals(TEST, vm1.getVar(TESTVAR), VARIABLE_VALUE_NOT_AS_EXPECTED);
126 }
127
128
129
130
131
132 @Test
133 void testSetVar2()
134 {
135 final VariableManager vm1 = new VariableManager();
136 assertThrows(NullPointerException.class, () ->
137 {
138 vm1.setVar(null, TEST);
139 }, NULL_POINTER_EXCEPTION
140 );
141 }
142
143
144
145
146
147 @Test
148 void testSetVar3()
149 {
150 final VariableManager vm1 = new VariableManager();
151 vm1.setVar(TESTVAR, null);
152 assertEquals("", vm1.getVar(TESTVAR), VARIABLE_VALUE_NOT_AS_EXPECTED);
153 }
154
155
156
157
158
159 @Test
160 void testExistsVar1()
161 {
162 final VariableManager vm1 = new VariableManager();
163 vm1.setVar(TESTVAR, TEST);
164 final boolean result = vm1.existsVar(TESTVAR);
165 assertTrue(result, VARIABLE_VALUE_NOT_AS_EXPECTED);
166 }
167
168
169
170
171
172 @Test
173 void testExistsVar2()
174 {
175 final VariableManager vm1 = new VariableManager();
176 final boolean result = vm1.existsVar(TESTVAR);
177 assertFalse(result, VARIABLE_VALUE_NOT_AS_EXPECTED);
178 }
179
180
181
182
183
184 @Test
185 void testExistsVar3()
186 {
187 final VariableManager vm1 = new VariableManager();
188 final boolean result = vm1.existsVar("");
189 assertFalse(result, VARIABLE_VALUE_NOT_AS_EXPECTED);
190 }
191
192
193
194
195
196 @Test
197 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
198 void testExistsVar4()
199 {
200 final VariableManager vm1 = new VariableManager();
201 assertThrows(NullPointerException.class, () ->
202 {
203 vm1.existsVar(null);
204 }, NULL_POINTER_EXCEPTION
205 );
206 }
207
208
209
210
211
212 @Test
213 void testGetVar1()
214 {
215 final VariableManager vm1 = new VariableManager();
216 vm1.setVar(TESTVAR, TEST);
217 final String result = vm1.getVar(TESTVAR);
218 assertEquals(TEST, result, VARIABLE_VALUE_NOT_AS_EXPECTED);
219 }
220
221
222
223
224
225 @Test
226 void testGetVar2()
227 {
228 final VariableManager vm1 = new VariableManager();
229 final String result = vm1.getVar(TESTVAR);
230 assertEquals("", result, VARIABLE_VALUE_NOT_AS_EXPECTED);
231 }
232
233
234
235
236
237 @Test
238 void testGetVar3()
239 {
240 final VariableManager vm1 = new VariableManager();
241 final String result = vm1.getVar("");
242 assertEquals("", result, VARIABLE_VALUE_NOT_AS_EXPECTED);
243 }
244
245
246
247
248
249 @Test
250 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
251 void testGetVar4()
252 {
253 final VariableManager vm1 = new VariableManager();
254 assertThrows(NullPointerException.class, () ->
255 {
256 vm1.getVar(null);
257 }, NULL_POINTER_EXCEPTION
258 );
259 }
260
261
262
263
264
265 @Test
266 void testUnsetVar1()
267 {
268 final VariableManager vm1 = new VariableManager();
269 vm1.setVar(TESTVAR, TEST);
270 vm1.unsetVar(TESTVAR);
271 final String result = vm1.getVar(TESTVAR);
272 assertEquals("", result, VARIABLE_VALUE_NOT_AS_EXPECTED);
273 }
274
275
276
277
278
279 @Test
280 void testUnsetVar2()
281 {
282 final VariableManager vm1 = new VariableManager();
283 vm1.unsetVar(TESTVAR);
284 final String result = vm1.getVar(TESTVAR);
285 assertEquals("", result, VARIABLE_VALUE_NOT_AS_EXPECTED);
286 }
287
288
289
290
291
292 @Test
293 void testUnsetVar3()
294 {
295 final VariableManager vm1 = new VariableManager();
296 vm1.unsetVar("");
297 final String result = vm1.getVar("");
298 assertEquals("", result, VARIABLE_VALUE_NOT_AS_EXPECTED);
299 }
300
301
302
303
304
305 @Test
306 void testUnsetVar4()
307 {
308 final VariableManager vm1 = new VariableManager();
309 assertThrows(NullPointerException.class, () ->
310 {
311 vm1.unsetVar(null);
312 }, NULL_POINTER_EXCEPTION
313 );
314 }
315
316
317
318
319
320 @Test
321 void testGetVars1()
322 {
323 final VariableManager vm1 = new VariableManager();
324 vm1.setVar(TESTVAR, TEST);
325 final List<String> results = vm1.getVars();
326 final String[] expected = {TESTVAR};
327 assertArrayEquals(expected, results.toArray(), RESULT_VALUES_NOT_AS_EXPECTED);
328 }
329
330
331
332
333
334 @Test
335 void testGetVars2()
336 {
337 final VariableManager vm1 = new VariableManager();
338 final List<String> results = vm1.getVars();
339 final String[] expected = {};
340 assertArrayEquals(expected, results.toArray(), RESULT_VALUES_NOT_AS_EXPECTED);
341 }
342
343
344
345
346
347 @Test
348 void testGetUndefined1()
349 {
350 final VariableManager vm1 = new VariableManager();
351 vm1.setVar(TESTVAR, "before{undefined}after");
352 final List<String> results = vm1.getUndefined(TESTVAR);
353 final String[] expected = {"undefined"};
354 assertArrayEquals(expected, results.toArray(), RESULT_VALUES_NOT_AS_EXPECTED);
355 }
356
357
358
359
360
361 @Test
362 void testGetUndefined2()
363 {
364 final VariableManager vm1 = new VariableManager();
365 vm1.setVar(TESTVAR, TEST);
366 final List<String> results = vm1.getUndefined(TESTVAR);
367 final String[] expected = {};
368 assertArrayEquals(expected, results.toArray(), RESULT_VALUES_NOT_AS_EXPECTED);
369 }
370
371
372
373
374
375 @Test
376 void testGetUndefined3()
377 {
378 final VariableManager vm1 = new VariableManager();
379 final List<String> results = vm1.getUndefined("");
380 final String[] expected = {};
381 assertArrayEquals(expected, results.toArray(), RESULT_VALUES_NOT_AS_EXPECTED);
382 }
383
384
385
386
387
388 @Test
389 void testGetUndefined4()
390 {
391 final VariableManager vm1 = new VariableManager();
392 assertThrows(NullPointerException.class, () ->
393 {
394 vm1.getUndefined(null);
395 }, NULL_POINTER_EXCEPTION
396 );
397 }
398
399
400
401
402
403 @Test
404 void testSubst1()
405 {
406 final VariableManager vm1 = new VariableManager();
407 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
408 vm1.setVar(SUBSTITUTE, TEST);
409 final String result = vm1.subst(TESTVAR);
410 assertEquals(BEFORETESTAFTER, result, RESULT_VALUES_NOT_AS_EXPECTED);
411 }
412
413
414
415
416
417 @Test
418 void testSubst2()
419 {
420 final VariableManager vm1 = new VariableManager();
421 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
422 final String result = vm1.subst(TESTVAR);
423 assertEquals(BEFORE_SUBSTITUTE_AFTER, result, RESULT_VALUES_NOT_AS_EXPECTED);
424 }
425
426
427
428
429
430 @Test
431 void testSubst3()
432 {
433 final VariableManager vm1 = new VariableManager();
434 final String result = vm1.subst(TESTVAR);
435 assertEquals("", result, RESULT_VALUES_NOT_AS_EXPECTED);
436 }
437
438
439
440
441
442 @Test
443 void testSubst4()
444 {
445 final VariableManager vm1 = new VariableManager();
446 final String result = vm1.subst("");
447 assertEquals("", result, RESULT_VALUES_NOT_AS_EXPECTED);
448 }
449
450
451
452
453
454 @Test
455 void testSubst5()
456 {
457 final VariableManager vm1 = new VariableManager();
458 assertThrows(NullPointerException.class, () ->
459 {
460 vm1.subst(null);
461 }, NULL_POINTER_EXCEPTION
462 );
463 }
464
465
466
467
468
469 @Test
470 void testParse1()
471 {
472 final VariableManager vm1 = new VariableManager();
473 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
474 vm1.setVar(SUBSTITUTE, TEST);
475 vm1.setVar(TARGET, DEFAULT);
476 final String result = vm1.parse(TARGET, TESTVAR, false);
477 assertEquals(BEFORETESTAFTER, result, RESULT_VALUES_NOT_AS_EXPECTED);
478 final String result2 = vm1.getVar(TARGET);
479 assertEquals(BEFORETESTAFTER, result2, RESULT_VALUES_NOT_AS_EXPECTED);
480 }
481
482
483
484
485
486 @Test
487 void testParse2()
488 {
489 final VariableManager vm1 = new VariableManager();
490 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
491 vm1.setVar(SUBSTITUTE, TEST);
492 vm1.setVar(TARGET, DEFAULT);
493 final String result = vm1.parse(TARGET, TESTVAR, true);
494 assertEquals(BEFORETESTAFTER, result, RESULT_VALUES_NOT_AS_EXPECTED);
495 final String result2 = vm1.getVar(TARGET);
496 assertEquals("defaultbeforetestafter", result2, RESULT_VALUES_NOT_AS_EXPECTED);
497 }
498
499
500
501
502
503 @Test
504 void testParse3()
505 {
506 final VariableManager vm1 = new VariableManager();
507 vm1.setVar(TARGET, DEFAULT);
508 final String result = vm1.parse(TARGET, "", false);
509 assertEquals("", result, RESULT_VALUES_NOT_AS_EXPECTED);
510 final String result2 = vm1.getVar(TARGET);
511 assertEquals("", result2, RESULT_VALUES_NOT_AS_EXPECTED);
512 }
513
514
515
516
517
518 @Test
519 void testParse4()
520 {
521 final VariableManager vm1 = new VariableManager();
522 vm1.setVar(TARGET, DEFAULT);
523 assertThrows(NullPointerException.class, () ->
524 {
525 vm1.parse(TARGET, null, false);
526 }, NULL_POINTER_EXCEPTION
527 );
528 }
529
530
531
532
533
534 @Test
535 void testParse5()
536 {
537 final VariableManager vm1 = new VariableManager();
538 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
539 vm1.setVar(SUBSTITUTE, TEST);
540 vm1.setVar("", DEFAULT);
541 final String result = vm1.parse("", TESTVAR, false);
542 assertEquals(BEFORETESTAFTER, result, RESULT_VALUES_NOT_AS_EXPECTED);
543 final String result2 = vm1.getVar("");
544 assertEquals(BEFORETESTAFTER, result2, RESULT_VALUES_NOT_AS_EXPECTED);
545 }
546
547
548
549
550
551 @Test
552 void testParse6()
553 {
554 final VariableManager vm1 = new VariableManager();
555 vm1.setVar(TESTVAR, BEFORE_SUBSTITUTE_AFTER);
556 vm1.setVar(SUBSTITUTE, TEST);
557 vm1.setVar("", DEFAULT);
558 assertThrows(NullPointerException.class, () ->
559 {
560 vm1.parse(null, TESTVAR, false);
561 }, NULL_POINTER_EXCEPTION
562 );
563 }
564
565
566
567
568
569 @Test
570 void testToString()
571 {
572 final VariableManager vm1 = new VariableManager();
573 vm1.setVar(TESTVAR, TEST);
574 final String string = vm1.toString();
575 assertEquals("VariableManager[vars=[testvar]]", string, "toString() result not as expected");
576 }
577
578
579
580
581
582 @Test
583 void testHashCode()
584 {
585 final VariableManager vm1 = new VariableManager();
586 final VariableManager vm2 = new VariableManager();
587 final VariableManager vm3 = new VariableManager();
588 vm3.setVar("testvar2", "test2");
589 assertAll("testHashCode",
590 () -> assertEquals(vm1.hashCode(), vm2.hashCode(), "hashCodes are not equal"),
591 () -> assertNotEquals(vm1.hashCode(), vm3.hashCode(), "hashCodes are equal")
592 );
593 }
594
595
596
597
598
599 @Test
600 @SuppressFBWarnings({"EC_NULL_ARG", "CE_CLASS_ENVY"})
601 @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
602 void testEquals()
603 {
604 final VariableManager vm1 = new VariableManager();
605 final VariableManager vm2 = new VariableManager();
606 final VariableManager vm3 = new VariableManager();
607 vm3.setVar("parent1", "before<!-- BEGIN blktest1 -->content<!-- END blktest1 -->after");
608 final VariableManager vm4 = new VariableManager();
609 final VariableManager vm5 = new VariableManager();
610 vm5.setVar("parent3", "before<!-- BEGIN blktest3 -->content<!-- END blktest3 -->after");
611 final VariableManager vm6 = new VariableManager();
612 vm6.setVar("parent4", "before<!-- BEGIN blktest4 -->content<!-- END blktest4 -->after");
613
614 assertAll("testEquals",
615 () -> assertTrue(vm1.equals(vm1), "VariableManager11 is not equal"),
616 () -> assertTrue(vm1.equals(vm2), "VariableManager12 are not equal"),
617 () -> assertTrue(vm2.equals(vm1), "VariableManager21 are not equal"),
618 () -> assertTrue(vm2.equals(vm4), "VariableManager24 are not equal"),
619 () -> assertTrue(vm1.equals(vm4), "VariableManager14 are not equal"),
620 () -> assertFalse(vm1.equals(vm3), "VariableManager13 are equal"),
621 () -> assertFalse(vm3.equals(vm1), "VariableManager31 are equal"),
622 () -> assertFalse(vm1.equals(null), "VariableManager10 is equal"),
623 () -> assertFalse(vm1.equals(vm5), "VariableManager15 is equal"),
624 () -> assertFalse(vm1.equals(vm6), "VariableManager16 is equal")
625 );
626 }
627
628 }