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 java.io.File;
16 import java.io.IOException;
17
18 import org.junit.jupiter.api.Test;
19
20 import de.powerstat.phplib.templateengine.intern.FileManager;
21 import de.powerstat.phplib.templateengine.intern.VariableManager;
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24
25
26
27
28 final class FileManagerTests
29 {
30
31
32
33 private static final String NOT_CONSTRUCTED = "Not constructed";
34
35
36
37
38 private static final String FILENAME1 = "filename1";
39
40
41
42
43 private static final String TEMPLATE1_FILE = "target/test-classes/templates/template1.tmpl";
44
45
46
47
48 private static final String ADD_FILE_FAILED = "addFile failed";
49
50
51
52
53 private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
54
55
56
57
58 private static final String EXISTS_FILE_SUCCEEDED = "existsFile succeeded";
59
60
61
62
63 private static final String EXISTS_FILE_FAILED = "existsFile failed";
64
65
66
67
68 private static final String TO_STRING_RESULT_NOT_AS_EXPECTED = "toString() result not as expected";
69
70
71
72
73
74 FileManagerTests()
75 {
76 super();
77 }
78
79
80
81
82
83 @Test
84 void testConstructor1()
85 {
86 final VariableManager vm1 = new VariableManager();
87 final FileManager fm1 = new FileManager(vm1);
88 assertNotNull(fm1, NOT_CONSTRUCTED);
89 }
90
91
92
93
94
95 @Test
96 void testConstructor2()
97 {
98 final VariableManager vm1 = new VariableManager();
99 final FileManager fm1 = new FileManager(vm1);
100 final FileManager fm2 = new FileManager(vm1, fm1);
101 assertNotNull(fm2, NOT_CONSTRUCTED);
102 }
103
104
105
106
107
108 @Test
109 void testAddFile1()
110 {
111 final VariableManager vm1 = new VariableManager();
112 final FileManager fm1 = new FileManager(vm1);
113
114 final boolean result = fm1.addFile(FILENAME1, new File(TEMPLATE1_FILE));
115 assertTrue(result, ADD_FILE_FAILED);
116 }
117
118
119
120
121
122 @Test
123 void testAddFile2()
124 {
125 final VariableManager vm1 = new VariableManager();
126 final FileManager fm1 = new FileManager(vm1);
127 final File tmplFile = new File(TEMPLATE1_FILE);
128 assertThrows(NullPointerException.class, () ->
129 {
130 fm1.addFile(null, tmplFile);
131 }, NULL_POINTER_EXCEPTION
132 );
133 }
134
135
136
137
138
139 @Test
140 void testAddFile3()
141 {
142 final VariableManager vm1 = new VariableManager();
143 final FileManager fm1 = new FileManager(vm1);
144
145 final boolean result = fm1.addFile("", new File(TEMPLATE1_FILE));
146 assertTrue(result, ADD_FILE_FAILED);
147 }
148
149
150
151
152
153 @Test
154 void testAddFile4()
155 {
156 final VariableManager vm1 = new VariableManager();
157 final FileManager fm1 = new FileManager(vm1);
158
159 final boolean result = fm1.addFile(FILENAME1, new File("target/test-classes/templates/template0.tmpl"));
160 assertFalse(result, "addFile succeeded");
161 }
162
163
164
165
166
167 @Test
168 void testAddFile5()
169 {
170 final VariableManager vm1 = new VariableManager();
171 final FileManager fm1 = new FileManager(vm1);
172
173 assertThrows(NullPointerException.class, () ->
174 {
175 fm1.addFile(FILENAME1, null);
176 }, NULL_POINTER_EXCEPTION
177 );
178 }
179
180
181
182
183
184 @Test
185 void testExistsFile1()
186 {
187 final VariableManager vm1 = new VariableManager();
188 final FileManager fm1 = new FileManager(vm1);
189
190 final boolean result = fm1.existsFile(FILENAME1);
191 assertFalse(result, EXISTS_FILE_SUCCEEDED);
192 }
193
194
195
196
197
198 @Test
199 void testExistsFile2()
200 {
201 final VariableManager vm1 = new VariableManager();
202 final FileManager fm1 = new FileManager(vm1);
203 fm1.addFile(FILENAME1, new File(TEMPLATE1_FILE));
204
205 final boolean result = fm1.existsFile(FILENAME1);
206 assertTrue(result, EXISTS_FILE_FAILED);
207 }
208
209
210
211
212
213 @Test
214 void testExistsFile3()
215 {
216 final VariableManager vm1 = new VariableManager();
217 final FileManager fm1 = new FileManager(vm1);
218
219 final boolean result = fm1.existsFile("");
220 assertFalse(result, EXISTS_FILE_SUCCEEDED);
221 }
222
223
224
225
226
227 @Test
228 void testExistsFile4()
229 {
230 final VariableManager vm1 = new VariableManager();
231 final FileManager fm1 = new FileManager(vm1);
232 fm1.addFile("", new File(TEMPLATE1_FILE));
233
234 final boolean result = fm1.existsFile("");
235 assertTrue(result, EXISTS_FILE_FAILED);
236 }
237
238
239
240
241
242 @Test
243 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
244 void testExistsFile5()
245 {
246 final VariableManager vm1 = new VariableManager();
247 final FileManager fm1 = new FileManager(vm1);
248
249 assertThrows(NullPointerException.class, () ->
250 {
251 fm1.existsFile(null);
252 }, NULL_POINTER_EXCEPTION
253 );
254 }
255
256
257
258
259
260
261
262 @Test
263 void testLoadFile1() throws IOException
264 {
265 final VariableManager vm1 = new VariableManager();
266 final FileManager fm1 = new FileManager(vm1);
267
268 final boolean result = fm1.loadFile(FILENAME1);
269 assertFalse(result, EXISTS_FILE_SUCCEEDED);
270 }
271
272
273
274
275
276
277
278 @Test
279 void testLoadFile2() throws IOException
280 {
281 final VariableManager vm1 = new VariableManager();
282 final FileManager fm1 = new FileManager(vm1);
283
284 final boolean result = fm1.loadFile("");
285 assertFalse(result, EXISTS_FILE_SUCCEEDED);
286 }
287
288
289
290
291
292 @Test
293 void testLoadFile3()
294 {
295 final VariableManager vm1 = new VariableManager();
296 final FileManager fm1 = new FileManager(vm1);
297
298 assertThrows(NullPointerException.class, () ->
299 {
300 fm1.loadFile(null);
301 }, NULL_POINTER_EXCEPTION
302 );
303 }
304
305
306
307
308
309
310
311 @Test
312 void testLoadFile4() throws IOException
313 {
314 final VariableManager vm1 = new VariableManager();
315 final FileManager fm1 = new FileManager(vm1);
316 fm1.addFile(FILENAME1, new File(TEMPLATE1_FILE));
317
318 final boolean result = fm1.loadFile(FILENAME1);
319 assertTrue(result, "loadFile failed");
320 }
321
322
323
324
325
326 @Test
327 void testToString1()
328 {
329 final VariableManager vm1 = new VariableManager();
330 final FileManager fm1 = new FileManager(vm1);
331
332 final String string = fm1.toString();
333 assertEquals("FileManager[files=Optional.empty]", string, TO_STRING_RESULT_NOT_AS_EXPECTED);
334 }
335
336
337
338
339
340 @Test
341 void testToString2()
342 {
343 final VariableManager vm1 = new VariableManager();
344 final FileManager fm1 = new FileManager(vm1);
345 fm1.addFile(FILENAME1, new File(TEMPLATE1_FILE));
346
347 final String string = fm1.toString();
348 assertEquals("FileManager[files=Optional[template1.tmpl]]", string, TO_STRING_RESULT_NOT_AS_EXPECTED);
349 }
350
351
352
353
354
355 @Test
356 void testHashCode()
357 {
358 final VariableManager vm1 = new VariableManager();
359 final FileManager fm1 = new FileManager(vm1);
360 final FileManager fm2 = new FileManager(vm1);
361 final FileManager fm3 = new FileManager(vm1);
362 fm3.addFile(FILENAME1, new File(TEMPLATE1_FILE));
363 assertAll("testHashCode",
364 () -> assertEquals(fm1.hashCode(), fm2.hashCode(), "hashCodes are not equal"),
365 () -> assertNotEquals(fm1.hashCode(), fm3.hashCode(), "hashCodes are equal")
366 );
367 }
368
369
370
371
372
373 @Test
374 @SuppressFBWarnings("EC_NULL_ARG")
375 @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
376 void testEquals()
377 {
378 final VariableManager vm1 = new VariableManager();
379 final VariableManager vm2 = new VariableManager();
380 vm2.setVar("test1", "test");
381 final VariableManager vm3 = new VariableManager();
382 final VariableManager vm4 = new VariableManager();
383 final FileManager fm1 = new FileManager(vm1);
384 final FileManager fm2 = new FileManager(vm1);
385 final FileManager fm3 = new FileManager(vm2);
386 final FileManager fm4 = new FileManager(vm1);
387 final FileManager fm5 = new FileManager(vm3);
388 fm5.addFile(FILENAME1, new File(TEMPLATE1_FILE));
389 final FileManager fm6 = new FileManager(vm4);
390 fm6.addFile("filename2", new File("target/test-classes/templates/template2.tmpl"));
391 assertAll("testEquals",
392 () -> assertTrue(fm1.equals(fm1), "FileManager11 is not equal"),
393 () -> assertTrue(fm1.equals(fm2), "FileManager12 are not equal"),
394 () -> assertTrue(fm2.equals(fm1), "FileManager21 are not equal"),
395 () -> assertTrue(fm2.equals(fm4), "FileManager24 are not equal"),
396 () -> assertTrue(fm1.equals(fm4), "FileManager14 are not equal"),
397 () -> assertFalse(fm1.equals(fm3), "FileManager13 are equal"),
398 () -> assertFalse(fm3.equals(fm1), "FileManager31 are equal"),
399 () -> assertFalse(fm1.equals(null), "FileManager10 is equal"),
400 () -> assertFalse(fm1.equals(fm5), "FileManager15 is equal"),
401 () -> assertFalse(fm1.equals(fm6), "FileManager16 is equal")
402 );
403 }
404
405 }