1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.plugin.http.tcpproxyfilter;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27
28 import java.text.ParseException;
29
30 import org.junit.After;
31 import org.junit.Test;
32
33 import HTTPClient.Codecs;
34
35
36
37
38
39
40
41 public class TestXSLTHelper {
42
43 @After public void setUp() throws Exception {
44 XSLTHelper.resetIndent();
45 XSLTHelper.setIndentString(" ");
46 }
47
48 @Test public void testBase64ToPython() throws Exception {
49 final byte[] bytes0 = { 0, -42, 1, 22, };
50
51 assertEquals("\"\\x00\\xD6\\x01\\x16\"",
52 XSLTHelper.base64ToPython(
53 new String(Codecs.base64Encode(bytes0))));
54
55 XSLTHelper.changeIndent(2);
56
57 assertEquals("\"\\x00\\xD6\\x01\\x16\"",
58 XSLTHelper.base64ToPython(new String(Codecs.base64Encode(bytes0))));
59
60 assertEquals("\"\"", XSLTHelper.base64ToPython(""));
61
62 final byte[] bytes1 = new byte[300];
63
64 for (int i = 0; i < bytes1.length; ++i) {
65 bytes1[i] = (byte) i;
66 }
67
68 assertEquals(
69 "\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F\"\n" +
70 " \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n" +
71 " \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\\x28\\x29\\x2A\\x2B\\x2C\\x2D\\x2E\\x2F\"\n" +
72 " \"\\x30\\x31\\x32\\x33\\x34\\x35\\x36\\x37\\x38\\x39\\x3A\\x3B\\x3C\\x3D\\x3E\\x3F\"\n" +
73 " \"\\x40\\x41\\x42\\x43\\x44\\x45\\x46\\x47\\x48\\x49\\x4A\\x4B\\x4C\\x4D\\x4E\\x4F\"\n" +
74 " \"\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E\\x0F\"\n" +
86 " \"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F\"\n" +
87 " \"\\x20\\x21\\x22\\x23\\x24\\x25\\x26\\x27\\x28\\x29\\x2A\\x2B\"",
88 XSLTHelper.base64ToPython(new String(Codecs.base64Encode(bytes1))));
89 }
90
91 @Test public void testBase64ToClojure() throws Exception {
92 final byte[] bytes0 = { 0, -42, 1, 22, };
93
94 assertEquals("[0x00 0xD6 0x01 0x16]",
95 XSLTHelper.base64ToClojure(
96 new String(Codecs.base64Encode(bytes0))));
97
98 XSLTHelper.changeIndent(2);
99
100 assertEquals("[0x00 0xD6 0x01 0x16]",
101 XSLTHelper.base64ToClojure(new String(Codecs.base64Encode(bytes0))));
102
103 assertEquals("[]", XSLTHelper.base64ToClojure(""));
104
105 final byte[] bytes1 = new byte[300];
106
107 for (int i = 0; i < bytes1.length; ++i) {
108 bytes1[i] = (byte) i;
109 }
110
111 assertEquals(
112 "[0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F\n" +
113 " 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F\n" +
114 " 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2A 0x2B 0x2C 0x2D 0x2E 0x2F\n" +
115 " 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x3B 0x3C 0x3D 0x3E 0x3F\n" +
116 " 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F\n" +
117 " 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A 0x5B 0x5C 0x5D 0x5E 0x5F\n" +
118 " 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6A 0x6B 0x6C 0x6D 0x6E 0x6F\n" +
119 " 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7A 0x7B 0x7C 0x7D 0x7E 0x7F\n" +
120 " 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8A 0x8B 0x8C 0x8D 0x8E 0x8F\n" +
121 " 0x90 0x91 0x92 0x93 0x94 0x95 0x96 0x97 0x98 0x99 0x9A 0x9B 0x9C 0x9D 0x9E 0x9F\n" +
122 " 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 0xA6 0xA7 0xA8 0xA9 0xAA 0xAB 0xAC 0xAD 0xAE 0xAF\n" +
123 " 0xB0 0xB1 0xB2 0xB3 0xB4 0xB5 0xB6 0xB7 0xB8 0xB9 0xBA 0xBB 0xBC 0xBD 0xBE 0xBF\n" +
124 " 0xC0 0xC1 0xC2 0xC3 0xC4 0xC5 0xC6 0xC7 0xC8 0xC9 0xCA 0xCB 0xCC 0xCD 0xCE 0xCF\n" +
125 " 0xD0 0xD1 0xD2 0xD3 0xD4 0xD5 0xD6 0xD7 0xD8 0xD9 0xDA 0xDB 0xDC 0xDD 0xDE 0xDF\n" +
126 " 0xE0 0xE1 0xE2 0xE3 0xE4 0xE5 0xE6 0xE7 0xE8 0xE9 0xEA 0xEB 0xEC 0xED 0xEE 0xEF\n" +
127 " 0xF0 0xF1 0xF2 0xF3 0xF4 0xF5 0xF6 0xF7 0xF8 0xF9 0xFA 0xFB 0xFC 0xFD 0xFE 0xFF\n" +
128 " 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F\n" +
129 " 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F\n" +
130 " 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2A 0x2B]",
131 XSLTHelper.base64ToClojure(new String(Codecs.base64Encode(bytes1))));
132 }
133
134 @Test public void testFormatTime() throws Exception {
135 try {
136 XSLTHelper.formatTime("abc");
137 fail("Expected ParseException");
138 }
139 catch (ParseException e) {
140 }
141
142 final String s = XSLTHelper.formatTime("2005-01-04T18:30:00");
143 assertNotNull(s);
144 }
145
146 @Test public void testQuoteForPython() throws Exception {
147 assertEquals("None", XSLTHelper.quoteForPython(null));
148 assertEquals("''", XSLTHelper.quoteForPython(""));
149 assertEquals("\'\\\"\'", XSLTHelper.quoteForPython("\""));
150 assertEquals("'foo'", XSLTHelper.quoteForPython("foo"));
151 assertEquals("'foo\\''", XSLTHelper.quoteForPython("foo'"));
152 assertEquals("' \\\\ '", XSLTHelper.quoteForPython(" \\ "));
153 assertEquals("'''foo \n bah'''", XSLTHelper.quoteForPython("foo \n bah"));
154 assertEquals("'''foo \\r bah'''", XSLTHelper.quoteForPython("foo \r bah"));
155 assertEquals("'foo \\\\n bah'", XSLTHelper.quoteForPython("foo \\n bah"));
156 }
157
158 @Test public void testQuoteEOLEscapedStringForPython() throws Exception {
159 assertEquals("None", XSLTHelper.quoteEOLEscapedStringForPython(null));
160 assertEquals("''", XSLTHelper.quoteEOLEscapedStringForPython(""));
161 assertEquals("\'\\\"\'", XSLTHelper.quoteEOLEscapedStringForPython("\""));
162 assertEquals("'foo'", XSLTHelper.quoteEOLEscapedStringForPython("foo"));
163 assertEquals("'foo\\''", XSLTHelper.quoteEOLEscapedStringForPython("foo'"));
164 assertEquals("' \\\\ '", XSLTHelper.quoteEOLEscapedStringForPython(" \\ "));
165 assertEquals("'''foo bah'''",
166 XSLTHelper.quoteEOLEscapedStringForPython("foo \n \r bah"));
167 assertEquals("'foo \\n bah\\\\'",
168 XSLTHelper.quoteEOLEscapedStringForPython("foo \\n bah\\"));
169 }
170
171 @Test public void testQuoteForClojure() throws Exception {
172 assertEquals("nil", XSLTHelper.quoteForClojure(null));
173 assertEquals("\"\"", XSLTHelper.quoteForClojure(""));
174 assertEquals("\"\\\"\"", XSLTHelper.quoteForClojure("\""));
175 assertEquals("\"\\\"\"", XSLTHelper.quoteForClojure("\""));
176 assertEquals("\"foo\"", XSLTHelper.quoteForClojure("foo"));
177 assertEquals("\"foo\\'\"", XSLTHelper.quoteForClojure("foo'"));
178 assertEquals("\" \\\\ \"", XSLTHelper.quoteForClojure(" \\ "));
179 assertEquals("\"foo \n bah\"", XSLTHelper.quoteForClojure("foo \n bah"));
180 assertEquals("\"foo \\r bah\"", XSLTHelper.quoteForClojure("foo \r bah"));
181 assertEquals("\"foo \\\\n bah\"", XSLTHelper.quoteForClojure("foo \\n bah"));
182 }
183
184 @Test public void testQuoteEOLEscapedStringForClojure() throws Exception {
185 assertEquals("nil", XSLTHelper.quoteEOLEscapedStringForClojure(null));
186 assertEquals("\"\"", XSLTHelper.quoteEOLEscapedStringForClojure(""));
187 assertEquals("\"\\\"\"", XSLTHelper.quoteEOLEscapedStringForClojure("\""));
188 assertEquals("\"foo\"", XSLTHelper.quoteEOLEscapedStringForClojure("foo"));
189 assertEquals("\"foo\\'\"", XSLTHelper.quoteEOLEscapedStringForClojure("foo'"));
190 }
191
192 @Test public void testEscape() throws Exception {
193 assertEquals("", XSLTHelper.escape(""));
194 assertEquals("\\'", XSLTHelper.escape("'"));
195 assertEquals("\\\"", XSLTHelper.escape("\""));
196 assertEquals("\\\\", XSLTHelper.escape("\\"));
197 assertEquals("Hello \\'quoted\\\" \\\\world",
198 XSLTHelper.escape("Hello 'quoted\" \\world"));
199 }
200
201 @Test public void testSummariseAsLine() throws Exception {
202 assertEquals("blah, blah", XSLTHelper.summariseAsLine("blah, blah", 20));
203 assertEquals("blah,...", XSLTHelper.summariseAsLine("blah, blah", 5));
204 assertEquals("blah,\\nblah", XSLTHelper.summariseAsLine("blah,\nblah", 20));
205 assertEquals("\\r blah,\\t", XSLTHelper.summariseAsLine("\r blah,\t", 20));
206 assertEquals("..bla...", XSLTHelper.summariseAsLine("\0\0blah", 5));
207 }
208
209 @Test public void testIndent() throws Exception {
210 assertEquals("", XSLTHelper.indent());
211
212 XSLTHelper.changeIndent(2);
213 assertEquals(" ", XSLTHelper.indent());
214
215 XSLTHelper.setIndentString("\t");
216 assertEquals("\t\t", XSLTHelper.indent());
217 }
218
219 @Test(expected=UnsupportedOperationException.class)
220 public void coverConstructor() throws Exception {
221 new XSLTHelper();
222 }
223 }