KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > testsuite > report > ReportTestSuite


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17  
18 package org.apache.geronimo.mavenplugins.testsuite.report;
19
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.text.NumberFormat JavaDoc;
30 import java.text.ParseException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 public class ReportTestSuite
38     extends DefaultHandler JavaDoc
39 {
40     private List JavaDoc testCases;
41
42     private int numberOfErrors;
43
44     private int numberOfFailures;
45     
46     private int numberOfSkipped;
47
48     private int numberOfTests;
49
50     private String JavaDoc name;
51
52     private String JavaDoc fullClassName;
53
54     private String JavaDoc packageName;
55
56     private float timeElapsed;
57
58     private NumberFormat JavaDoc numberFormat = NumberFormat.getInstance();
59
60     /**
61      * @noinspection StringBufferField
62      */

63     private StringBuffer JavaDoc currentElement;
64
65     private ReportTestCase testCase;
66
67     public void parse( String JavaDoc xmlPath )
68         throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc
69     {
70         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
71
72         SAXParser JavaDoc saxParser = factory.newSAXParser();
73
74         saxParser.parse( new File JavaDoc( xmlPath ), this );
75     }
76
77     
78     private int getAttributeAsInt( Attributes JavaDoc attributes, String JavaDoc name )
79     {
80         // may or may not exist
81
String JavaDoc valueAsString = attributes.getValue( name );
82         if ( valueAsString != null )
83         {
84             return Integer.parseInt( valueAsString );
85         }
86         return 0;
87     }
88     
89     public void startElement( String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes )
90         throws SAXException JavaDoc
91     {
92         try
93         {
94             if ( "testsuite".equals( qName ) )
95             {
96                 numberOfErrors = getAttributeAsInt( attributes, "errors" );
97                 numberOfFailures = getAttributeAsInt( attributes, "failures" );
98                 numberOfSkipped = getAttributeAsInt( attributes, "skipped" );
99                 numberOfTests = getAttributeAsInt( attributes, "tests" );
100
101                 Number JavaDoc time = numberFormat.parse( attributes.getValue( "time" ) );
102
103                 timeElapsed = time.floatValue();
104
105                 //check if group attribute is existing
106
if ( attributes.getValue( "group" ) != null && !"".equals( attributes.getValue( "group" ) ) )
107                 {
108                     packageName = attributes.getValue( "group" );
109
110                     name = attributes.getValue( "name" );
111
112                     fullClassName = packageName + "." + name;
113                 }
114                 else
115                 {
116                     fullClassName = attributes.getValue( "name" );
117
118                     name = fullClassName.substring( fullClassName.lastIndexOf( "." ) + 1, fullClassName.length() );
119
120                     int lastDotPosition = fullClassName.lastIndexOf( "." );
121                     if ( lastDotPosition < 0 )
122                     {
123                         /* no package name */
124                         packageName = "";
125                     }
126                     else
127                     {
128                         packageName = fullClassName.substring( 0, lastDotPosition );
129                     }
130                 }
131
132                 testCases = new ArrayList JavaDoc();
133             }
134             else if ( "testcase".equals( qName ) )
135             {
136                 currentElement = new StringBuffer JavaDoc();
137
138                 testCase = new ReportTestCase();
139
140                 testCase.setFullClassName( fullClassName );
141
142                 testCase.setName( attributes.getValue( "name" ) );
143
144                 testCase.setClassName( name );
145
146                 String JavaDoc timeAsString = attributes.getValue( "time" );
147
148                 Number JavaDoc time = new Integer JavaDoc( 0 );
149
150                 if ( timeAsString != null )
151                 {
152                     time = numberFormat.parse( timeAsString );
153                 }
154
155                 testCase.setTime( time.floatValue() );
156
157                 testCase.setFullName( packageName + "." + name + "." + testCase.getName() );
158             }
159             else if ( "failure".equals( qName ) )
160             {
161                 testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) );
162             }
163             else if ( "error".equals( qName ) )
164             {
165                 testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) );
166             }
167         }
168         catch ( ParseException JavaDoc e )
169         {
170             throw new SAXException JavaDoc( e.getMessage(), e );
171         }
172     }
173
174     public void endElement( String JavaDoc uri, String JavaDoc localName, String JavaDoc qName )
175         throws SAXException JavaDoc
176     {
177         if ( "testcase".equals( qName ) )
178         {
179             testCases.add( testCase );
180         }
181         else if ( "failure".equals( qName ) )
182         {
183             Map JavaDoc failure = testCase.getFailure();
184
185             failure.put( "detail", parseCause( currentElement.toString() ) );
186         }
187         else if ( "error".equals( qName ) )
188         {
189             Map JavaDoc error = testCase.getFailure();
190
191             error.put( "detail", parseCause( currentElement.toString() ) );
192         }
193     }
194
195     public void characters( char[] ch, int start, int length )
196         throws SAXException JavaDoc
197     {
198         String JavaDoc s = new String JavaDoc( ch, start, length );
199
200         if ( ! "".equals( s.trim() ) )
201         {
202             currentElement.append( s );
203         }
204     }
205
206     public List JavaDoc getTestCases()
207     {
208         return this.testCases;
209     }
210
211     public int getNumberOfErrors()
212     {
213         return numberOfErrors;
214     }
215
216     public void setNumberOfErrors( int numberOfErrors )
217     {
218         this.numberOfErrors = numberOfErrors;
219     }
220
221     public int getNumberOfFailures()
222     {
223         return numberOfFailures;
224     }
225
226     public void setNumberOfFailures( int numberOfFailures )
227     {
228         this.numberOfFailures = numberOfFailures;
229     }
230     
231     public int getNumberOfSkipped()
232     {
233         return numberOfSkipped;
234     }
235     
236     public void setNumberOfSkipped( int numberOfSkipped )
237     {
238         this.numberOfSkipped = numberOfSkipped;
239     }
240
241     public int getNumberOfTests()
242     {
243         return numberOfTests;
244     }
245
246     public void setNumberOfTests( int numberOfTests )
247     {
248         this.numberOfTests = numberOfTests;
249     }
250
251     public String JavaDoc getName()
252     {
253         return name;
254     }
255
256     public void setName( String JavaDoc name )
257     {
258         this.name = name;
259     }
260
261     public String JavaDoc getFName()
262     {
263         return name;
264     }
265
266     public void setFName( String JavaDoc name )
267     {
268         this.name = name;
269     }
270
271     public String JavaDoc getPackageName()
272     {
273         return packageName;
274     }
275
276     public void setPackageName( String JavaDoc packageName )
277     {
278         this.packageName = packageName;
279     }
280
281     public float getTimeElapsed()
282     {
283         return this.timeElapsed;
284     }
285
286     public void setTimeElapsed( float timeElapsed )
287     {
288         this.timeElapsed = timeElapsed;
289     }
290
291     private List JavaDoc parseCause( String JavaDoc detail )
292     {
293         String JavaDoc fullName = testCase.getFullName();
294         String JavaDoc name = fullName.substring( fullName.lastIndexOf( "." ) + 1 );
295         return parseCause( detail, name );
296     }
297
298     private List JavaDoc parseCause( String JavaDoc detail, String JavaDoc compareTo )
299     {
300         StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc( detail, "\n" );
301         List JavaDoc parsedDetail = new ArrayList JavaDoc( stringTokenizer.countTokens() );
302
303         while ( stringTokenizer.hasMoreTokens() )
304         {
305             String JavaDoc lineString = stringTokenizer.nextToken().trim();
306             parsedDetail.add( lineString );
307             if ( lineString.indexOf( compareTo ) >= 0 )
308             {
309                 break;
310             }
311         }
312
313         return parsedDetail;
314     }
315
316     public void setTestCases( List JavaDoc testCases )
317     {
318         this.testCases = Collections.unmodifiableList( testCases );
319     }
320 }
321
Popular Tags