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

File XMLFilterTest.java

 

Code metrics

0
38
16
1
189
132
16
0.42
2.38
16
1

Classes

Class Line # Actions
XMLFilterTest 49 38 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 10 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.filter.xml;
21   
22    import java.awt.Color;
23    import java.io.StringReader;
24    import java.io.StringWriter;
25    import java.util.Collections;
26   
27    import javax.xml.parsers.SAXParser;
28    import javax.xml.parsers.SAXParserFactory;
29    import javax.xml.stream.FactoryConfigurationError;
30    import javax.xml.stream.XMLStreamException;
31    import javax.xml.transform.stream.StreamResult;
32    import javax.xml.transform.stream.StreamSource;
33   
34    import org.junit.Assert;
35    import org.junit.Rule;
36    import org.junit.Test;
37    import org.xml.sax.ContentHandler;
38    import org.xml.sax.InputSource;
39    import org.xml.sax.XMLReader;
40    import org.xwiki.component.manager.ComponentLookupException;
41    import org.xwiki.filter.test.ExtendedTestData;
42    import org.xwiki.filter.test.TestFilter;
43    import org.xwiki.filter.xml.parser.XMLParserFactory;
44    import org.xwiki.filter.xml.serializer.XMLSerializerFactory;
45    import org.xwiki.test.annotation.AllComponents;
46    import org.xwiki.test.mockito.MockitoComponentManagerRule;
47   
48    @AllComponents
 
49    public class XMLFilterTest
50    {
51    @Rule
52    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
53   
54    private StringWriter stringWriter;
55   
 
56  12 toggle private TestFilter createFilter() throws ComponentLookupException, XMLStreamException, FactoryConfigurationError
57    {
58  12 this.stringWriter = new StringWriter();
59   
60  12 XMLSerializerFactory serializerFactory = this.componentManager.getInstance(XMLSerializerFactory.class);
61  12 return serializerFactory.createSerializer(TestFilter.class, new StreamResult(this.stringWriter), null);
62    }
63   
 
64  5 toggle private void assertParseAndSerialize(String inputexpect) throws Exception
65    {
66  5 assertParseAndSerialize(inputexpect, inputexpect);
67    }
68   
 
69  6 toggle private void assertParseAndSerialize(String expect, String input) throws Exception
70    {
71  6 TestFilter testFilter = createFilter();
72   
73  6 XMLParserFactory parserFactory = this.componentManager.getInstance(XMLParserFactory.class);
74  6 parserFactory.parse(new StreamSource(new StringReader(input)), testFilter, null);
75   
76  6 assertSerialized(expect);
77    }
78   
 
79  3 toggle private void assertParseAndSerializeFromSAX(String inputexpect) throws Exception
80    {
81  3 assertParseAndSerializeFromSAX(inputexpect, inputexpect);
82    }
83   
 
84  3 toggle private void assertParseAndSerializeFromSAX(String expect, String input) throws Exception
85    {
86  3 TestFilter testFilter = createFilter();
87   
88  3 XMLParserFactory parserFactory = this.componentManager.getInstance(XMLParserFactory.class);
89  3 ContentHandler parser = parserFactory.createContentHandler(testFilter, null);
90   
91  3 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
92  3 XMLReader xmlReader = saxParser.getXMLReader();
93  3 xmlReader.setContentHandler(parser);
94  3 xmlReader.parse(new InputSource(new StringReader(input)));
95   
96  3 assertSerialized(expect);
97    }
98   
 
99  12 toggle private void assertSerialized(String expect)
100    {
101  12 Assert.assertEquals(expect, this.stringWriter.toString());
102    }
103   
104    // Tests
105   
 
106  1 toggle @Test
107    public void testContainer() throws Exception
108    {
109  1 assertParseAndSerialize("<container></container>");
110    }
111   
 
112  1 toggle @Test
113    public void testContainerAndChild() throws Exception
114    {
115  1 assertParseAndSerialize("<container><child></child></container>");
116    }
117   
 
118  1 toggle @Test
119    public void testContainerWithParameters() throws Exception
120    {
121  1 assertParseAndSerialize("<containerWithParameters param0=\"value0\" param1=\"1\"></containerWithParameters>");
122    }
123   
 
124  1 toggle @Test
125    public void testContainerWithNamedParameters() throws Exception
126    {
127  1 assertParseAndSerialize("<containerWithNamedParameters namedParam=\"value0\" param1=\"1\"></containerWithNamedParameters>");
128    }
129   
 
130  1 toggle @Test
131    public void testContainerWithMap() throws Exception
132    {
133  1 assertParseAndSerialize("<containerWithMap><p><map><entry><string>key</string><int>1</int></entry></map></p></containerWithMap>");
134  1 assertParseAndSerialize(
135    "<containerWithMap><p><map><entry><string>key</string><int>1</int></entry></map></p></containerWithMap>",
136    "<containerWithMap><p><_0><entry><string>key</string><int>1</int></entry></_0></p></containerWithMap>");
137    }
138   
 
139  1 toggle @Test
140    public void testCustomData() throws Exception
141    {
142  1 assertParseAndSerializeFromSAX("<customData><p><custom><field1>5</field1></custom></p></customData>");
143  1 assertParseAndSerializeFromSAX("<customData></customData>");
144    }
145   
 
146  1 toggle @Test
147    public void testFromSAX() throws Exception
148    {
149  1 assertParseAndSerializeFromSAX("<containerWithNamedParameters namedParam=\"value0\" param1=\"1\"></containerWithNamedParameters>");
150    }
151   
152    // Serialize
153   
 
154  1 toggle @Test
155    public void testSerializeExtendedTestData() throws ComponentLookupException, XMLStreamException,
156    FactoryConfigurationError
157    {
158  1 TestFilter testFilter = createFilter();
159   
160  1 ExtendedTestData extendedTestData = new ExtendedTestData();
161   
162  1 testFilter.beginCustomData(extendedTestData);
163  1 testFilter.endCustomData(extendedTestData);
164   
165  1 assertSerialized("<customData><p><custom><field1>1</field1></custom></p></customData>");
166    }
167   
 
168  1 toggle @Test
169    public void testSerializeWithDefaultValue() throws ComponentLookupException, XMLStreamException,
170    FactoryConfigurationError
171    {
172  1 TestFilter testFilter = createFilter();
173   
174  1 testFilter.onChildWithDefaultValue(42, "default value", Color.WHITE, Collections.EMPTY_MAP);
175   
176  1 assertSerialized("<childWithDefaultValue></childWithDefaultValue>");
177    }
178   
 
179  1 toggle @Test
180    public void testSerializeWithNamedChild() throws ComponentLookupException, XMLStreamException,
181    FactoryConfigurationError
182    {
183  1 TestFilter testFilter = createFilter();
184   
185  1 testFilter.onNamedChild();
186   
187  1 assertSerialized("<childwithname></childwithname>");
188    }
189    }