1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.velocity.tools; |
22 |
|
|
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.LinkedHashMap; |
26 |
|
|
27 |
|
import org.junit.Assert; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Test; |
30 |
|
|
31 |
|
|
32 |
|
@link |
33 |
|
|
34 |
|
@version |
35 |
|
@since |
36 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (89) |
Complexity: 25 |
Complexity Density: 0.39 |
|
37 |
|
public class EscapeToolTest |
38 |
|
{ |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
private EscapeTool tool; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
47 |
24 |
@Before... |
48 |
|
public void setUp() |
49 |
|
{ |
50 |
24 |
this.tool = new EscapeTool(); |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
53 |
1 |
@Test... |
54 |
|
public void escapeSimpleXML() |
55 |
|
{ |
56 |
1 |
String escapedText = this.tool.xml("a < a' && a' < a\" => a < a\""); |
57 |
|
|
58 |
1 |
Assert.assertFalse("Failed to escape <", escapedText.contains("<")); |
59 |
1 |
Assert.assertFalse("Failed to escape >", escapedText.contains(">")); |
60 |
1 |
Assert.assertFalse("Failed to escape '", escapedText.contains("'")); |
61 |
1 |
Assert.assertFalse("Failed to escape \"", escapedText.contains("\"")); |
62 |
1 |
Assert.assertFalse("Failed to escape &", escapedText.contains("&&")); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
65 |
1 |
@Test... |
66 |
|
public void escapeXMLApos() |
67 |
|
{ |
68 |
1 |
Assert.assertFalse("' wrongly escaped to non-HTML '", this.tool.xml("'").equals("'")); |
69 |
|
} |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
71 |
1 |
@Test... |
72 |
|
public void escapeXMLWithNull() |
73 |
|
{ |
74 |
1 |
Assert.assertNull("null should be null", this.tool.xml(null)); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
77 |
1 |
@Test... |
78 |
|
public void escapeXMLNonAscii() |
79 |
|
{ |
80 |
1 |
Assert.assertTrue("Non-ASCII characters shouldn't be escaped", this.tool.xml("\u0123").equals("\u0123")); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
83 |
1 |
@Test... |
84 |
|
public void escapeJSON() |
85 |
|
{ |
86 |
1 |
String escapedText = this.tool.json("\"'\\/\b\f\n\r\t\u1234 plain text"); |
87 |
|
|
88 |
1 |
Assert.assertTrue("Failed to escape [\"]", escapedText.contains("\\\"")); |
89 |
1 |
Assert.assertTrue("Wrongly escaped [']", escapedText.contains("'")); |
90 |
1 |
Assert.assertTrue("Failed to escape [\\]", escapedText.contains("\\\\")); |
91 |
1 |
Assert.assertTrue("Failed to escape [/]", escapedText.contains("\\/")); |
92 |
1 |
Assert.assertTrue("Failed to escape [\\b]", escapedText.contains("\\b")); |
93 |
1 |
Assert.assertTrue("Failed to escape [\\f]", escapedText.contains("\\f")); |
94 |
1 |
Assert.assertTrue("Failed to escape [\\n]", escapedText.contains("\\n")); |
95 |
1 |
Assert.assertTrue("Failed to escape [\\r]", escapedText.contains("\\r")); |
96 |
1 |
Assert.assertTrue("Failed to escape [\\t]", escapedText.contains("\\t")); |
97 |
1 |
Assert.assertTrue("Failed to escape [\\u1234]", escapedText.contains("\\u1234")); |
98 |
1 |
Assert.assertTrue("Wrongly escaped plain text", escapedText.contains(" plain text")); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
101 |
1 |
@Test... |
102 |
|
public void escapeJSONWithNullInput() |
103 |
|
{ |
104 |
1 |
Assert.assertNull("Unexpected non-null output for null input", this.tool.json(null)); |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
107 |
1 |
@Test... |
108 |
|
public void escapeJSONWithNonStringInput() |
109 |
|
{ |
110 |
1 |
Assert.assertEquals("true", this.tool.json(true)); |
111 |
1 |
Assert.assertEquals("42", this.tool.json(42)); |
112 |
1 |
Assert.assertEquals(this.tool.toString(), this.tool.json(this.tool)); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void quotedPrintableWithSimpleText() |
117 |
|
{ |
118 |
1 |
Assert.assertEquals("Hello World", this.tool.quotedPrintable("Hello World")); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
121 |
1 |
@Test... |
122 |
|
public void quotedPrintableWithSpecialChars() |
123 |
|
{ |
124 |
1 |
Assert.assertEquals("a=3Db=0A", this.tool.quotedPrintable("a=b\n")); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
public void quotedPrintableWithNonAsciiChars() |
129 |
|
{ |
130 |
1 |
Assert.assertEquals("=C4=A3", this.tool.quotedPrintable("\u0123")); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
public void quotedPrintableWithNull() |
135 |
|
{ |
136 |
1 |
Assert.assertNull(this.tool.quotedPrintable(null)); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
139 |
1 |
@Test... |
140 |
|
public void qWithSimpleText() |
141 |
|
{ |
142 |
1 |
Assert.assertEquals("=?UTF-8?Q?Hello_World?=", this.tool.q("Hello World")); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
145 |
1 |
@Test... |
146 |
|
public void qWithSpecialChars() |
147 |
|
{ |
148 |
1 |
Assert.assertEquals("=?UTF-8?Q?a=3Db=3F=5F=0A?=", this.tool.q("a=b?_\n")); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void qWithNonAsciiChars() |
153 |
|
{ |
154 |
1 |
Assert.assertEquals("=?UTF-8?Q?=C4=A3?=", this.tool.q("\u0123")); |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void qWithNull() |
159 |
|
{ |
160 |
1 |
Assert.assertNull(this.tool.q(null)); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
163 |
1 |
@Test... |
164 |
|
public void bWithSimpleText() |
165 |
|
{ |
166 |
1 |
Assert.assertEquals("=?UTF-8?B?SGVsbG8gV29ybGQ=?=", this.tool.b("Hello World")); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
169 |
1 |
@Test... |
170 |
|
public void bWithSpecialChars() |
171 |
|
{ |
172 |
1 |
Assert.assertEquals("=?UTF-8?B?YT1iPwo=?=", this.tool.b("a=b?\n")); |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
175 |
1 |
@Test... |
176 |
|
public void bWithNonAsciiChars() |
177 |
|
{ |
178 |
1 |
Assert.assertEquals("=?UTF-8?B?xKM=?=", this.tool.b("\u0123")); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
181 |
1 |
@Test... |
182 |
|
public void bWithNull() |
183 |
|
{ |
184 |
1 |
Assert.assertNull(this.tool.b(null)); |
185 |
|
} |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
187 |
1 |
@Test... |
188 |
|
public void url() |
189 |
|
{ |
190 |
1 |
HashMap<String, String> map = new LinkedHashMap<>(); |
191 |
1 |
map.put("hello", "world"); |
192 |
1 |
map.put(null, "value"); |
193 |
1 |
map.put("B& B", "yes"); |
194 |
1 |
map.put("empty", null); |
195 |
1 |
Assert.assertEquals("hello=world&B%26%20B=yes&empty=", this.tool.url(map)); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
198 |
1 |
@Test... |
199 |
|
public void urlWithDouble() |
200 |
|
{ |
201 |
1 |
HashMap<String, Double> map = new LinkedHashMap<>(); |
202 |
1 |
map.put("A&A", 1.5); |
203 |
1 |
map.put("B&B", 1.2); |
204 |
1 |
Assert.assertEquals("A%26A=1.5&B%26B=1.2", this.tool.url(map)); |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
207 |
1 |
@Test... |
208 |
|
public void urlWithArray() |
209 |
|
{ |
210 |
1 |
HashMap<String, String[]> map = new HashMap<>(); |
211 |
1 |
String[] array = {"M&M", null, "Astronomy&Astrophysics"}; |
212 |
1 |
map.put("couple", array); |
213 |
1 |
Assert.assertEquals("couple=M%26M&couple=&couple=Astronomy%26Astrophysics", this.tool.url(map)); |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
216 |
1 |
@Test... |
217 |
|
public void urlWithCollection() |
218 |
|
{ |
219 |
1 |
HashMap<String, ArrayList<String>> map = new HashMap<>(); |
220 |
1 |
ArrayList<String> collection1 = new ArrayList<>(); |
221 |
1 |
collection1.add("test"); |
222 |
1 |
map.put("alice", collection1); |
223 |
1 |
ArrayList<String> collection2 = new ArrayList<>(); |
224 |
1 |
collection2.add(null); |
225 |
1 |
collection2.add("t&t"); |
226 |
1 |
collection2.add("R&D"); |
227 |
1 |
map.put("bob", collection2); |
228 |
1 |
Assert.assertEquals("bob=&bob=t%26t&bob=R%26D&alice=test", this.tool.url(map)); |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
@see |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
234 |
1 |
@Test... |
235 |
|
public void css() |
236 |
|
{ |
237 |
1 |
Assert.assertEquals(this.tool.css("a#b.c d[e=f]g{h:i;j}k"), "a\\#b\\.c\\ d\\[e\\=f\\]g\\{h\\:i\\;j\\}k"); |
238 |
|
|
239 |
|
|
240 |
1 |
Assert.assertNull(this.tool.css("a\u0000b")); |
241 |
|
} |
242 |
|
} |