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.integration; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.Map; |
26 |
|
import java.util.regex.Pattern; |
27 |
|
|
28 |
|
import org.reflections.Reflections; |
29 |
|
import org.reflections.scanners.ResourcesScanner; |
30 |
|
import org.reflections.util.ClasspathHelper; |
31 |
|
import org.reflections.util.ConfigurationBuilder; |
32 |
|
import org.reflections.util.FilterBuilder; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
|
|
| 95.2% |
Uncovered Elements: 3 (62) |
Complexity: 20 |
Complexity Density: 0.44 |
|
40 |
|
public class TestDataGenerator |
41 |
|
{ |
42 |
|
private TestDataParser parser = new TestDataParser(); |
43 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
44 |
31 |
public Collection<Object[]> generateData(String testPackage, String pattern)... |
45 |
|
{ |
46 |
31 |
Reflections reflections = |
47 |
|
new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()) |
48 |
|
.setUrls(ClasspathHelper.forPackage("")) |
49 |
|
.filterInputsBy(new FilterBuilder.Include(Pattern.quote(testPackage) + ".*"))); |
50 |
|
|
51 |
31 |
Collection<Object[]> data = new ArrayList<Object[]>(); |
52 |
31 |
for (String testResourceName : reflections.getResources(Pattern.compile(pattern))) { |
53 |
332 |
data.addAll(parseSingleResource(testResourceName)); |
54 |
|
} |
55 |
|
|
56 |
31 |
return data; |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@return |
64 |
|
|
|
|
| 93.9% |
Uncovered Elements: 3 (49) |
Complexity: 16 |
Complexity Density: 0.43 |
|
65 |
332 |
private Collection<Object[]> parseSingleResource(String testResourceName)... |
66 |
|
{ |
67 |
332 |
String resourceName = "/" + testResourceName; |
68 |
332 |
TestData data; |
69 |
332 |
try { |
70 |
332 |
InputStream source = getClass().getResourceAsStream(resourceName); |
71 |
332 |
if (source == null) { |
72 |
0 |
throw new RuntimeException("Failed to find test file [" + resourceName + "]"); |
73 |
|
} |
74 |
332 |
data = this.parser.parse(source, resourceName); |
75 |
|
} catch (Exception e) { |
76 |
0 |
throw new RuntimeException("Failed to read test data from [" + resourceName + "]", e); |
77 |
|
} |
78 |
|
|
79 |
332 |
boolean hasEventOutput = false; |
80 |
332 |
int inputCounter = 0; |
81 |
|
|
82 |
332 |
Collection<Object[]> result = new ArrayList<Object[]>(); |
83 |
332 |
for (Map.Entry<String, String> entry : data.inputs.entrySet()) { |
84 |
478 |
inputCounter++; |
85 |
478 |
for (String targetSyntaxId : data.expectations.keySet()) { |
86 |
|
|
87 |
1225 |
String parserId = entry.getKey(); |
88 |
1225 |
String input = entry.getValue(); |
89 |
|
|
90 |
1225 |
if ("xhtml/1.0".equals(parserId) && !input.startsWith("<?xml") && !input.startsWith("<!DOCTYPE")) { |
91 |
518 |
input = normalizeHTML(input); |
92 |
707 |
} else if ("docbook/4.4".equals(parserId) && !input.startsWith("<?xml") |
93 |
|
&& !input.startsWith("<!DOCTYPE")) { |
94 |
5 |
input = normalizeDocBook(input); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
1225 |
if (inputCounter < 2 || !hasEventOutput || targetSyntaxId.equals("event/1.0")) { |
105 |
|
|
106 |
887 |
Object[] singleResult = new Object[8]; |
107 |
887 |
singleResult[0] = computeTestName(testResourceName, parserId, targetSyntaxId); |
108 |
887 |
singleResult[1] = input; |
109 |
|
|
110 |
887 |
String expected = data.expectations.get(targetSyntaxId); |
111 |
887 |
if ("docbook/4.4".equals(targetSyntaxId) && !expected.startsWith("<?xml") |
112 |
|
&& !expected.startsWith("<!DOCTYPE")) { |
113 |
1 |
expected = normalizeDocBook(expected); |
114 |
|
} |
115 |
|
|
116 |
887 |
singleResult[2] = expected; |
117 |
887 |
singleResult[3] = parserId; |
118 |
887 |
singleResult[4] = targetSyntaxId; |
119 |
887 |
singleResult[5] = data.streaming; |
120 |
887 |
singleResult[6] = data.runTransformations; |
121 |
887 |
singleResult[7] = data.configuration; |
122 |
|
|
123 |
887 |
result.add(singleResult); |
124 |
|
|
125 |
887 |
if (targetSyntaxId.equals("event/1.0")) { |
126 |
447 |
hasEventOutput = true; |
127 |
|
} |
128 |
|
} |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
332 |
return result; |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
518 |
private String normalizeHTML(String content)... |
136 |
|
{ |
137 |
518 |
return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " |
138 |
|
+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" + "<html>" + content + "</html>"; |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
6 |
private String normalizeDocBook(String content)... |
142 |
|
{ |
143 |
6 |
return "<?xml version=\"1.0\"?>" |
144 |
|
+ "<!DOCTYPE article PUBLIC \"-//OASIS//DTD Simplified DocBook XML V1.1//EN\" " |
145 |
|
+ "\"http://www.oasis-open.org/docbook/xml/simple/1.1/sdocbook.dtd\">" + content; |
146 |
|
} |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
148 |
887 |
private String computeTestName(String prefix, String parserId, String targetSyntaxId)... |
149 |
|
{ |
150 |
|
|
151 |
|
|
152 |
887 |
return prefix + " [" + parserId + ", " + targetSyntaxId + "]"; |
153 |
|
} |
154 |
|
} |