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

File XMLUtilsTest.java

 

Code metrics

0
48
21
1
199
146
21
0.44
2.29
21
1

Classes

Class Line # Actions
XMLUtilsTest 31 48 0% 21 0
1.0100%
 

Contributing tests

This file is covered by 21 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    package org.xwiki.xml;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24   
25    /**
26    * Unit tests for {@link org.xwiki.xml.XMLUtils}.
27    *
28    * @version $Id: 4af9818fef92e7246fd57abce1109ddd807b4814 $
29    * @since 1.6M1
30    */
 
31    public class XMLUtilsTest
32    {
 
33  1 toggle @Test
34    public void testEscapeXMLComment()
35    {
36  1 Assert.assertEquals("-\\- ", XMLUtils.escapeXMLComment("-- "));
37  1 Assert.assertEquals("-\\", XMLUtils.escapeXMLComment("-"));
38  1 Assert.assertEquals("-\\-\\-\\", XMLUtils.escapeXMLComment("---"));
39  1 Assert.assertEquals("- ", XMLUtils.escapeXMLComment("- "));
40    }
41   
 
42  1 toggle @Test
43    public void testUnescapeXMLComment()
44    {
45  1 Assert.assertEquals("", XMLUtils.unescapeXMLComment("\\"));
46  1 Assert.assertEquals("\\", XMLUtils.unescapeXMLComment("\\\\"));
47  1 Assert.assertEquals("--", XMLUtils.unescapeXMLComment("\\-\\-"));
48  1 Assert.assertEquals("--", XMLUtils.unescapeXMLComment("\\-\\-\\"));
49    }
50   
 
51  1 toggle @Test
52    public void testEscape()
53    {
54  1 String escapedText = XMLUtils.escape("a < a' && a' < a\" => a < a\"");
55   
56  1 Assert.assertFalse("Failed to escape <", escapedText.contains("<"));
57  1 Assert.assertFalse("Failed to escape >", escapedText.contains(">"));
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    }
62   
 
63  1 toggle @Test
64    public void testEscapeApos()
65    {
66  1 Assert.assertFalse("' wrongly escaped to non-HTML &apos;", XMLUtils.escape("'").equals("&apos;"));
67    }
68   
 
69  1 toggle @Test
70    public void testEscapeEmptyString()
71    {
72  1 Assert.assertEquals("\"\" should be \"\"", "", XMLUtils.escape(""));
73    }
74   
 
75  1 toggle @Test
76    public void testEscapeWithNull()
77    {
78  1 Assert.assertNull("null should be null", XMLUtils.escape(null));
79    }
80   
 
81  1 toggle @Test
82    public void testEscapeNonAscii()
83    {
84  1 Assert.assertTrue("Non-ASCII characters were escaped", XMLUtils.escape("\u0123").equals("\u0123"));
85    }
86   
 
87  1 toggle @Test
88    public void testEscapeAttributeValue()
89    {
90   
91  1 String escapedText = XMLUtils.escapeAttributeValue("a < a' && a' < a\" => a < a\"");
92   
93  1 Assert.assertFalse("Failed to escape <", escapedText.contains("<"));
94  1 Assert.assertFalse("Failed to escape >", escapedText.contains(">"));
95  1 Assert.assertFalse("Failed to escape '", escapedText.contains("'"));
96  1 Assert.assertFalse("Failed to escape \"", escapedText.contains("\""));
97  1 Assert.assertFalse("Failed to escape &", escapedText.contains("&&"));
98    }
99   
 
100  1 toggle @Test
101    public void testEscapeAttributeValueApos()
102    {
103  1 Assert.assertFalse("' wrongly escaped to non-HTML &apos;", XMLUtils.escapeAttributeValue("'")
104    .equals("&apos;"));
105    }
106   
 
107  1 toggle @Test
108    public void testEscapeFAttributeValueEmptyString()
109    {
110  1 Assert.assertEquals("\"\" should be \"\"", "", XMLUtils.escapeAttributeValue(""));
111    }
112   
 
113  1 toggle @Test
114    public void testEscapeFAttributeValueWithNull()
115    {
116  1 Assert.assertNull("null should be null", XMLUtils.escapeAttributeValue(null));
117    }
118   
 
119  1 toggle @Test
120    public void testEscapeAttributeValueNonAscii()
121    {
122  1 Assert.assertTrue("Non-ASCII characters were escaped", XMLUtils.escapeAttributeValue("\u0123")
123    .equals("\u0123"));
124    }
125   
 
126  1 toggle @Test
127    public void testEscapeElementContent()
128    {
129   
130  1 String escapedText = XMLUtils.escapeElementContent("a < a' && a' < a\" => a < a\"");
131   
132  1 Assert.assertFalse("Failed to escape <", escapedText.contains("<"));
133  1 Assert.assertFalse("Failed to escape >", escapedText.contains(">"));
134  1 Assert.assertTrue("Wrongfully escaped '", escapedText.contains("'"));
135  1 Assert.assertTrue("Wrongfully escaped \"", escapedText.contains("\""));
136  1 Assert.assertFalse("Failed to escape &", escapedText.contains("&&"));
137    }
138   
 
139  1 toggle @Test
140    public void testEscapeElementContentEmptyString()
141    {
142  1 Assert.assertEquals("\"\" should be \"\"", "", XMLUtils.escapeElementContent(""));
143    }
144   
 
145  1 toggle @Test
146    public void testEscapeElementContentWithNull()
147    {
148  1 Assert.assertNull("null should be null", XMLUtils.escapeElementContent(null));
149    }
150   
 
151  1 toggle @Test
152    public void testEscapeElementContentNonAscii()
153    {
154  1 Assert.assertTrue("Non-ASCII characters were escaped", XMLUtils.escapeElementContent("\u0123")
155    .equals("\u0123"));
156    }
157   
 
158  1 toggle @Test
159    public void testUnescape()
160    {
161  1 Assert.assertEquals("Failed to unescaped named entities", "&'\"<>",
162    XMLUtils.unescape("&amp;&apos;&quot;&lt;&gt;"));
163  1 Assert.assertEquals("Failed to unescaped decimal entities", "&'\"<>",
164    XMLUtils.unescape("&#38;&#39;&#34;&#60;&#62;"));
165  1 Assert.assertEquals("Failed to unescaped decimal entities with leading zeros", "&'\"<>",
166    XMLUtils.unescape("&#038;&#0039;&#00034;&#000060;&#0000062;"));
167  1 Assert.assertEquals("Failed to unescaped hexadecimal entities", "&'\"<<>>",
168    XMLUtils.unescape("&#x26;&#x27;&#x22;&#x3c;&#x3C;&#x3e;&#x3E;"));
169  1 Assert.assertEquals("Failed to unescaped hexadecimal entities with leading zeros", "&'\"<<>>",
170    XMLUtils.unescape("&#x026;&#x0027;&#x00022;&#x00003c;&#x0003C;&#x003e;&#x03E;"));
171    }
172   
 
173  1 toggle @Test
174    public void testUnescapeEmptyString()
175    {
176  1 Assert.assertEquals("\"\" should be \"\"", "", XMLUtils.unescape(""));
177    }
178   
 
179  1 toggle @Test
180    public void testUnescapeWithNull()
181    {
182  1 Assert.assertNull("null should be null", XMLUtils.unescape(null));
183    }
184   
 
185  1 toggle @Test
186    public void testUnescapeOtherEscapes()
187    {
188  1 Assert.assertEquals("Extra named entities were unescaped", "&deg;", XMLUtils.unescape("&deg;"));
189  1 Assert.assertEquals("Extra decimal entities were unescaped", "&#65;", XMLUtils.unescape("&#65;"));
190  1 Assert.assertEquals("Extra hexadecimal entities were unescaped", "&#x5;", XMLUtils.unescape("&#x5;"));
191    }
192   
 
193  1 toggle @Test
194    public void testGetDomDocument()
195    {
196    // Nothing much that we can test here...
197  1 Assert.assertNotNull(XMLUtils.createDOMDocument());
198    }
199    }