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

File FilterTest.java

 

Code metrics

40
86
9
1
292
199
33
0.38
9.56
9
3.67

Classes

Class Line # Actions
FilterTest 63 86 0% 33 39
0.711111171.1%
 

Contributing tests

This file is covered by 40 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.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.FileUtils;
30    import org.apache.commons.io.IOUtils;
31    import org.apache.commons.lang3.StringUtils;
32    import org.junit.Test;
33    import org.junit.jupiter.api.Assertions;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.configuration.ConfigurationSource;
36    import org.xwiki.filter.FilterException;
37    import org.xwiki.filter.descriptor.FilterStreamPropertyDescriptor;
38    import org.xwiki.filter.input.DefaultFileInputSource;
39    import org.xwiki.filter.input.DefaultURLInputSource;
40    import org.xwiki.filter.input.FileInputSource;
41    import org.xwiki.filter.input.InputFilterStream;
42    import org.xwiki.filter.input.InputFilterStreamFactory;
43    import org.xwiki.filter.input.InputSource;
44    import org.xwiki.filter.input.InputStreamInputSource;
45    import org.xwiki.filter.input.ReaderInputSource;
46    import org.xwiki.filter.input.StringInputSource;
47    import org.xwiki.filter.output.ByteArrayOutputTarget;
48    import org.xwiki.filter.output.OutputFilterStream;
49    import org.xwiki.filter.output.OutputFilterStreamFactory;
50    import org.xwiki.filter.output.OutputTarget;
51    import org.xwiki.filter.output.StringWriterOutputTarget;
52    import org.xwiki.filter.test.internal.FileAssert;
53    import org.xwiki.filter.utils.FilterStreamConstants;
54    import org.xwiki.test.internal.MockConfigurationSource;
55   
56    /**
57    * A generic JUnit Test used by {@link FilterTestSuite} to parse some passed content and verify it matches some passed
58    * expectation. The format of the input/expectation is specified in {@link TestDataParser}.
59    *
60    * @version $Id: 10266c228a35cec04193c586f69d10f8fea5fb99 $
61    * @since 6.2M1
62    */
 
63    public class FilterTest
64    {
65    private TestConfiguration configuration;
66   
67    private ComponentManager componentManager;
68   
 
69  40 toggle public FilterTest(TestConfiguration configuration, ComponentManager componentManager)
70    {
71  40 this.configuration = configuration;
72  40 this.componentManager = componentManager;
73    }
74   
 
75  40 toggle @Test
76    public void execute() throws Throwable
77    {
78  40 TimeZone currentTimeZone = TimeZone.getDefault();
79   
80    // Make sure to have a stable timezone during tests
81  40 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
82   
83  40 Map<String, String> originalConfiguration = new HashMap<String, String>();
84  40 if (this.configuration.configuration != null) {
85  40 ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class);
86   
87  40 if (configurationSource instanceof MockConfigurationSource) {
88  40 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
89   
90  40 for (Map.Entry<String, String> entry : this.configuration.configuration.entrySet()) {
91  0 originalConfiguration.put(entry.getKey(),
92    mockConfigurationSource.<String>getProperty(entry.getKey()));
93  0 mockConfigurationSource.setProperty(entry.getKey(), TestDataParser.interpret(entry.getValue()));
94    }
95    }
96    }
97   
98  40 try {
99  40 runTestInternal();
100    } finally {
101    // Restore current Timezone
102  40 TimeZone.setDefault(currentTimeZone);
103   
104    // Revert Configuration that have been set
105  40 if (this.configuration.configuration != null) {
106  40 ConfigurationSource configurationSource = getComponentManager().getInstance(ConfigurationSource.class);
107   
108  40 if (configurationSource instanceof MockConfigurationSource) {
109  40 MockConfigurationSource mockConfigurationSource = (MockConfigurationSource) configurationSource;
110   
111  40 for (Map.Entry<String, String> entry : originalConfiguration.entrySet()) {
112  0 if (entry.getValue() == null) {
113  0 mockConfigurationSource.removeProperty(entry.getKey());
114    } else {
115  0 mockConfigurationSource.setProperty(entry.getKey(), entry.getValue());
116    }
117    }
118    }
119    }
120    }
121    }
122   
 
123  8 toggle private InputSource getInputSource(TestConfiguration testConfiguration, String value) throws FilterException
124    {
125  8 InputSource source;
126   
127  8 String sourceString = TestDataParser.interpret(value);
128   
129  8 if (sourceString.startsWith("file:")) {
130  0 sourceString = sourceString.substring("file:".length());
131    }
132   
133  8 File file = new File(sourceString);
134   
135  8 if (file.exists()) {
136    // It's a file
137   
138  8 source = new DefaultFileInputSource(file);
139    } else {
140    // If not a file it's probably a resource
141   
142  0 if (!sourceString.startsWith("/")) {
143  0 sourceString =
144    StringUtils.substringBeforeLast(testConfiguration.resourceName, "/") + '/' + sourceString;
145    }
146   
147  0 URL resource = getClass().getResource(sourceString);
148   
149  0 if (resource == null) {
150  0 throw new FilterException("Resource [" + sourceString + "] does not exist");
151    }
152   
153  0 if (resource.getProtocol().equals("file")) {
154    // If the resource is a local file let's return it as such
155  0 file = FileUtils.toFile(resource);
156  0 source = new DefaultFileInputSource(file);
157    } else {
158    // Otherwise keep it as URL
159  0 source = new DefaultURLInputSource(resource);
160    }
161    }
162   
163  8 return source;
164    }
165   
 
