1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File EscapeToolTest.java

 

Code metrics

0
64
25
1
242
175
25
0.39
2.56
25
1

Classes

Class Line # Actions
EscapeToolTest 37 64 0% 25 0
1.0100%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link EscapeTool}.
33    *
34    * @version $Id: 5d4e80cbe4d6fe015c1c9897e8318741501906e3 $
35    * @since 2.7RC1
36    */
 
37    public class EscapeToolTest
38    {
39    /**
40    * The tested tool.
41    */
42    private EscapeTool tool;
43   
44    /**
45    * Initialize the tested tool.
46    */
 
47  24 toggle @Before
48    public void setUp()
49    {
50  24 this.tool = new EscapeTool();
51    }
52   
 
53  1 toggle @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   
 
65  1 toggle @Test
66    public void escapeXMLApos()
67    {
68  1 Assert.assertFalse("' wrongly escaped to non-HTML &apos;", this.tool.xml("'").equals("&apos;"));
69    }
70   
 
71  1 toggle @Test
72    public void escapeXMLWithNull()
73    {
74  1 Assert.assertNull("null should be null", this.tool.xml(null));
75    }
76   
 
77  1 toggle @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   
 
83  1 toggle @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   
 
101  1 toggle @Test
102    public void escapeJSONWithNullInput()
103    {
104  1 Assert.assertNull("Unexpected non-null output for null input", this.tool.json(null));
105    }
106   
 
107  1 toggle @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   
 
115  1 toggle @Test
116    public void quotedPrintableWithSimpleText()
117    {
118  1 Assert.assertEquals("Hello World", this.tool.quotedPrintable("Hello World"));
119    }
120   
 
121  1 toggle @Test
122    public void quotedPrintableWithSpecialChars()
123    {
124  1 Assert.assertEquals("a=3Db=0A", this.tool.quotedPrintable("a=b\n"));
125    }
126   
 
127  1 toggle @Test
128    public void quotedPrintableWithNonAsciiChars()
129    {
130  1 Assert.assertEquals("=C4=A3", this.tool.quotedPrintable("\u0123"));
131    }
132   
 
133  1 toggle @Test
134    public void quotedPrintableWithNull()
135    {
136  1 Assert.assertNull(this.tool.quotedPrintable(null));
137    }
138   
 
139  1 toggle @Test
140    public void qWithSimpleText()
141    {
142  1 Assert.assertEquals("=?UTF-8?Q?Hello_World?=", this.tool.q("Hello World"));
143    }
144   
 
145  1 toggle @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   
 
151  1 toggle @Test
152    public void qWithNonAsciiChars()
153    {
154  1 Assert.assertEquals("=?UTF-8?Q?=C4=A3?=", this.tool.q("\u0123"));
155    }
156   
 
157  1 toggle @Test
158    public void qWithNull()
159    {
160  1 Assert.assertNull(this.tool.q(null));
161    }
162   
 
163  1 toggle @Test
164    public void bWithSimpleText()
165    {
166  1 Assert.assertEquals("=?UTF-8?B?SGVsbG8gV29ybGQ=?=", this.tool.b("Hello World"));
167    }
168   
 
169  1 toggle @Test
170    public void bWithSpecialChars()
171    {
172  1 Assert.assertEquals("=?UTF-8?B?YT1iPwo=?=", this.tool.b("a=b?\n"));
173    }
174   
 
175  1 toggle @Test
176    public void bWithNonAsciiChars()
177    {
178  1 Assert.assertEquals("=?UTF-8?B?xKM=?=", this.tool.b("\u0123"));
179    }
180   
 
181  1 toggle @Test
182    public void bWithNull()
183    {
184  1 Assert.assertNull(this.tool.b(null));
185    }
186   
 
187  1 toggle @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   
 
198  1 toggle @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   
 
207  1 toggle @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   
 
216  1 toggle @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 <a href="https://drafts.csswg.org/cssom/#serialize-an-identifier">serialize-an-identifier</a>
233    */
 
234  1 toggle @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    // Invalid character U+0000 (the exception must be caught)
240  1 Assert.assertNull(this.tool.css("a\u0000b"));
241    }
242    }