1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.cleaner

File HTMLCleanerTestSuite.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
63
15
2
297
165
25
0.4
4.2
7.5
1.67

Classes

Class Line # Actions
HTMLCleanerTestSuite 46 40 0% 15 1
0.983606698.4%
HTMLCleanerTestSuite.TestClassRunnerForParameters 67 23 0% 10 2
0.935483993.5%
 

Contributing tests

No tests hitting this source file were found.

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.wysiwyg.server.internal.cleaner;
21   
22    import java.io.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.InputStreamReader;
26    import java.util.ArrayList;
27    import java.util.Collections;
28    import java.util.List;
29   
30    import org.junit.runner.Description;
31    import org.junit.runner.Runner;
32    import org.junit.runner.notification.RunNotifier;
33    import org.junit.runners.BlockJUnit4ClassRunner;
34    import org.junit.runners.Suite;
35    import org.junit.runners.model.FrameworkMethod;
36    import org.junit.runners.model.InitializationError;
37    import org.junit.runners.model.Statement;
38    import org.xwiki.gwt.wysiwyg.client.cleaner.HTMLCleaner;
39    import org.xwiki.test.jmock.XWikiComponentInitializer;
40   
41    /**
42    * A suite of {@link HTMLCleanerTest}.
43    *
44    * @version $Id: ed97b2f4f3beea1cb0d772b543f050782c3a259e $
45    */
 
46    public class HTMLCleanerTestSuite extends Suite
47    {
48    /**
49    * The line that separates the HTML input from the expected clean HTML.
50    */
51    private static final String INPUT_EXPECTED_SEPARATOR = "---";
52   
53    /**
54    * The string that prefixes comment lines.
55    */
56    private static final String COMMENT_LINE_PREFIX = "#";
57   
58    /**
59    * The string used to escape the new line character. This is very useful when you want to split the HTML input or
60    * the expected clean HTML on multiple lines without inserting new line characters in the content.
61    */
62    private static final String NEW_LINE_ESCAPE = "\\";
63   
64    /**
65    * The class used to instantiate test, passing the test data to the constructor.
66    */
 
67    private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner
68    {
69    /**
70    * The index of this test.
71    */
72    private final int parameterSetNumber;
73   
74    /**
75    * The object used to initialize the component manager.
76    */
77    private final XWikiComponentInitializer componentInitializer = new XWikiComponentInitializer();
78   
79    /**
80    * The list of all tests data.
81    */
82    private final List<Object[]> parameterList;
83   
84    /**
85    * @param type the test class
86    * @param parameterList the list of data for all tests
87    * @param i the index of the current test
88    * @throws InitializationError if the runner initialization fails
89    */
 
90  3 toggle TestClassRunnerForParameters(Class< ? > type, List<Object[]> parameterList, int i) throws InitializationError
91    {
92  3 super(type);
93  3 this.parameterList = parameterList;
94  3 this.parameterSetNumber = i;
95    }
96   
 
97  3 toggle @Override
98    public Object createTest() throws Exception
99    {
100  3 return getTestClass().getOnlyConstructor().newInstance(computeParams());
101    }
102   
103    /**
104    * @return the parameters that are passed to the test constructor
105    * @throws Exception if we fail to include the component manager in the list of parameters
106    */
 
107  3 toggle private Object[] computeParams() throws Exception
108    {
109    // Add the Component Manager as the last parameter in order to pass it to the Test constructor
110    // Remove the first parameter which is the test name and that is not needed in HTMLCleanerTest.
111  3 Object[] originalObjects = this.parameterList.get(this.parameterSetNumber);
112  3 Object[] newObjects = new Object[originalObjects.length];
113  3 System.arraycopy(originalObjects, 1, newObjects, 0, originalObjects.length - 1);
114  3 HTMLCleaner cleaner = this.componentInitializer.getComponentManager().getInstance(HTMLCleaner.class);
115  3 newObjects[originalObjects.length - 1] = cleaner;
116  3 return newObjects;
117    }
118   
 
119  6 toggle @Override
120    protected String getName()
121    {
122  6 return (String) this.parameterList.get(this.parameterSetNumber)[0];
123    }
124   
 
125  3 toggle @Override
126    protected String testName(FrameworkMethod method)
127    {
128  3 return getName();
129    }
130   
 
131  3 toggle @Override
132    protected void validateConstructor(List<Throwable> errors)
133    {
134  3 validateOnlyOneConstructor(errors);
135    }
136   
 
137  3 toggle @Override
138    protected Statement classBlock(RunNotifier notifier)
139    {
140  3 return childrenInvoker(notifier);
141    }
142   
143    /**
144    * {@inheritDoc}
145    * <p>
146    * Initialize the Component Manager before each test is executed, to ensure test isolation.
147    * </p>
148    */
 
149  3 toggle @Override
150    protected void runChild(FrameworkMethod method, RunNotifier notifier)
151    {
152  3 try {
153  3 this.componentInitializer.initializeConfigurationSource();
154  3 this.componentInitializer.initializeExecution();
155    } catch (Exception e) {
156  0 throw new RuntimeException("Failed to initialize Component Manager", e);
157    }
158   
159  3 try {
160  3 super.runChild(method, notifier);
161    } finally {
162  3 try {
163  3 this.componentInitializer.shutdown();
164    } catch (Exception e) {
165  0 throw new RuntimeException("Failed to shutdown Component Manager", e);
166    }
167    }
168    }
169    }
170   
171    /**
172    * The tests to run.
173    */
174    private final List<Runner> runners = new ArrayList<Runner>();
175   
176    /**
177    * Only called reflectively.
178    *
179    * @param klass the class that is run with this suite
180    * @throws Exception if the if the suite can't be initialized
181    */
 
182  1 toggle public HTMLCleanerTestSuite(Class< ? > klass) throws Exception
183    {
184  1 super(HTMLCleanerTest.class, Collections.<Runner> emptyList());
185   
186  1 List<Object[]> testsData = readTestsDataFromResource("/HTMLCleanerTests.txt");
187  4 for (int i = 0; i < testsData.size(); i++) {
188  3 this.runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), testsData, i));
189    }
190    }
191   
 
