]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/template/TestTemplate.java
ADD: testcases for the templates.
[gigi.git] / tests / org / cacert / gigi / template / TestTemplate.java
1 package org.cacert.gigi.template;
2
3 import static org.junit.Assert.*;
4
5 import java.io.CharArrayWriter;
6 import java.io.PrintWriter;
7 import java.io.StringReader;
8 import java.util.HashMap;
9 import java.util.Locale;
10 import java.util.Map;
11
12 import org.cacert.gigi.dbObjects.Digest;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.Outputable;
15 import org.cacert.gigi.output.template.HashAlgorithms;
16 import org.cacert.gigi.output.template.IterableDataset;
17 import org.cacert.gigi.output.template.OutputableArrayIterable;
18 import org.cacert.gigi.output.template.Template;
19 import org.junit.Test;
20
21 public class TestTemplate {
22
23     private String testExecute(Language l, HashMap<String, Object> vars, String input) {
24         Template t = new Template(new StringReader(input));
25         CharArrayWriter caw = new CharArrayWriter();
26         PrintWriter pw = new PrintWriter(caw);
27         t.output(pw, l, vars);
28         pw.flush();
29         return new String(caw.toCharArray());
30     }
31
32     HashMap<String, Object> vars = new HashMap<>();
33
34     @Test
35     public void testVarEscape() {
36         vars.put("var", "val");
37         assertEquals("vall", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=$var?>l"));
38         vars.put("var", "val<");
39         assertEquals("val&lt;l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=$var?>l"));
40         assertEquals("val<l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=$!var?>l"));
41         vars.put("var", "val\">");
42         assertEquals("val&quot;&gt;l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=$var?>l"));
43         assertEquals("val\">l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=$!var?>l"));
44
45     }
46
47     @Test
48     public void testVarSprintf() {
49         vars.put("var", "val\">");
50         vars.put("var2", "val3<\"");
51         vars.put("var3", "val4>");
52         assertEquals("This val&quot;&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s text?>l"));
53         assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s text?>l"));
54
55         assertEquals("This val&quot;&gt; val3&lt;&quot; the val4&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s %s the %s text?>l"));
56         assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s %s the %s text?>l"));
57
58         assertEquals("This blargh&lt;&gt;!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,\"blargh<>!\",!\"<>!\",This %s, %s text?>l"));
59     }
60
61     @Test
62     public void testIf() {
63         vars.put("existent", "val");
64         assertEquals(">existent<", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? if($existent) { ?>>existent<? } ?><<? if($nonexistent) { ?>nonexistent<? } ?>"));
65     }
66
67     @Test
68     public void testForeach() {
69         vars.put("it", new IterableDataset() {
70
71             int i = 0;
72
73             @Override
74             public boolean next(Language l, Map<String, Object> vars) {
75                 vars.put("e", Integer.toString(i++));
76                 return i < 10;
77             }
78         });
79         assertEquals("012345678<", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? foreach($it) { ?><?=$e?><? } ?><<? foreach($nonexistent) { ?>nonexistent<? } ?>"));
80     }
81
82     @Test
83     public void testNullContent() {
84         assertEquals("<null>", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<<?=$undef?>>"));
85
86     }
87
88     @Test
89     public void testTranslate() {
90         assertEquals("&lt;null&gt;", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_<null>?>"));
91
92     }
93
94     @Test
95     public void testOutputable() {
96         Outputable o = new Outputable() {
97
98             @Override
99             public void output(PrintWriter out, Language l, Map<String, Object> vars) {
100                 out.print("bl");
101             }
102         };
103         vars.put("v", new OutputableArrayIterable(new Object[] {
104                 o, o, o, o, o
105         }, "e"));
106         assertEquals("[0]bl[1]bl[2]bl[3]bl[4]bl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? foreach($v) { ?>[<?=$i?>]<?=$e?><? } ?>"));
107
108     }
109
110     @Test
111     public void testHashalgs() {
112         vars.put("v", new HashAlgorithms(Digest.SHA256));
113         assertEquals("SHA256[ checked='checked']SHA384[]SHA512[]", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? foreach($v) { ?><?=$algorithm?>[<?=$!checked?>]<? } ?>"));
114
115     }
116
117 }