1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.test.integration

File TestDataGenerator.java

 

Coverage histogram

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

Code metrics

12
45
5
1
154
93
20
0.44
9
5
4

Classes

Class Line # Actions
TestDataGenerator 40 45 0% 20 3
0.951612995.2%
 

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.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    * Finds all test files in the current classloader, read them and return test data to represent them.
36    *
37    * @version $Id: a77e9303ecc1e25f2b01cd6a473bdf2c954c8633 $
38    * @since 3.0RC1
39    */
 
40    public class TestDataGenerator
41    {
42    private TestDataParser parser = new TestDataParser();
43   
 
44  31 toggle 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    * Parse a single test data file and return test data objects that represent the file data.
61    *
62    * @param testResourceName the name of the resource file containing the test data in the current classloader
63    * @return the in-memory Objects representing the test data
64    */
 
65  332 toggle 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    // In order to improve test performance we exclude unneeded tests. A test is not required when the
98    // following conditions are met:
99    // - the target syntax is not event/1.0
100    // - there's already another parser with an output of event/1.0
101    // The reason these tests are not needed is because rendering is done from the XDOM and the event/1.0
102    // syntax is an exact representation of the XDOM object and thus we only need to check once the
103    // expected output (except for event/1.0).
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   
 
135  518 toggle 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   
 
141  6 toggle 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   
 
148  887 toggle private String computeTestName(String prefix, String parserId, String targetSyntaxId)
149    {
150    // Note: For some reason the Eclipse JUnit test runner strips the information found in parenthesis. Thus we use
151    // square brackets instead.
152  887 return prefix + " [" + parserId + ", " + targetSyntaxId + "]";
153    }
154    }