1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.filter.test.integration; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.net.URL; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.TimeZone; |
28 |
|
|
29 |
|
import org.apache.commons.io.IOUtils; |
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.junit.Assert; |
32 |
|
import org.junit.Test; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.configuration.ConfigurationSource; |
35 |
|
import org.xwiki.filter.FilterException; |
36 |
|
import org.xwiki.filter.descriptor.FilterStreamPropertyDescriptor; |
37 |
|
import org.xwiki.filter.input.DefaultFileInputSource; |
38 |
|
import org.xwiki.filter.input.DefaultURLInputSource; |
39 |
|
import org.xwiki.filter.input.FileInputSource; |
40 |
|
import org.xwiki.filter.input.InputFilterStream; |
41 |
|
import org.xwiki.filter.input.InputFilterStreamFactory; |
42 |
|
import org.xwiki.filter.input.InputSource; |
43 |
|
import org.xwiki.filter.input.InputStreamInputSource; |
44 |
|
import org.xwiki.filter.input.ReaderInputSource; |
45 |
|
import org.xwiki.filter.input.StringInputSource; |
46 |
|
import org.xwiki.filter.output.ByteArrayOutputTarget; |
47 |
|
import org.xwiki.filter.output.OutputFilterStream; |
48 |
|
import org.xwiki.filter.output.OutputFilterStreamFactory; |
49 |
|
import org.xwiki.filter.output.OutputTarget; |
50 |
|
import org.xwiki.filter.output.StringWriterOutputTarget; |
51 |
|
import org.xwiki.filter.test.internal.FileAssert; |
52 |
|
import org.xwiki.filter.utils.FilterStreamConstants; |
53 |
|
import org.xwiki.test.internal.MockConfigurationSource; |
54 |
|
|
55 |
|
|
56 |
|
@link |
57 |
|
@link |
58 |
|
|
59 |
|
@version |
60 |
|
@since |
61 |
|
|
|
|
| 71.5% |
Uncovered Elements: 37 (130) |
Complexity: 32 |
Complexity Density: 0.39 |
|
62 |
|
public class FilterTest |
63 |
|
{ |
64 |
|
private TestConfiguration configuration; |
65 |
|
|
66 |
|
private ComponentManager componentManager; |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
68 |
32 |
public FilterTest(TestConfiguration configuration, ComponentManager componentManager)... |
69 |
|
{ |
70 |
32 |
this.configuration = configuration; |
71 |
32 |
this.componentManager = componentManager; |
72 |
|
} |
73 |
|
|
|
|
| 64.5% |
Uncovered Elements: 11 (31) |
Complexity: 6 |
Complexity Density: 0.29 |
1PASS
|
|
74 |
32 |
@Test... |
75 |
|
public void execute() throws Throwable |
76 |
|
{ |
77 |
32 |
TimeZone currentTimeZone = TimeZone.getDefault(); |
78 |
|
|
79 |
|
|
80 |
32 |
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
81 |
|
|
82 |
32 |
Map<String, String> originalConfiguration = new HashMap<String, String>(); |
83 |
32 |
if (this.configuration.configuration != null) { |
84 |
32 |
ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class); |
85 |
|
|
86 |
32 |
if (configurationSource instanceof MockConfigurationSource) { |
87 |
32 |
MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource; |
88 |
|
|
89 |
32 |
for (Map.Entry<String, String> entry : this.configuration.configuration.entrySet()) { |
90 |
0 |
originalConfiguration.put(entry.getKey(), |
91 |
|
mockConfigurationSource.<String>getProperty(entry.getKey())); |
92 |
0 |
mockConfigurationSource.setProperty(entry.getKey(), TestDataParser.interpret(entry.getValue())); |
93 |
|
} |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
32 |
try { |
98 |
32 |
runTestInternal(); |
99 |
|
} finally { |
100 |
|
|
101 |
32 |
TimeZone.setDefault(currentTimeZone); |
102 |
|
|
103 |
|
|
104 |
32 |
if (this.configuration.configuration != null) { |
105 |
32 |
ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class); |
106 |
|
|
107 |
32 |
if (configurationSource instanceof MockConfigurationSource) { |
108 |
32 |
MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource; |
109 |
|
|
110 |
32 |
for (Map.Entry<String, String> entry : originalConfiguration.entrySet()) { |
111 |
0 |
if (entry.getValue() == null) { |
112 |
0 |
mockConfigurationSource.removeProperty(entry.getKey()); |
113 |
|
} else { |
114 |
0 |
mockConfigurationSource.setProperty(entry.getKey(), entry.getValue()); |
115 |
|
} |
116 |
|
} |
117 |
|
} |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
|
|
| 77.3% |
Uncovered Elements: 5 (22) |
Complexity: 5 |
Complexity Density: 0.36 |
|
122 |
4 |
private InputSource getInputSource(TestConfiguration testConfiguration, String value) throws FilterException... |
123 |
|
{ |
124 |
4 |
InputSource source; |
125 |
|
|
126 |
4 |
String sourceString = TestDataParser.interpret(value); |
127 |
|
|
128 |
4 |
if (sourceString.startsWith("file:")) { |
129 |
0 |
sourceString = sourceString.substring("file:".length()); |
130 |
|
} |
131 |
|
|
132 |
4 |
File file = new File(sourceString); |
133 |
|
|
134 |
4 |
if (file.exists()) { |
135 |
|
|
136 |
|
|
137 |
1 |
source = new DefaultFileInputSource(file); |
138 |
|
} else { |
139 |
|
|
140 |
|
|
141 |
3 |
if (!sourceString.startsWith("/")) { |
142 |
3 |
sourceString = |
143 |
|
StringUtils.substringBeforeLast(testConfiguration.resourceName, "/") + '/' + sourceString; |
144 |
|
} |
145 |
|
|
146 |
3 |
URL url = getClass().getResource(sourceString); |
147 |
|
|
148 |
3 |
if (url == null) { |
149 |
0 |
throw new FilterException("Resource [" + sourceString + "] does not exist"); |
150 |
|
} |
151 |
|
|
152 |
3 |
source = new DefaultURLInputSource(url); |
153 |
|
} |
154 |
|
|
155 |
4 |
return source; |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 7 |
Complexity Density: 0.78 |
|
158 |
32 |
private Map<String, Object> toInputConfiguration(InputFilterStreamFactory inputFactory,... |
159 |
|
TestConfiguration testConfiguration, InputTestConfiguration inputTestConfiguration) throws FilterException |
160 |
|
{ |
161 |
32 |
Map<String, Object> inputConfiguration = new HashMap<>(); |
162 |
32 |
for (Map.Entry<String, String> entry : inputTestConfiguration.entrySet()) { |
163 |
36 |
FilterStreamPropertyDescriptor<?> propertyDescriptor = |
164 |
|
inputFactory.getDescriptor().getPropertyDescriptor(entry.getKey()); |
165 |
|
|
166 |
36 |
if (propertyDescriptor != null && propertyDescriptor.getType() == InputSource.class |
167 |
|
&& entry.getValue() != null |
168 |
|
&& (entry.getKey().startsWith("file:") || entry.getKey().indexOf(':') < 0)) { |
169 |
4 |
inputConfiguration.put(entry.getKey(), getInputSource(testConfiguration, entry.getValue())); |
170 |
|
} else { |
171 |
32 |
inputConfiguration.put(entry.getKey(), TestDataParser.interpret(entry.getValue())); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
32 |
if (!inputConfiguration.containsKey(FilterStreamConstants.PROPERTY_SOURCE)) { |
177 |
28 |
inputConfiguration.put(FilterStreamConstants.PROPERTY_SOURCE, |
178 |
|
new StringInputSource(inputTestConfiguration.buffer)); |
179 |
|
} |
180 |
|
|
181 |
32 |
return inputConfiguration; |
182 |
|
} |
183 |
|
|
|
|
| 70% |
Uncovered Elements: 6 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
184 |
32 |
private Map<String, Object> toOutputConfiguration(TestConfiguration testConfiguration,... |
185 |
|
ExpectTestConfiguration expectTestConfiguration, InputSource expect) throws FilterException |
186 |
|
{ |
187 |
32 |
Map<String, Object> outputConfiguration = new HashMap<>(); |
188 |
32 |
for (Map.Entry<String, String> entry : expectTestConfiguration.entrySet()) { |
189 |
32 |
outputConfiguration.put(entry.getKey(), TestDataParser.interpret(entry.getValue())); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
32 |
if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_TARGET)) { |
194 |
32 |
if (expect instanceof ReaderInputSource) { |
195 |
32 |
outputConfiguration.put(FilterStreamConstants.PROPERTY_TARGET, new StringWriterOutputTarget()); |
196 |
|
} else { |
197 |
0 |
outputConfiguration.put(FilterStreamConstants.PROPERTY_TARGET, new ByteArrayOutputTarget()); |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
32 |
if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_FORMAT)) { |
203 |
32 |
outputConfiguration.put(FilterStreamConstants.PROPERTY_FORMAT, true); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
32 |
if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_ENCODING)) { |
208 |
0 |
outputConfiguration.put(FilterStreamConstants.PROPERTY_ENCODING, "UTF-8"); |
209 |
|
} |
210 |
|
|
211 |
32 |
return outputConfiguration; |
212 |
|
} |
213 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
214 |
32 |
private InputSource getExpectInputSource(TestConfiguration testConfiguration,... |
215 |
|
ExpectTestConfiguration expectConfiguration) throws FilterException |
216 |
|
{ |
217 |
32 |
String expectPath = expectConfiguration.get(FilterStreamConstants.PROPERTY_SOURCE); |
218 |
32 |
if (expectPath == null) { |
219 |
32 |
return new StringInputSource(expectConfiguration.buffer.toString()); |
220 |
|
} else { |
221 |
0 |
return getInputSource(testConfiguration, expectPath); |
222 |
|
} |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
225 |
32 |
private void runTestInternal() throws Throwable... |
226 |
|
{ |
227 |
|
|
228 |
|
|
229 |
32 |
InputSource expect = getExpectInputSource(this.configuration, this.configuration.expectConfiguration); |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
32 |
InputFilterStreamFactory inputFactory = getComponentManager().getInstance(InputFilterStreamFactory.class, |
234 |
|
this.configuration.inputConfiguration.typeId); |
235 |
32 |
InputFilterStream inputFilter = inputFactory.createInputFilterStream( |
236 |
|
toInputConfiguration(inputFactory, this.configuration, this.configuration.inputConfiguration)); |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
32 |
Map<String, Object> outputConfiguration = |
241 |
|
toOutputConfiguration(this.configuration, this.configuration.expectConfiguration, expect); |
242 |
32 |
OutputFilterStreamFactory outputFactory = getComponentManager().getInstance(OutputFilterStreamFactory.class, |
243 |
|
this.configuration.expectConfiguration.typeId); |
244 |
32 |
OutputFilterStream outputFilter = outputFactory.createOutputFilterStream(outputConfiguration); |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
32 |
inputFilter.read(outputFilter.getFilter()); |
249 |
|
|
250 |
32 |
inputFilter.close(); |
251 |
32 |
outputFilter.close(); |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
32 |
assertExpectedResult(this.configuration.expectConfiguration.typeId, expect, |
256 |
|
(OutputTarget) outputConfiguration.get(FilterStreamConstants.PROPERTY_TARGET)); |
257 |
|
} |
258 |
|
|
|
|
| 18.8% |
Uncovered Elements: 13 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
259 |
32 |
private void assertExpectedResult(String typeId, InputSource expected, OutputTarget actual) throws IOException... |
260 |
|
{ |
261 |
32 |
if (actual instanceof StringWriterOutputTarget) { |
262 |
32 |
Assert.assertEquals(expected.toString(), actual.toString()); |
263 |
0 |
} else if (actual instanceof ByteArrayOutputTarget) { |
264 |
0 |
byte[] actualBytes = ((ByteArrayOutputTarget) actual).toByteArray(); |
265 |
|
|
266 |
0 |
if (expected instanceof FileInputSource) { |
267 |
0 |
FileAssert.assertEquals(((FileInputSource) expected).getFile(), actualBytes); |
268 |
|
} else { |
269 |
0 |
byte[] expectedBytes = IOUtils.toByteArray(((InputStreamInputSource) expected).getInputStream()); |
270 |
0 |
expected.close(); |
271 |
|
|
272 |
0 |
Assert.assertArrayEquals(expectedBytes, actualBytes); |
273 |
|
} |
274 |
|
} else { |
275 |
|
|
276 |
0 |
Assert.fail("Output target type [" + actual.getClass() + "] is not supported"); |
277 |
|
} |
278 |
|
} |
279 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
280 |
128 |
public ComponentManager getComponentManager() throws Exception... |
281 |
|
{ |
282 |
128 |
return this.componentManager; |
283 |
|
} |
284 |
|
} |