166  40 toggle private Map<String, Object> toInputConfiguration(InputFilterStreamFactory inputFactory,
167    TestConfiguration testConfiguration, InputTestConfiguration inputTestConfiguration) throws FilterException
168    {
169  40 Map<String, Object> inputConfiguration = new HashMap<>();
170  40 for (Map.Entry<String, String> entry : inputTestConfiguration.entrySet()) {
171  49 FilterStreamPropertyDescriptor<?> propertyDescriptor =
172    inputFactory.getDescriptor().getPropertyDescriptor(entry.getKey());
173   
174  49 if (propertyDescriptor != null && propertyDescriptor.getType() == InputSource.class
175    && entry.getValue() != null
176    && (entry.getKey().startsWith("file:") || entry.getKey().indexOf(':') < 0)) {
177  7 inputConfiguration.put(entry.getKey(), getInputSource(testConfiguration, entry.getValue()));
178    } else {
179  42 inputConfiguration.put(entry.getKey(), TestDataParser.interpret(entry.getValue()));
180    }
181    }
182   
183    // Generate a source f it does not exist
184  40 if (!inputConfiguration.containsKey(FilterStreamConstants.PROPERTY_SOURCE)) {
185  33 inputConfiguration.put(FilterStreamConstants.PROPERTY_SOURCE,
186    new StringInputSource(inputTestConfiguration.buffer));
187    }
188   
189  40 return inputConfiguration;
190    }
191   
 
192  40 toggle private Map<String, Object> toOutputConfiguration(TestConfiguration testConfiguration,
193    ExpectTestConfiguration expectTestConfiguration, InputSource expect)
194    {
195  40 Map<String, Object> outputConfiguration = new HashMap<>();
196  40 for (Map.Entry<String, String> entry : expectTestConfiguration.entrySet()) {
197  50 outputConfiguration.put(entry.getKey(), TestDataParser.interpret(entry.getValue()));
198    }
199   
200    // Generate a source if it does not exist
201  40 if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_TARGET)) {
202  40 if (expect instanceof ReaderInputSource) {
203  39 outputConfiguration.put(FilterStreamConstants.PROPERTY_TARGET, new StringWriterOutputTarget());
204    } else {
205  1 outputConfiguration.put(FilterStreamConstants.PROPERTY_TARGET, new ByteArrayOutputTarget());
206    }
207    }
208   
209    // Format by default
210  40 if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_FORMAT)) {
211  40 outputConfiguration.put(FilterStreamConstants.PROPERTY_FORMAT, true);
212    }
213   
214    // Encoding by default
215  40 if (!outputConfiguration.containsKey(FilterStreamConstants.PROPERTY_ENCODING)) {
216  0 outputConfiguration.put(FilterStreamConstants.PROPERTY_ENCODING, "UTF-8");
217    }
218   
219  40 return outputConfiguration;
220    }
221   
 
222  40 toggle private InputSource getExpectInputSource(TestConfiguration testConfiguration,
223    ExpectTestConfiguration expectConfiguration) throws FilterException
224    {
225  40 String expectPath = expectConfiguration.get(FilterStreamConstants.PROPERTY_SOURCE);
226  40 if (expectPath == null) {
227  39 return new StringInputSource(expectConfiguration.buffer.toString());
228    } else {
229  1 return getInputSource(testConfiguration, expectPath);
230    }
231    }
232   
 
233  40 toggle private void runTestInternal() throws Throwable
234    {
235    // Expect
236   
237  40 InputSource expect = getExpectInputSource(this.configuration, this.configuration.expectConfiguration);
238   
239    // Input
240   
241  40 InputFilterStreamFactory inputFactory = getComponentManager().getInstance(InputFilterStreamFactory.class,
242    this.configuration.inputConfiguration.typeId);
243  40 InputFilterStream inputFilter = inputFactory.createInputFilterStream(
244    toInputConfiguration(inputFactory, this.configuration, this.configuration.inputConfiguration));
245   
246    // Output
247   
248  40 Map<String, Object> outputConfiguration =
249    toOutputConfiguration(this.configuration, this.configuration.expectConfiguration, expect);
250  40 OutputFilterStreamFactory outputFactory = getComponentManager().getInstance(OutputFilterStreamFactory.class,
251    this.configuration.expectConfiguration.typeId);
252  40 OutputFilterStream outputFilter = outputFactory.createOutputFilterStream(outputConfiguration);
253   
254    // Convert
255   
256  40 inputFilter.read(outputFilter.getFilter());
257   
258  40 inputFilter.close();
259  40 outputFilter.close();
260   
261    // Verify the expected result against the result we got.
262   
263  40 assertExpectedResult(this.configuration.expectConfiguration.typeId, expect,
264    (OutputTarget) outputConfiguration.get(FilterStreamConstants.PROPERTY_TARGET));
265    }
266   
 
267  40 toggle private void assertExpectedResult(String typeId, InputSource expected, OutputTarget actual) throws IOException
268    {
269  40 if (actual instanceof StringWriterOutputTarget) {
270  39 Assertions.assertEquals(expected.toString(), actual.toString());
271  1 } else if (actual instanceof ByteArrayOutputTarget) {
272  1 byte[] actualBytes = ((ByteArrayOutputTarget) actual).toByteArray();
273   
274  1 if (expected instanceof FileInputSource) {
275  1 FileAssert.assertEquals(((FileInputSource) expected).getFile(), actualBytes);
276    } else {
277  0 byte[] expectedBytes = IOUtils.toByteArray(((InputStreamInputSource) expected).getInputStream());
278  0 expected.close();
279   
280  0 Assertions.assertArrayEquals(expectedBytes, actualBytes);
281    }
282    } else {
283    // No idea how to compare that
284  0 Assertions.fail("Output target type [" + actual.getClass() + "] is not supported");
285    }
286    }
287   
 
288  160 toggle public ComponentManager getComponentManager() throws Exception
289    {
290  160 return this.componentManager;
291    }
292    }