001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.mavenplugins.testsuite.report;
017:
018: import org.xml.sax.Attributes;
019: import org.xml.sax.SAXException;
020: import org.xml.sax.helpers.DefaultHandler;
021:
022: import javax.xml.parsers.ParserConfigurationException;
023: import javax.xml.parsers.SAXParser;
024: import javax.xml.parsers.SAXParserFactory;
025: import java.io.File;
026: import java.io.IOException;
027: import java.text.NumberFormat;
028: import java.text.ParseException;
029: import java.util.ArrayList;
030: import java.util.Collections;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.StringTokenizer;
034:
035: public class ReportTestSuite extends DefaultHandler {
036: private List testCases;
037:
038: private int numberOfErrors;
039:
040: private int numberOfFailures;
041:
042: private int numberOfSkipped;
043:
044: private int numberOfTests;
045:
046: private String name;
047:
048: private String fullClassName;
049:
050: private String packageName;
051:
052: private float timeElapsed;
053:
054: private NumberFormat numberFormat = NumberFormat.getInstance();
055:
056: /**
057: * @noinspection StringBufferField
058: */
059: private StringBuffer currentElement;
060:
061: private ReportTestCase testCase;
062:
063: public void parse(String xmlPath)
064: throws ParserConfigurationException, SAXException,
065: IOException {
066: SAXParserFactory factory = SAXParserFactory.newInstance();
067:
068: SAXParser saxParser = factory.newSAXParser();
069:
070: saxParser.parse(new File(xmlPath), this );
071: }
072:
073: private int getAttributeAsInt(Attributes attributes, String name) {
074: // may or may not exist
075: String valueAsString = attributes.getValue(name);
076: if (valueAsString != null) {
077: return Integer.parseInt(valueAsString);
078: }
079: return 0;
080: }
081:
082: public void startElement(String uri, String localName,
083: String qName, Attributes attributes) throws SAXException {
084: try {
085: if ("testsuite".equals(qName)) {
086: numberOfErrors = getAttributeAsInt(attributes, "errors");
087: numberOfFailures = getAttributeAsInt(attributes,
088: "failures");
089: numberOfSkipped = getAttributeAsInt(attributes,
090: "skipped");
091: numberOfTests = getAttributeAsInt(attributes, "tests");
092:
093: Number time = numberFormat.parse(attributes
094: .getValue("time"));
095:
096: timeElapsed = time.floatValue();
097:
098: //check if group attribute is existing
099: if (attributes.getValue("group") != null
100: && !"".equals(attributes.getValue("group"))) {
101: packageName = attributes.getValue("group");
102:
103: name = attributes.getValue("name");
104:
105: fullClassName = packageName + "." + name;
106: } else {
107: fullClassName = attributes.getValue("name");
108:
109: name = fullClassName.substring(fullClassName
110: .lastIndexOf(".") + 1, fullClassName
111: .length());
112:
113: int lastDotPosition = fullClassName
114: .lastIndexOf(".");
115: if (lastDotPosition < 0) {
116: /* no package name */
117: packageName = "";
118: } else {
119: packageName = fullClassName.substring(0,
120: lastDotPosition);
121: }
122: }
123:
124: testCases = new ArrayList();
125: } else if ("testcase".equals(qName)) {
126: currentElement = new StringBuffer();
127:
128: testCase = new ReportTestCase();
129:
130: testCase.setFullClassName(fullClassName);
131:
132: testCase.setName(attributes.getValue("name"));
133:
134: testCase.setClassName(name);
135:
136: String timeAsString = attributes.getValue("time");
137:
138: Number time = new Integer(0);
139:
140: if (timeAsString != null) {
141: time = numberFormat.parse(timeAsString);
142: }
143:
144: testCase.setTime(time.floatValue());
145:
146: testCase.setFullName(packageName + "." + name + "."
147: + testCase.getName());
148: } else if ("failure".equals(qName)) {
149: testCase.addFailure(attributes.getValue("message"),
150: attributes.getValue("type"));
151: } else if ("error".equals(qName)) {
152: testCase.addFailure(attributes.getValue("message"),
153: attributes.getValue("type"));
154: }
155: } catch (ParseException e) {
156: throw new SAXException(e.getMessage(), e);
157: }
158: }
159:
160: public void endElement(String uri, String localName, String qName)
161: throws SAXException {
162: if ("testcase".equals(qName)) {
163: testCases.add(testCase);
164: } else if ("failure".equals(qName)) {
165: Map failure = testCase.getFailure();
166:
167: failure
168: .put("detail",
169: parseCause(currentElement.toString()));
170: } else if ("error".equals(qName)) {
171: Map error = testCase.getFailure();
172:
173: error.put("detail", parseCause(currentElement.toString()));
174: }
175: }
176:
177: public void characters(char[] ch, int start, int length)
178: throws SAXException {
179: String s = new String(ch, start, length);
180:
181: if (!"".equals(s.trim())) {
182: currentElement.append(s);
183: }
184: }
185:
186: public List getTestCases() {
187: return this .testCases;
188: }
189:
190: public int getNumberOfErrors() {
191: return numberOfErrors;
192: }
193:
194: public void setNumberOfErrors(int numberOfErrors) {
195: this .numberOfErrors = numberOfErrors;
196: }
197:
198: public int getNumberOfFailures() {
199: return numberOfFailures;
200: }
201:
202: public void setNumberOfFailures(int numberOfFailures) {
203: this .numberOfFailures = numberOfFailures;
204: }
205:
206: public int getNumberOfSkipped() {
207: return numberOfSkipped;
208: }
209:
210: public void setNumberOfSkipped(int numberOfSkipped) {
211: this .numberOfSkipped = numberOfSkipped;
212: }
213:
214: public int getNumberOfTests() {
215: return numberOfTests;
216: }
217:
218: public void setNumberOfTests(int numberOfTests) {
219: this .numberOfTests = numberOfTests;
220: }
221:
222: public String getName() {
223: return name;
224: }
225:
226: public void setName(String name) {
227: this .name = name;
228: }
229:
230: public String getFName() {
231: return name;
232: }
233:
234: public void setFName(String name) {
235: this .name = name;
236: }
237:
238: public String getPackageName() {
239: return packageName;
240: }
241:
242: public void setPackageName(String packageName) {
243: this .packageName = packageName;
244: }
245:
246: public float getTimeElapsed() {
247: return this .timeElapsed;
248: }
249:
250: public void setTimeElapsed(float timeElapsed) {
251: this .timeElapsed = timeElapsed;
252: }
253:
254: private List parseCause(String detail) {
255: String fullName = testCase.getFullName();
256: String name = fullName.substring(fullName.lastIndexOf(".") + 1);
257: return parseCause(detail, name);
258: }
259:
260: private List parseCause(String detail, String compareTo) {
261: StringTokenizer stringTokenizer = new StringTokenizer(detail,
262: "\n");
263: List parsedDetail = new ArrayList(stringTokenizer.countTokens());
264:
265: while (stringTokenizer.hasMoreTokens()) {
266: String lineString = stringTokenizer.nextToken().trim();
267: parsedDetail.add(lineString);
268: if (lineString.indexOf(compareTo) >= 0) {
269: break;
270: }
271: }
272:
273: return parsedDetail;
274: }
275:
276: public void setTestCases(List testCases) {
277: this.testCases = Collections.unmodifiableList(testCases);
278: }
279: }
|