View Javadoc
1   /*
2    * Copyright (C) 2019-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
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   * Variable manager tests.
26   */
27  final class VariableManagerTests
28   {
29    /**
30     * Construction failed constant.
31     */
32    private static final String CONSTRUCTION_FAILED = "Construction failed";
33  
34    /**
35     * Testvar constant.
36     */
37    private static final String TESTVAR = "testvar";
38  
39    /**
40     * Test constant.
41     */
42    private static final String TEST = "test";
43  
44    /**
45     * Variable value not as expected constant.
46     */
47    private static final String VARIABLE_VALUE_NOT_AS_EXPECTED = "Variable value not as expected";
48  
49    /**
50     * Null pointer exception constant.
51     */
52    private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
53  
54    /**
55     * Result values not as expected constant.
56     */
57    private static final String RESULT_VALUES_NOT_AS_EXPECTED = "Result values not as expected";
58  
59    /**
60     * Before substitute after constant.
61     */
62    private static final String BEFORE_SUBSTITUTE_AFTER = "before{substitute}after";
63  
64    /**
65     * Substitute constant.
66     */
67    private static final String SUBSTITUTE = "substitute";
68  
69    /**
70     * Before test after constant.
71     */
72    private static final String BEFORETESTAFTER = "beforetestafter";
73  
74    /**
75     * Target constant.
76     */
77    private static final String TARGET = "target";
78  
79    /**
80     * Default constant.
81     */
82    private static final String DEFAULT = "default";
83  
84  
85    /**
86     * Default constructor.
87     */
88    /* default */ VariableManagerTests()
89     {
90      super();
91     }
92  
93  
94    /**
95     * Constructor test.
96     */
97    @Test
98    /* default */ void testConstructor1()
99     {
100     final VariableManager vm1 = new VariableManager();
101     assertNotNull(vm1, CONSTRUCTION_FAILED);
102    }
103 
104 
105   /**
106    * Constructor test.
107    */
108   @Test
109   /* default */ 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    * setVar test.
119    */
120   @Test
121   /* default */ 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    * setVar test.
131    */
132   @Test
133   /* default */ 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    * setVar test.
146    */
147   @Test
148   /* default */ 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    * existsVar test.
158    */
159   @Test
160   /* default */ 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    * existsVar test.
171    */
172   @Test
173   /* default */ 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    * existsVar test.
183    */
184   @Test
185   /* default */ 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    * existsVar test.
195    */
196   @Test
197   @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
198   /* default */ void testExistsVar4()
199    {
200     final VariableManager vm1 = new VariableManager();
201     assertThrows(NullPointerException.class, () ->
202      {
203       /* final boolean result = */ vm1.existsVar(null);
204      }, NULL_POINTER_EXCEPTION
205     );
206    }
207 
208 
209   /**
210    * getVar test.
211    */
212   @Test
213   /* default */ 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    * getVar test.
224    */
225   @Test
226   /* default */ 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    * getVar test.
236    */
237   @Test
238   /* default */ 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    * getVar test.
248    */
249   @Test
250   @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
251   /* default */ void testGetVar4()
252    {
253     final VariableManager vm1 = new VariableManager();
254     assertThrows(NullPointerException.class, () ->
255      {
256       /* final String result = */ vm1.getVar(null);
257      }, NULL_POINTER_EXCEPTION
258     );
259    }
260 
261 
262   /**
263    * unsetVar test.
264    */
265   @Test
266   /* default */ 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    * unsetVar test.
278    */
279   @Test
280   /* default */ 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    * unsetVar test.
291    */
292   @Test
293   /* default */ 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    * unsetVar test.
304    */
305   @Test
306   /* default */ 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    * getVars test.
319    */
320   @Test
321   /* default */ 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    * getVars test.
333    */
334   @Test
335   /* default */ 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    * getUndefined test.
346    */
347   @Test
348   /* default */ 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    * getUndefined test.
360    */
361   @Test
362   /* default */ 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    * getUndefined test.
374    */
375   @Test
376   /* default */ 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    * getUndefined test.
387    */
388   @Test
389   /* default */ void testGetUndefined4()
390    {
391     final VariableManager vm1 = new VariableManager();
392     assertThrows(NullPointerException.class, () ->
393      {
394       /* final List<String> results = */ vm1.getUndefined(null);
395      }, NULL_POINTER_EXCEPTION
396     );
397    }
398 
399 
400   /**
401    * subst test.
402    */
403   @Test
404   /* default */ 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    * subst test.
416    */
417   @Test
418   /* default */ 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    * subst test.
429    */
430   @Test
431   /* default */ 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    * subst test.
441    */
442   @Test
443   /* default */ 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    * subst test.
453    */
454   @Test
455   /* default */ void testSubst5()
456    {
457     final VariableManager vm1 = new VariableManager();
458     assertThrows(NullPointerException.class, () ->
459      {
460       /* final String result = */ vm1.subst(null);
461      }, NULL_POINTER_EXCEPTION
462     );
463    }
464 
465 
466   /**
467    * parse test.
468    */
469   @Test
470   /* default */ 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    * parse test.
485    */
486   @Test
487   /* default */ 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    * parse test.
502    */
503   @Test
504   /* default */ 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    * parse test.
517    */
518   @Test
519   /* default */ void testParse4()
520    {
521     final VariableManager vm1 = new VariableManager();
522     vm1.setVar(TARGET, DEFAULT);
523     assertThrows(NullPointerException.class, () ->
524      {
525       /* final String result = */ vm1.parse(TARGET, null, false);
526      }, NULL_POINTER_EXCEPTION
527     );
528    }
529 
530 
531   /**
532    * parse test.
533    */
534   @Test
535   /* default */ 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    * parse test.
550    */
551   @Test
552   /* default */ 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       /* final String result = */ vm1.parse(null, TESTVAR, false);
561      }, NULL_POINTER_EXCEPTION
562     );
563    }
564 
565 
566   /**
567    * Test toString.
568    */
569   @Test
570   /* default */ 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"); //$NON-NLS-1$ //$NON-NLS-2$
576    }
577 
578 
579   /**
580    * Test hash code.
581    */
582   @Test
583   /* default */ 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", //$NON-NLS-1$
590       () -> assertEquals(vm1.hashCode(), vm2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
591       () -> assertNotEquals(vm1.hashCode(), vm3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
592     );
593    }
594 
595 
596   /**
597    * Test equals.
598    */
599   @Test
600   @SuppressFBWarnings({"EC_NULL_ARG", "CE_CLASS_ENVY"})
601   @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
602   /* default */ 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", //$NON-NLS-1$
615       () -> assertTrue(vm1.equals(vm1), "VariableManager11 is not equal"), //$NON-NLS-1$
616       () -> assertTrue(vm1.equals(vm2), "VariableManager12 are not equal"), //$NON-NLS-1$
617       () -> assertTrue(vm2.equals(vm1), "VariableManager21 are not equal"), //$NON-NLS-1$
618       () -> assertTrue(vm2.equals(vm4), "VariableManager24 are not equal"), //$NON-NLS-1$
619       () -> assertTrue(vm1.equals(vm4), "VariableManager14 are not equal"), //$NON-NLS-1$
620       () -> assertFalse(vm1.equals(vm3), "VariableManager13 are equal"), //$NON-NLS-1$
621       () -> assertFalse(vm3.equals(vm1), "VariableManager31 are equal"), //$NON-NLS-1$
622       () -> assertFalse(vm1.equals(null), "VariableManager10 is equal"), //$NON-NLS-1$
623       () -> assertFalse(vm1.equals(vm5), "VariableManager15 is equal"), //$NON-NLS-1$
624       () -> assertFalse(vm1.equals(vm6), "VariableManager16 is equal") //$NON-NLS-1$
625     );
626    }
627 
628  }