1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
43 |
|
|
44 |
|
@version |
45 |
|
|
|
|
| 98.4% |
Uncovered Elements: 1 (61) |
Complexity: 15 |
Complexity Density: 0.38 |
|
46 |
|
public class HTMLCleanerTestSuite extends Suite |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private static final String INPUT_EXPECTED_SEPARATOR = "---"; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private static final String COMMENT_LINE_PREFIX = "#"; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String NEW_LINE_ESCAPE = "\\"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
|
|
| 93.5% |
Uncovered Elements: 2 (31) |
Complexity: 10 |
Complexity Density: 0.43 |
|
67 |
|
private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner |
68 |
|
{ |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private final int parameterSetNumber; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private final XWikiComponentInitializer componentInitializer = new XWikiComponentInitializer(); |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private final List<Object[]> parameterList; |
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
@param |
87 |
|
@param |
88 |
|
@throws |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
90 |
3 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
3 |
@Override... |
98 |
|
public Object createTest() throws Exception |
99 |
|
{ |
100 |
3 |
return getTestClass().getOnlyConstructor().newInstance(computeParams()); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
@return |
105 |
|
@throws |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
107 |
3 |
private Object[] computeParams() throws Exception... |
108 |
|
{ |
109 |
|
|
110 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
6 |
@Override... |
120 |
|
protected String getName() |
121 |
|
{ |
122 |
6 |
return (String) this.parameterList.get(this.parameterSetNumber)[0]; |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
3 |
@Override... |
126 |
|
protected String testName(FrameworkMethod method) |
127 |
|
{ |
128 |
3 |
return getName(); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
3 |
@Override... |
132 |
|
protected void validateConstructor(List<Throwable> errors) |
133 |
|
{ |
134 |
3 |
validateOnlyOneConstructor(errors); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
3 |
@Override... |
138 |
|
protected Statement classBlock(RunNotifier notifier) |
139 |
|
{ |
140 |
3 |
return childrenInvoker(notifier); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
@inheritDoc |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
|
149 |
3 |
@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 |
|
|
173 |
|
|
174 |
|
private final List<Runner> runners = new ArrayList<Runner>(); |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@throws |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
182 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
1 |
@Override... |
193 |
|
protected List<Runner> getChildren() |
194 |
|
{ |
195 |
1 |
return this.runners; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
@inheritDoc |
200 |
|
|
201 |
|
|
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
2 |
@Override... |
204 |
|
public Description getDescription() |
205 |
|
{ |
206 |
2 |
return Description.createSuiteDescription(getTestClass().getJavaClass()); |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
@return |
214 |
|
@throws |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
216 |
1 |
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 |
|
|
227 |
|
|
228 |
|
@param |
229 |
|
@return |
230 |
|
@throws |
231 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
232 |
1 |
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 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@return |
255 |
|
@throws |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
257 |
3 |
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 |
|
|
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 |
|
|
283 |
|
|
284 |
|
@param |
285 |
|
@param |
286 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
287 |
27 |
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 |
|
} |