192  1 toggle @Override
193    protected List<Runner> getChildren()
194    {
195  1 return this.runners;
196    }
197   
198    /**
199    * {@inheritDoc}
200    * <p>We override this method so that the JUnit results are not displayed in a test hierarchy with a
201    * single test result for each node (as it would be otherwise since RenderingTest has a single test method).</p>
202    */
 
203  2 toggle @Override
204    public Description getDescription()
205    {
206  2 return Description.createSuiteDescription(getTestClass().getJavaClass());
207    }
208   
209    /**
210    * Reads the data for the tests listed in the specified resource file.
211    *
212    * @param testResourceName the name of a resource file that lists the tests to be run
213    * @return the data for all the test to be run
214    * @throws IOException if reading the test resource fails
215    */
 
216  1 toggle private List<Object[]> readTestsDataFromResource(String testResourceName) throws IOException
217    {
218  1 List<Object[]> testsData = new ArrayList<Object[]>();
219  1 for (String testName : getTestNames(getClass().getResourceAsStream(testResourceName))) {
220  3 testsData.add(readTestDataFromResource(testName));
221    }
222  1 return testsData;
223    }
224   
225    /**
226    * Reads the list of test names from the given input stream.
227    *
228    * @param source where to read the test names from
229    * @return a list of test names
230    * @throws IOException if reading the test names fails
231    */
 
232  1 toggle private List<String> getTestNames(InputStream source) throws IOException
233    {
234  1 BufferedReader reader = new BufferedReader(new InputStreamReader(source));
235  1 List<String> testNames = new ArrayList<String>();
236  1 try {
237  1 String line = reader.readLine();
238  4 while (line != null) {
239  3 if (!line.startsWith(COMMENT_LINE_PREFIX)) {
240  3 testNames.add(line.trim());
241    }
242  3 line = reader.readLine();
243    }
244    } finally {
245  1 reader.close();
246    }
247  1 return testNames;
248    }
249   
250    /**
251    * Reads the test data, the HTML input and the expected clean HTML, from the resource file with the specified name.
252    *
253    * @param testResourceName the name of a resource file that contains the test data
254    * @return the test data read from the specified resource
255    * @throws IOException if reading the resource files fails
256    */
 
257  3 toggle private String[] readTestDataFromResource(String testResourceName) throws IOException
258    {
259  3 InputStream source = getClass().getResourceAsStream('/' + testResourceName + ".test");
260  3 BufferedReader reader = new BufferedReader(new InputStreamReader(source));
261  3 try {
262  3 StringBuilder input = new StringBuilder();
263  3 String line = reader.readLine();
264  21 while (line != null && !line.equals(INPUT_EXPECTED_SEPARATOR)) {
265  18 appendLine(input, line);
266  18 line = reader.readLine();
267    }
268  3 StringBuilder expected = new StringBuilder();
269    // Skip the line that separates the input from the expected HTML.
270  3 line = reader.readLine();
271  12 while (line != null) {
272  9 appendLine(expected, line);
273  9 line = reader.readLine();
274    }
275  3 return new String[] {testResourceName, input.toString(), expected.toString()};
276    } finally {
277  3 reader.close();
278    }
279    }
280   
281    /**
282    * Skips the line if it's a comment and removes the new line character if it is escaped.
283    *
284    * @param output where to append the given line
285    * @param line the line of text to be appended
286    */
 
287  27 toggle private void appendLine(StringBuilder output, String line)
288    {
289  27 if (line.startsWith(COMMENT_LINE_PREFIX)) {
290  9 return;
291  18 } else if (line.endsWith(NEW_LINE_ESCAPE)) {
292  12 output.append(line, 0, line.length() - NEW_LINE_ESCAPE.length());
293    } else {
294  6 output.append(line).append('\n');
295    }
296    }
297    }