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.apache.maven.reporting.MavenReportException;
019: import org.codehaus.plexus.util.DirectoryScanner;
020: import org.codehaus.plexus.util.StringUtils;
021: import org.xml.sax.SAXException;
022:
023: import javax.xml.parsers.ParserConfigurationException;
024: import java.io.File;
025: import java.io.IOException;
026: import java.text.NumberFormat;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.ListIterator;
031: import java.util.Locale;
032: import java.util.Map;
033:
034: public class SurefireReportParser {
035: private NumberFormat numberFormat = NumberFormat.getInstance();
036:
037: private File reportsDirectory;
038:
039: private List testSuites = new ArrayList();
040:
041: private Locale locale;
042:
043: private static final int PCENT = 100;
044:
045: public SurefireReportParser() {
046: }
047:
048: public SurefireReportParser(File reportsDirectory, Locale locale) {
049: this .reportsDirectory = reportsDirectory;
050:
051: setLocale(locale);
052: }
053:
054: public List parseXMLReportFiles() throws MavenReportException {
055: if (reportsDirectory.exists()) {
056: String[] xmlReportFiles = getIncludedFiles(
057: reportsDirectory, "*.xml", "*.txt");
058:
059: for (int index = 0; index < xmlReportFiles.length; index++) {
060: ReportTestSuite testSuite = new ReportTestSuite();
061:
062: String currentReport = xmlReportFiles[index];
063:
064: try {
065: testSuite.parse(reportsDirectory + "/"
066: + currentReport);
067: } catch (ParserConfigurationException e) {
068: throw new MavenReportException(
069: "Error setting up parser for JUnit XML report",
070: e);
071: } catch (SAXException e) {
072: throw new MavenReportException(
073: "Error parsing JUnit XML report "
074: + currentReport, e);
075: } catch (IOException e) {
076: throw new MavenReportException(
077: "Error reading JUnit XML report "
078: + currentReport, e);
079: }
080:
081: testSuites.add(testSuite);
082: }
083: }
084:
085: return testSuites;
086: }
087:
088: protected String parseTestSuiteName(String lineString) {
089: return lineString.substring(lineString.lastIndexOf(".") + 1,
090: lineString.length());
091: }
092:
093: protected String parseTestSuitePackageName(String lineString) {
094: return lineString.substring(lineString.indexOf(":") + 2,
095: lineString.lastIndexOf("."));
096: }
097:
098: protected String parseTestCaseName(String lineString) {
099: return lineString.substring(0, lineString.indexOf("("));
100: }
101:
102: public Map getSummary(List suites) {
103: Map totalSummary = new HashMap();
104:
105: ListIterator iter = suites.listIterator();
106:
107: int totalNumberOfTests = 0;
108:
109: int totalNumberOfErrors = 0;
110:
111: int totalNumberOfFailures = 0;
112:
113: int totalNumberOfSkipped = 0;
114:
115: float totalElapsedTime = 0.0f;
116:
117: while (iter.hasNext()) {
118: ReportTestSuite suite = (ReportTestSuite) iter.next();
119:
120: totalNumberOfTests += suite.getNumberOfTests();
121:
122: totalNumberOfErrors += suite.getNumberOfErrors();
123:
124: totalNumberOfFailures += suite.getNumberOfFailures();
125:
126: totalNumberOfSkipped += suite.getNumberOfSkipped();
127:
128: totalElapsedTime += suite.getTimeElapsed();
129: }
130:
131: String totalPercentage = computePercentage(totalNumberOfTests,
132: totalNumberOfErrors, totalNumberOfFailures,
133: totalNumberOfSkipped);
134:
135: totalSummary.put("totalTests", Integer
136: .toString(totalNumberOfTests));
137:
138: totalSummary.put("totalErrors", Integer
139: .toString(totalNumberOfErrors));
140:
141: totalSummary.put("totalFailures", Integer
142: .toString(totalNumberOfFailures));
143:
144: totalSummary.put("totalSkipped", Integer
145: .toString(totalNumberOfSkipped));
146:
147: totalSummary.put("totalElapsedTime", numberFormat
148: .format(totalElapsedTime));
149:
150: totalSummary.put("totalPercentage", totalPercentage);
151:
152: return totalSummary;
153: }
154:
155: public void setReportsDirectory(File reportsDirectory) {
156: this .reportsDirectory = reportsDirectory;
157: }
158:
159: public File getReportsDirectory() {
160: return this .reportsDirectory;
161: }
162:
163: public final void setLocale(Locale locale) {
164: this .locale = locale;
165: numberFormat = NumberFormat.getInstance(locale);
166: }
167:
168: public Locale getLocale() {
169: return this .locale;
170: }
171:
172: public void setNumberFormat(NumberFormat numberFormat) {
173: this .numberFormat = numberFormat;
174: }
175:
176: public NumberFormat getNumberFormat() {
177: return this .numberFormat;
178: }
179:
180: public Map getSuitesGroupByPackage(List testSuitesList) {
181: ListIterator iter = testSuitesList.listIterator();
182:
183: Map suitePackage = new HashMap();
184:
185: while (iter.hasNext()) {
186: ReportTestSuite suite = (ReportTestSuite) iter.next();
187:
188: List suiteList = new ArrayList();
189:
190: if (suitePackage.get(suite.getPackageName()) != null) {
191: suiteList = (List) suitePackage.get(suite
192: .getPackageName());
193: }
194:
195: suiteList.add(suite);
196:
197: suitePackage.put(suite.getPackageName(), suiteList);
198: }
199:
200: return suitePackage;
201: }
202:
203: public String computePercentage(int tests, int errors,
204: int failures, int skipped) {
205: float percentage;
206: if (tests == 0) {
207: percentage = 0;
208: } else {
209: percentage = ((float) (tests - errors - failures - skipped) / (float) tests)
210: * PCENT;
211: }
212:
213: return numberFormat.format(percentage);
214: }
215:
216: public List getFailureDetails(List testSuitesList) {
217: ListIterator iter = testSuitesList.listIterator();
218:
219: List failureDetailList = new ArrayList();
220:
221: while (iter.hasNext()) {
222: ReportTestSuite suite = (ReportTestSuite) iter.next();
223:
224: List testCaseList = suite.getTestCases();
225:
226: if (testCaseList != null) {
227: ListIterator caseIter = testCaseList.listIterator();
228:
229: while (caseIter.hasNext()) {
230: ReportTestCase tCase = (ReportTestCase) caseIter
231: .next();
232:
233: if (tCase.getFailure() != null) {
234: failureDetailList.add(tCase);
235: }
236: }
237: }
238: }
239:
240: return failureDetailList;
241: }
242:
243: private String[] getIncludedFiles(File directory, String includes,
244: String excludes) {
245: DirectoryScanner scanner = new DirectoryScanner();
246:
247: scanner.setBasedir(directory);
248:
249: scanner.setIncludes(StringUtils.split(includes, ","));
250:
251: scanner.setExcludes(StringUtils.split(excludes, ","));
252:
253: scanner.scan();
254:
255: return scanner.getIncludedFiles();
256: }
257: }
|