View Javadoc

1   package org.apache.maven.continuum.reports.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.xml.sax.Attributes;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.helpers.DefaultHandler;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.text.NumberFormat;
28  import java.text.ParseException;
29  import java.util.Collections;
30  import java.util.LinkedList;
31  import java.util.List;
32  import javax.xml.parsers.ParserConfigurationException;
33  import javax.xml.parsers.SAXParser;
34  import javax.xml.parsers.SAXParserFactory;
35  
36  /**
37   * @author <a href="mailto:olamy@apache.org">olamy</a>
38   * @version $Id: ReportTestSuite.java 1372260 2012-08-13 04:29:09Z brett $
39   * @since 12 nov. 07
40   */
41  public class ReportTestSuite
42      extends DefaultHandler
43  {
44      private List<ReportTestCase> testCases;
45  
46      private int numberOfErrors;
47  
48      private int numberOfFailures;
49  
50      private int numberOfTests;
51  
52      private String name;
53  
54      private String fullClassName;
55  
56      private String packageName;
57  
58      private float timeElapsed;
59  
60      private final NumberFormat numberFormat = NumberFormat.getInstance();
61  
62      /**
63       * @noinspection StringBufferField
64       */
65      private StringBuffer currentElement;
66  
67      private ReportTestCase testCase;
68  
69      private List<ReportFailure> reportFailures;
70  
71  
72      public void parse( String xmlPath )
73          throws ParserConfigurationException, SAXException, IOException
74      {
75          SAXParserFactory factory = SAXParserFactory.newInstance();
76  
77          SAXParser saxParser = factory.newSAXParser();
78  
79          saxParser.parse( new File( xmlPath ), this );
80      }
81  
82      public void startElement( String uri, String localName, String qName, Attributes attributes )
83          throws SAXException
84      {
85          try
86          {
87              if ( "testsuite".equals( qName ) )
88              {
89                  numberOfErrors = Integer.parseInt( attributes.getValue( "errors" ) );
90  
91                  numberOfFailures = Integer.parseInt( attributes.getValue( "failures" ) );
92  
93                  numberOfTests = Integer.parseInt( attributes.getValue( "tests" ) );
94  
95                  Number time = numberFormat.parse( attributes.getValue( "time" ) );
96  
97                  timeElapsed = time.floatValue();
98  
99                  //check if group attribute is existing
100                 if ( attributes.getValue( "group" ) != null && !"".equals( attributes.getValue( "group" ) ) )
101                 {
102                     packageName = attributes.getValue( "group" );
103 
104                     name = attributes.getValue( "name" );
105 
106                     fullClassName = packageName + "." + name;
107                 }
108                 else
109                 {
110                     fullClassName = attributes.getValue( "name" );
111 
112                     name = fullClassName.substring( fullClassName.lastIndexOf( "." ) + 1, fullClassName.length() );
113 
114                     int lastDotPosition = fullClassName.lastIndexOf( "." );
115                     if ( lastDotPosition < 0 )
116                     {
117                         /* no package name */
118                         packageName = "";
119                     }
120                     else
121                     {
122                         packageName = fullClassName.substring( 0, lastDotPosition );
123                     }
124                 }
125 
126                 testCases = new LinkedList<ReportTestCase>();
127             }
128             else if ( "testcase".equals( qName ) )
129             {
130                 currentElement = new StringBuffer();
131 
132                 testCase = new ReportTestCase();
133 
134                 testCase.setFullClassName( fullClassName );
135 
136                 testCase.setName( attributes.getValue( "name" ) );
137 
138                 testCase.setClassName( name );
139 
140                 String timeAsString = attributes.getValue( "time" );
141 
142                 Number time = 0;
143 
144                 if ( timeAsString != null )
145                 {
146                     time = numberFormat.parse( timeAsString );
147                 }
148 
149                 testCase.setTime( time.floatValue() );
150 
151                 testCase.setFullName( packageName + "." + name + "." + testCase.getName() );
152             }
153             else if ( "failure".equals( qName ) )
154             {
155                 testCase.setFailureType( attributes.getValue( "type" ) );
156                 testCase.setFailureMessage( attributes.getValue( "message" ) );
157             }
158             else if ( "error".equals( qName ) )
159             {
160                 testCase.setFailureType( attributes.getValue( "type" ) );
161                 testCase.setFailureMessage( attributes.getValue( "message" ) );
162             }
163         }
164         catch ( ParseException e )
165         {
166             throw new SAXException( e.getMessage(), e );
167         }
168     }
169 
170     public void endElement( String uri, String localName, String qName )
171         throws SAXException
172     {
173         if ( "testcase".equals( qName ) )
174         {
175             testCases.add( testCase );
176         }
177         else if ( "failure".equals( qName ) )
178         {
179             testCase.setFailureDetails( currentElement.toString() );
180             this.addReportFailure( new ReportFailure( testCase.getFailureType(), testCase.getFailureDetails(),
181                                                       testCase.getName() ) );
182         }
183         else if ( "error".equals( qName ) )
184         {
185             testCase.setFailureDetails( currentElement.toString() );
186             this.addReportFailure( new ReportFailure( testCase.getFailureType(), testCase.getFailureDetails(),
187                                                       testCase.getName() ) );
188         }
189     }
190 
191     public void characters( char[] ch, int start, int length )
192         throws SAXException
193     {
194         String s = new String( ch, start, length );
195 
196         if ( !"".equals( s.trim() ) )
197         {
198             currentElement.append( s );
199         }
200     }
201 
202     public List<ReportTestCase> getTestCases()
203     {
204         return this.testCases;
205     }
206 
207     public int getNumberOfErrors()
208     {
209         return numberOfErrors;
210     }
211 
212     public void setNumberOfErrors( int numberOfErrors )
213     {
214         this.numberOfErrors = numberOfErrors;
215     }
216 
217     public int getNumberOfFailures()
218     {
219         return numberOfFailures;
220     }
221 
222     public void setNumberOfFailures( int numberOfFailures )
223     {
224         this.numberOfFailures = numberOfFailures;
225     }
226 
227     public int getNumberOfTests()
228     {
229         return numberOfTests;
230     }
231 
232     public void setNumberOfTests( int numberOfTests )
233     {
234         this.numberOfTests = numberOfTests;
235     }
236 
237     public String getName()
238     {
239         return name;
240     }
241 
242     public void setName( String name )
243     {
244         this.name = name;
245     }
246 
247     public String getFName()
248     {
249         return name;
250     }
251 
252     public void setFName( String name )
253     {
254         this.name = name;
255     }
256 
257     public String getPackageName()
258     {
259         return packageName;
260     }
261 
262     public void setPackageName( String packageName )
263     {
264         this.packageName = packageName;
265     }
266 
267     public float getTimeElapsed()
268     {
269         return this.timeElapsed;
270     }
271 
272     public void setTimeElapsed( float timeElapsed )
273     {
274         this.timeElapsed = timeElapsed;
275     }
276 
277     /*
278     private List<String> parseCause( String detail )
279     {
280         String fullName = testCase.getFullName();
281         String name = fullName.substring( fullName.lastIndexOf( "." ) + 1 );
282         return parseCause( detail, name );
283     }
284     
285 
286     private List<String> parseCause( String detail, String compareTo )
287     {
288         StringTokenizer stringTokenizer = new StringTokenizer( detail, "\n" );
289         List<String> parsedDetail = new ArrayList<String>( stringTokenizer.countTokens() );
290 
291         while ( stringTokenizer.hasMoreTokens() )
292         {
293             String lineString = stringTokenizer.nextToken().trim();
294             parsedDetail.add( lineString );
295             if ( lineString.indexOf( compareTo ) >= 0 )
296             {
297                 break;
298             }
299         }
300 
301         return parsedDetail;
302     }
303     */
304 
305     public void setTestCases( List<ReportTestCase> testCases )
306     {
307         this.testCases = Collections.unmodifiableList( testCases );
308     }
309 
310     @SuppressWarnings( "unchecked" )
311     public List<ReportFailure> getReportFailures()
312     {
313         return reportFailures == null ? Collections.EMPTY_LIST : reportFailures;
314     }
315 
316     public void setReportFailures( List<ReportFailure> reportFailures )
317     {
318         this.reportFailures = reportFailures;
319     }
320 
321     public void addReportFailure( ReportFailure reportFailure )
322     {
323         if ( this.reportFailures == null )
324         {
325             this.reportFailures = new LinkedList<ReportFailure>();
326         }
327         this.reportFailures.add( reportFailure );
328     }
329 }