View Javadoc
1   /*
2    * Copyright (C) 2019-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.phplib.templateengine.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.FileNotFoundException;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.nio.charset.StandardCharsets;
20  import java.nio.file.Files;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.junit.jupiter.api.Test;
27  
28  import de.powerstat.phplib.templateengine.HandleUndefined;
29  import de.powerstat.phplib.templateengine.TemplateEngine;
30  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31  
32  
33  /**
34   * TemplateEngine tests.
35   */
36  @SuppressFBWarnings({"CE_CLASS_ENVY", "EC_NULL_ARG"})
37  @SuppressWarnings({"PMD.LinguisticNaming", "PMD.ExcessiveClassLength", "PMD.LongVariable", "PMD.ExcessivePublicCount"})
38  final class TemplateEngineTests
39   {
40    /**
41     * Logger.
42     */
43    private static final Logger LOGGER = LogManager.getLogger(TemplateEngineTests.class);
44  
45    /**
46     * Template file 1 path constant.
47     */
48    private static final String TEMPLATE1_TMPL = "target/test-classes/templates/template1.tmpl"; //$NON-NLS-1$
49  
50    /**
51     * Template file 2 path constant.
52     */
53    private static final String TEMPLATE2_TMPL = "target/test-classes/templates/template2.tmpl"; //$NON-NLS-1$
54  
55    /**
56     * Template file 3 path constant.
57     */
58    private static final String TEMPLATE3_TMPL = "target/test-classes/templates/template3.tmpl";
59  
60    /**
61     * Template 4 path.
62     */
63    private static final String TEMPLATE4_TMPL = "target/test-classes/templates/template4.tmpl"; //$NON-NLS-1$
64  
65    /**
66     * Template 8 path.
67     */
68    private static final String TEMPLATE8_TMPL = "target/test-classes/templates/template8.tmpl"; //$NON-NLS-1$
69  
70    /**
71     * Template 9 path (maximum file size).
72     */
73    private static final String TEMPLATE9_TMPL = "target/test-classes/templates/template9.tmpl"; //$NON-NLS-1$
74  
75    /**
76     * Template 10 path (file to large).
77     */
78    private static final String TEMPLATE10_TMPL = "target/test-classes/templates/template10.tmpl"; //$NON-NLS-1$
79  
80    /**
81     * Illegal argument exception expected.
82     */
83    private static final String ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED = "Illegal argument exception expected"; //$NON-NLS-1$
84  
85    /**
86     * Assertion error expected message.
87     */
88    private static final String ASSERTION_ERROR_EXPECTED = "Assertion error expected"; //$NON-NLS-1$
89  
90    /**
91     * Found more or less undefined variables message.
92     */
93    private static final String FOUND_MORE_OR_LESS_UNDEFINED_VARIABLES = "Found more or less undefined variables"; //$NON-NLS-1$
94  
95    /**
96     * Not found expected undefined variable message.
97     */
98    private static final String NOT_FOUND_EXPECTED_UNDEFINED_VARIABLE = "Not found expected undefined variable"; //$NON-NLS-1$
99  
100   /**
101    * Template file 1 name constant.
102    */
103   private static final String FILE1 = "file1"; //$NON-NLS-1$
104 
105   /**
106    * Template file 2 name constant.
107    */
108   private static final String FILE2 = "file2"; //$NON-NLS-1$
109 
110   /**
111    * Template file 3 name constant.
112    */
113   private static final String FILE3 = "file3"; //$NON-NLS-1$
114 
115   /**
116    * Template file 4 name constant.
117    */
118   private static final String FILE4 = "file4"; //$NON-NLS-1$
119 
120   /**
121    * Template variable 1 name constant.
122    */
123   private static final String VARIABLE1 = "variable1"; //$NON-NLS-1$
124 
125   /**
126    * Template variable 2 name constant.
127    */
128   private static final String VARIABLE2 = "variable2"; //$NON-NLS-1$
129 
130   /**
131    * Template variable 3 name constant.
132    */
133   private static final String VARIABLE3 = "variable3"; //$NON-NLS-1$
134 
135   /**
136    * Template variable 4 name constant with illegal character.
137    */
138   private static final String VARIABLE4 = "file~"; //$NON-NLS-1$
139 
140   /**
141    * Test 0.
142    */
143   private static final String TEST0 = "test0";
144 
145   /**
146    * Value 1.
147    */
148   private static final String VALUE1 = "TEST1"; //$NON-NLS-1$
149 
150   /**
151    * Value 2.
152    */
153   private static final String VALUE2 = "TEST2"; //$NON-NLS-1$
154 
155   /**
156    * Value 3.
157    */
158   private static final String VALUE3 = "TEST3"; //$NON-NLS-1$
159 
160   /**
161    * Maximum allowed varname.
162    */
163   private static final String MAX_VARNAME = "1234567890123456789012345678901234567890123456789012345678901234"; //$NON-NLS-1$
164 
165   /**
166    * To long varname.
167    */
168   private static final String TO_LONG_VARNAME = "12345678901234567890123456789012345678901234567890123456789012345"; //$NON-NLS-1$
169 
170   /**
171    * Template variable value constant.
172    */
173   private static final String TEST = "TEST"; //$NON-NLS-1$
174 
175   /**
176    * Template block 1 block name constant.
177    */
178   private static final String BLK1_BLK = "BLK1_BLK"; //$NON-NLS-1$
179 
180   /**
181    * Template block 1 name constant.
182    */
183   private static final String BLK1 = "BLK1"; //$NON-NLS-1$
184 
185   /**
186    * Template output variable name constant.
187    */
188   private static final String OUTPUT = "output"; //$NON-NLS-1$
189 
190   /**
191    * Template constant.
192    */
193   private static final String TEMPLATE = "template"; //$NON-NLS-1$
194 
195   /**
196    * No template found message.
197    */
198   private static final String NO_TEMPLATE_FOUND = "No 'template' found"; //$NON-NLS-1$
199 
200   /**
201    * Variable value not as expected message.
202    */
203   private static final String VARIABLE_VALUE_NOT_AS_EXPECTED = "Variable value not as expected"; //$NON-NLS-1$
204 
205   /**
206    * Set block not as expected message.
207    */
208   private static final String SET_BLOCK_NOT_AS_EXPECTED = "SetBlock not as expected"; //$NON-NLS-1$
209 
210   /**
211    * Template file template1.tmpl could not be loaded message.
212    */
213   private static final String TEMPLATE_FILE_TEMPLATE1_TMPL_COULD_NOT_BE_LOADED = "Template file template1.tmpl could not be loaded!"; //$NON-NLS-1$
214 
215   /**
216    * Equals block.
217    */
218   private static final String EQUALS_123_VARIABLE1_456 = "123\n{variable1}\n456\n"; //$NON-NLS-1$
219 
220   /**
221    * Undefined variable(s) found message.
222    */
223   private static final String UNDEFINED_VARIABLE_S_FOUND = "Undefined variable(s) found!"; //$NON-NLS-1$
224 
225   /**
226    * Output not as expected message.
227    */
228   private static final String OUTPUT_NOT_AS_EXPECTED = "Output not as expected"; //$NON-NLS-1$
229 
230   /**
231    * Illegal state exception expected message.
232    */
233   private static final String ILLEGAL_STATE_EXCEPTION_EXPECTED = "Illegal state exception expected"; //$NON-NLS-1$
234 
235   /**
236    * Block value not as expected message.
237    */
238   private static final String BLOCK_VALUE_NOT_AS_EXPECTED = "Block value not as expected"; //$NON-NLS-1$
239 
240   /**
241    * Block test value.
242    */
243   private static final String VALUE_1234 = "1234"; //$NON-NLS-1$
244 
245   /**
246    * Output value not as expected message.
247    */
248   private static final String OUTPUT_VALUE_NOT_AS_EXPECTED = "Output value not as expected"; //$NON-NLS-1$
249 
250   /**
251    * Parse result not as expected message.
252    */
253   private static final String PARSE_RESULT_NOT_AS_EXPECTED = "Parse result not as expected"; //$NON-NLS-1$
254 
255   /**
256    * File 0 name.
257    */
258   private static final String FILE0 = "file0"; //$NON-NLS-1$
259 
260   /**
261    * File 5 name.
262    */
263   private static final String FILE5 = "file5"; //$NON-NLS-1$
264 
265   /**
266    * Filename template0.tmpl.
267    */
268   private static final String TEMPLATE0_TMPL = "template0.tmpl"; //$NON-NLS-1$
269 
270   /**
271    * No undefined variable(s) found message.
272    */
273   private static final String NO_UNDEFINED_VARIABLE_S_FOUND = "No undefined variable(s) found!"; //$NON-NLS-1$
274 
275   /**
276    * Undefined variable not as expected message.
277    */
278   private static final String UNDEFINED_VARIABLE_NOT_AS_EXPECTED = "Undefined variable not as expected"; //$NON-NLS-1$
279 
280   /**
281    * Null pointer exception expected message.
282    */
283   private static final String NULL_POINTER_EXCEPTION_EXPECTED = "Null pointer exception expected"; //$NON-NLS-1$
284 
285   /**
286    * newInstance result not as expected message.
287    */
288   private static final String NEW_INSTANCE_RESULT_NOT_AS_EXPECTED = "newInstance result not as expected"; //$NON-NLS-1$
289 
290   /**
291    * Parent constant.
292    */
293   private static final String PARENT = "parent";
294 
295 
296   /**
297    * Default constructor.
298    */
299   /* default */ TemplateEngineTests()
300    {
301     super();
302    }
303 
304 
305   /* *
306    * Log template engine variables for debugging.
307    *
308    * @param engine TemplateEngine
309    */
310   /*
311   private static void logVars(final TemplateEngine engine)
312    {
313     final String[] variables = engine.getVars();
314     LOGGER.info("Number of variables: " + variables.length); //$NON-NLS-1$
315     for (final String varname : variables)
316      {
317       LOGGER.info("variables: " + varname + " = " + engine.getVar(varname));  //$NON-NLS-1$//$NON-NLS-2$
318      }
319    }
320   */
321 
322 
323   /* *
324    * Log undefined variables for debugging.
325    *
326    * @param engine TemplateEngine
327    * @param varname Variable name for which get the undefined variables
328    * @throws IOException IO exception
329    */
330   /*
331   private static void logUndefVars(final TemplateEngine engine, final String varname) throws IOException
332    {
333     final List<String> undefinedVariables = engine.getUndefined(varname);
334     LOGGER.info("length: " + undefinedVariables.size()); //$NON-NLS-1$
335     for (final String undefVarname : undefinedVariables)
336      {
337       LOGGER.info("variables: " + undefVarname); //$NON-NLS-1$
338      }
339    }
340   */
341 
342 
343   /**
344    * Test default constructor.
345    */
346   @Test
347   /* default */ void testDefaultConstructor()
348    {
349     final TemplateEngine engine = new TemplateEngine();
350     assertNotNull(engine, "Default constructor failed!"); //$NON-NLS-1$
351    }
352 
353 
354   /**
355    * Test set template file.
356    */
357   @Test
358   /* default */ void testSetFile()
359    {
360     final TemplateEngine engine = new TemplateEngine();
361     final boolean success = engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
362     assertTrue(success, TemplateEngineTests.TEMPLATE_FILE_TEMPLATE1_TMPL_COULD_NOT_BE_LOADED);
363    }
364 
365 
366   /**
367    * Test set template file with max length.
368    */
369   @Test
370   /* default */ void testSetFileMaxLength()
371    {
372     final TemplateEngine engine = new TemplateEngine();
373     final boolean success = engine.setFile(TemplateEngineTests.MAX_VARNAME, new File(TemplateEngineTests.TEMPLATE1_TMPL));
374     assertTrue(success, TemplateEngineTests.TEMPLATE_FILE_TEMPLATE1_TMPL_COULD_NOT_BE_LOADED);
375    }
376 
377 
378   /**
379    * Test set template file.
380    */
381   @Test
382   /* default */ void testSetFileEmpty()
383    {
384     final TemplateEngine engine = new TemplateEngine();
385     final File tmplFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
386     assertThrows(IllegalArgumentException.class, () ->
387      {
388       /* final boolean success = */ engine.setFile("", tmplFile); //$NON-NLS-1$
389      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
390     );
391    }
392 
393 
394   /**
395    * Test set template file with to long filename.
396    */
397   @Test
398   /* default */ void testSetFileToLong()
399    {
400     final TemplateEngine engine = new TemplateEngine();
401     final File tmplFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
402     assertThrows(IllegalArgumentException.class, () ->
403      {
404       /* final boolean success = */ engine.setFile(TemplateEngineTests.TO_LONG_VARNAME, tmplFile);
405      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
406     );
407    }
408 
409 
410   /**
411    * Test set template file with wrong filename.
412    */
413   @Test
414   /* default */ void testSetFileWrongname()
415    {
416     final TemplateEngine engine = new TemplateEngine();
417     final File templFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
418     assertThrows(IllegalArgumentException.class, () ->
419      {
420       /* final boolean success = */ engine.setFile("file~1", templFile); //$NON-NLS-1$
421      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
422     );
423    }
424 
425 
426   /**
427    * Test set template file with to large file.
428    */
429   @Test
430   /* default */ void testSetFileToLarge()
431    {
432     final TemplateEngine engine = new TemplateEngine();
433     final File templFile = new File(TemplateEngineTests.TEMPLATE10_TMPL);
434     assertThrows(IllegalArgumentException.class, () ->
435      {
436       /* final boolean success = */ engine.setFile(TemplateEngineTests.VARIABLE1, templFile);
437      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
438     );
439    }
440 
441 
442   /**
443    * Test read template file.
444    *
445    * @throws IOException IO exception
446    */
447   @Test
448   /* default */ void testSubst() throws IOException
449    {
450     final TemplateEngine engine = new TemplateEngine();
451     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
452     final String variableValue = engine.subst(TemplateEngineTests.FILE1);
453     TemplateEngineTests.LOGGER.debug("file1 = {}", variableValue); //$NON-NLS-1$
454     assertEquals(TemplateEngineTests.EQUALS_123_VARIABLE1_456, variableValue, TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED);
455    }
456 
457 
458   /**
459    * Test read empty template file.
460    */
461   @Test
462   /* default */ void testSubstEmpty()
463    {
464     final TemplateEngine engine = new TemplateEngine();
465     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
466     assertThrows(IllegalArgumentException.class, () ->
467      {
468       /* final String variableValue = */ engine.subst(""); //$NON-NLS-1$
469      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
470     );
471    }
472 
473 
474   /**
475    * Test subst with to long varname.
476    */
477   @Test
478   /* default */ void testSubstToLong()
479    {
480     final TemplateEngine engine = new TemplateEngine();
481     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
482     assertThrows(IllegalArgumentException.class, () ->
483      {
484       /* final String variableValue = */ engine.subst(TemplateEngineTests.TO_LONG_VARNAME);
485      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
486     );
487    }
488 
489 
490   /**
491    * Test subst with wrong varname.
492    */
493   @Test
494   /* default */ void testSubstWrongName()
495    {
496     final TemplateEngine engine = new TemplateEngine();
497     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
498     assertThrows(IllegalArgumentException.class, () ->
499      {
500       /* final String variableValue = */ engine.subst(TemplateEngineTests.VARIABLE4);
501      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
502     );
503    }
504 
505 
506   /**
507    * Test subst with null.
508    *
509    * @throws IOException IO exception
510    */
511   @Test
512   /* default */ void testSubstNull() throws IOException
513    {
514     final TemplateEngine engine = new TemplateEngine();
515     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
516     engine.setVar(TemplateEngineTests.VARIABLE1, null);
517     final String variableValue = engine.subst(TemplateEngineTests.VARIABLE1);
518     assertEquals("", variableValue, "Unexpected value");
519    }
520 
521 
522   /**
523    * Test with to large template.
524    */
525   @Test
526   /* default */ void testToLargeTemplate()
527    {
528     final TemplateEngine engine = new TemplateEngine();
529     final File templFile = new File(TemplateEngineTests.TEMPLATE10_TMPL);
530     assertThrows(IllegalArgumentException.class, () ->
531      {
532       /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, templFile);
533      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
534     );
535    }
536 
537 
538   /**
539    * Test subst with max varname length varname.
540    *
541    * @throws IOException IO exception
542    */
543   @Test
544   /* default */ void testSubstMaxLength() throws IOException
545    {
546     final TemplateEngine engine = new TemplateEngine();
547     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
548     final String variableValue = engine.subst(TemplateEngineTests.MAX_VARNAME);
549     assertEquals("", variableValue, "Subst result not as expected"); //$NON-NLS-1$ //$NON-NLS-2$
550    }
551 
552 
553   /**
554    * Get undefined variables from template.
555    *
556    * @throws IOException IO exception
557    */
558   @Test
559   /* default */ void testGetUndefined1() throws IOException
560    {
561     final TemplateEngine engine = new TemplateEngine();
562     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
563     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
564     assertAll(
565       () -> assertEquals(1, undefinedVars.size(), TemplateEngineTests.FOUND_MORE_OR_LESS_UNDEFINED_VARIABLES),
566       () -> assertEquals(TemplateEngineTests.VARIABLE1, undefinedVars.get(0), TemplateEngineTests.NOT_FOUND_EXPECTED_UNDEFINED_VARIABLE)
567     );
568    }
569 
570 
571   /**
572    * Get undefined variables from template.
573    *
574    * @throws IOException IO exception
575    */
576   @Test
577   /* default */ void testGetUndefined2() throws IOException
578    {
579     final TemplateEngine engine = new TemplateEngine();
580     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE3, new File(TemplateEngineTests.TEMPLATE3_TMPL));
581     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE3);
582     final int expectedSize = 3;
583     assertAll(
584       () -> assertEquals(expectedSize, undefinedVars.size(), TemplateEngineTests.FOUND_MORE_OR_LESS_UNDEFINED_VARIABLES),
585       () -> assertEquals(TemplateEngineTests.TEST0, undefinedVars.get(0), TemplateEngineTests.NOT_FOUND_EXPECTED_UNDEFINED_VARIABLE)
586     );
587    }
588 
589 
590   /**
591    * Get undefined variables from empty template.
592    *
593    * @throws IOException IO exception
594    */
595   @Test
596   /* default */ void testGetUndefinedFromEmptyFile() throws IOException
597    {
598     final TemplateEngine engine = new TemplateEngine();
599     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
600     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE4);
601     assertEquals(0, undefinedVars.size(), "Found undefined variables"); //$NON-NLS-1$
602    }
603 
604 
605   /**
606    * Get empty undefined variables.
607    */
608   @Test
609   /* default */ void testGetUndefinedEmpty()
610    {
611     final TemplateEngine engine = new TemplateEngine();
612     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
613     assertThrows(IllegalArgumentException.class, () ->
614      {
615       /* final List<String> undefinedVars = */ engine.getUndefined(""); //$NON-NLS-1$
616      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
617     );
618    }
619 
620 
621   /**
622    * Get undefined variables with to long varname.
623    */
624   @Test
625   /* default */ void testGetUndefinedToLong()
626    {
627     final TemplateEngine engine = new TemplateEngine();
628     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
629     assertThrows(IllegalArgumentException.class, () ->
630      {
631       /* final List<String> undefinedVars = */ engine.getUndefined(TemplateEngineTests.TO_LONG_VARNAME);
632      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
633     );
634    }
635 
636 
637   /**
638    * Get undefined variables with wrong varname.
639    */
640   @Test
641   /* default */ void testGetUndefinedWrongName()
642    {
643     final TemplateEngine engine = new TemplateEngine();
644     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
645     assertThrows(IllegalArgumentException.class, () ->
646      {
647       /* final List<String> undefinedVars = */ engine.getUndefined(TemplateEngineTests.VARIABLE4);
648      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
649     );
650    }
651 
652 
653   /**
654    * Get undefined variables with maximum varname length.
655    *
656    * @throws IOException IO exception
657    */
658   @Test
659   /* default */ void testGetUndefinedMaxLength() throws IOException
660    {
661     final TemplateEngine engine = new TemplateEngine();
662     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
663     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.MAX_VARNAME);
664     assertEquals(new ArrayList<>(), undefinedVars, "GetUndefined result not as expected"); //$NON-NLS-1$
665    }
666 
667 
668   /**
669    * Test set variable as empty.
670    *
671    * @throws IOException IO exception
672    */
673   @Test
674   /* default */ void testSetVarEmpty() throws IOException
675    {
676     final TemplateEngine engine = new TemplateEngine();
677     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
678     engine.setVar(TemplateEngineTests.VARIABLE1);
679     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
680     assertAll(
681       () -> assertTrue(undefinedVars.isEmpty(), TemplateEngineTests.UNDEFINED_VARIABLE_S_FOUND),
682       () -> assertTrue(engine.getVar(TemplateEngineTests.VARIABLE1).isEmpty(), "variable1 is not empty!") //$NON-NLS-1$
683     );
684    }
685 
686 
687   /**
688    * Test set variable with value.
689    *
690    * @throws IOException IO exception
691    */
692   @Test
693   /* default */ void testSetVar() throws IOException
694    {
695     final TemplateEngine engine = new TemplateEngine();
696     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
697     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TEST);
698     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
699     assertAll(
700       () -> assertTrue(undefinedVars.isEmpty(), TemplateEngineTests.UNDEFINED_VARIABLE_S_FOUND),
701       () -> assertEquals(TemplateEngineTests.TEST, engine.getVar(TemplateEngineTests.VARIABLE1), TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED)
702     );
703    }
704 
705 
706   /**
707    * Test set variable with null value.
708    *
709    * @throws IOException IO exception
710    */
711   @Test
712   /* default */ void testSetVarNull() throws IOException
713    {
714     final TemplateEngine engine = new TemplateEngine();
715     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
716     engine.setVar(TemplateEngineTests.VARIABLE1, null);
717     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
718     assertAll(
719       () -> assertTrue(undefinedVars.isEmpty(), TemplateEngineTests.UNDEFINED_VARIABLE_S_FOUND),
720       () -> assertEquals("", engine.getVar(TemplateEngineTests.VARIABLE1), TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED) //$NON-NLS-1$
721     );
722    }
723 
724 
725   /**
726    * Test set empty variable with value.
727    */
728   @Test
729   /* default */ void testSetEmptyVar()
730    {
731     final TemplateEngine engine = new TemplateEngine();
732     assertThrows(IllegalArgumentException.class, () ->
733      {
734       engine.setVar("", TemplateEngineTests.TEST); //$NON-NLS-1$
735      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
736     );
737    }
738 
739 
740   /**
741    * Test set variable with maximum long name.
742    */
743   @Test
744   /* default */ void testSetVarMaxLong()
745    {
746     final TemplateEngine engine = new TemplateEngine();
747     engine.setVar(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.TEST);
748     assertEquals(TemplateEngineTests.TEST, engine.getVar(TemplateEngineTests.MAX_VARNAME), TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED);
749    }
750 
751 
752   /**
753    * Test set variable with to long name.
754    */
755   @Test
756   /* default */ void testSetVarToLong()
757    {
758     final TemplateEngine engine = new TemplateEngine();
759     assertThrows(IllegalArgumentException.class, () ->
760      {
761       engine.setVar(TemplateEngineTests.TO_LONG_VARNAME, TemplateEngineTests.TEST);
762      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
763     );
764    }
765 
766 
767   /**
768    * Test set variable with wrong name.
769    */
770   @Test
771   /* default */ void testSetVarWrongName()
772    {
773     final TemplateEngine engine = new TemplateEngine();
774     assertThrows(IllegalArgumentException.class, () ->
775      {
776       engine.setVar(TemplateEngineTests.VARIABLE4, TemplateEngineTests.TEST);
777      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
778     );
779    }
780 
781 
782   /**
783    * Test set variable with maximum value size.
784    *
785    * @throws IOException IO exception
786    */
787   @Test
788   /* default */ void testSetVarMaxLarge() throws IOException
789    {
790     final TemplateEngine engine = new TemplateEngine();
791     engine.setVar(TemplateEngineTests.VARIABLE1, readStringFromFile(new File(TemplateEngineTests.TEMPLATE9_TMPL)));
792     assertNotNull(engine.getVar(TemplateEngineTests.VARIABLE1), TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED);
793    }
794 
795 
796   /**
797    * Test set variable with to large value size.
798    *
799    * @throws IOException IO exception
800    */
801   @Test
802   /* default */ void testSetVarToLarge() throws IOException
803    {
804     final TemplateEngine engine = new TemplateEngine();
805     final String templ = readStringFromFile(new File(TemplateEngineTests.TEMPLATE10_TMPL));
806     assertThrows(IllegalArgumentException.class, () ->
807      {
808       engine.setVar(TemplateEngineTests.VARIABLE1, templ);
809      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
810     );
811    }
812 
813 
814   /**
815    * Test get removed variable parsing.
816    *
817    * @throws IOException IO exception
818    */
819   @Test
820   /* default */ void testGetRemoved() throws IOException
821    {
822     final TemplateEngine engine = new TemplateEngine();
823     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
824     final String parseResult = engine.parse(TemplateEngineTests.OUTPUT, TemplateEngineTests.FILE1);
825     final String output = engine.get(TemplateEngineTests.OUTPUT);
826     assertAll(
827       () -> assertNotNull(parseResult, "Parse result is null!"), //$NON-NLS-1$
828       () -> assertEquals("123\n\n456\n", output, TemplateEngineTests.OUTPUT_NOT_AS_EXPECTED) //$NON-NLS-1$
829     );
830    }
831 
832 
833   /**
834    * Test get keep variable parsing.
835    *
836    * @throws IOException IO exception
837    */
838   @Test
839   /* default */ void testGetKeep() throws IOException
840    {
841     final TemplateEngine engine = new TemplateEngine(HandleUndefined.KEEP);
842     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
843     /* String parseResult = */ engine.parse(TemplateEngineTests.OUTPUT, TemplateEngineTests.FILE1);
844     final String output = engine.get(TemplateEngineTests.OUTPUT);
845     assertEquals(TemplateEngineTests.EQUALS_123_VARIABLE1_456, output, "Output not as exptected"); //$NON-NLS-1$
846    }
847 
848 
849   /**
850    * Test get replace with comment variable parsing.
851    *
852    * @throws IOException IO exception
853    */
854   @Test
855   /* default */ void testGetComment() throws IOException
856    {
857     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
858     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
859     /* String parseResult = */ engine.parse(TemplateEngineTests.OUTPUT, TemplateEngineTests.FILE1);
860     final String output = engine.get(TemplateEngineTests.OUTPUT);
861     assertEquals("123\n<!-- Template variable 'variable1' undefined -->\n456\n", output, TemplateEngineTests.OUTPUT_NOT_AS_EXPECTED); //$NON-NLS-1$
862    }
863 
864 
865   /**
866    * Test set non existing block.
867    */
868   @Test
869   /* default */ void testSetBlockFailure()
870    {
871     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
872     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
873     assertThrows(IllegalStateException.class, () ->
874      {
875       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, "blk2"); //$NON-NLS-1$
876      }, TemplateEngineTests.ILLEGAL_STATE_EXCEPTION_EXPECTED
877     );
878    }
879 
880 
881   /**
882    * Test set empty block name.
883    */
884   @Test
885   /* default */ void testSetBlockEmpty()
886    {
887     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
888     assertThrows(IllegalArgumentException.class, () ->
889      {
890       /* final boolean successBlock = */ engine.setBlock("", ""); //$NON-NLS-1$ //$NON-NLS-2$
891      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
892     );
893    }
894 
895 
896   /**
897    * Test set empty block name.
898    */
899   @Test
900   /* default */ void testSetBlockMixed()
901    {
902     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
903     assertThrows(IllegalArgumentException.class, () ->
904      {
905       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, ""); //$NON-NLS-1$
906      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
907     );
908    }
909 
910 
911   /**
912    * Test set existing block.
913    *
914    * @throws IOException IO exception
915    */
916   @Test
917   /* default */ void testSetBlock() throws IOException
918    {
919     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
920     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
921     final boolean successBlock = engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
922     final String block = engine.getVar(TemplateEngineTests.BLK1);
923     assertAll(
924       () -> assertTrue(successBlock, "Block could not be cut out successfully!"), //$NON-NLS-1$
925       () -> assertEquals("\n789\n{variable2}\nabc\n", block, TemplateEngineTests.BLOCK_VALUE_NOT_AS_EXPECTED) //$NON-NLS-1$
926     );
927    }
928 
929 
930   /**
931    * Test set block with to long parent name.
932    */
933   @Test
934   /* default */ void testSetBlockParentToLong()
935    {
936     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
937     assertThrows(IllegalArgumentException.class, () ->
938      {
939       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.TO_LONG_VARNAME, TemplateEngineTests.VARIABLE1, ""); //$NON-NLS-1$
940      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
941     );
942    }
943 
944 
945   /**
946    * Test set block with to long varname.
947    */
948   @Test
949   /* default */ void testSetBlockVarnameToLong()
950    {
951     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
952     assertThrows(IllegalArgumentException.class, () ->
953      {
954       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TO_LONG_VARNAME, ""); //$NON-NLS-1$
955      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
956     );
957    }
958 
959 
960   /**
961    * Test set block with to long name.
962    */
963   @Test
964   /* default */ void testSetBlockNameToLong()
965    {
966     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
967     assertThrows(IllegalArgumentException.class, () ->
968      {
969       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE_1234, TemplateEngineTests.TO_LONG_VARNAME);
970      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
971     );
972    }
973 
974 
975   /**
976    * Test set block with to wrong parent name.
977    */
978   @Test
979   /* default */ void testSetBlockParentWrong()
980    {
981     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
982     assertThrows(IllegalArgumentException.class, () ->
983      {
984       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.VARIABLE4, TemplateEngineTests.VARIABLE1, ""); //$NON-NLS-1$
985      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
986     );
987    }
988 
989 
990   /**
991    * Test set block with wrong varname.
992    */
993   @Test
994   /* default */ void testSetBlockVarnameWrong()
995    {
996     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
997     assertThrows(IllegalArgumentException.class, () ->
998      {
999       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VARIABLE4, ""); //$NON-NLS-1$
1000      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1001     );
1002    }
1003 
1004 
1005   /**
1006    * Test set block with wrong name.
1007    */
1008   @Test
1009   /* default */ void testSetBlockNameWrong()
1010    {
1011     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1012     assertThrows(IllegalArgumentException.class, () ->
1013      {
1014       /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE_1234, TemplateEngineTests.VARIABLE4);
1015      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1016     );
1017    }
1018 
1019 
1020   /**
1021    * Test set block with to max length parent name.
1022    *
1023    * @throws IOException IO exception
1024    */
1025   @Test
1026   /* default */ void testSetBlockParentMaxLength() throws IOException
1027    {
1028     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1029     final boolean successBlock = engine.setBlock(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.VARIABLE1, ""); //$NON-NLS-1$
1030     assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1031    }
1032 
1033 
1034   /**
1035    * Test set block with to max length parent name.
1036    *
1037    * @throws IOException IO exception
1038    */
1039   @Test
1040   /* default */ void testSetBlockParentMaxLength2() throws IOException
1041    {
1042     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1043     final boolean successBlock = engine.setBlock(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.VARIABLE1);
1044     assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1045    }
1046 
1047 
1048   /**
1049    * Test set block with max length varname.
1050    *
1051    * @throws IOException IO exception
1052    */
1053   @Test
1054   /* default */ void testSetBlockVarnameMaxLength() throws IOException
1055    {
1056     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1057     final boolean successBlock = engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.MAX_VARNAME, ""); //$NON-NLS-1$
1058     assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1059    }
1060 
1061 
1062   /**
1063    * Test set block with max length name.
1064    *
1065    * @throws IOException IO exception
1066    */
1067   @Test
1068   /* default */ void testSetBlockNameMaxLength() throws IOException
1069    {
1070     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1071     final boolean successBlock = engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE_1234, TemplateEngineTests.MAX_VARNAME);
1072     assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1073    }
1074 
1075 
1076   /**
1077    * Test parsing without append.
1078    *
1079    * @throws IOException IO exception
1080    */
1081   @Test
1082   /* default */ void testParseNonAppend() throws IOException
1083    {
1084     // tag::NonAppendBlock[]
1085     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1086     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1087     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1088     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1089     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1090     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1091     /* String parseResult = */ engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, false);
1092     /* String parseResult = */ engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, false);
1093     // end::NonAppendBlock[]
1094     assertEquals("\n789\nTEST2\nabc\n", engine.getVar(TemplateEngineTests.BLK1_BLK), TemplateEngineTests.BLOCK_VALUE_NOT_AS_EXPECTED); //$NON-NLS-1$
1095    }
1096 
1097 
1098   /**
1099    * Test empty parsing.
1100    *
1101    * @throws IOException IO exception
1102    */
1103   @Test
1104   /* default */ void testParseEmpty() throws IOException
1105    {
1106     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1107     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1108     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1109     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1110     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1111     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1112     assertThrows(IllegalArgumentException.class, () ->
1113      {
1114       /* String parseResult = */ engine.parse("", "", false); //$NON-NLS-1$ //$NON-NLS-2$
1115      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1116     );
1117    }
1118 
1119 
1120   /**
1121    * Test empty parsing.
1122    *
1123    * @throws IOException IO exception
1124    */
1125   @Test
1126   /* default */ void testParseMixed() throws IOException
1127    {
1128     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1129     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1130     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1131     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1132     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1133     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1134     assertThrows(IllegalArgumentException.class, () ->
1135      {
1136       /* String parseResult = */ engine.parse(TemplateEngineTests.BLK1_BLK, "", false); //$NON-NLS-1$
1137      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1138     );
1139    }
1140 
1141 
1142   /**
1143    * Test parsing with append.
1144    *
1145    * @throws IOException IO exception
1146    */
1147   @Test
1148   /* default */ void testParseAppend() throws IOException
1149    {
1150     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1151     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1152     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1153     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1154     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1155     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1156     /* String parseResult = */ engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, true);
1157     /* String parseResult = */ engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, true);
1158     assertEquals("\n789\nTEST2\nabc\n\n789\nTEST2\nabc\n", engine.getVar(TemplateEngineTests.BLK1_BLK), TemplateEngineTests.BLOCK_VALUE_NOT_AS_EXPECTED); //$NON-NLS-1$
1159    }
1160 
1161 
1162   /**
1163    * Test a specific parsing problem.
1164    *
1165    * Problem description for original phplib template engine:
1166    *
1167    * When using the same template-variable within a page and a block thats contained by the page,
1168    * then the behavior during parsing the page might be different.
1169    *
1170    * Thats because inside the subst() method a loop runs over the list of known template variables.
1171    *
1172    * When a variable has been replaced before a block (that contains the same variable) will be replaced,
1173    * then the variable will not be replaced within the block. When the block will be parsed first,
1174    * then the variable will also be replaced within the block.
1175    *
1176    * @throws IOException IO exception
1177    */
1178   @Test
1179   @SuppressWarnings("checkstyle:CommentsIndentation")
1180   /* default */ void testParseProblem() throws IOException
1181    {
1182     final TemplateEngine engine = new TemplateEngine(HandleUndefined.KEEP);
1183     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE3, new File(TemplateEngineTests.TEMPLATE3_TMPL));
1184     engine.setVar(TemplateEngineTests.TEST0, "000"); //$NON-NLS-1$
1185     engine.setVar("test1", "111"); //$NON-NLS-1$ //$NON-NLS-2$
1186     engine.setVar("test3", "333"); //$NON-NLS-1$ //$NON-NLS-2$
1187     final boolean successBlock = engine.setBlock(TemplateEngineTests.FILE3, "test2"); //$NON-NLS-1$
1188     // engine.parse("test2", "test2", false); // Parse block before template to have no problems! //$NON-NLS-1$ //$NON-NLS-2$
1189     final String output = engine.parse(TemplateEngineTests.OUTPUT, TemplateEngineTests.FILE3);
1190     assertAll(
1191       () -> assertTrue(successBlock, "Could not cut out block!"), //$NON-NLS-1$
1192       () -> assertEquals("000 \n111 \n \nabc {test1} def 333 ghi \n \n333 \n000 \n", output, TemplateEngineTests.OUTPUT_VALUE_NOT_AS_EXPECTED) // Buggy result, because of order problem //$NON-NLS-1$
1193       // () -> assertEquals("000 \n111 \n \nabc {test1} def {test3} ghi \n \n333 \n000 \n", output) // Wanted result without block parsing //$NON-NLS-1$
1194       // () -> assertEquals("000 \n111 \n \nabc 111 def 333 ghi \n \n333 \n000 \n", output) // Result with block parsing //$NON-NLS-1$
1195     );
1196     // logVars(engine);
1197     // logUndefVars(engine, "file1");
1198    }
1199 
1200 
1201   /**
1202    * Test parsing with to long target name.
1203    *
1204    * @throws IOException IO exception
1205    */
1206   @Test
1207   /* default */ void testParseTargetToLong() throws IOException
1208    {
1209     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1210     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1211     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1212     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1213     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1214     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1215     assertThrows(IllegalArgumentException.class, () ->
1216      {
1217       /* String parseResult = */ engine.parse(TemplateEngineTests.TO_LONG_VARNAME, TemplateEngineTests.VARIABLE1, false);
1218      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1219     );
1220    }
1221 
1222 
1223   /**
1224    * Test parsing with to long varname.
1225    *
1226    * @throws IOException IO exception
1227    */
1228   @Test
1229   /* default */ void testParseVarnameToLong() throws IOException
1230    {
1231     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1232     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1233     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1234     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1235     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1236     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1237     assertThrows(IllegalArgumentException.class, () ->
1238      {
1239       /* String parseResult = */ engine.parse(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TO_LONG_VARNAME, false);
1240      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1241     );
1242    }
1243 
1244 
1245   /**
1246    * Test parsing with wrong target name.
1247    *
1248    * @throws IOException IO exception
1249    */
1250   @Test
1251   /* default */ void testParseTargetWrong() throws IOException
1252    {
1253     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1254     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1255     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1256     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1257     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1258     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1259     assertThrows(IllegalArgumentException.class, () ->
1260      {
1261       /* String parseResult = */ engine.parse(TemplateEngineTests.VARIABLE4, TemplateEngineTests.VARIABLE1, false);
1262      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1263     );
1264    }
1265 
1266 
1267   /**
1268    * Test parsing with wrong varname.
1269    *
1270    * @throws IOException IO exception
1271    */
1272   @Test
1273   /* default */ void testParseVarnameWrong() throws IOException
1274    {
1275     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1276     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1277     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1278     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1279     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1280     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1281     assertThrows(IllegalArgumentException.class, () ->
1282      {
1283       /* String parseResult = */ engine.parse(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VARIABLE4, false);
1284      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1285     );
1286    }
1287 
1288 
1289   /**
1290    * Test parsing with maximum target name length.
1291    *
1292    * @throws IOException IO exception
1293    */
1294   @Test
1295   /* default */ void testParseTargetMaxLength() throws IOException
1296    {
1297     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1298     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1299     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1300     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1301     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1302     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1303     final String parseResult = engine.parse(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.VARIABLE1, false);
1304     assertEquals(TemplateEngineTests.VALUE1, parseResult, TemplateEngineTests.PARSE_RESULT_NOT_AS_EXPECTED);
1305    }
1306 
1307 
1308   /**
1309    * Test parsing with maximum varname length.
1310    *
1311    * @throws IOException IO exception
1312    */
1313   @Test
1314   /* default */ void testParseVarnameMaxLength() throws IOException
1315    {
1316     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1317     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1318     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE1);
1319     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.VALUE2);
1320     engine.setVar(TemplateEngineTests.VARIABLE3, TemplateEngineTests.VALUE3);
1321     /* final boolean successBlock = */ engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1322     final String parseResult = engine.parse(TemplateEngineTests.VARIABLE1, TemplateEngineTests.MAX_VARNAME, false);
1323     assertEquals("", parseResult, TemplateEngineTests.PARSE_RESULT_NOT_AS_EXPECTED); //$NON-NLS-1$
1324    }
1325 
1326 
1327   /**
1328    * Test empty variable list.
1329    */
1330   @Test
1331   /* default */ void testGetVarsEmpty()
1332    {
1333     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1334     final List<String> variables = engine.getVars();
1335     assertTrue(variables.isEmpty(), "Variables within empty template found!"); //$NON-NLS-1$
1336    }
1337 
1338 
1339   /**
1340    * Test variable list.
1341    */
1342   @Test
1343   /* default */ void testGetVars()
1344    {
1345     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1346     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TEST);
1347     engine.setVar(TemplateEngineTests.VARIABLE2, TemplateEngineTests.TEST);
1348     final List<String> variables = engine.getVars();
1349     assertAll(
1350       () -> assertTrue(!variables.isEmpty(), "No variables within template found!"), //$NON-NLS-1$
1351       () -> assertNotNull(variables.get(0), "No variable in list!") //$NON-NLS-1$
1352     );
1353    }
1354 
1355 
1356   /**
1357    * Test get variable with maximum long name.
1358    */
1359   @Test
1360   /* default */ void testGetVarMaxLong()
1361    {
1362     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1363     final String value = engine.getVar(TemplateEngineTests.MAX_VARNAME);
1364     assertEquals("", value, "Unexpected value found"); //$NON-NLS-1$ //$NON-NLS-2$
1365    }
1366 
1367 
1368   /**
1369    * Test get variable with to long name.
1370    */
1371   @Test
1372   /* default */ void testGetVarToLong()
1373    {
1374     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1375     assertThrows(IllegalArgumentException.class, () ->
1376      {
1377       /* final String value = */ engine.getVar(TemplateEngineTests.TO_LONG_VARNAME);
1378      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1379     );
1380    }
1381 
1382 
1383   /**
1384    * Test get variable with wrong name.
1385    */
1386   @Test
1387   /* default */ void testGetVarWrongName()
1388    {
1389     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1390     assertThrows(IllegalArgumentException.class, () ->
1391      {
1392       /* final String value = */ engine.getVar(TemplateEngineTests.VARIABLE4);
1393      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1394     );
1395    }
1396 
1397 
1398   /**
1399    * Test subst empty file.
1400    *
1401    * @throws IOException IO exception
1402    */
1403   @Test
1404   /* default */ void testSubstEmptyFile() throws IOException
1405    {
1406     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1407     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
1408     final String result = engine.subst(TemplateEngineTests.FILE4);
1409     assertEquals("", result, "Template not empty!"); //$NON-NLS-1$ //$NON-NLS-2$
1410    }
1411 
1412 
1413   /**
1414    * Test set existing block.
1415    *
1416    * @throws IOException IO exception
1417    */
1418   @Test
1419   /* default */ void testSetBlockEmptyFile() throws IOException
1420    {
1421     final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1422     /* final boolean successFile = */ engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
1423     final boolean successBlock = engine.setBlock(TemplateEngineTests.FILE4, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1424     assertFalse(successBlock, "Block could be cut out successfully from empty template!"); //$NON-NLS-1$
1425    }
1426 
1427 
1428   /**
1429    * Test read template file from classpath.
1430    *
1431    * @throws IOException IO exception
1432    */
1433   @Test
1434   /* default */ void testReadFromClasspath() throws IOException
1435    {
1436     final TemplateEngine engine = new TemplateEngine();
1437     final boolean success = engine.setFile(TemplateEngineTests.FILE5, new File("template5.tmpl")); //$NON-NLS-1$
1438     final String variableValue = engine.subst(TemplateEngineTests.FILE5);
1439     assertAll(
1440       () -> assertTrue(success, "Template file template5.tmpl could not be set!"), //$NON-NLS-1$
1441       () -> assertNotNull(variableValue, "Template file template5.tmpl could not be loaded from classpath") //$NON-NLS-1$
1442     );
1443    }
1444 
1445 
1446   /**
1447    * Test read template file from classpath.
1448    *
1449    * @throws IOException IO exception
1450    */
1451   @Test
1452   /* default */ void testReadNonExisting() throws IOException
1453    {
1454     final TemplateEngine engine = new TemplateEngine();
1455     final boolean success = engine.setFile(TemplateEngineTests.FILE0, new File(TemplateEngineTests.TEMPLATE0_TMPL));
1456     final String variableValue = engine.subst(TemplateEngineTests.FILE0);
1457     assertAll(
1458       () -> assertFalse(success, "Template file template0.tmpl could be set!"), //$NON-NLS-1$
1459       () -> assertEquals("", variableValue, "Template file template0.tmpl could be loaded from classpath") //$NON-NLS-1$ //$NON-NLS-2$
1460     );
1461    }
1462 
1463 
1464   /**
1465    * Test unset variable.
1466    *
1467    * @throws IOException IO exception
1468    */
1469   @Test
1470   /* default */ void testUnsetVar() throws IOException
1471    {
1472     final TemplateEngine engine = new TemplateEngine();
1473     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
1474     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TEST);
1475     engine.unsetVar(TemplateEngineTests.VARIABLE1);
1476     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
1477     assertAll(
1478       () -> assertFalse(undefinedVars.isEmpty(), TemplateEngineTests.NO_UNDEFINED_VARIABLE_S_FOUND),
1479       () -> assertEquals(TemplateEngineTests.VARIABLE1, undefinedVars.get(0), TemplateEngineTests.UNDEFINED_VARIABLE_NOT_AS_EXPECTED)
1480     );
1481    }
1482 
1483 
1484   /**
1485    * Test unset empty variable.
1486    */
1487   @Test
1488   /* default */ void testUnsetVarEmpty()
1489    {
1490     final TemplateEngine engine = new TemplateEngine();
1491     assertThrows(IllegalArgumentException.class, () ->
1492      {
1493       engine.unsetVar(""); //$NON-NLS-1$
1494      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1495     );
1496    }
1497 
1498 
1499   /**
1500    * Test unset variable with maximum name length.
1501    *
1502    * @throws IOException IO exception
1503    */
1504   @Test
1505   /* default */ void testUnsetVarMaxLength() throws IOException
1506    {
1507     final TemplateEngine engine = new TemplateEngine();
1508     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE8_TMPL));
1509     engine.setVar(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.TEST);
1510     engine.unsetVar(TemplateEngineTests.MAX_VARNAME);
1511     final List<String> undefinedVars = engine.getUndefined(TemplateEngineTests.FILE1);
1512     assertAll(
1513       () -> assertFalse(undefinedVars.isEmpty(), TemplateEngineTests.NO_UNDEFINED_VARIABLE_S_FOUND),
1514       () -> assertEquals(TemplateEngineTests.MAX_VARNAME, undefinedVars.get(0), TemplateEngineTests.UNDEFINED_VARIABLE_NOT_AS_EXPECTED)
1515     );
1516    }
1517 
1518 
1519   /**
1520    * Test unset variable with to long name.
1521    */
1522   @Test
1523   /* default */ void testUnsetVarToLong()
1524    {
1525     final TemplateEngine engine = new TemplateEngine();
1526     assertThrows(IllegalArgumentException.class, () ->
1527      {
1528       engine.unsetVar(TemplateEngineTests.TO_LONG_VARNAME);
1529      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1530     );
1531    }
1532 
1533 
1534   /**
1535    * Test unset variable with wrong name.
1536    */
1537   @Test
1538   /* default */ void testUnsetVarWrongName()
1539    {
1540     final TemplateEngine engine = new TemplateEngine();
1541     assertThrows(IllegalArgumentException.class, () ->
1542      {
1543       engine.unsetVar(TemplateEngineTests.VARIABLE4);
1544      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1545     );
1546    }
1547 
1548 
1549   /**
1550    * Test toString.
1551    */
1552   @Test
1553   /* default */ void testToString()
1554    {
1555     final TemplateEngine engine = new TemplateEngine();
1556     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
1557     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
1558     engine.setVar(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TEST);
1559     final String string = engine.toString();
1560     assertEquals("TemplateEngine[unknowns=REMOVE, vManager=VariableManager[vars=[variable1]], fManager=FileManager[files=Optional[template2.tmpl, template1.tmpl]], bManager=BlockManager[vars=[variable1]]]", string, "toString() result not as expected"); //$NON-NLS-1$ //$NON-NLS-2$
1561    }
1562 
1563 
1564   /**
1565    * Test newInstance from file.
1566    *
1567    * @throws IOException IO exception
1568    */
1569   @Test
1570   /* default */ void testNewInstanceFile1() throws IOException
1571    {
1572     final TemplateEngine engine = TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE1_TMPL));
1573     /* String result = */ engine.subst(TemplateEngineTests.TEMPLATE);
1574     final String value = engine.getVar(TemplateEngineTests.TEMPLATE);
1575     assertAll(
1576       () -> assertNotNull(value, TemplateEngineTests.NO_TEMPLATE_FOUND),
1577       () -> assertFalse(value.isEmpty(), TemplateEngineTests.NO_TEMPLATE_FOUND)
1578     );
1579    }
1580 
1581 
1582   /**
1583    * Test newInstance from non existing file.
1584    */
1585   @Test
1586   /* default */ void testNewInstanceFileNonExisting()
1587    {
1588     assertThrows(FileNotFoundException.class, () ->
1589      {
1590       /* final TemplateEngine engine = */ TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE0_TMPL));
1591      }, "File not found exception expected"
1592     );
1593    }
1594 
1595 
1596   /**
1597    * Test newInstance from directory.
1598    */
1599   @Test
1600   /* default */ void testNewInstanceFileFromDirectory()
1601    {
1602     final File file = new File("target/test-classes/templates/");
1603     assertThrows(AssertionError.class, () ->
1604      {
1605       /* final TemplateEngine engine = */ TemplateEngine.newInstance(file);
1606      }, TemplateEngineTests.ASSERTION_ERROR_EXPECTED
1607     );
1608    }
1609 
1610 
1611   /**
1612    * Test newInstance from File with null.
1613    */
1614   @Test
1615   /* default */ void testNewInstanceFileNull()
1616    {
1617     assertThrows(NullPointerException.class, () ->
1618      {
1619       /* final TemplateEngine engine = */ TemplateEngine.newInstance((File)null);
1620      }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1621     );
1622    }
1623 
1624 
1625   /**
1626    * Test newInstance with a to large file.
1627    */
1628   @Test
1629   /* default */ void testNewInstanceFileToLarge()
1630    {
1631     assertThrows(IOException.class, () ->
1632      {
1633       /* final TemplateEngine engine = */ TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE10_TMPL));
1634      }, "IO exception expected"
1635     );
1636    }
1637 
1638 
1639   /**
1640    * Test newInstance with maximum size file.
1641    *
1642    * @throws IOException IO exception
1643    */
1644   @Test
1645   /* default */ void testNewInstanceFileMaxSize() throws IOException
1646    {
1647     final TemplateEngine engine = TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE9_TMPL));
1648     assertNotNull(engine, TemplateEngineTests.NEW_INSTANCE_RESULT_NOT_AS_EXPECTED);
1649    }
1650 
1651 
1652   /**
1653    * Test newInstance from InputStream with null.
1654    */
1655   @Test
1656   /* default */ void testNewInstanceInputStreamNull()
1657    {
1658     assertThrows(NullPointerException.class, () ->
1659      {
1660       /* final TemplateEngine engine = */ TemplateEngine.newInstance((InputStream)null);
1661      }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1662     );
1663    }
1664 
1665 
1666   /**
1667    * Test newInstance from InputStream.
1668    *
1669    * @throws IOException IO exception
1670    */
1671   @Test
1672   /* default */ void testNewInstanceInputStream() throws IOException
1673    {
1674     try (InputStream stream = this.getClass().getResourceAsStream("/template5.tmpl")) //$NON-NLS-1$
1675      {
1676       final TemplateEngine engine = TemplateEngine.newInstance(stream);
1677       final String value = engine.getVar(TemplateEngineTests.TEMPLATE);
1678       assertAll(
1679         () -> assertNotNull(value, TemplateEngineTests.NO_TEMPLATE_FOUND),
1680         () -> assertFalse(value.isEmpty(), TemplateEngineTests.NO_TEMPLATE_FOUND)
1681       );
1682      }
1683    }
1684 
1685 
1686   /**
1687    * Test newInstance from empty InputStream.
1688    *
1689    * @throws IOException IO exception
1690    */
1691   @Test
1692   /* default */ void testNewInstanceInputStreamEmpty() throws IOException
1693    {
1694     try (InputStream stream = this.getClass().getResourceAsStream("/template6.tmpl")) //$NON-NLS-1$
1695      {
1696       assertThrows(IllegalStateException.class, () ->
1697        {
1698         /* final TemplateEngine engine = */ TemplateEngine.newInstance(stream);
1699        }, TemplateEngineTests.ILLEGAL_STATE_EXCEPTION_EXPECTED
1700       );
1701      }
1702    }
1703 
1704 
1705   /**
1706    * Test newInstance from String with null.
1707    */
1708   @Test
1709   /* default */ void testNewInstanceStringNull()
1710    {
1711     assertThrows(NullPointerException.class, () ->
1712      {
1713       /* final TemplateEngine engine = */ TemplateEngine.newInstance((String)null);
1714      }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1715     );
1716    }
1717 
1718 
1719   /**
1720    * Test newInstance from String with empty string.
1721    */
1722   @Test
1723   /* default */ void testNewInstanceStringEmpty()
1724    {
1725     assertThrows(IllegalArgumentException.class, () ->
1726      {
1727       /* final TemplateEngine engine = */ TemplateEngine.newInstance(""); //$NON-NLS-1$
1728      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1729     );
1730    }
1731 
1732 
1733   /**
1734    * Test newInstance from String.
1735    */
1736   @Test
1737   /* default */ void testNewInstanceString()
1738    {
1739     final TemplateEngine engine = TemplateEngine.newInstance("123\r\n{variable1}\r\n456\r\n"); //$NON-NLS-1$
1740     final String value = engine.getVar(TemplateEngineTests.TEMPLATE);
1741     assertAll(
1742       () -> assertNotNull(value, TemplateEngineTests.NO_TEMPLATE_FOUND),
1743       () -> assertFalse(value.isEmpty(), TemplateEngineTests.NO_TEMPLATE_FOUND)
1744     );
1745    }
1746 
1747 
1748   /**
1749    * Read String from File.
1750    *
1751    * @param file File to read from
1752    * @return File content as String
1753    * @throws IOException IO exception
1754    */
1755   private static String readStringFromFile(final File file) throws IOException
1756    {
1757     return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
1758    }
1759 
1760 
1761   /**
1762    * Test newInstance from String with to large string.
1763    *
1764    * @throws IOException IO exception
1765    */
1766   @Test
1767   /* default */ void testNewInstanceStringToLarge() throws IOException
1768    {
1769     final String templ = readStringFromFile(new File(TemplateEngineTests.TEMPLATE10_TMPL));
1770     assertThrows(IllegalArgumentException.class, () ->
1771      {
1772       /* final TemplateEngine engine = */ TemplateEngine.newInstance(templ);
1773      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1774     );
1775    }
1776 
1777 
1778   /**
1779    * Test newInstance from String with max size string.
1780    *
1781    * @throws IOException IO exception
1782    */
1783   @Test
1784   /* default */ void testNewInstanceStringMaxSize() throws IOException
1785    {
1786     final TemplateEngine engine = TemplateEngine.newInstance(readStringFromFile(new File(TemplateEngineTests.TEMPLATE9_TMPL)));
1787     assertNotNull(engine, TemplateEngineTests.NEW_INSTANCE_RESULT_NOT_AS_EXPECTED);
1788    }
1789 
1790 
1791   /**
1792    * Test newInstance from TemplateEngine copy.
1793    *
1794    * @throws IOException IO exception
1795    */
1796   @Test
1797   /* default */ void testNewInstanceCopy() throws IOException
1798    {
1799     final TemplateEngine engine1 = TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE1_TMPL));
1800     /* String result = */ engine1.subst(TemplateEngineTests.TEMPLATE);
1801     final TemplateEngine engine2 = TemplateEngine.newInstance(engine1);
1802     final String value2 = engine2.getVar(TemplateEngineTests.TEMPLATE);
1803     assertAll(
1804       () -> assertNotNull(value2, TemplateEngineTests.NO_TEMPLATE_FOUND),
1805       () -> assertFalse(value2.isEmpty(), TemplateEngineTests.NO_TEMPLATE_FOUND)
1806     );
1807    }
1808 
1809 
1810   /**
1811    * Test copy constructor.
1812    */
1813   @Test
1814   /* default */ void testCopyConstructor()
1815    {
1816     final TemplateEngine engine1 = new TemplateEngine();
1817     final TemplateEngine engine2 = new TemplateEngine(engine1);
1818     assertNotNull(engine2, "Copy constructor failed!"); //$NON-NLS-1$
1819    }
1820 
1821 
1822   /**
1823    * Test get empty variable with value.
1824    */
1825   @Test
1826   /* default */ void testGetVarEmpty()
1827    {
1828     final TemplateEngine engine = new TemplateEngine();
1829     assertThrows(IllegalArgumentException.class, () ->
1830      {
1831       /* final String value = */ engine.getVar(""); //$NON-NLS-1$
1832      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1833     );
1834    }
1835 
1836 
1837   /**
1838    * Test empty finish.
1839    */
1840   @Test
1841   /* default */ void testFinishEmpty()
1842    {
1843     final TemplateEngine engine = new TemplateEngine();
1844     assertThrows(IllegalArgumentException.class, () ->
1845      {
1846       engine.finish(""); //$NON-NLS-1$
1847      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1848     );
1849    }
1850 
1851 
1852   /**
1853    * Test finish with to large template.
1854    *
1855    * @throws IOException IO exception
1856    */
1857   @Test
1858   /* default */ void testFinishToLarge() throws IOException
1859    {
1860     final TemplateEngine engine = new TemplateEngine();
1861     final String templ = readStringFromFile(new File(TemplateEngineTests.TEMPLATE10_TMPL));
1862     assertThrows(IllegalArgumentException.class, () ->
1863      {
1864       /* String result = */ engine.finish(templ);
1865      }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1866     );
1867    }
1868 
1869 
1870   /**
1871    * Test finish with maximum template size.
1872    *
1873    * @throws IOException IO exception
1874    */
1875   @Test
1876   /* default */ void testFinishMaxSize() throws IOException
1877    {
1878     final TemplateEngine engine = new TemplateEngine();
1879     final String result = engine.finish(readStringFromFile(new File(TemplateEngineTests.TEMPLATE9_TMPL)));
1880     assertNotNull(result, "finish result not as expected"); //$NON-NLS-1$
1881    }
1882 
1883 
1884   /**
1885    * Test two { after each other within template.
1886    *
1887    * @throws IOException IO exception
1888    */
1889   @Test
1890   /* default */ void testDoubleOpenCurlyBrace() throws IOException
1891    {
1892     final TemplateEngine engine = new TemplateEngine();
1893     /* final boolean success = */ engine.setFile(TemplateEngineTests.FILE1, new File("target/test-classes/templates/template7.tmpl")); //$NON-NLS-1$
1894     engine.setVar(TemplateEngineTests.VARIABLE1, ""); //$NON-NLS-1$
1895     final String variableValue = engine.subst(TemplateEngineTests.FILE1);
1896     assertEquals("123\n{\n456\n", variableValue, TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED); //$NON-NLS-1$
1897    }
1898 
1899 
1900   /**
1901    * Test hash code.
1902    */
1903   @Test
1904   /* default */ void testHashCode()
1905    {
1906     final TemplateEngine tmpl1 = new TemplateEngine(HandleUndefined.REMOVE);
1907     final TemplateEngine tmpl2 = new TemplateEngine(HandleUndefined.REMOVE);
1908     final TemplateEngine tmpl3 = new TemplateEngine(HandleUndefined.KEEP);
1909     assertAll("testHashCode", //$NON-NLS-1$
1910       () -> assertEquals(tmpl1.hashCode(), tmpl2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
1911       () -> assertNotEquals(tmpl1.hashCode(), tmpl3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
1912     );
1913    }
1914 
1915 
1916   /**
1917    * Test equals.
1918    *
1919    * @throws IOException IO exception
1920    */
1921   @Test
1922   @SuppressFBWarnings("EC_NULL_ARG")
1923   @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
1924   /* default */ void testEquals() throws IOException
1925    {
1926     final TemplateEngine tmpl1 = new TemplateEngine(HandleUndefined.REMOVE);
1927     final TemplateEngine tmpl2 = new TemplateEngine(HandleUndefined.REMOVE);
1928     final TemplateEngine tmpl3 = new TemplateEngine(HandleUndefined.KEEP);
1929     final TemplateEngine tmpl4 = new TemplateEngine(HandleUndefined.REMOVE);
1930     final TemplateEngine tmpl5 = new TemplateEngine(HandleUndefined.REMOVE);
1931     final TemplateEngine tmpl6 = new TemplateEngine(HandleUndefined.REMOVE);
1932     final TemplateEngine tmpl7 = new TemplateEngine(HandleUndefined.REMOVE);
1933     tmpl5.setVar("key", "value"); //$NON-NLS-1$ //$NON-NLS-2$
1934     /* boolean success = */ tmpl6.setFile("file", new File(TemplateEngineTests.TEMPLATE1_TMPL)); //$NON-NLS-1$
1935     tmpl7.setVar(PARENT, "before<!-- BEGIN blktest -->content<!-- END blktest -->after");
1936     /* final boolean result = */ tmpl7.setBlock(PARENT, "blktest");
1937     assertAll("testEquals", //$NON-NLS-1$
1938       () -> assertTrue(tmpl1.equals(tmpl1), "TemplateEngine11 is not equal"), //$NON-NLS-1$
1939       () -> assertTrue(tmpl1.equals(tmpl2), "TemplateEngine12 are not equal"), //$NON-NLS-1$
1940       () -> assertTrue(tmpl2.equals(tmpl1), "TemplateEngine21 are not equal"), //$NON-NLS-1$
1941       () -> assertTrue(tmpl2.equals(tmpl4), "TemplateEngine24 are not equal"), //$NON-NLS-1$
1942       () -> assertTrue(tmpl1.equals(tmpl4), "TemplateEngine14 are not equal"), //$NON-NLS-1$
1943       () -> assertFalse(tmpl1.equals(tmpl3), "TemplateEngine13 are equal"), //$NON-NLS-1$
1944       () -> assertFalse(tmpl3.equals(tmpl1), "TemplateEngine31 are equal"), //$NON-NLS-1$
1945       () -> assertFalse(tmpl1.equals(null), "TemplateEngine10 is equal"), //$NON-NLS-1$
1946       () -> assertFalse(tmpl1.equals(tmpl5), "TemplateEngine15 is equal"), //$NON-NLS-1$
1947       () -> assertFalse(tmpl1.equals(tmpl6), "TemplateEngine16 is equal"), //$NON-NLS-1$
1948       () -> assertFalse(tmpl1.equals(tmpl7), "TemplateEngine17 is equal") //$NON-NLS-1$
1949     );
1950    }
1951 
1952  }