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

File XMLScriptServiceTest.java

 

Code metrics

0
132
30
1
365
299
30
0.23
4.4
30
1

Classes

Class Line # Actions
XMLScriptServiceTest 43 132 0% 30 4
0.9753086697.5%
 

Contributing tests

This file is covered by 27 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.script;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.StringReader;
24    import java.io.UnsupportedEncodingException;
25   
26    import javax.xml.transform.stream.StreamSource;
27   
28    import org.junit.Assert;
29    import org.junit.Test;
30    import org.w3c.dom.Document;
31    import org.w3c.dom.Element;
32    import org.w3c.dom.ls.LSInput;
33    import org.xwiki.script.service.ScriptService;
34    import org.xwiki.test.jmock.AbstractComponentTestCase;
35    import org.xwiki.xml.script.XMLScriptService;
36   
37    /**
38    * Unit tests for {@link org.xwiki.xml.script.XMLScriptService}.
39    *
40    * @version $Id: acefba464a53f321f2c6a572fae232cb9be66e46 $
41    * @since 2.7RC1
42    */
 
43    public class XMLScriptServiceTest extends AbstractComponentTestCase
44    {
45    private XMLScriptService xml;
46   
 
47  27 toggle @Override
48    public void setUp() throws Exception
49    {
50  27 this.xml = (XMLScriptService) getComponentManager().getInstance(ScriptService.class, "xml");
51    }
52   
53   
54   
 
55  1 toggle @Test
56    public void testGetDomDocument()
57    {
58    // Nothing much that we can test here...
59  1 Assert.assertNotNull(this.xml.createDOMDocument());
60    }
61   
 
62  1 toggle @Test
63    public void testParseString()
64    {
65  1 Document result = this.xml.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b='c'>d</a>");
66  1 Assert.assertNotNull("Failed to parse content", result);
67  1 Assert.assertEquals("Incorrect root node", "a", result.getDocumentElement().getLocalName());
68    }
69   
 
70  1 toggle @Test
71    public void testParseByteArray() throws UnsupportedEncodingException
72    {
73  1 Document result = this.xml.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b='c'>d</a>".getBytes("UTF-8"));
74  1 Assert.assertNotNull("Failed to parse content", result);
75  1 Assert.assertEquals("Incorrect root node", "a", result.getDocumentElement().getLocalName());
76    }
77   
 
78  1 toggle @Test
79    public void testParseInputStream() throws UnsupportedEncodingException
80    {
81  1 Document result = this.xml.parse(
82    new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b='c'>d</a>".getBytes("UTF-8")));
83  1 Assert.assertNotNull("Failed to parse content", result);
84  1 Assert.assertEquals("Incorrect root node", "a", result.getDocumentElement().getLocalName());
85    }
86   
 
87  1 toggle @Test
88    public void testParseWithDifferentEncoding() throws UnsupportedEncodingException
89    {
90  1 Document result =
91    this.xml.parse("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><a>\u00E9</a>".getBytes("ISO-8859-1"));
92  1 Assert.assertNotNull("Failed to parse content", result);
93  1 Assert.assertEquals("Incorrect root node", "a", result.getDocumentElement().getLocalName());
94  1 Assert.assertEquals("Incorrect content", "\u00E9", result.getDocumentElement().getTextContent());
95    }
96   
 
97  1 toggle @Test
98    public void testParseWithWrongEncoding() throws UnsupportedEncodingException
99    {
100  1 Document result =
101    this.xml.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>\u00E9</a>".getBytes("ISO-8859-1"));
102  1 Assert.assertNull("Content should be invalid with the specified encoding", result);
103    }
104   
 
105  1 toggle @Test
106    public void testParseWithoutXMLDeclaration()
107    {
108  1 Document result = this.xml.parse("<a>\u00E9</a>");
109  1 Assert.assertNotNull("Failed to parse content", result);
110  1 Assert.assertEquals("Incorrect root node", "a", result.getDocumentElement().getLocalName());
111    }
112   
 
113  1 toggle @Test
114    public void testParseInvalidDocument() throws UnsupportedEncodingException
115    {
116  1 Document result = this.xml.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><a></b>");
117  1 Assert.assertNull("Invalid content shouldn't be parsed", result);
118    }
119   
 
120  1 toggle @Test
121    public void testParseNull()
122    {
123  1 Document result = this.xml.parse((String) null);
124  1 Assert.assertNull("Null Document input shouldn't be parsed", result);
125  1 result = this.xml.parse((byte[]) null);
126  1 Assert.assertNull("Null byte[] input shouldn't be parsed", result);
127  1 result = this.xml.parse((ByteArrayInputStream) null);
128  1 Assert.assertNull("Null InputStream input shouldn't be parsed", result);
129  1 result = this.xml.parse((LSInput) null);
130  1 Assert.assertNull("Null LSInput input shouldn't be parsed", result);
131    }
132   
 
133  1 toggle @Test
134    public void testParseAndSerialize()
135    {
136  1 String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\u00E9</a>";
137  1 String result = this.xml.serialize(this.xml.parse(content));
138  1 Assert.assertEquals("Not identical content after parse + serialize", content, result);
139   
140  1 content = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<a>\u00E9</a>";
141  1 result = this.xml.serialize(this.xml.parse(content));
142  1 Assert.assertEquals("Not identical content after parse + serialize", content, result);
143   
144  1 content = "<a>\u00E9</a>";
145  1 result = this.xml.serialize(this.xml.parse(content), false);
146  1 Assert.assertEquals("Not identical content after parse + serialize", content, result);
147    }
148   
 
149  1 toggle @Test
150    public void testSerialize()
151    {
152  1 Document d = createSimpleDocument();
153  1 String result = this.xml.serialize(d);
154  1 Assert.assertEquals("Wrong serialization", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\u00E9</a>", result);
155    }
156   
 
157  1 toggle @Test
158    public void testSerializeDocumentElement()
159    {
160  1 Document d = createSimpleDocument();
161  1 String result = this.xml.serialize(d.getDocumentElement());
162  1 Assert.assertEquals("Wrong serialization", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a>\u00E9</a>", result);
163    }
164   
 
165  1 toggle @Test
166    public void testSerializeNode()
167    {
168  1 Document d = createSimpleDocument();
169  1 Element b = d.createElement("b");
170  1 b.setTextContent("c");
171  1 d.getDocumentElement().appendChild(b);
172  1 String result = this.xml.serialize(d.getDocumentElement().getElementsByTagName("b").item(0));
173  1 Assert.assertEquals("Wrong serialization", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<b>c</b>", result);
174    }
175   
 
176  1 toggle @Test
177    public void testSerializeNull()
178    {
179  1 Assert.assertEquals("Wrong serialization for null document", "", this.xml.serialize(null));
180    }
181   
 
182  0 toggle public void testSerializeWithoutXmlDeclaration()
183    {
184  0 Document d = createSimpleDocument();
185  0 String result = this.xml.serialize(d.getDocumentElement(), false);
186  0 Assert.assertEquals("Wrong serialization", "<a>\u00E9</a>", result);
187    }
188   
 
189  1 toggle @Test
190    public void testNewlineSerialization()
191    {
192  1 Document d = this.xml.parse("<a>a\nb\n</a>");
193  1 String result = this.xml.serialize(d, false);
194  1 Assert.assertEquals("Wrong newlines", "<a>a\nb\n</a>", result);
195   
196  1 d = this.xml.parse("<a>a\r\nb\r\n</a>");
197  1 result = this.xml.serialize(d, false);
198  1 Assert.assertEquals("Wrong newlines", "<a>a\nb\n</a>", result);
199   
200  1 d = this.xml.parse("<a>a\rb\r</a>");
201  1 result = this.xml.serialize(d, false);
202  1 Assert.assertEquals("Wrong newlines", "<a>a\nb\n</a>", result);
203    }
204   
 
205  1 toggle @Test
206    public void testSerializationWithDoctype()
207    {
208  1 Document d = this.xml.parse("<?xml version='1.0' encoding='UTF-8' ?>"
209    + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
210    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
211    + "<html><body>a</body></html>");
212  1 String result = this.xml.serialize(d, true);
213  1 Assert.assertEquals("Failed Doctype", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
214    + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
215    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
216    + "<html><body>a</body></html>", result);
217  1 result = this.xml.serialize(d, false);
218  1 Assert.assertEquals("Failed Doctype", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
219    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
220    + "<html><body>a</body></html>", result);
221    }
222   
 
223  1 toggle @Test
224    public void testDoctypeSerialization()
225    {
226  1 Document d = this.xml.parse("<?xml version='1.0' encoding='UTF-8' ?>"
227    + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
228    + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
229    + "<html><body>a</body></html>");
230  1 String result = this.xml.serialize(d.getDoctype(), true);
231  1 Assert.assertEquals("Doctype alone shouldn't be serialized", "", result);
232    }
233   
 
234  1 toggle @Test
235    public void testSerializeToSmallerCharset()
236    {
237  1 Document d = this.xml.parse("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><a>\u0345</a>");
238  1 String result = this.xml.serialize(d);
239  1 Assert.assertFalse("Non-latin1 character shouldn't be present in the output", result.contains("\u0345"));
240    }
241   
 
242  1 toggle @Test
243    public void testTransformDocument()
244    {
245  1 Document d = this.xml.parse("<a b='c'>d</a>");
246  1 Document s = this.xml.parse("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
247    "<xsl:output method='xml' omit-xml-declaration='yes'/>"
248    + "<xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>");
249  1 String result = this.xml.transform(d, s);
250  1 Assert.assertEquals("<a/>", result);
251    }
252   
 
253  1 toggle @Test
254    public void testTransformByteArray() throws UnsupportedEncodingException
255    {
256  1 byte[] d = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b='c'>d</a>".getBytes("UTF-8");
257  1 byte[] s = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
258    + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
259    "<xsl:output method='xml' omit-xml-declaration='yes'/>"
260    + "<xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>").getBytes("UTF-8");
261  1 String result = this.xml.transform(d, s);
262  1 Assert.assertEquals("<a/>", result);
263    }
264   
 
265  1 toggle @Test
266    public void testTransformString() throws UnsupportedEncodingException
267    {
268  1 String d = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b='c'>d</a>";
269  1 String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
270    + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
271    "<xsl:output method='xml' omit-xml-declaration='yes'/>"
272    + "<xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>";
273  1 String result = this.xml.transform(d, s);
274  1 Assert.assertEquals("<a/>", result);
275    }
276   
 
277  1 toggle @Test
278    public void testTransformWithNullInputs()
279    {
280  1 StreamSource d = new StreamSource(new StringReader("<a b='c'>d</a>"));
281  1 StreamSource s = new StreamSource(new StringReader(
282    "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'><xsl:output method='xml'/>"
283    + "<xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>"));
284  1 String result = this.xml.transform(null, s);
285  1 Assert.assertNull(null, result);
286  1 result = this.xml.transform(d, null);
287  1 Assert.assertNull(null, result);
288  1 result = this.xml.transform((StreamSource) null, (StreamSource) null);
289  1 Assert.assertNull(null, result);
290    }
291   
 
292  1 toggle @Test
293    public void testTransformWithNullDocuments()
294    {
295  1 Document d = this.xml.parse("<a b='c'>d</a>");
296  1 Document s = this.xml.parse("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
297    "<xsl:output method='xml'/><xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>");
298  1 String result = this.xml.transform(null, s);
299  1 Assert.assertNull(null, result);
300  1 result = this.xml.transform(d, null);
301  1 Assert.assertNull(null, result);
302  1 result = this.xml.transform((Document) null, (Document) null);
303  1 Assert.assertNull(null, result);
304    }
305   
 
306  1 toggle @Test
307    public void testTransformWithNullStrings()
308    {
309  1 String d = "<a b='c'>d</a>";
310  1 String s = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
311    "<xsl:output method='xml'/><xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>";
312  1 String result = this.xml.transform(null, s);
313  1 Assert.assertNull(null, result);
314  1 result = this.xml.transform(d, null);
315  1 Assert.assertNull(null, result);
316  1 result = this.xml.transform((String) null, (String) null);
317  1 Assert.assertNull(null, result);
318    }
319   
 
320  1 toggle @Test
321    public void testTransformWithNullByteArrays() throws UnsupportedEncodingException
322    {
323  1 byte[] d = "<a b='c'>d</a>".getBytes("UTF-8");
324  1 byte[] s = ("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
325    "<xsl:output method='xml'/><xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>")
326    .getBytes();
327  1 String result = this.xml.transform(null, s);
328  1 Assert.assertNull(null, result);
329  1 result = this.xml.transform(d, null);
330  1 Assert.assertNull(null, result);
331  1 result = this.xml.transform((byte[]) null, (byte[]) null);
332  1 Assert.assertNull(null, result);
333    }
334   
 
335  1 toggle @Test
336    public void testTransformWithInvalidSheet()
337    {
338  1 Document d = this.xml.parse("<a b='c'>d</a>");
339  1 Document s = this.xml.parse("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
340    "<xsl:output method='xml'/><xsl:template match='node()'><xsl:copy/></xsl:template></xsl:stylesheet>");
341  1 String result = this.xml.transform(s, d);
342  1 Assert.assertEquals(null, result);
343    }
344   
 
345  1 toggle @Test
346    public void testTransformToText()
347    {
348  1 Document d = this.xml.parse("<a b='c'>d</a>");
349  1 Document s =
350    this.xml.parse("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>" +
351    "<xsl:output method='text'/><xsl:template match='node()'><xsl:value-of select='@*'/></xsl:template>"
352    + "</xsl:stylesheet>");
353  1 String result = this.xml.transform(d, s);
354  1 Assert.assertEquals("c", result);
355    }
356   
 
357  3 toggle private Document createSimpleDocument()
358    {
359  3 Document d = this.xml.createDOMDocument();
360  3 Element a = d.createElement("a");
361  3 a.setTextContent("\u00E9");
362  3 d.appendChild(a);
363  3 return d;
364    }
365    }