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.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
24
25 final class BlockManagerTests
26 {
27
28
29
30 private static final String NOT_CONSTRUCTED = "Not constructed";
31
32
33
34
35 private static final String PARENT = "parent";
36
37
38
39
40 private static final String BLKSTRING = "before<!-- BEGIN blktest -->content<!-- END blktest -->after";
41
42
43
44
45 private static final String BLKTEST = "blktest";
46
47
48
49
50 private static final String BLKNAME = "blkname";
51
52
53
54
55 private static final String BLOCK_COULD_NOT_BE_SET = "Block could not be set";
56
57
58
59
60 private static final String PARENT_NOT_AS_EXPECTED = "parent not as expected";
61
62
63
64
65 private static final String CONTENT = "content";
66
67
68
69
70 private static final String BLKTEST_NOT_AS_EXPECTED = "blktest not as expected";
71
72
73
74
75 private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
76
77
78
79
80 private static final String BEFORE_BLKTEST_AFTER = "before{blktest}after";
81
82
83
84
85
86 BlockManagerTests()
87 {
88 super();
89 }
90
91
92
93
94
95 @Test
96 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
106
107 @Test
108 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
119
120 @Test
121 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
137
138 @Test
139 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 bm1.setBlock(null, BLKTEST, BLKNAME);
147 }, NULL_POINTER_EXCEPTION
148 );
149 }
150
151
152
153
154
155 @Test
156 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 bm1.setBlock(PARENT, null, BLKNAME);
164 }, NULL_POINTER_EXCEPTION
165 );
166 }
167
168
169
170
171
172 @Test
173 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
189
190 @Test
191 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 bm1.setBlock("", BLKTEST, BLKNAME);
199 }, NULL_POINTER_EXCEPTION
200 );
201 }
202
203
204
205
206
207 @Test
208 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 bm1.setBlock(PARENT, "", BLKNAME);
216 }, NULL_POINTER_EXCEPTION
217 );
218 }
219
220
221
222
223
224 @Test
225 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
241
242 @Test
243 void testToString()
244 {
245 final VariableManager vm1 = new VariableManager();
246 final BlockManager bm1 = new BlockManager(vm1);
247 vm1.setVar(PARENT, BLKSTRING);
248 bm1.setBlock(PARENT, BLKTEST, BLKNAME);
249 final String string = bm1.toString();
250 assertEquals("BlockManager[vars=[parent, blktest]]", string, "toString() result not as expected");
251 }
252
253
254
255
256
257 @Test
258 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",
267 () -> assertEquals(bm1.hashCode(), bm2.hashCode(), "hashCodes are not equal"),
268 () -> assertNotEquals(bm1.hashCode(), bm3.hashCode(), "hashCodes are equal")
269 );
270 }
271
272
273
274
275
276 @Test
277 @SuppressFBWarnings("EC_NULL_ARG")
278 @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
279 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",
295 () -> assertTrue(bm1.equals(bm1), "BlockManager11 is not equal"),
296 () -> assertTrue(bm1.equals(bm2), "BlockManager12 are not equal"),
297 () -> assertTrue(bm2.equals(bm1), "BlockManager21 are not equal"),
298 () -> assertTrue(bm2.equals(bm4), "BlockManager24 are not equal"),
299 () -> assertTrue(bm1.equals(bm4), "BlockManager14 are not equal"),
300 () -> assertFalse(bm1.equals(bm3), "BlockManager13 are equal"),
301 () -> assertFalse(bm3.equals(bm1), "BlockManager31 are equal"),
302 () -> assertFalse(bm1.equals(null), "BlockManager10 is equal"),
303 () -> assertFalse(bm1.equals(bm5), "BlockManager15 is equal"),
304 () -> assertFalse(bm1.equals(bm6), "BlockManager16 is equal")
305 );
306 }
307
308 }