1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.test.cts; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.net.URL; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Properties; |
28 |
|
import java.util.Set; |
29 |
|
import java.util.TreeSet; |
30 |
|
import java.util.regex.Pattern; |
31 |
|
|
32 |
|
import org.apache.commons.configuration.CompositeConfiguration; |
33 |
|
import org.apache.commons.configuration.PropertiesConfiguration; |
34 |
|
import org.apache.commons.io.IOUtils; |
35 |
|
import org.apache.commons.lang3.StringUtils; |
36 |
|
import org.apache.commons.lang3.tuple.ImmutablePair; |
37 |
|
import org.apache.commons.lang3.tuple.Pair; |
38 |
|
import org.reflections.Reflections; |
39 |
|
import org.reflections.scanners.ResourcesScanner; |
40 |
|
import org.reflections.util.ClasspathHelper; |
41 |
|
import org.reflections.util.ConfigurationBuilder; |
42 |
|
import org.reflections.util.FilterBuilder; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
@see |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (119) |
Complexity: 17 |
Complexity Density: 0.18 |
|
52 |
|
public class TestDataParser |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private static final String SLASH = "/"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String DOT = "."; |
63 |
|
|
64 |
|
|
65 |
|
@link |
66 |
|
|
67 |
|
@param |
68 |
|
@param |
69 |
|
@param |
70 |
|
@param |
71 |
|
|
72 |
|
@return |
73 |
|
@throws |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
75 |
18 |
public List<TestData> parseTestData(String syntaxId, String ctsRootPackageName, String packageFilter,... |
76 |
|
String pattern) throws Exception |
77 |
|
{ |
78 |
18 |
ClassLoader classLoader = getClass().getClassLoader(); |
79 |
|
|
80 |
18 |
List<TestData> data = new ArrayList<TestData>(); |
81 |
18 |
String syntaxDirectory = computeSyntaxDirectory(syntaxId); |
82 |
|
|
83 |
|
|
84 |
18 |
TestDataConfiguration configuration = parseTestConfiguration(syntaxDirectory, ctsRootPackageName, classLoader); |
85 |
|
|
86 |
18 |
Set<String> relativeDirectoryNames = findRelativeTestDirectoryNames(ctsRootPackageName, packageFilter, pattern); |
87 |
18 |
for (String relativeDirectoryName : relativeDirectoryNames) { |
88 |
630 |
List<TestData> testDatas = parseSingleTestData(syntaxDirectory, ctsRootPackageName, relativeDirectoryName, |
89 |
|
configuration, classLoader); |
90 |
630 |
for (TestData testData : testDatas) { |
91 |
1281 |
testData.syntaxId = syntaxId; |
92 |
1281 |
testData.prefix = relativeDirectoryName; |
93 |
1281 |
testData.configuration = configuration; |
94 |
1281 |
data.add(testData); |
95 |
|
} |
96 |
|
} |
97 |
18 |
return data; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
|
105 |
|
@param |
106 |
|
@param |
107 |
|
@param |
108 |
|
@param |
109 |
|
@return |
110 |
|
@throws |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 4 |
Complexity Density: 0.15 |
|
112 |
630 |
public List<TestData> parseSingleTestData(String syntaxDirectory, String ctsRootPackageName,... |
113 |
|
String relativeDirectoryName, TestDataConfiguration configuration, ClassLoader classLoader) throws IOException |
114 |
|
{ |
115 |
|
|
116 |
630 |
TestData testDataIN = new TestData(); |
117 |
630 |
testDataIN.isSyntaxInputTest = true; |
118 |
630 |
TestData testDataOUT = new TestData(); |
119 |
|
|
120 |
|
|
121 |
630 |
Pair<Pair<String, String>, Pair<String, String>> ctsData = |
122 |
|
readDataForPrefix(ctsRootPackageName + SLASH + relativeDirectoryName, "xml", classLoader); |
123 |
|
|
124 |
630 |
Pair<Pair<String, String>, Pair<String, String>> syntaxData = readDataForPrefix( |
125 |
|
syntaxDirectory + SLASH + relativeDirectoryName, configuration.fileExtension, classLoader); |
126 |
|
|
127 |
630 |
testDataIN.syntaxData = syntaxData.getLeft().getLeft(); |
128 |
630 |
testDataIN.syntaxExtension = syntaxData.getLeft().getRight(); |
129 |
630 |
testDataIN.ctsData = ctsData.getRight().getLeft(); |
130 |
630 |
testDataIN.ctsExtension = ctsData.getRight().getRight(); |
131 |
630 |
testDataOUT.syntaxData = syntaxData.getRight().getLeft(); |
132 |
630 |
testDataOUT.syntaxExtension = syntaxData.getRight().getRight(); |
133 |
630 |
testDataOUT.ctsData = ctsData.getLeft().getLeft(); |
134 |
630 |
testDataOUT.ctsExtension = ctsData.getLeft().getRight(); |
135 |
|
|
136 |
|
|
137 |
630 |
List<TestData> testDataINAliases = parseAliasesTestData(syntaxDirectory, relativeDirectoryName, ctsData, |
138 |
|
configuration, classLoader); |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
630 |
if (configuration.inheritSyntax != null) { |
143 |
185 |
Pair<Pair<String, String>, Pair<String, String>> inheritedSyntaxData = readDataForPrefix( |
144 |
|
computeSyntaxDirectory(configuration.inheritSyntax) + SLASH + relativeDirectoryName, |
145 |
|
configuration.fileExtension, |
146 |
|
classLoader); |
147 |
185 |
if (testDataIN.syntaxData == null) { |
148 |
175 |
testDataIN.syntaxData = inheritedSyntaxData.getLeft().getLeft(); |
149 |
175 |
testDataIN.syntaxExtension = inheritedSyntaxData.getLeft().getRight(); |
150 |
|
} |
151 |
185 |
if (testDataOUT.syntaxData == null) { |
152 |
157 |
testDataOUT.syntaxData = inheritedSyntaxData.getRight().getLeft(); |
153 |
157 |
testDataOUT.syntaxExtension = inheritedSyntaxData.getRight().getRight(); |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
630 |
List<TestData> result = new ArrayList<TestData>(); |
158 |
630 |
result.add(testDataIN); |
159 |
630 |
result.addAll(testDataINAliases); |
160 |
630 |
result.add(testDataOUT); |
161 |
630 |
return result; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
@param |
168 |
|
|
169 |
|
@param |
170 |
|
@param |
171 |
|
@param |
172 |
|
@param |
173 |
|
@return |
174 |
|
@throws |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
176 |
630 |
private List<TestData> parseAliasesTestData(String syntaxDirectory, String relativeDirectoryName,... |
177 |
|
Pair<Pair<String, String>, Pair<String, String>> ctsData, TestDataConfiguration configuration, |
178 |
|
ClassLoader classLoader) throws IOException |
179 |
|
{ |
180 |
630 |
List<TestData> testDataINAliases = new ArrayList<TestData>(); |
181 |
630 |
Pair<Pair<String, String>, Pair<String, String>> syntaxDataAlias; |
182 |
630 |
int i = 1; |
183 |
630 |
do { |
184 |
651 |
syntaxDataAlias = readDataForPrefix( |
185 |
|
syntaxDirectory + SLASH + relativeDirectoryName, i + DOT + configuration.fileExtension, classLoader); |
186 |
651 |
if (syntaxDataAlias.getLeft().getLeft() != null) { |
187 |
21 |
TestData testDataINAlias = new TestData(); |
188 |
21 |
testDataINAlias.isSyntaxInputTest = true; |
189 |
21 |
testDataINAlias.syntaxData = syntaxDataAlias.getLeft().getLeft(); |
190 |
21 |
testDataINAlias.syntaxExtension = syntaxDataAlias.getLeft().getRight(); |
191 |
21 |
testDataINAlias.ctsData = ctsData.getRight().getLeft(); |
192 |
21 |
testDataINAlias.ctsExtension = ctsData.getRight().getRight(); |
193 |
21 |
testDataINAliases.add(testDataINAlias); |
194 |
|
} |
195 |
651 |
i++; |
196 |
651 |
} while (syntaxDataAlias.getLeft().getLeft() != null); |
197 |
630 |
return testDataINAliases; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@param |
205 |
|
@param |
206 |
|
@return |
207 |
|
@throws |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
209 |
18 |
public TestDataConfiguration parseTestConfiguration(String syntaxDirectory, String ctsRootPackageName,... |
210 |
|
ClassLoader classLoader) throws Exception |
211 |
|
{ |
212 |
18 |
TestDataConfiguration configuration = new TestDataConfiguration(); |
213 |
|
|
214 |
18 |
CompositeConfiguration compositeConfiguration = new CompositeConfiguration(); |
215 |
18 |
addConfigurationData(compositeConfiguration, ctsRootPackageName, classLoader); |
216 |
18 |
addConfigurationData(compositeConfiguration, syntaxDirectory, classLoader); |
217 |
|
|
218 |
|
|
219 |
18 |
configuration.notApplicableTests = |
220 |
|
(List<String>) (List<?>) compositeConfiguration.getList("notApplicableTests", Collections.emptyList()); |
221 |
18 |
configuration.failingTests = |
222 |
|
(List<String>) (List<?>) compositeConfiguration.getList("failingTests", Collections.emptyList()); |
223 |
18 |
configuration.testDescriptions = compositeConfiguration.getProperties("testDescriptions", new Properties()); |
224 |
18 |
configuration.inheritSyntax = compositeConfiguration.getString("inheritSyntax"); |
225 |
18 |
configuration.fileExtension = compositeConfiguration.getString("fileExtension", "txt"); |
226 |
|
|
227 |
18 |
return configuration; |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
@param |
234 |
|
@param |
235 |
|
@param |
236 |
|
@throws |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
238 |
36 |
private void addConfigurationData(CompositeConfiguration configuration, String rootPackageName,... |
239 |
|
ClassLoader classLoader) throws Exception |
240 |
|
{ |
241 |
36 |
URL configurationURL = classLoader.getResource(rootPackageName + "/config.properties"); |
242 |
36 |
if (configurationURL != null) { |
243 |
33 |
configuration.addConfiguration(new PropertiesConfiguration(configurationURL)); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
@param |
251 |
|
@param |
252 |
|
@param |
253 |
|
@return |
254 |
|
@throws |
255 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
|
256 |
2096 |
private Pair<Pair<String, String>, Pair<String, String>> readDataForPrefix(String prefix, String fileExtension,... |
257 |
|
ClassLoader classLoader) throws IOException |
258 |
|
{ |
259 |
2096 |
String in; |
260 |
2096 |
String out; |
261 |
2096 |
String inExtension = ".inout." + fileExtension; |
262 |
2096 |
String outExtension = inExtension; |
263 |
2096 |
String inOut = readData(prefix + inExtension, classLoader); |
264 |
2096 |
if (inOut == null) { |
265 |
1307 |
inExtension = ".in." + fileExtension; |
266 |
1307 |
outExtension = ".out." + fileExtension; |
267 |
1307 |
in = readData(prefix + inExtension, classLoader); |
268 |
1307 |
out = readData(prefix + outExtension, classLoader); |
269 |
|
} else { |
270 |
789 |
in = inOut; |
271 |
789 |
out = inOut; |
272 |
|
} |
273 |
|
|
274 |
2096 |
return new ImmutablePair<Pair<String, String>, Pair<String, String>>( |
275 |
|
new ImmutablePair<String, String>(in, inExtension), |
276 |
|
new ImmutablePair<String, String>(out, outExtension)); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
@param |
281 |
|
@param |
282 |
|
@return |
283 |
|
@throws |
284 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
285 |
4710 |
private String readData(String resourceName, ClassLoader classLoader) throws IOException... |
286 |
|
{ |
287 |
4710 |
String input = null; |
288 |
|
|
289 |
4710 |
URL inputURL = classLoader.getResource(resourceName); |
290 |
4710 |
if (inputURL != null) { |
291 |
1218 |
input = IOUtils.toString(inputURL); |
292 |
|
} |
293 |
4710 |
return input; |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
@param |
302 |
|
@param |
303 |
|
@param |
304 |
|
|
305 |
|
@return |
306 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
307 |
19 |
public Set<String> findRelativeTestDirectoryNames(String ctsRootPackageName, String packageFilter, String pattern)... |
308 |
|
{ |
309 |
19 |
Reflections reflections = new Reflections(new ConfigurationBuilder() |
310 |
|
.setScanners(new ResourcesScanner()) |
311 |
|
.setUrls(ClasspathHelper.forPackage(ctsRootPackageName)) |
312 |
|
.filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(ctsRootPackageName + DOT + packageFilter)))); |
313 |
|
|
314 |
19 |
Set<String> prefixes = new TreeSet<String>(); |
315 |
19 |
for (String fullTestDirectoryName : reflections.getResources(Pattern.compile(pattern))) { |
316 |
|
|
317 |
631 |
String testDirectoryName = StringUtils.substringAfter(fullTestDirectoryName, ctsRootPackageName + SLASH); |
318 |
631 |
testDirectoryName = StringUtils.substringBeforeLast(testDirectoryName, ".inout.xml"); |
319 |
631 |
prefixes.add(testDirectoryName); |
320 |
|
} |
321 |
|
|
322 |
19 |
return prefixes; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
@param |
330 |
|
@return |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
332 |
203 |
private String computeSyntaxDirectory(String syntaxId)... |
333 |
|
{ |
334 |
|
|
335 |
203 |
return syntaxId.replace(SLASH, "").replace(DOT, ""); |
336 |
|
} |
337 |
|
} |