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 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   * File manager tests.
27   */
28  final class FileManagerTests
29   {
30    /**
31     * Not constructed constant.
32     */
33    private static final String NOT_CONSTRUCTED = "Not constructed";
34  
35    /**
36     * Filename1 constant.
37     */
38    private static final String FILENAME1 = "filename1";
39  
40    /**
41     * Template 1 file name constant.
42     */
43    private static final String TEMPLATE1_FILE = "target/test-classes/templates/template1.tmpl";
44  
45    /**
46     * Add file failed constant.
47     */
48    private static final String ADD_FILE_FAILED = "addFile failed";
49  
50    /**
51     * Null pointer exception constant.
52     */
53    private static final String NULL_POINTER_EXCEPTION = "Null pointer exception";
54  
55    /**
56     * Exists file succeeded constant.
57     */
58    private static final String EXISTS_FILE_SUCCEEDED = "existsFile succeeded";
59  
60    /**
61     * Exists file failed constant.
62     */
63    private static final String EXISTS_FILE_FAILED = "existsFile failed";
64  
65    /**
66     * toSTring result not as expected constant.
67     */
68    private static final String TO_STRING_RESULT_NOT_AS_EXPECTED = "toString() result not as expected";
69  
70  
71    /**
72     * Default constructor.
73     */
74    /* default */ FileManagerTests()
75     {
76      super();
77     }
78  
79  
80    /**
81     * Constructor test.
82     */
83    @Test
84    /* default */ 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     * Constructor test.
94     */
95    @Test
96    /* default */ 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    * addFile test.
107    */
108   @Test
109   /* default */ 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    * addFile test.
121    */
122   @Test
123   /* default */ 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       /* final boolean result = */ fm1.addFile(null, tmplFile);
131      }, NULL_POINTER_EXCEPTION
132     );
133    }
134 
135 
136   /**
137    * addFile test.
138    */
139   @Test
140   /* default */ 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    * addFile test.
152    */
153   @Test
154   /* default */ 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    * addFile test.
166    */
167   @Test
168   /* default */ void testAddFile5()
169    {
170     final VariableManager vm1 = new VariableManager();
171     final FileManager fm1 = new FileManager(vm1);
172 
173     assertThrows(NullPointerException.class, () ->
174      {
175       /* final boolean result = */ fm1.addFile(FILENAME1, null);
176      }, NULL_POINTER_EXCEPTION
177     );
178    }
179 
180 
181   /**
182    * existsFile test.
183    */
184   @Test
185   /* default */ 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    * existsFile test.
197    */
198   @Test
199   /* default */ void testExistsFile2()
200    {
201     final VariableManager vm1 = new VariableManager();
202     final FileManager fm1 = new FileManager(vm1);
203     /* boolean result = */ 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    * existsFile test.
212    */
213   @Test
214   /* default */ 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    * existsFile test.
226    */
227   @Test
228   /* default */ void testExistsFile4()
229    {
230     final VariableManager vm1 = new VariableManager();
231     final FileManager fm1 = new FileManager(vm1);
232     /* boolean result = */ fm1.addFile("", new File(TEMPLATE1_FILE));
233 
234     final boolean result = fm1.existsFile("");
235     assertTrue(result, EXISTS_FILE_FAILED);
236    }
237 
238 
239   /**
240    * existsFile test.
241    */
242   @Test
243   @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
244   /* default */ void testExistsFile5()
245    {
246     final VariableManager vm1 = new VariableManager();
247     final FileManager fm1 = new FileManager(vm1);
248 
249     assertThrows(NullPointerException.class, () ->
250      {
251       /* final boolean result = */ fm1.existsFile(null);
252      }, NULL_POINTER_EXCEPTION
253     );
254    }
255 
256 
257   /**
258    * loadFile test.
259    *
260    * @throws IOException IO exception
261    */
262   @Test
263   /* default */ 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    * loadFile test.
275    *
276    * @throws IOException IO exception
277    */
278   @Test
279   /* default */ 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    * loadFile test.
291    */
292   @Test
293   /* default */ void testLoadFile3()
294    {
295     final VariableManager vm1 = new VariableManager();
296     final FileManager fm1 = new FileManager(vm1);
297 
298     assertThrows(NullPointerException.class, () ->
299      {
300       /* final boolean result = */ fm1.loadFile(null);
301      }, NULL_POINTER_EXCEPTION
302     );
303    }
304 
305 
306   /**
307    * loadFile test.
308    *
309    * @throws IOException IO exception
310    */
311   @Test
312   /* default */ void testLoadFile4() throws IOException
313    {
314     final VariableManager vm1 = new VariableManager();
315     final FileManager fm1 = new FileManager(vm1);
316     /* boolean result = */ 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    * Test toString.
325    */
326   @Test
327   /* default */ 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); //$NON-NLS-1$
334    }
335 
336 
337   /**
338    * Test toString.
339    */
340   @Test
341   /* default */ void testToString2()
342    {
343     final VariableManager vm1 = new VariableManager();
344     final FileManager fm1 = new FileManager(vm1);
345     /* final boolean result = */ 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); //$NON-NLS-1$
349    }
350 
351 
352   /**
353    * Test hash code.
354    */
355   @Test
356   /* default */ 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     /* final boolean result = */ fm3.addFile(FILENAME1, new File(TEMPLATE1_FILE));
363     assertAll("testHashCode", //$NON-NLS-1$
364       () -> assertEquals(fm1.hashCode(), fm2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
365       () -> assertNotEquals(fm1.hashCode(), fm3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
366     );
367    }
368 
369 
370   /**
371    * Test equals.
372    */
373   @Test
374   @SuppressFBWarnings("EC_NULL_ARG")
375   @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
376   /* default */ 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     /* final boolean result = */ fm5.addFile(FILENAME1, new File(TEMPLATE1_FILE));
389     final FileManager fm6 = new FileManager(vm4);
390     /* final boolean result = */ fm6.addFile("filename2", new File("target/test-classes/templates/template2.tmpl"));
391     assertAll("testEquals", //$NON-NLS-1$
392       () -> assertTrue(fm1.equals(fm1), "FileManager11 is not equal"), //$NON-NLS-1$
393       () -> assertTrue(fm1.equals(fm2), "FileManager12 are not equal"), //$NON-NLS-1$
394       () -> assertTrue(fm2.equals(fm1), "FileManager21 are not equal"), //$NON-NLS-1$
395       () -> assertTrue(fm2.equals(fm4), "FileManager24 are not equal"), //$NON-NLS-1$
396       () -> assertTrue(fm1.equals(fm4), "FileManager14 are not equal"), //$NON-NLS-1$
397       () -> assertFalse(fm1.equals(fm3), "FileManager13 are equal"), //$NON-NLS-1$
398       () -> assertFalse(fm3.equals(fm1), "FileManager31 are equal"), //$NON-NLS-1$
399       () -> assertFalse(fm1.equals(null), "FileManager10 is equal"), //$NON-NLS-1$
400       () -> assertFalse(fm1.equals(fm5), "FileManager15 is equal"), //$NON-NLS-1$
401       () -> assertFalse(fm1.equals(fm6), "FileManager16 is equal") //$NON-NLS-1$
402     );
403    }
404 
405  }