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.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotEquals;
11  import static org.junit.jupiter.api.Assertions.assertNotNull;
12  import static org.junit.jupiter.api.Assertions.assertThrows;
13  import static org.junit.jupiter.api.Assertions.assertTrue;
14  
15  import org.junit.jupiter.api.Test;
16  
17  import de.powerstat.phplib.templateengine.intern.BlockManager;
18  import de.powerstat.phplib.templateengine.intern.VariableManager;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * Block manager tests.
24   */
25  final class BlockManagerTests
26   {
27    /**
28     * Not constructed constant.
29     */
30    private static final String NOT_CONSTRUCTED = "Not constructed";
31  
32    /**
33     * Parent constant.
34     */
35    private static final String PARENT = "parent";
36  
37    /**
38     * Block string constant.
39     */
40    private static final String BLKSTRING = "before<!-- BEGIN blktest -->content<!-- END blktest -->after";
41  
42    /**
43     * Block test constant.
44     */
45    private static final String BLKTEST = "blktest";
46  
47    /**
48     * Block name constant.
49     */
50    private static final String BLKNAME = "blkname";
51  
52    /**
53     * Block could not be set constant.
54     */
55    private static final String BLOCK_COULD_NOT_BE_SET = "Block could not be set";
56  
57    /**
58     * Parent not as expected constant.
59     */
60    private static final String PARENT_NOT_AS_EXPECTED = "parent not as expected";
61  
62    /**
63     * Content constant.
64     */
65    private static final String CONTENT = "content";
66  
67    /**
68     * Block test not as expected.
69     */
70    private static final String BLKTEST_NOT_AS_EXPECTED = "blktest not as expected";
71  
72    /**
73     * Null pointer exception constant.
74     */
75    private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
76  
77    /**
78     * Before blktest after constant.
79     */
80    private static final String BEFORE_BLKTEST_AFTER = "before{blktest}after";
81  
82  
83    /**
84     * Default constructor.
85     */
86    /* default */ BlockManagerTests()
87     {
88      super();
89     }
90  
91  
92    /**
93     * Constructor test.
94     */
95    @Test
96    /* default */ void testConstructor1()
97     {
98      final VariableManager vm1 = new VariableManager();
99      final BlockManager bm1 = new BlockManager(vm1);
100     assertNotNull(bm1, NOT_CONSTRUCTED);
101    }
102 
103 
104   /**
105    * Constructor test.
106    */
107   @Test
108   /* default */ void testConstructor2()
109    {
110     final VariableManager vm1 = new VariableManager();
111     final BlockManager bm1 = new BlockManager(vm1);
112     final BlockManager bm2 = new BlockManager(vm1, bm1);
113     assertNotNull(bm2, NOT_CONSTRUCTED);
114    }
115 
116 
117   /**
118    * Set block test.
119    */
120   @Test
121   /* default */ void testSetBlock1()
122    {
123     final VariableManager vm1 = new VariableManager();
124     final BlockManager bm1 = new BlockManager(vm1);
125     vm1.setVar(PARENT, BLKSTRING);
126     final boolean result = bm1.setBlock(PARENT, BLKTEST, BLKNAME);
127     assertTrue(result, BLOCK_COULD_NOT_BE_SET);
128     final String parent = vm1.getVar(PARENT);
129     final String blktest = vm1.getVar(BLKTEST);
130     assertEquals("before{blkname}after", parent, PARENT_NOT_AS_EXPECTED);
131     assertEquals(CONTENT, blktest, BLKTEST_NOT_AS_EXPECTED);
132    }
133 
134 
135   /**
136    * Set block test.
137    */
138   @Test
139   /* default */ void testSetBlock2()
140    {
141     final VariableManager vm1 = new VariableManager();
142     final BlockManager bm1 = new BlockManager(vm1);
143     vm1.setVar(PARENT, BLKSTRING);
144     assertThrows(NullPointerException.class, () ->
145      {
146       /* final boolean result = */ bm1.setBlock(null, BLKTEST, BLKNAME);
147      }, NULL_POINTER_EXCEPTION
148     );
149    }
150 
151 
152   /**
153    * Set block test.
154    */
155   @Test
156   /* default */ void testSetBlock3()
157    {
158     final VariableManager vm1 = new VariableManager();
159     final BlockManager bm1 = new BlockManager(vm1);
160     vm1.setVar(PARENT, BLKSTRING);
161     assertThrows(IllegalStateException.class, () ->
162      {
163       /* final boolean result = */ bm1.setBlock(PARENT, null, BLKNAME);
164      }, NULL_POINTER_EXCEPTION
165     );
166    }
167 
168 
169   /**
170    * Set block test.
171    */
172   @Test
173   /* default */ void testSetBlock4()
174    {
175     final VariableManager vm1 = new VariableManager();
176     final BlockManager bm1 = new BlockManager(vm1);
177     vm1.setVar(PARENT, BLKSTRING);
178     final boolean result = bm1.setBlock(PARENT, BLKTEST, null);
179     assertTrue(result, BLOCK_COULD_NOT_BE_SET);
180     final String parent = vm1.getVar(PARENT);
181     final String blktest = vm1.getVar(BLKTEST);
182     assertEquals(BEFORE_BLKTEST_AFTER, parent, PARENT_NOT_AS_EXPECTED);
183     assertEquals(CONTENT, blktest, BLKTEST_NOT_AS_EXPECTED);
184    }
185 
186 
187   /**
188    * Set block test.
189    */
190   @Test
191   /* default */ void testSetBlock5()
192    {
193     final VariableManager vm1 = new VariableManager();
194     final BlockManager bm1 = new BlockManager(vm1);
195     vm1.setVar(PARENT, BLKSTRING);
196     assertThrows(IllegalStateException.class, () ->
197      {
198       /* final boolean result = */ bm1.setBlock("", BLKTEST, BLKNAME);
199      }, NULL_POINTER_EXCEPTION
200     );
201    }
202 
203 
204   /**
205    * Set block test.
206    */
207   @Test
208   /* default */ void testSetBlock6()
209    {
210     final VariableManager vm1 = new VariableManager();
211     final BlockManager bm1 = new BlockManager(vm1);
212     vm1.setVar(PARENT, BLKSTRING);
213     assertThrows(IllegalStateException.class, () ->
214      {
215       /* final boolean result = */ bm1.setBlock(PARENT, "", BLKNAME);
216      }, NULL_POINTER_EXCEPTION
217     );
218    }
219 
220 
221   /**
222    * Set block test.
223    */
224   @Test
225   /* default */ void testSetBlock7()
226    {
227     final VariableManager vm1 = new VariableManager();
228     final BlockManager bm1 = new BlockManager(vm1);
229     vm1.setVar(PARENT, BLKSTRING);
230     final boolean result = bm1.setBlock(PARENT, BLKTEST, "");
231     assertTrue(result, BLOCK_COULD_NOT_BE_SET);
232     final String parent = vm1.getVar(PARENT);
233     final String blktest = vm1.getVar(BLKTEST);
234     assertEquals(BEFORE_BLKTEST_AFTER, parent, PARENT_NOT_AS_EXPECTED);
235     assertEquals(CONTENT, blktest, BLKTEST_NOT_AS_EXPECTED);
236    }
237 
238 
239   /**
240    * Test toString.
241    */
242   @Test
243   /* default */ void testToString()
244    {
245     final VariableManager vm1 = new VariableManager();
246     final BlockManager bm1 = new BlockManager(vm1);
247     vm1.setVar(PARENT, BLKSTRING);
248     /* final boolean result = */ bm1.setBlock(PARENT, BLKTEST, BLKNAME);
249     final String string = bm1.toString();
250     assertEquals("BlockManager[vars=[parent, blktest]]", string, "toString() result not as expected"); //$NON-NLS-1$ //$NON-NLS-2$
251    }
252 
253 
254   /**
255    * Test hash code.
256    */
257   @Test
258   /* default */ void testHashCode()
259    {
260     final VariableManager vm1 = new VariableManager();
261     vm1.setVar(PARENT, BLKSTRING);
262     final VariableManager vm2 = new VariableManager();
263     final BlockManager bm1 = new BlockManager(vm1);
264     final BlockManager bm2 = new BlockManager(vm1);
265     final BlockManager bm3 = new BlockManager(vm2);
266     assertAll("testHashCode", //$NON-NLS-1$
267       () -> assertEquals(bm1.hashCode(), bm2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
268       () -> assertNotEquals(bm1.hashCode(), bm3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
269     );
270    }
271 
272 
273   /**
274    * Test equals.
275    */
276   @Test
277   @SuppressFBWarnings("EC_NULL_ARG")
278   @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
279   /* default */ void testEquals()
280    {
281     final VariableManager vm1 = new VariableManager();
282     final VariableManager vm2 = new VariableManager();
283     vm2.setVar(PARENT, BLKSTRING);
284     final VariableManager vm3 = new VariableManager();
285     vm3.setVar("parent2", "before<!-- BEGIN blktest2 -->content<!-- END blktest2 -->after");
286     final VariableManager vm4 = new VariableManager();
287     vm4.setVar("parent3", "before<!-- BEGIN blktest3 -->content<!-- END blktest3 -->after");
288     final BlockManager bm1 = new BlockManager(vm1);
289     final BlockManager bm2 = new BlockManager(vm1);
290     final BlockManager bm3 = new BlockManager(vm2);
291     final BlockManager bm4 = new BlockManager(vm1);
292     final BlockManager bm5 = new BlockManager(vm3);
293     final BlockManager bm6 = new BlockManager(vm4);
294     assertAll("testEquals", //$NON-NLS-1$
295       () -> assertTrue(bm1.equals(bm1), "BlockManager11 is not equal"), //$NON-NLS-1$
296       () -> assertTrue(bm1.equals(bm2), "BlockManager12 are not equal"), //$NON-NLS-1$
297       () -> assertTrue(bm2.equals(bm1), "BlockManager21 are not equal"), //$NON-NLS-1$
298       () -> assertTrue(bm2.equals(bm4), "BlockManager24 are not equal"), //$NON-NLS-1$
299       () -> assertTrue(bm1.equals(bm4), "BlockManager14 are not equal"), //$NON-NLS-1$
300       () -> assertFalse(bm1.equals(bm3), "BlockManager13 are equal"), //$NON-NLS-1$
301       () -> assertFalse(bm3.equals(bm1), "BlockManager31 are equal"), //$NON-NLS-1$
302       () -> assertFalse(bm1.equals(null), "BlockManager10 is equal"), //$NON-NLS-1$
303       () -> assertFalse(bm1.equals(bm5), "BlockManager15 is equal"), //$NON-NLS-1$
304       () -> assertFalse(bm1.equals(bm6), "BlockManager16 is equal") //$NON-NLS-1$
305     );
306    }
307 
308  }