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

File TestDataParser.java

 

Coverage histogram

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

Code metrics

34
67
5
1
197
118
23
0.34
13.4
5
4.6

Classes

Class Line # Actions
TestDataParser 47 67 0% 23 9
0.915094391.5%
 

Contributing tests

This file is covered by 33 tests. .

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.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.InputStreamReader;
26    import java.util.HashMap;
27    import java.util.Map;
28    import java.util.StringTokenizer;
29    import java.util.regex.Matcher;
30    import java.util.regex.Pattern;
31   
32    /**
33    * Parses test data defined using the following syntax, shown with this example: {@code
34    * .configuration <key=value>
35    * .input|<type>
36    * <optional input content here>
37    * .expect|<type>
38    * <optional expected content here>
39    * }
40    * <p>
41    * Note that there can be several {@code .input} and {@code .expect} entries. For each {@code .input} definition, all
42    * the found {@code .expect} will be executed and checked.
43    *
44    * @version $Id: cf7fd3e7c4a31c86562457e22b795c73e42b0697 $
45    * @since 6.2M1
46    */
 
47    public class TestDataParser
48    {
49    private static final Pattern PATTERN_VARIABLE = Pattern.compile("(\\\\)?(\\$\\{\\{\\{(.*)\\}\\}\\})");
50   
 
51  72 toggle public static String interpret(String source)
52    {
53  72 StringBuilder result = new StringBuilder();
54   
55  72 Matcher matcher = PATTERN_VARIABLE.matcher(source);
56   
57  72 int current = 0;
58  76 while (matcher.find()) {
59  4 if (matcher.group(1) == null) {
60  3 String var = matcher.group(3);
61   
62  3 String value = System.getProperty(var);
63  3 if (value != null) {
64  3 result.append(source, current, matcher.start());
65  3 result.append(value);
66  3 current = matcher.end();
67    }
68    } else {
69  1 result.append(source, current, matcher.start());
70  1 current = matcher.start(2);
71    }
72    }
73   
74  72 if (current < source.length()) {
75  71 result.append(source, current, source.length());
76    }
77   
78  72 return result.toString();
79    }
80   
 
81  15 toggle public TestResourceData parse(InputStream source, String resourceName) throws IOException
82    {
83  15 TestResourceData data = new TestResourceData();
84   
85    // Resources should always be encoded as UTF-8, to reduce the dependency on the system encoding
86  15 BufferedReader reader = new BufferedReader(new InputStreamReader(source, "UTF-8"));
87   
88    // Read each line and look for lines starting with ".". When this happens it means we've found a separate
89    // test case.
90  15 try {
91  15 String action = null;
92  15 String typeId = null;
93  15 boolean skip = false;
94   
95  15 data.resourceName = resourceName;
96   
97  15 StringBuilder buffer = new StringBuilder();
98  15 Map<String, String> configuration = data.configuration;
99   
100  48602 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
101  48587 if (line.startsWith(".")) {
102  105 if (line.startsWith(".#")) {
103    // Ignore comments and print it to the stdout if it's a todo.
104  71 if (line.toLowerCase().contains("todo")) {
105  0 System.out.println(line);
106    }
107  34 } else if (line.startsWith(".configuration")) {
108  4 StringTokenizer st = new StringTokenizer(line.substring(".configuration".length() + 1), "=");
109  4 configuration.put(st.nextToken(), st.nextToken());
110    } else {
111  30 if (!skip) {
112  30 saveData(data, action, typeId, buffer, configuration);
113    }
114   
115    // Reset buffer
116  30 buffer.setLength(0);
117    // Reset configuration
118  30 configuration = new HashMap<String, String>();
119   
120    // Parse the directive line starting with "." and with "|" separators.
121    // For example ".input|xwiki/2.0|skip" or ".expect|xhtml"
122  30 StringTokenizer st = new StringTokenizer(line.substring(1), "|");
123    // First token is "input", "expect" or "inputexpect".
124  30 action = st.nextToken();
125    // Second token is either the input syntax id or the expectation renderer short name
126  30 typeId = st.nextToken();
127    // Third (optional) token is whether the test should be skipped (useful while waiting for
128    // a fix to wikimodel for example).
129  30 skip = false;
130  30 if (st.hasMoreTokens()) {
131  0 skip = true;
132  0 System.out.println("[WARNING] Skipping test for [" + typeId + "] in resource ["
133    + resourceName + "] since it has been marked as skipped in the test. This needs to be "
134    + "reviewed and fixed.");
135    }
136    }
137    } else {
138  48482 buffer.append(line).append('\n');
139    }
140    }
141   
142  15 if (!skip) {
143  15 saveData(data, action, typeId, buffer, configuration);
144    }
145   
146    } finally {
147  15 reader.close();
148    }
149   
150  15 return data;
151    }
152   
 
153  45 toggle private void saveData(TestResourceData data, String action, String typeId, StringBuilder buffer,
154    Map<String, String> configuration)
155    {
156  45 if (action != null) {
157    // Remove the last newline since our test format forces an additional new lines
158    // at the end of input texts.
159  30 if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == '\n') {
160  26 buffer.setLength(buffer.length() - 1);
161    }
162   
163  30 if (action.equalsIgnoreCase("input")) {
164  10 addInput(data, typeId, buffer, configuration);
165  20 } else if (action.equalsIgnoreCase("expect")) {
166  8 addExpect(data, typeId, buffer, configuration);
167  12 } else if (action.equalsIgnoreCase("inputexpect")) {
168  12 addExpect(data, typeId, buffer, configuration);
169  12 addInput(data, typeId, buffer, configuration);
170    }
171    }
172    }
173   
 
174  22 toggle private void addInput(TestResourceData data, String typeId, StringBuilder buffer, Map<String, String> configuration)
175    {
176  22 InputTestConfiguration inputConfiguration = new InputTestConfiguration(typeId, buffer.toString());
177   
178    // Default properties
179  22 inputConfiguration.setEncoding("UTF-8");
180   
181  22 inputConfiguration.putAll(configuration);
182   
183  22 data.inputs.add(inputConfiguration);
184    }
185   
 
186  20 toggle private void addExpect(TestResourceData data, String typeId, StringBuilder buffer, Map<String, String> configuration)
187    {
188  20 ExpectTestConfiguration expectTestConfiguration = new ExpectTestConfiguration(typeId, buffer.toString());
189   
190    // Default properties
191  20 expectTestConfiguration.setEncoding("UTF-8");
192   
193  20 expectTestConfiguration.putAll(configuration);
194   
195  20 data.expects.add(expectTestConfiguration);
196    }
197    }