View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.impl.wsdl.teststeps;
14  
15  import java.awt.event.ActionEvent;
16  import java.io.PrintWriter;
17  import java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.Date;
20  import java.util.List;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.Action;
24  
25  import com.eviware.soapui.model.testsuite.TestStep;
26  import com.eviware.soapui.model.testsuite.TestStepResult;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.action.swing.ActionList;
29  import com.eviware.soapui.support.action.swing.DefaultActionList;
30  
31  /***
32   * Default implementation of TestStepResult interface
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public class WsdlTestStepResult implements TestStepResult
38  {
39  	private static final String[] EMPTY_MESSAGES = new String[0];
40  	private final WsdlTestStep testStep;
41  	private List<String> messages = new ArrayList<String>();
42  	private Throwable error;
43  	private TestStepStatus status = TestStepStatus.UNKNOWN;
44  	private long timeTaken;
45  	private long timeStamp;
46  	private long size;
47  	private DefaultActionList actionList;
48  	private long startTime;
49  	private boolean discarded;
50  	private String testStepName;
51  
52  	private static DefaultActionList discardedActionList = new DefaultActionList( null );
53  
54  	static
55  	{
56  		discardedActionList.setDefaultAction( new AbstractAction()
57  		{
58  			public void actionPerformed( ActionEvent arg0 )
59  			{
60  				UISupport.showErrorMessage( "Result has been discarded" );
61  			}
62  		} );
63  	}
64  
65  	public WsdlTestStepResult( WsdlTestStep testStep )
66  	{
67  		this.testStep = testStep;
68  		testStepName = testStep.getName();
69  		timeStamp = System.currentTimeMillis();
70  	}
71  
72  	public TestStepStatus getStatus()
73  	{
74  		return status;
75  	}
76  
77  	public void setStatus( TestStepStatus status )
78  	{
79  		this.status = status;
80  	}
81  
82  	public TestStep getTestStep()
83  	{
84  		try
85  		{
86  			if( testStep != null )
87  				testStep.getName();
88  			
89  			return testStep;
90  		}
91  		catch( Throwable t )
92  		{
93  		}
94  		
95  		return null;
96  	}
97  
98  	public ActionList getActions()
99  	{
100 		if( isDiscarded() )
101 			return discardedActionList;
102 
103 		if( actionList == null )
104 		{
105 			actionList = new DefaultActionList( testStepName );
106 			actionList.setDefaultAction( new AbstractAction()
107 			{
108 
109 				public void actionPerformed( ActionEvent e )
110 				{
111 					if( getMessages().length > 0 )
112 					{
113 						StringBuffer buf = new StringBuffer( "<html><body>");
114 						if( getError() != null )
115 							buf.append( getError().toString() ).append( "<br/>" );
116 
117 						for( String s : getMessages() )
118 							buf.append( s ).append( "<br/>" );
119 
120 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStepName + "] ran with status ["
121 								+ getStatus() + "]", buf.toString(), null );
122 					}
123 					else if( getError() != null )
124 					{
125 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStepName + "] ran with status ["
126 								+ getStatus() + "]", getError().toString(), null );
127 					}
128 					else
129 					{
130 						UISupport.showInfoMessage( "Step [" + testStepName + "] ran with status [" + getStatus() + "]",
131 								"TestStep Result" );
132 					}
133 				}
134 			} );
135 		}
136 
137 		return actionList;
138 	}
139 
140 	public void addAction( Action action, boolean isDefault )
141 	{
142 		if( isDiscarded() )
143 			return;
144 
145 		if( actionList == null )
146 		{
147 			actionList = new DefaultActionList( testStepName );
148 		}
149 
150 		actionList.addAction( action );
151 		if( isDefault )
152 			actionList.setDefaultAction( action );
153 	}
154 
155 	public Throwable getError()
156 	{
157 		return error;
158 	}
159 
160 	public void setError( Throwable error )
161 	{
162 		this.error = error;
163 	}
164 
165 	public String[] getMessages()
166 	{
167 		return messages == null ? EMPTY_MESSAGES : messages.toArray( new String[messages.size()] );
168 	}
169 
170 	public void addMessage( String message )
171 	{
172 		if( messages != null )
173 			messages.add( message );
174 	}
175 
176 	public long getTimeTaken()
177 	{
178 		return timeTaken;
179 	}
180 
181 	public void setTimeTaken( long timeTaken )
182 	{
183 		this.timeTaken = timeTaken;
184 	}
185 
186 	public long getTimeStamp()
187 	{
188 		return timeStamp;
189 	}
190 
191 	public void setTimeStamp( long timeStamp )
192 	{
193 		this.timeStamp = timeStamp;
194 	}
195 
196 	public void setSize( long size )
197 	{
198 		this.size = size;
199 	}
200 
201 	public long getSize()
202 	{
203 		return size;
204 	}
205 
206 	public void writeTo( PrintWriter writer )
207 	{
208 		writer.println( "Status: " + getStatus() );
209 		writer.println( "Time Taken: " + getTimeTaken() );
210 		writer.println( "Size: " + getSize() );
211 		writer.println( "Timestamp: " + new Date( getTimeStamp() ).toString() );
212 		writer.println( "TestStep: " + getTestStep().getName() );
213 		if( error != null )
214 			writer.println( "Error:" + error.toString() );
215 
216 		if( messages != null )
217 		{
218 			writer.println( "\r\n----------------- Messages ------------------------------" );
219 			for( String message : messages )
220 				if( message != null )
221 					writer.println( message );
222 		}
223 
224 		if( isDiscarded() )
225 			writer.println( "Result has been Discarded!" );
226 	}
227 
228 	public void startTimer()
229 	{
230 		startTime = System.nanoTime();
231 	}
232 
233 	public void stopTimer()
234 	{
235 		timeTaken = ( ( System.nanoTime() - startTime ) / 1000000 );
236 	}
237 
238 	public void discard()
239 	{
240 		discarded = true;
241 
242 		messages = null;
243 		error = null;
244 		actionList = null;
245 	}
246 
247 	public boolean isDiscarded()
248 	{
249 		return discarded;
250 	}
251 
252 	public void addMessages( String[] messages )
253 	{
254 		if( this.messages != null )
255 			this.messages.addAll( Arrays.asList( messages ) );
256 	}
257 }