View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.report;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.math.BigDecimal;
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.apache.xmlbeans.XmlOptions;
23  
24  import com.eviware.soapui.junit.Properties;
25  import com.eviware.soapui.junit.Property;
26  import com.eviware.soapui.junit.Testcase;
27  import com.eviware.soapui.junit.Testsuite;
28  import com.eviware.soapui.junit.TestsuiteDocument;
29  import com.eviware.soapui.junit.FailureDocument.Failure;
30  
31  /***
32   * Wrapper for a number of Test runs
33   */
34  
35  public class JUnitReport
36  {
37  	TestsuiteDocument testsuiteDoc;
38  	int noofTestCases, noofFailures, noofErrors;
39  	double totalTime;
40  	StringBuffer systemOut;
41  	StringBuffer systemErr;
42  
43  	public JUnitReport()
44  	{
45  		systemOut = new StringBuffer();
46  		systemErr = new StringBuffer();
47  
48  		testsuiteDoc = TestsuiteDocument.Factory.newInstance();
49  		Testsuite testsuite = testsuiteDoc.addNewTestsuite();
50  		Properties properties = testsuite.addNewProperties();
51  		setSystemProperties( properties );
52  	}
53  
54  	public void setTotalTime( double time )
55  	{
56  		testsuiteDoc.getTestsuite().setTime( new BigDecimal( time ) );
57  	}
58  
59  	public void setTestSuiteName( String name )
60  	{
61  		testsuiteDoc.getTestsuite().setName( name );
62  	}
63  
64  	public void setNoofErrorsInTestSuite( int errors )
65  	{
66  		testsuiteDoc.getTestsuite().setErrors( errors );
67  	}
68  
69  	public void setNoofFailuresInTestSuite( int failures )
70  	{
71  		testsuiteDoc.getTestsuite().setFailures( failures );
72  	}
73  
74  	public void systemOut( String systemout )
75  	{
76  		systemOut.append( systemout );
77  	}
78  
79  	public void systemErr( String systemerr )
80  	{
81  		systemErr.append( systemerr );
82  	}
83  
84  	public void setSystemOut( String systemout )
85  	{
86  		testsuiteDoc.getTestsuite().setSystemOut( systemout );
87  	}
88  
89  	public void setSystemErr( String systemerr )
90  	{
91  		testsuiteDoc.getTestsuite().setSystemErr( systemerr );
92  	}
93  
94  	public Testcase addTestCase( String name, double time )
95  	{
96  		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
97  		testcase.setName( name );
98  		testcase.setTime( time / 1000 );
99  		noofTestCases++ ;
100 		totalTime += time;
101 		return testcase;
102 	}
103 
104 	public Testcase addTestCaseWithFailure( String name, double time, String failure, String stacktrace )
105 	{
106 		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
107 		testcase.setName( name );
108 		testcase.setTime( time / 1000 );
109 		Failure fail = testcase.addNewFailure();
110 		fail.setType( failure );
111 		fail.setMessage( failure );
112 		fail.setStringValue( stacktrace );
113 		noofTestCases++ ;
114 		noofFailures++ ;
115 		totalTime += time;
116 		return testcase;
117 	}
118 
119 	public Testcase addTestCaseWithError( String name, double time, String error, String stacktrace )
120 	{
121 		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
122 		testcase.setName( name );
123 		testcase.setTime( time / 1000 );
124 		com.eviware.soapui.junit.ErrorDocument.Error err = testcase.addNewError();
125 		err.setType( error );
126 		err.setMessage( error );
127 		err.setStringValue( stacktrace );
128 		noofTestCases++ ;
129 		noofErrors++ ;
130 		totalTime += time;
131 		return testcase;
132 	}
133 
134 	private void setSystemProperties( Properties properties )
135 	{
136 		Set<?> keys = System.getProperties().keySet();
137 		for( Object keyO : keys )
138 		{
139 			String key = keyO.toString();
140 			String value = System.getProperty( key );
141 			Property prop = properties.addNewProperty();
142 			prop.setName( key );
143 			prop.setValue( value );
144 		}
145 	}
146 
147 	@SuppressWarnings( "unchecked" )
148 	public void save( File file ) throws IOException
149 	{
150 		finishReport();
151 
152 		Map prefixes = new HashMap();
153 		prefixes.put( "", "http://eviware.com/soapui/junit" );
154 
155 		testsuiteDoc.save( file, new XmlOptions().setSaveOuter().setCharacterEncoding( "utf-8" ).setUseDefaultNamespace()
156 				.setSaveImplicitNamespaces( prefixes ) );
157 	}
158 
159 	public TestsuiteDocument finishReport()
160 	{
161 		testsuiteDoc.getTestsuite().setTests( noofTestCases );
162 		testsuiteDoc.getTestsuite().setFailures( noofFailures );
163 		testsuiteDoc.getTestsuite().setErrors( noofErrors );
164 		testsuiteDoc.getTestsuite().setTime( new BigDecimal( totalTime / 1000 ) );
165 
166 		return testsuiteDoc;
167 	}
168 }