1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
| 100% |
Uncovered Elements: 0 (54) |
Complexity: 16 |
Complexity Density: 0.42 |
|
49 |
|
public class XMLFilterTest |
50 |
|
{ |
51 |
|
@Rule |
52 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
53 |
|
|
54 |
|
private StringWriter stringWriter; |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
56 |
12 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
5 |
private void assertParseAndSerialize(String inputexpect) throws Exception... |
65 |
|
{ |
66 |
5 |
assertParseAndSerialize(inputexpect, inputexpect); |
67 |
|
} |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
69 |
6 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
3 |
private void assertParseAndSerializeFromSAX(String inputexpect) throws Exception... |
80 |
|
{ |
81 |
3 |
assertParseAndSerializeFromSAX(inputexpect, inputexpect); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
84 |
3 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
12 |
private void assertSerialized(String expect)... |
100 |
|
{ |
101 |
12 |
Assert.assertEquals(expect, this.stringWriter.toString()); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void testContainer() throws Exception |
108 |
|
{ |
109 |
1 |
assertParseAndSerialize("<container></container>"); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
public void testContainerAndChild() throws Exception |
114 |
|
{ |
115 |
1 |
assertParseAndSerialize("<container><child></child></container>"); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void testContainerWithParameters() throws Exception |
120 |
|
{ |
121 |
1 |
assertParseAndSerialize("<containerWithParameters param0=\"value0\" param1=\"1\"></containerWithParameters>"); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void testContainerWithNamedParameters() throws Exception |
126 |
|
{ |
127 |
1 |
assertParseAndSerialize("<containerWithNamedParameters namedParam=\"value0\" param1=\"1\"></containerWithNamedParameters>"); |
128 |
|
} |
129 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
130 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
139 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void testFromSAX() throws Exception |
148 |
|
{ |
149 |
1 |
assertParseAndSerializeFromSAX("<containerWithNamedParameters namedParam=\"value0\" param1=\"1\"></containerWithNamedParameters>"); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
154 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
168 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
179 |
1 |
@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 |
|
} |