1
2
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
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
42
43 private static final Logger LOGGER = LogManager.getLogger(TemplateEngineTests.class);
44
45
46
47
48 private static final String TEMPLATE1_TMPL = "target/test-classes/templates/template1.tmpl";
49
50
51
52
53 private static final String TEMPLATE2_TMPL = "target/test-classes/templates/template2.tmpl";
54
55
56
57
58 private static final String TEMPLATE3_TMPL = "target/test-classes/templates/template3.tmpl";
59
60
61
62
63 private static final String TEMPLATE4_TMPL = "target/test-classes/templates/template4.tmpl";
64
65
66
67
68 private static final String TEMPLATE8_TMPL = "target/test-classes/templates/template8.tmpl";
69
70
71
72
73 private static final String TEMPLATE9_TMPL = "target/test-classes/templates/template9.tmpl";
74
75
76
77
78 private static final String TEMPLATE10_TMPL = "target/test-classes/templates/template10.tmpl";
79
80
81
82
83 private static final String ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED = "Illegal argument exception expected";
84
85
86
87
88 private static final String ASSERTION_ERROR_EXPECTED = "Assertion error expected";
89
90
91
92
93 private static final String FOUND_MORE_OR_LESS_UNDEFINED_VARIABLES = "Found more or less undefined variables";
94
95
96
97
98 private static final String NOT_FOUND_EXPECTED_UNDEFINED_VARIABLE = "Not found expected undefined variable";
99
100
101
102
103 private static final String FILE1 = "file1";
104
105
106
107
108 private static final String FILE2 = "file2";
109
110
111
112
113 private static final String FILE3 = "file3";
114
115
116
117
118 private static final String FILE4 = "file4";
119
120
121
122
123 private static final String VARIABLE1 = "variable1";
124
125
126
127
128 private static final String VARIABLE2 = "variable2";
129
130
131
132
133 private static final String VARIABLE3 = "variable3";
134
135
136
137
138 private static final String VARIABLE4 = "file~";
139
140
141
142
143 private static final String TEST0 = "test0";
144
145
146
147
148 private static final String VALUE1 = "TEST1";
149
150
151
152
153 private static final String VALUE2 = "TEST2";
154
155
156
157
158 private static final String VALUE3 = "TEST3";
159
160
161
162
163 private static final String MAX_VARNAME = "1234567890123456789012345678901234567890123456789012345678901234";
164
165
166
167
168 private static final String TO_LONG_VARNAME = "12345678901234567890123456789012345678901234567890123456789012345";
169
170
171
172
173 private static final String TEST = "TEST";
174
175
176
177
178 private static final String BLK1_BLK = "BLK1_BLK";
179
180
181
182
183 private static final String BLK1 = "BLK1";
184
185
186
187
188 private static final String OUTPUT = "output";
189
190
191
192
193 private static final String TEMPLATE = "template";
194
195
196
197
198 private static final String NO_TEMPLATE_FOUND = "No 'template' found";
199
200
201
202
203 private static final String VARIABLE_VALUE_NOT_AS_EXPECTED = "Variable value not as expected";
204
205
206
207
208 private static final String SET_BLOCK_NOT_AS_EXPECTED = "SetBlock not as expected";
209
210
211
212
213 private static final String TEMPLATE_FILE_TEMPLATE1_TMPL_COULD_NOT_BE_LOADED = "Template file template1.tmpl could not be loaded!";
214
215
216
217
218 private static final String EQUALS_123_VARIABLE1_456 = "123\n{variable1}\n456\n";
219
220
221
222
223 private static final String UNDEFINED_VARIABLE_S_FOUND = "Undefined variable(s) found!";
224
225
226
227
228 private static final String OUTPUT_NOT_AS_EXPECTED = "Output not as expected";
229
230
231
232
233 private static final String ILLEGAL_STATE_EXCEPTION_EXPECTED = "Illegal state exception expected";
234
235
236
237
238 private static final String BLOCK_VALUE_NOT_AS_EXPECTED = "Block value not as expected";
239
240
241
242
243 private static final String VALUE_1234 = "1234";
244
245
246
247
248 private static final String OUTPUT_VALUE_NOT_AS_EXPECTED = "Output value not as expected";
249
250
251
252
253 private static final String PARSE_RESULT_NOT_AS_EXPECTED = "Parse result not as expected";
254
255
256
257
258 private static final String FILE0 = "file0";
259
260
261
262
263 private static final String FILE5 = "file5";
264
265
266
267
268 private static final String TEMPLATE0_TMPL = "template0.tmpl";
269
270
271
272
273 private static final String NO_UNDEFINED_VARIABLE_S_FOUND = "No undefined variable(s) found!";
274
275
276
277
278 private static final String UNDEFINED_VARIABLE_NOT_AS_EXPECTED = "Undefined variable not as expected";
279
280
281
282
283 private static final String NULL_POINTER_EXCEPTION_EXPECTED = "Null pointer exception expected";
284
285
286
287
288 private static final String NEW_INSTANCE_RESULT_NOT_AS_EXPECTED = "newInstance result not as expected";
289
290
291
292
293 private static final String PARENT = "parent";
294
295
296
297
298
299 TemplateEngineTests()
300 {
301 super();
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 @Test
347 void testDefaultConstructor()
348 {
349 final TemplateEngine engine = new TemplateEngine();
350 assertNotNull(engine, "Default constructor failed!");
351 }
352
353
354
355
356
357 @Test
358 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
368
369 @Test
370 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
380
381 @Test
382 void testSetFileEmpty()
383 {
384 final TemplateEngine engine = new TemplateEngine();
385 final File tmplFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
386 assertThrows(IllegalArgumentException.class, () ->
387 {
388 engine.setFile("", tmplFile);
389 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
390 );
391 }
392
393
394
395
396
397 @Test
398 void testSetFileToLong()
399 {
400 final TemplateEngine engine = new TemplateEngine();
401 final File tmplFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
402 assertThrows(IllegalArgumentException.class, () ->
403 {
404 engine.setFile(TemplateEngineTests.TO_LONG_VARNAME, tmplFile);
405 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
406 );
407 }
408
409
410
411
412
413 @Test
414 void testSetFileWrongname()
415 {
416 final TemplateEngine engine = new TemplateEngine();
417 final File templFile = new File(TemplateEngineTests.TEMPLATE1_TMPL);
418 assertThrows(IllegalArgumentException.class, () ->
419 {
420 engine.setFile("file~1", templFile);
421 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
422 );
423 }
424
425
426
427
428
429 @Test
430 void testSetFileToLarge()
431 {
432 final TemplateEngine engine = new TemplateEngine();
433 final File templFile = new File(TemplateEngineTests.TEMPLATE10_TMPL);
434 assertThrows(IllegalArgumentException.class, () ->
435 {
436 engine.setFile(TemplateEngineTests.VARIABLE1, templFile);
437 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
438 );
439 }
440
441
442
443
444
445
446
447 @Test
448 void testSubst() throws IOException
449 {
450 final TemplateEngine engine = new TemplateEngine();
451 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
452 final String variableValue = engine.subst(TemplateEngineTests.FILE1);
453 TemplateEngineTests.LOGGER.debug("file1 = {}", variableValue);
454 assertEquals(TemplateEngineTests.EQUALS_123_VARIABLE1_456, variableValue, TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED);
455 }
456
457
458
459
460
461 @Test
462 void testSubstEmpty()
463 {
464 final TemplateEngine engine = new TemplateEngine();
465 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
466 assertThrows(IllegalArgumentException.class, () ->
467 {
468 engine.subst("");
469 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
470 );
471 }
472
473
474
475
476
477 @Test
478 void testSubstToLong()
479 {
480 final TemplateEngine engine = new TemplateEngine();
481 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
482 assertThrows(IllegalArgumentException.class, () ->
483 {
484 engine.subst(TemplateEngineTests.TO_LONG_VARNAME);
485 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
486 );
487 }
488
489
490
491
492
493 @Test
494 void testSubstWrongName()
495 {
496 final TemplateEngine engine = new TemplateEngine();
497 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
498 assertThrows(IllegalArgumentException.class, () ->
499 {
500 engine.subst(TemplateEngineTests.VARIABLE4);
501 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
502 );
503 }
504
505
506
507
508
509
510
511 @Test
512 void testSubstNull() throws IOException
513 {
514 final TemplateEngine engine = new TemplateEngine();
515 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
524
525 @Test
526 void testToLargeTemplate()
527 {
528 final TemplateEngine engine = new TemplateEngine();
529 final File templFile = new File(TemplateEngineTests.TEMPLATE10_TMPL);
530 assertThrows(IllegalArgumentException.class, () ->
531 {
532 engine.setFile(TemplateEngineTests.FILE1, templFile);
533 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
534 );
535 }
536
537
538
539
540
541
542
543 @Test
544 void testSubstMaxLength() throws IOException
545 {
546 final TemplateEngine engine = new TemplateEngine();
547 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");
550 }
551
552
553
554
555
556
557
558 @Test
559 void testGetUndefined1() throws IOException
560 {
561 final TemplateEngine engine = new TemplateEngine();
562 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
573
574
575
576 @Test
577 void testGetUndefined2() throws IOException
578 {
579 final TemplateEngine engine = new TemplateEngine();
580 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
592
593
594
595 @Test
596 void testGetUndefinedFromEmptyFile() throws IOException
597 {
598 final TemplateEngine engine = new TemplateEngine();
599 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");
602 }
603
604
605
606
607
608 @Test
609 void testGetUndefinedEmpty()
610 {
611 final TemplateEngine engine = new TemplateEngine();
612 engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
613 assertThrows(IllegalArgumentException.class, () ->
614 {
615 engine.getUndefined("");
616 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
617 );
618 }
619
620
621
622
623
624 @Test
625 void testGetUndefinedToLong()
626 {
627 final TemplateEngine engine = new TemplateEngine();
628 engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
629 assertThrows(IllegalArgumentException.class, () ->
630 {
631 engine.getUndefined(TemplateEngineTests.TO_LONG_VARNAME);
632 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
633 );
634 }
635
636
637
638
639
640 @Test
641 void testGetUndefinedWrongName()
642 {
643 final TemplateEngine engine = new TemplateEngine();
644 engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
645 assertThrows(IllegalArgumentException.class, () ->
646 {
647 engine.getUndefined(TemplateEngineTests.VARIABLE4);
648 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
649 );
650 }
651
652
653
654
655
656
657
658 @Test
659 void testGetUndefinedMaxLength() throws IOException
660 {
661 final TemplateEngine engine = new TemplateEngine();
662 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");
665 }
666
667
668
669
670
671
672
673 @Test
674 void testSetVarEmpty() throws IOException
675 {
676 final TemplateEngine engine = new TemplateEngine();
677 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!")
683 );
684 }
685
686
687
688
689
690
691
692 @Test
693 void testSetVar() throws IOException
694 {
695 final TemplateEngine engine = new TemplateEngine();
696 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
708
709
710
711 @Test
712 void testSetVarNull() throws IOException
713 {
714 final TemplateEngine engine = new TemplateEngine();
715 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)
721 );
722 }
723
724
725
726
727
728 @Test
729 void testSetEmptyVar()
730 {
731 final TemplateEngine engine = new TemplateEngine();
732 assertThrows(IllegalArgumentException.class, () ->
733 {
734 engine.setVar("", TemplateEngineTests.TEST);
735 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
736 );
737 }
738
739
740
741
742
743 @Test
744 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
754
755 @Test
756 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
769
770 @Test
771 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
784
785
786
787 @Test
788 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
798
799
800
801 @Test
802 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
816
817
818
819 @Test
820 void testGetRemoved() throws IOException
821 {
822 final TemplateEngine engine = new TemplateEngine();
823 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!"),
828 () -> assertEquals("123\n\n456\n", output, TemplateEngineTests.OUTPUT_NOT_AS_EXPECTED)
829 );
830 }
831
832
833
834
835
836
837
838 @Test
839 void testGetKeep() throws IOException
840 {
841 final TemplateEngine engine = new TemplateEngine(HandleUndefined.KEEP);
842 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
843 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");
846 }
847
848
849
850
851
852
853
854 @Test
855 void testGetComment() throws IOException
856 {
857 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
858 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
859 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);
862 }
863
864
865
866
867
868 @Test
869 void testSetBlockFailure()
870 {
871 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
872 engine.setFile(TemplateEngineTests.FILE2, new File(TemplateEngineTests.TEMPLATE2_TMPL));
873 assertThrows(IllegalStateException.class, () ->
874 {
875 engine.setBlock(TemplateEngineTests.FILE2, "blk2");
876 }, TemplateEngineTests.ILLEGAL_STATE_EXCEPTION_EXPECTED
877 );
878 }
879
880
881
882
883
884 @Test
885 void testSetBlockEmpty()
886 {
887 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
888 assertThrows(IllegalArgumentException.class, () ->
889 {
890 engine.setBlock("", "");
891 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
892 );
893 }
894
895
896
897
898
899 @Test
900 void testSetBlockMixed()
901 {
902 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
903 assertThrows(IllegalArgumentException.class, () ->
904 {
905 engine.setBlock(TemplateEngineTests.FILE2, "");
906 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
907 );
908 }
909
910
911
912
913
914
915
916 @Test
917 void testSetBlock() throws IOException
918 {
919 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
920 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!"),
925 () -> assertEquals("\n789\n{variable2}\nabc\n", block, TemplateEngineTests.BLOCK_VALUE_NOT_AS_EXPECTED)
926 );
927 }
928
929
930
931
932
933 @Test
934 void testSetBlockParentToLong()
935 {
936 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
937 assertThrows(IllegalArgumentException.class, () ->
938 {
939 engine.setBlock(TemplateEngineTests.TO_LONG_VARNAME, TemplateEngineTests.VARIABLE1, "");
940 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
941 );
942 }
943
944
945
946
947
948 @Test
949 void testSetBlockVarnameToLong()
950 {
951 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
952 assertThrows(IllegalArgumentException.class, () ->
953 {
954 engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TO_LONG_VARNAME, "");
955 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
956 );
957 }
958
959
960
961
962
963 @Test
964 void testSetBlockNameToLong()
965 {
966 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
967 assertThrows(IllegalArgumentException.class, () ->
968 {
969 engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE_1234, TemplateEngineTests.TO_LONG_VARNAME);
970 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
971 );
972 }
973
974
975
976
977
978 @Test
979 void testSetBlockParentWrong()
980 {
981 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
982 assertThrows(IllegalArgumentException.class, () ->
983 {
984 engine.setBlock(TemplateEngineTests.VARIABLE4, TemplateEngineTests.VARIABLE1, "");
985 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
986 );
987 }
988
989
990
991
992
993 @Test
994 void testSetBlockVarnameWrong()
995 {
996 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
997 assertThrows(IllegalArgumentException.class, () ->
998 {
999 engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VARIABLE4, "");
1000 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1001 );
1002 }
1003
1004
1005
1006
1007
1008 @Test
1009 void testSetBlockNameWrong()
1010 {
1011 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1012 assertThrows(IllegalArgumentException.class, () ->
1013 {
1014 engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VALUE_1234, TemplateEngineTests.VARIABLE4);
1015 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1016 );
1017 }
1018
1019
1020
1021
1022
1023
1024
1025 @Test
1026 void testSetBlockParentMaxLength() throws IOException
1027 {
1028 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1029 final boolean successBlock = engine.setBlock(TemplateEngineTests.MAX_VARNAME, TemplateEngineTests.VARIABLE1, "");
1030 assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1031 }
1032
1033
1034
1035
1036
1037
1038
1039 @Test
1040 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
1050
1051
1052
1053 @Test
1054 void testSetBlockVarnameMaxLength() throws IOException
1055 {
1056 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1057 final boolean successBlock = engine.setBlock(TemplateEngineTests.VARIABLE1, TemplateEngineTests.MAX_VARNAME, "");
1058 assertFalse(successBlock, TemplateEngineTests.SET_BLOCK_NOT_AS_EXPECTED);
1059 }
1060
1061
1062
1063
1064
1065
1066
1067 @Test
1068 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
1078
1079
1080
1081 @Test
1082 void testParseNonAppend() throws IOException
1083 {
1084
1085 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1086 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1091 engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, false);
1092 engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, false);
1093
1094 assertEquals("\n789\nTEST2\nabc\n", engine.getVar(TemplateEngineTests.BLK1_BLK), TemplateEngineTests.BLOCK_VALUE_NOT_AS_EXPECTED);
1095 }
1096
1097
1098
1099
1100
1101
1102
1103 @Test
1104 void testParseEmpty() throws IOException
1105 {
1106 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1107 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1112 assertThrows(IllegalArgumentException.class, () ->
1113 {
1114 engine.parse("", "", false);
1115 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1116 );
1117 }
1118
1119
1120
1121
1122
1123
1124
1125 @Test
1126 void testParseMixed() throws IOException
1127 {
1128 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1129 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1134 assertThrows(IllegalArgumentException.class, () ->
1135 {
1136 engine.parse(TemplateEngineTests.BLK1_BLK, "", false);
1137 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1138 );
1139 }
1140
1141
1142
1143
1144
1145
1146
1147 @Test
1148 void testParseAppend() throws IOException
1149 {
1150 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1151 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1156 engine.parse(TemplateEngineTests.BLK1_BLK, TemplateEngineTests.BLK1, true);
1157 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);
1159 }
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178 @Test
1179 @SuppressWarnings("checkstyle:CommentsIndentation")
1180 void testParseProblem() throws IOException
1181 {
1182 final TemplateEngine engine = new TemplateEngine(HandleUndefined.KEEP);
1183 engine.setFile(TemplateEngineTests.FILE3, new File(TemplateEngineTests.TEMPLATE3_TMPL));
1184 engine.setVar(TemplateEngineTests.TEST0, "000");
1185 engine.setVar("test1", "111");
1186 engine.setVar("test3", "333");
1187 final boolean successBlock = engine.setBlock(TemplateEngineTests.FILE3, "test2");
1188
1189 final String output = engine.parse(TemplateEngineTests.OUTPUT, TemplateEngineTests.FILE3);
1190 assertAll(
1191 () -> assertTrue(successBlock, "Could not cut out block!"),
1192 () -> assertEquals("000 \n111 \n \nabc {test1} def 333 ghi \n \n333 \n000 \n", output, TemplateEngineTests.OUTPUT_VALUE_NOT_AS_EXPECTED)
1193
1194
1195 );
1196
1197
1198 }
1199
1200
1201
1202
1203
1204
1205
1206 @Test
1207 void testParseTargetToLong() throws IOException
1208 {
1209 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1210 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1215 assertThrows(IllegalArgumentException.class, () ->
1216 {
1217 engine.parse(TemplateEngineTests.TO_LONG_VARNAME, TemplateEngineTests.VARIABLE1, false);
1218 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1219 );
1220 }
1221
1222
1223
1224
1225
1226
1227
1228 @Test
1229 void testParseVarnameToLong() throws IOException
1230 {
1231 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1232 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1237 assertThrows(IllegalArgumentException.class, () ->
1238 {
1239 engine.parse(TemplateEngineTests.VARIABLE1, TemplateEngineTests.TO_LONG_VARNAME, false);
1240 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1241 );
1242 }
1243
1244
1245
1246
1247
1248
1249
1250 @Test
1251 void testParseTargetWrong() throws IOException
1252 {
1253 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1254 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1259 assertThrows(IllegalArgumentException.class, () ->
1260 {
1261 engine.parse(TemplateEngineTests.VARIABLE4, TemplateEngineTests.VARIABLE1, false);
1262 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1263 );
1264 }
1265
1266
1267
1268
1269
1270
1271
1272 @Test
1273 void testParseVarnameWrong() throws IOException
1274 {
1275 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1276 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 engine.setBlock(TemplateEngineTests.FILE2, TemplateEngineTests.BLK1, TemplateEngineTests.BLK1_BLK);
1281 assertThrows(IllegalArgumentException.class, () ->
1282 {
1283 engine.parse(TemplateEngineTests.VARIABLE1, TemplateEngineTests.VARIABLE4, false);
1284 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1285 );
1286 }
1287
1288
1289
1290
1291
1292
1293
1294 @Test
1295 void testParseTargetMaxLength() throws IOException
1296 {
1297 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1298 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 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
1310
1311
1312
1313 @Test
1314 void testParseVarnameMaxLength() throws IOException
1315 {
1316 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1317 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 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);
1324 }
1325
1326
1327
1328
1329
1330 @Test
1331 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!");
1336 }
1337
1338
1339
1340
1341
1342 @Test
1343 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!"),
1351 () -> assertNotNull(variables.get(0), "No variable in list!")
1352 );
1353 }
1354
1355
1356
1357
1358
1359 @Test
1360 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");
1365 }
1366
1367
1368
1369
1370
1371 @Test
1372 void testGetVarToLong()
1373 {
1374 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1375 assertThrows(IllegalArgumentException.class, () ->
1376 {
1377 engine.getVar(TemplateEngineTests.TO_LONG_VARNAME);
1378 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1379 );
1380 }
1381
1382
1383
1384
1385
1386 @Test
1387 void testGetVarWrongName()
1388 {
1389 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1390 assertThrows(IllegalArgumentException.class, () ->
1391 {
1392 engine.getVar(TemplateEngineTests.VARIABLE4);
1393 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1394 );
1395 }
1396
1397
1398
1399
1400
1401
1402
1403 @Test
1404 void testSubstEmptyFile() throws IOException
1405 {
1406 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1407 engine.setFile(TemplateEngineTests.FILE4, new File(TemplateEngineTests.TEMPLATE4_TMPL));
1408 final String result = engine.subst(TemplateEngineTests.FILE4);
1409 assertEquals("", result, "Template not empty!");
1410 }
1411
1412
1413
1414
1415
1416
1417
1418 @Test
1419 void testSetBlockEmptyFile() throws IOException
1420 {
1421 final TemplateEngine engine = new TemplateEngine(HandleUndefined.COMMENT);
1422 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!");
1425 }
1426
1427
1428
1429
1430
1431
1432
1433 @Test
1434 void testReadFromClasspath() throws IOException
1435 {
1436 final TemplateEngine engine = new TemplateEngine();
1437 final boolean success = engine.setFile(TemplateEngineTests.FILE5, new File("template5.tmpl"));
1438 final String variableValue = engine.subst(TemplateEngineTests.FILE5);
1439 assertAll(
1440 () -> assertTrue(success, "Template file template5.tmpl could not be set!"),
1441 () -> assertNotNull(variableValue, "Template file template5.tmpl could not be loaded from classpath")
1442 );
1443 }
1444
1445
1446
1447
1448
1449
1450
1451 @Test
1452 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!"),
1459 () -> assertEquals("", variableValue, "Template file template0.tmpl could be loaded from classpath")
1460 );
1461 }
1462
1463
1464
1465
1466
1467
1468
1469 @Test
1470 void testUnsetVar() throws IOException
1471 {
1472 final TemplateEngine engine = new TemplateEngine();
1473 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
1486
1487 @Test
1488 void testUnsetVarEmpty()
1489 {
1490 final TemplateEngine engine = new TemplateEngine();
1491 assertThrows(IllegalArgumentException.class, () ->
1492 {
1493 engine.unsetVar("");
1494 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1495 );
1496 }
1497
1498
1499
1500
1501
1502
1503
1504 @Test
1505 void testUnsetVarMaxLength() throws IOException
1506 {
1507 final TemplateEngine engine = new TemplateEngine();
1508 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
1521
1522 @Test
1523 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
1536
1537 @Test
1538 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
1551
1552 @Test
1553 void testToString()
1554 {
1555 final TemplateEngine engine = new TemplateEngine();
1556 engine.setFile(TemplateEngineTests.FILE1, new File(TemplateEngineTests.TEMPLATE1_TMPL));
1557 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");
1561 }
1562
1563
1564
1565
1566
1567
1568
1569 @Test
1570 void testNewInstanceFile1() throws IOException
1571 {
1572 final TemplateEngine engine = TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE1_TMPL));
1573 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
1584
1585 @Test
1586 void testNewInstanceFileNonExisting()
1587 {
1588 assertThrows(FileNotFoundException.class, () ->
1589 {
1590 TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE0_TMPL));
1591 }, "File not found exception expected"
1592 );
1593 }
1594
1595
1596
1597
1598
1599 @Test
1600 void testNewInstanceFileFromDirectory()
1601 {
1602 final File file = new File("target/test-classes/templates/");
1603 assertThrows(AssertionError.class, () ->
1604 {
1605 TemplateEngine.newInstance(file);
1606 }, TemplateEngineTests.ASSERTION_ERROR_EXPECTED
1607 );
1608 }
1609
1610
1611
1612
1613
1614 @Test
1615 void testNewInstanceFileNull()
1616 {
1617 assertThrows(NullPointerException.class, () ->
1618 {
1619 TemplateEngine.newInstance((File)null);
1620 }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1621 );
1622 }
1623
1624
1625
1626
1627
1628 @Test
1629 void testNewInstanceFileToLarge()
1630 {
1631 assertThrows(IOException.class, () ->
1632 {
1633 TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE10_TMPL));
1634 }, "IO exception expected"
1635 );
1636 }
1637
1638
1639
1640
1641
1642
1643
1644 @Test
1645 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
1654
1655 @Test
1656 void testNewInstanceInputStreamNull()
1657 {
1658 assertThrows(NullPointerException.class, () ->
1659 {
1660 TemplateEngine.newInstance((InputStream)null);
1661 }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1662 );
1663 }
1664
1665
1666
1667
1668
1669
1670
1671 @Test
1672 void testNewInstanceInputStream() throws IOException
1673 {
1674 try (InputStream stream = this.getClass().getResourceAsStream("/template5.tmpl"))
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
1688
1689
1690
1691 @Test
1692 void testNewInstanceInputStreamEmpty() throws IOException
1693 {
1694 try (InputStream stream = this.getClass().getResourceAsStream("/template6.tmpl"))
1695 {
1696 assertThrows(IllegalStateException.class, () ->
1697 {
1698 TemplateEngine.newInstance(stream);
1699 }, TemplateEngineTests.ILLEGAL_STATE_EXCEPTION_EXPECTED
1700 );
1701 }
1702 }
1703
1704
1705
1706
1707
1708 @Test
1709 void testNewInstanceStringNull()
1710 {
1711 assertThrows(NullPointerException.class, () ->
1712 {
1713 TemplateEngine.newInstance((String)null);
1714 }, TemplateEngineTests.NULL_POINTER_EXCEPTION_EXPECTED
1715 );
1716 }
1717
1718
1719
1720
1721
1722 @Test
1723 void testNewInstanceStringEmpty()
1724 {
1725 assertThrows(IllegalArgumentException.class, () ->
1726 {
1727 TemplateEngine.newInstance("");
1728 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1729 );
1730 }
1731
1732
1733
1734
1735
1736 @Test
1737 void testNewInstanceString()
1738 {
1739 final TemplateEngine engine = TemplateEngine.newInstance("123\r\n{variable1}\r\n456\r\n");
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
1750
1751
1752
1753
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
1763
1764
1765
1766 @Test
1767 void testNewInstanceStringToLarge() throws IOException
1768 {
1769 final String templ = readStringFromFile(new File(TemplateEngineTests.TEMPLATE10_TMPL));
1770 assertThrows(IllegalArgumentException.class, () ->
1771 {
1772 TemplateEngine.newInstance(templ);
1773 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1774 );
1775 }
1776
1777
1778
1779
1780
1781
1782
1783 @Test
1784 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
1793
1794
1795
1796 @Test
1797 void testNewInstanceCopy() throws IOException
1798 {
1799 final TemplateEngine engine1 = TemplateEngine.newInstance(new File(TemplateEngineTests.TEMPLATE1_TMPL));
1800 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
1812
1813 @Test
1814 void testCopyConstructor()
1815 {
1816 final TemplateEngine engine1 = new TemplateEngine();
1817 final TemplateEngine engine2 = new TemplateEngine(engine1);
1818 assertNotNull(engine2, "Copy constructor failed!");
1819 }
1820
1821
1822
1823
1824
1825 @Test
1826 void testGetVarEmpty()
1827 {
1828 final TemplateEngine engine = new TemplateEngine();
1829 assertThrows(IllegalArgumentException.class, () ->
1830 {
1831 engine.getVar("");
1832 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1833 );
1834 }
1835
1836
1837
1838
1839
1840 @Test
1841 void testFinishEmpty()
1842 {
1843 final TemplateEngine engine = new TemplateEngine();
1844 assertThrows(IllegalArgumentException.class, () ->
1845 {
1846 engine.finish("");
1847 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1848 );
1849 }
1850
1851
1852
1853
1854
1855
1856
1857 @Test
1858 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 engine.finish(templ);
1865 }, TemplateEngineTests.ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
1866 );
1867 }
1868
1869
1870
1871
1872
1873
1874
1875 @Test
1876 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");
1881 }
1882
1883
1884
1885
1886
1887
1888
1889 @Test
1890 void testDoubleOpenCurlyBrace() throws IOException
1891 {
1892 final TemplateEngine engine = new TemplateEngine();
1893 engine.setFile(TemplateEngineTests.FILE1, new File("target/test-classes/templates/template7.tmpl"));
1894 engine.setVar(TemplateEngineTests.VARIABLE1, "");
1895 final String variableValue = engine.subst(TemplateEngineTests.FILE1);
1896 assertEquals("123\n{\n456\n", variableValue, TemplateEngineTests.VARIABLE_VALUE_NOT_AS_EXPECTED);
1897 }
1898
1899
1900
1901
1902
1903 @Test
1904 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",
1910 () -> assertEquals(tmpl1.hashCode(), tmpl2.hashCode(), "hashCodes are not equal"),
1911 () -> assertNotEquals(tmpl1.hashCode(), tmpl3.hashCode(), "hashCodes are equal")
1912 );
1913 }
1914
1915
1916
1917
1918
1919
1920
1921 @Test
1922 @SuppressFBWarnings("EC_NULL_ARG")
1923 @SuppressWarnings({"PMD.EqualsNull", "java:S5785"})
1924 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");
1934 tmpl6.setFile("file", new File(TemplateEngineTests.TEMPLATE1_TMPL));
1935 tmpl7.setVar(PARENT, "before<!-- BEGIN blktest -->content<!-- END blktest -->after");
1936 tmpl7.setBlock(PARENT, "blktest");
1937 assertAll("testEquals",
1938 () -> assertTrue(tmpl1.equals(tmpl1), "TemplateEngine11 is not equal"),
1939 () -> assertTrue(tmpl1.equals(tmpl2), "TemplateEngine12 are not equal"),
1940 () -> assertTrue(tmpl2.equals(tmpl1), "TemplateEngine21 are not equal"),
1941 () -> assertTrue(tmpl2.equals(tmpl4), "TemplateEngine24 are not equal"),
1942 () -> assertTrue(tmpl1.equals(tmpl4), "TemplateEngine14 are not equal"),
1943 () -> assertFalse(tmpl1.equals(tmpl3), "TemplateEngine13 are equal"),
1944 () -> assertFalse(tmpl3.equals(tmpl1), "TemplateEngine31 are equal"),
1945 () -> assertFalse(tmpl1.equals(null), "TemplateEngine10 is equal"),
1946 () -> assertFalse(tmpl1.equals(tmpl5), "TemplateEngine15 is equal"),
1947 () -> assertFalse(tmpl1.equals(tmpl6), "TemplateEngine16 is equal"),
1948 () -> assertFalse(tmpl1.equals(tmpl7), "TemplateEngine17 is equal")
1949 );
1950 }
1951
1952 }