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

File TestDataGenerator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
25
3
1
105
58
5
0.2
8.33
3
1.67

Classes

Class Line # Actions
TestDataGenerator 39 25 0% 5 3
0.990%
 

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.filter.test.integration;
21   
22    import java.io.InputStream;
23    import java.util.ArrayList;
24    import java.util.Collection;
25    import java.util.regex.Pattern;
26   
27    import org.reflections.Reflections;
28    import org.reflections.scanners.ResourcesScanner;
29    import org.reflections.util.ClasspathHelper;
30    import org.reflections.util.ConfigurationBuilder;
31    import org.reflections.util.FilterBuilder;
32   
33    /**
34    * Finds all test files in the current classloader, read them and return test data to represent them.
35    *
36    * @version $Id: 1cfa32e64df1b55f6d7bc9d46343b939f7eeee97 $
37    * @since 6.2M1
38    */
 
39    public class TestDataGenerator
40    {
41    private TestDataParser parser = new TestDataParser();
42   
 
43  3 toggle public Collection<TestConfiguration> generateData(String testPackage, String pattern)
44    {
45  3 Reflections reflections =
46    new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner())
47    .setUrls(ClasspathHelper.forPackage(""))
48    .filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(testPackage))));
49   
50  3 Collection<TestConfiguration> data = new ArrayList<TestConfiguration>();
51  3 for (String testResourceName : reflections.getResources(Pattern.compile(pattern))) {
52  15 data.addAll(parseSingleResource(testResourceName));
53    }
54   
55  3 return data;
56    }
57   
 
58  15 toggle private Collection<TestConfiguration> parseSingleResource(String testResourceName)
59    {
60  15 String resourceName = "/" + testResourceName;
61  15 TestResourceData data;
62  15 try {
63  15 InputStream source = getClass().getResourceAsStream(resourceName);
64  15 if (source == null) {
65  0 throw new RuntimeException("Failed to find test file [" + resourceName + "]");
66    }
67  15 data = this.parser.parse(source, resourceName);
68    } catch (Exception e) {
69  0 throw new RuntimeException("Failed to read test data from [" + resourceName + "]", e);
70    }
71   
72  15 Collection<TestConfiguration> result = new ArrayList<TestConfiguration>();
73  15 for (InputTestConfiguration inputConfiguration : data.inputs) {
74  22 for (ExpectTestConfiguration expectConfiguration : data.expects) {
75  32 TestConfiguration testConfiguration = new TestConfiguration();
76   
77  32 testConfiguration.resourceName = data.resourceName;
78   
79  32 testConfiguration.configuration = data.configuration;
80   
81    // Input
82  32 testConfiguration.inputConfiguration = new InputTestConfiguration(inputConfiguration);
83   
84    // Expect
85  32 testConfiguration.expectConfiguration = new ExpectTestConfiguration(expectConfiguration);
86   
87    // Set test name
88  32 testConfiguration.name =
89    computeTestName(testResourceName, inputConfiguration.typeId, expectConfiguration.typeId);
90   
91    // Add the test to the list
92  32 result.add(testConfiguration);
93    }
94    }
95   
96  15 return result;
97    }
98   
 
99  32 toggle private String computeTestName(String prefix, String parserId, String targetSyntaxId)
100    {
101    // Note: For some reason the Eclipse JUnit test runner strips the information found in parenthesis. Thus we use
102    // square brackets instead.
103  32 return prefix + " [" + parserId + ", " + targetSyntaxId + "]";
104    }
105    }