1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter.xml

File XMLFilterTest.java

 

Code metrics

4
45
16
1
206
144
18
0.4
2.81
16
1.12

Classes

Class Line # Actions
XMLFilterTest 51 45 0% 18 2
0.969230896.9%
 

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