1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.test.xhtml

File XHTMLWhitespaceXMLFilterTest.java

 

Code metrics

2
51
5
1
184
148
6
0.12
10.2
5
1.2

Classes

Class Line # Actions
XHTMLWhitespaceXMLFilterTest 36 51 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 2 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.rendering.wikimodel.test.xhtml;
21   
22    import java.io.StringReader;
23   
24    import javax.xml.parsers.SAXParserFactory;
25   
26    import org.xml.sax.InputSource;
27    import org.xml.sax.XMLReader;
28    import org.xwiki.rendering.wikimodel.xhtml.filter.XHTMLWhitespaceXMLFilter;
29   
30    import junit.framework.TestCase;
31   
32    /**
33    * @version $Id: 40cb8b46679a677d9578132f12579a5afd51e588 $
34    * @since 4.0M1
35    */
 
36    public class XHTMLWhitespaceXMLFilterTest extends TestCase
37    {
38    private XMLWriter writerFilter;
39   
40    private XHTMLWhitespaceXMLFilter whitespaceFilter;
41   
 
42  2 toggle @Override
43    protected void setUp() throws Exception
44    {
45  2 XMLReader xmlReader = SAXParserFactory
46    .newInstance()
47    .newSAXParser()
48    .getXMLReader();
49  2 whitespaceFilter = new XHTMLWhitespaceXMLFilter(xmlReader);
50  2 writerFilter = new XMLWriter();
51   
52  2 whitespaceFilter.setFeature(
53    "http://xml.org/sax/features/namespaces",
54    true);
55  2 whitespaceFilter.setContentHandler(writerFilter);
56  2 whitespaceFilter.setProperty(
57    "http://xml.org/sax/properties/lexical-handler",
58    writerFilter);
59    }
60   
 
61  1 toggle public void testWhiteSpaceStripping() throws Exception
62    {
63  1 assertCleanedHTML("<p>one two</p>", "<p> one two </p>");
64  1 assertCleanedHTML(
65    "<p>one two <b>three</b></p>",
66    "<p> one two <b>three</b></p>");
67  1 assertCleanedHTML("<p>one two</p>", "<p>\n\r\tone\n\r\ttwo\n\r\t</p>");
68  1 assertCleanedHTML(
69    "<p>one <b>two</b> <b>three</b></p>",
70    "<p>one <b>two</b> <b>three</b></p>");
71  1 assertCleanedHTML(
72    "<p>one <b>two</b> <em><b>three</b>a</em></p>",
73    "<p>one <b>two</b> <em><b>three</b>a</em></p>");
74  1 assertCleanedHTML(
75    "<p>one</p>two<p>three</p>",
76    "<p>one</p> two <p>three</p>");
77  1 assertCleanedHTML("<![CDATA[\n one \n]]>", "<![CDATA[\n one \n]]>");
78  1 assertCleanedHTML("<pre>\n one \n</pre>", "<pre>\n one \n</pre>");
79  1 assertCleanedHTML(
80    "<p>toto <tt>\n one \n</tt></p>",
81    "<p>toto <tt class=\"wikimodel-verbatim\">\n one \n</tt></p>");
82  1 assertCleanedHTML(
83    "<p>one <tt>two</tt> three</p>",
84    "<p>one <tt class=\"wikimodel-verbatim\">two</tt> three</p>");
85  1 assertCleanedHTML(
86    "<p>one <!--comment-->two</p>",
87    "<p> one <!--comment--> two </p>");
88  1 assertCleanedHTML(
89    "<p><!--comment-->one <b>two</b></p>",
90    "<p><!--comment--> one <b>two</b></p>");
91  1 assertCleanedHTML(
92    "<p>one <b>two</b> three</p>",
93    "<p>one <b>two</b> three </p>");
94  1 assertCleanedHTML(
95    "<!--comment-->one<![CDATA[two]]>",
96    "<!--comment--> one <![CDATA[two]]>");
97  1 assertCleanedHTML("<p></p>", "<p> </p>");
98  1 assertCleanedHTML("<p><b>text</b></p>", "<p> <b> text </b> </p>");
99  1 assertCleanedHTML(
100    "<!--startmacro:something--> <!--nonsemantic--><!--stopmacro-->",
101    " <!--startmacro:something--> <!--nonsemantic--> <!--stopmacro--> ");
102  1 assertCleanedHTML(
103    "<p>one <!--startmacro:something--><!--stopmacro--></p>",
104    "<p>one <!--startmacro:something--><!--stopmacro--></p>");
105  1 assertCleanedHTML(
106    "<p>one <!--startmacro:something--><!--stopmacro--> two</p>",
107    "<p>one <!--startmacro:something--><!--stopmacro--> two</p>");
108  1 assertCleanedHTML(
109    "<!--comment-->one<![CDATA[two]]>",
110    "<!--comment--> one<![CDATA[two]]>");
111  1 assertCleanedHTML(
112    "<p>one <!--comment-->two<![CDATA[three]]></p>",
113    "<p>one <!--comment--> two<![CDATA[three]]></p>");
114  1 assertCleanedHTML(
115    "<p>one <span>two </span><!--comment-->three<![CDATA[four]]></p>",
116    "<p> one <span> two </span><!--comment--> three <![CDATA[four]]></p>");
117  1 assertCleanedHTML(
118    "<p>This<strong> Spore Cheat Sheet</strong></p>",
119    "<p>This<strong>\nSpore Cheat Sheet</strong></p>");
120  1 assertCleanedHTML(
121    "<table><tbody><tr><td>First doc:<div><p>inside</p></div></td></tr></tbody></table>",
122    "<table><tbody>\n<tr><td>First doc:<div>\n<p>inside</p></div></td></tr></tbody></table>");
123  1 assertCleanedHTML(
124    "<p>one two three<br></br><br></br>hello</p>",
125    "<p>one two three<br/><br/>hello</p>");
126  1 assertCleanedHTML(
127    "<p><strong><span>hello</span></strong><span>world</span></p>",
128    "<p><strong><span>hello</span></strong><span>world</span></p>");
129  1 assertCleanedHTML(
130    "<div><p></p><p></p></div>",
131    "<div><p></p> <p></p></div>");
132  1 assertCleanedHTML(
133    "<unknow>hello word</unknow>",
134    " <unknow> hello word </unknow> ");
135  1 assertCleanedHTML(
136    "<p><unknow>hello word </unknow>text</p>",
137    "<p> <unknow> hello word </unknow> text</p>");
138  1 assertCleanedHTML(
139    "one<script> one two </script> two",
140    "one <script> one two </script> two");
141  1 assertCleanedHTML(
142    "<span>one two</span>",
143    "<span> one two </span>",
144    false);
145  1 assertCleanedHTML(
146    "one<script> one two </script>",
147    "one <script> one two </script>");
148  1 assertCleanedHTML(
149    "one<script>//<![CDATA[\nsome script\n//]]></script>",
150    "one<script>//<![CDATA[\nsome script\n//]]></script>");
151  1 assertCleanedHTML("one <img></img> two", "one <img/> two");
152    }
153   
 
154  1 toggle public void testWhiteSpaceStrippingForBlockElements() throws Exception
155    {
156  1 assertCleanedHTML("<p></p><p></p>", "<p></p> \n\r\t<p></p>");
157  1 assertCleanedHTML(
158    "<ul><li></li><li></li></ul>",
159    "<ul> <li></li> <li></li> </ul>");
160  1 assertCleanedHTML("<table></table><p></p>", "<table></table> <p></p>");
161    }
162   
 
163  36 toggle private void assertCleanedHTML(String expected, String originalContent)
164    throws Exception
165    {
166  36 assertCleanedHTML(expected, originalContent, true);
167    }
168   
 
169  37 toggle private void assertCleanedHTML(
170    String expected,
171    String originalContent,
172    boolean protect) throws Exception
173    {
174  37 if (protect) {
175  36 expected = "<html>" + expected + "</html>";
176  36 originalContent = "<html>" + originalContent + "</html>";
177    }
178   
179  37 InputSource source = new InputSource(new StringReader(originalContent));
180  37 whitespaceFilter.parse(source);
181  37 assertEquals(expected, writerFilter.getBuffer());
182  37 writerFilter.reset();
183    }
184    }