1 // Copyright (c) 2008 Fabien DUMINY (fduminy@jnode.org) |
|
2 // Modified by Levente S\u00e1ntha (lsantha@jnode.org) |
|
3 |
|
4 // This file is part of Mauve. |
|
5 |
|
6 // Mauve is free software; you can redistribute it and/or modify |
|
7 // it under the terms of the GNU General Public License as published by |
|
8 // the Free Software Foundation; either version 2, or (at your option) |
|
9 // any later version. |
|
10 |
|
11 // Mauve is distributed in the hope that it will be useful, |
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 // GNU General Public License for more details. |
|
15 |
|
16 // You should have received a copy of the GNU General Public License |
|
17 // along with Mauve; see the file COPYING. If not, write to |
|
18 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
19 // Boston, MA 02111-1307, USA. */ |
|
20 |
|
21 package gnu.testlet.runner; |
|
22 |
|
23 import java.io.BufferedReader; |
|
24 import java.io.File; |
|
25 import java.io.FileReader; |
|
26 import java.io.IOException; |
|
27 import java.io.Reader; |
|
28 import java.util.Enumeration; |
|
29 |
|
30 import net.sourceforge.nanoxml.XMLElement; |
|
31 import net.sourceforge.nanoxml.XMLParseException; |
|
32 |
|
33 /** |
|
34 * XML parser for mauve reports. |
|
35 * |
|
36 * @author fabien |
|
37 * |
|
38 */ |
|
39 public class XMLReportParser implements XMLReportConstants { |
|
40 /** |
|
41 * Parse the given file and return the corresponding mauve result. |
|
42 * |
|
43 * @param input |
|
44 * @return |
|
45 * @throws XMLParseException |
|
46 * @throws IOException |
|
47 */ |
|
48 public RunResult parse(File input) throws XMLParseException, IOException { |
|
49 return parse(new FileReader(input)); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Parse the given reader and return the corresponding mauve result. |
|
54 * |
|
55 * @param r |
|
56 * @return |
|
57 * @throws XMLParseException |
|
58 * @throws IOException |
|
59 */ |
|
60 public RunResult parse(Reader r) throws XMLParseException, IOException { |
|
61 XMLElement xmlRun = new XMLElement(); |
|
62 BufferedReader reader = null; |
|
63 |
|
64 try { |
|
65 reader = new BufferedReader(r); |
|
66 xmlRun.parseFromReader(reader); |
|
67 |
|
68 checkTag(xmlRun, RUN_RESULT); |
|
69 |
|
70 String attr; |
|
71 RunResult run = new RunResult(getValue(xmlRun, RUN_NAME, "")); |
|
72 |
|
73 for (Enumeration enumPkg = xmlRun.enumerateChildren(); enumPkg.hasMoreElements(); ) { |
|
74 XMLElement xmlPkg = (XMLElement) enumPkg.nextElement(); |
|
75 int indexTag = checkTag(xmlPkg, new String[]{PACKAGE_RESULT, PROPERTY}); |
|
76 |
|
77 if (indexTag == 1) { |
|
78 String name = getValue(xmlPkg, PROPERTY_NAME, ""); |
|
79 String value = getValue(xmlPkg, PROPERTY_VALUE, ""); |
|
80 run.setSystemProperty(name, value); |
|
81 continue; |
|
82 } |
|
83 |
|
84 attr = getValue(xmlPkg, PACKAGE_NAME, ""); |
|
85 PackageResult pkg = new PackageResult(attr); |
|
86 run.add(pkg); |
|
87 |
|
88 for (Enumeration enumCls = xmlPkg.enumerateChildren(); enumCls.hasMoreElements(); ) { |
|
89 XMLElement xmlCls = (XMLElement) enumCls.nextElement(); |
|
90 checkTag(xmlCls, CLASS_RESULT); |
|
91 |
|
92 attr = getValue(xmlCls, CLASS_NAME, ""); |
|
93 ClassResult cls = new ClassResult(attr); |
|
94 pkg.add(cls); |
|
95 |
|
96 for (Enumeration enumTest = xmlCls.enumerateChildren(); enumTest.hasMoreElements(); ) { |
|
97 XMLElement xmlTest = (XMLElement) enumTest.nextElement(); |
|
98 checkTag(xmlTest, TEST_RESULT); |
|
99 |
|
100 attr = getValue(xmlTest, TEST_NAME, ""); |
|
101 TestResult test = new TestResult(attr); |
|
102 cls.add(test); |
|
103 |
|
104 for (Enumeration enumCheck = xmlTest.enumerateChildren(); enumCheck.hasMoreElements(); ) { |
|
105 XMLElement xmlCheck = (XMLElement) enumCheck.nextElement(); |
|
106 if (TEST_ERROR.equals(xmlCheck.getName())) { |
|
107 test.setFailedMessage(xmlCheck.getContent()); |
|
108 } else { |
|
109 checkTag(xmlCheck, CHECK_RESULT); |
|
110 |
|
111 test.add(createCheck(xmlCheck)); |
|
112 } |
|
113 } |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 return run; |
|
119 } finally { |
|
120 if (reader != null) { |
|
121 reader.close(); |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 private CheckResult createCheck(XMLElement xmlCheck) { |
|
127 String attr = getValue(xmlCheck, CHECK_NUMBER, "-1"); |
|
128 int number = Integer.valueOf(attr).intValue(); |
|
129 |
|
130 attr = getValue(xmlCheck, CHECK_PASSED, "false"); |
|
131 boolean passed = Boolean.valueOf(attr).booleanValue(); |
|
132 |
|
133 CheckResult check = new CheckResult(number, passed); |
|
134 |
|
135 attr = getValue(xmlCheck, CHECK_POINT, ""); |
|
136 check.setCheckPoint(attr); |
|
137 |
|
138 attr = getValue(xmlCheck, CHECK_EXPECTED, ""); |
|
139 check.setExpected(attr); |
|
140 |
|
141 attr = getValue(xmlCheck, CHECK_ACTUAL, ""); |
|
142 check.setActual(attr); |
|
143 |
|
144 // get the log if any |
|
145 if (xmlCheck.countChildren() > 0) { |
|
146 XMLElement firstChild = (XMLElement) xmlCheck.enumerateChildren().nextElement(); |
|
147 checkTag(firstChild, CHECK_LOG); |
|
148 |
|
149 String content = firstChild.getContent(); |
|
150 if (content != null) { |
|
151 check.appendToLog(attr); |
|
152 } |
|
153 } |
|
154 |
|
155 return check; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Get the value of an xml element's attribute and return the given default |
|
160 * value if the attribute is not defined. |
|
161 * @param xml The xml element for which we want an attribute. |
|
162 * @param attributeName The name of the attribute. |
|
163 * @param defaultValue The default value to return if the attribute is not defined. |
|
164 * @return The value of the xml element's attribute or, if not defined, the given |
|
165 * <code>defaultValue</code> parameter. |
|
166 */ |
|
167 private String getValue(XMLElement xml, String attributeName, String defaultValue) { |
|
168 Object attr = xml.getAttribute(attributeName); |
|
169 return (attr == null) ? defaultValue : String.valueOf(attr); |
|
170 } |
|
171 |
|
172 /** |
|
173 * Checks that the xml element represents the given tag. |
|
174 * @param xml The xml element to check. |
|
175 * @param tag The tag which is expected. |
|
176 * @throws XMLParseException if the xml element doesn't represent the given tag. |
|
177 */ |
|
178 private void checkTag(XMLElement xml, String tag) { |
|
179 final String actualTag = xml.getName(); |
|
180 if (!tag.equals(actualTag)) { |
|
181 throw new XMLParseException("", "tag is not '" + tag + "' (actual: '" + actualTag + "')"); |
|
182 } |
|
183 } |
|
184 |
|
185 /** |
|
186 * Checks that the xml element represents one of the given tags. |
|
187 * @param xml The xml element to check. |
|
188 * @param tag The tags which are expected. |
|
189 * @throws XMLParseException if the xml element doesn't represent one of the given tags. |
|
190 */ |
|
191 private int checkTag(XMLElement xml, String[] tags) { |
|
192 final String actualTag = xml.getName(); |
|
193 int indexTag = -1; |
|
194 for (int i = 0; i < tags.length; i++) { |
|
195 if (tags[i].equals(actualTag)) { |
|
196 indexTag = i; |
|
197 break; |
|
198 } |
|
199 } |
|
200 |
|
201 if (indexTag < 0) { |
|
202 StringBuffer sb = new StringBuffer('('); |
|
203 for (int i = 0; i < tags.length; i++) { |
|
204 if (i > 0) { |
|
205 sb.append(','); |
|
206 } |
|
207 sb.append(tags[i]); |
|
208 } |
|
209 sb.append(')'); |
|
210 throw new XMLParseException("", "tag is not one of " + sb.toString() + " (actual: '" + actualTag + "')"); |
|
211 } |
|
212 |
|
213 return indexTag; |
|
214 } |
|
215 } |
|