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

File TestDataParser.java

 

Coverage histogram

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

Code metrics

28
44
3
1
136
78
18
0.41
14.67
3
6

Classes

Class Line # Actions
TestDataParser 47 44 0% 18 4
0.9466666694.7%
 

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.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.InputStreamReader;
26    import java.util.Map;
27    import java.util.StringTokenizer;
28   
29    /**
30    * Parses test data defined using the following syntax, shown with this example:
31    * {@code
32    * .streaming
33    * .runTransformations
34    * .configuration <key=value>
35    * .input|<syntax parser id>
36    * <input content here>
37    * .expect|<renderer id>
38    * <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: 493b1bf65af63d92818b4ea9f4cb4ae905666709 $
45    * @since 3.0RC1
46    */
 
47    public class TestDataParser
48    {
 
49  332 toggle public TestData parse(InputStream source, String resourceName) throws IOException
50    {
51  332 TestData data = new TestData();
52   
53    // Resources should always be encoded as UTF-8, to reduce the dependency on the system encoding
54  332 BufferedReader reader = new BufferedReader(new InputStreamReader(source, "UTF-8"));
55   
56    // Read each line and look for lines starting with ".". When this happens it means we've found a separate
57    // test case.
58  332 try {
59  332 String action = null;
60  332 String keyName = null;
61  332 boolean skip = false;
62  332 StringBuffer buffer = new StringBuffer();
63  332 String line;
64  ? while ((line = reader.readLine()) != null) {
65  12694 if (line.startsWith(".")) {
66  4077 if (line.startsWith(".#")) {
67    // Ignore comments
68  1343 } else if (line.startsWith(".streaming")) {
69  2 data.streaming = true;
70  1341 } else if (line.startsWith(".runTransformations")) {
71  128 data.runTransformations = true;
72  1213 } else if (line.startsWith(".configuration")) {
73  2 StringTokenizer st = new StringTokenizer(line.substring(".configuration".length() + 1), "=");
74  2 data.configuration.put(st.nextToken(), st.nextToken());
75    } else {
76  1211 if (!skip) {
77  1211 saveData(action, buffer, data, keyName);
78    }
79  1211 buffer.setLength(0);
80    // Parse the directive line starting with "." and with "|" separators.
81    // For example ".input|xwiki/2.0|skip" or ".expect|xhtml"
82  1211 StringTokenizer st = new StringTokenizer(line.substring(1), "|");
83    // First token is "input", "expect" or "inputexpect".
84  1211 action = st.nextToken();
85    // Second token is either the input syntax id or the expectation renderer short name
86  1211 keyName = st.nextToken();
87    // Third (optional) token is whether the test should be skipped (useful while waiting for
88    // a fix to wikimodel for example).
89  1211 skip = false;
90  1211 if (st.hasMoreTokens()) {
91  1 skip = true;
92  1 System.out.println("[WARNING] Skipping test for [" + keyName + "] in resource ["
93    + resourceName + "] since it has been marked as skipped in the test. This needs to be "
94    + "reviewed and fixed.");
95    }
96    }
97    } else {
98  8617 buffer.append(line).append('\n');
99    }
100    }
101   
102  332 if (!skip) {
103  331 saveData(action, buffer, data, keyName);
104    }
105   
106    } finally {
107  332 reader.close();
108    }
109   
110  332 return data;
111    }
112   
 
113  1542 toggle private void saveData(String action, StringBuffer buffer, TestData data, String keyName)
114    {
115  1542 if (action != null) {
116  1210 if (action.equalsIgnoreCase("input")) {
117  470 saveBuffer(buffer, data.inputs, keyName);
118  740 } else if (action.equalsIgnoreCase("expect")) {
119  732 saveBuffer(buffer, data.expectations, keyName);
120  8 } else if (action.equalsIgnoreCase("inputexpect")) {
121  8 saveBuffer(buffer, data.inputs, keyName);
122  8 saveBuffer(buffer, data.expectations, keyName);
123    }
124    }
125    }
126   
 
127  1218 toggle private void saveBuffer(StringBuffer buffer, Map<String, String> map, String keyName)
128    {
129    // Remove the last newline since our test format forces an additional new lines
130    // at the end of input texts.
131  1218 if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == '\n') {
132  1206 buffer.setLength(buffer.length() - 1);
133    }
134  1218 map.put(keyName, buffer.toString());
135    }
136    }