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.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  
51  	private static DefaultActionList discardedActionList = new DefaultActionList( null );
52  
53  	static
54  	{
55  		discardedActionList.setDefaultAction( new AbstractAction()
56  		{
57  			public void actionPerformed( ActionEvent arg0 )
58  			{
59  				UISupport.showErrorMessage( "Result has been discarded" );
60  			}
61  		} );
62  	}
63  
64  	public WsdlTestStepResult( WsdlTestStep testStep )
65  	{
66  		this.testStep = testStep;
67  		timeStamp = System.currentTimeMillis();
68  	}
69  
70  	public TestStepStatus getStatus()
71  	{
72  		return status;
73  	}
74  
75  	public void setStatus( TestStepStatus status )
76  	{
77  		this.status = status;
78  	}
79  
80  	public TestStep getTestStep()
81  	{
82  		return testStep;
83  	}
84  
85  	public ActionList getActions()
86  	{
87  		if( isDiscarded() )
88  			return discardedActionList;
89  
90  		if( actionList == null )
91  		{
92  			actionList = new DefaultActionList( testStep.getName() );
93  			actionList.setDefaultAction( new AbstractAction()
94  			{
95  
96  				public void actionPerformed( ActionEvent e )
97  				{
98  					if( getMessages().length > 0 )
99  					{
100 						StringBuffer buf = new StringBuffer();
101 						if( getError() != null )
102 							buf.append( getError().toString() ).append( "\r\n" );
103 
104 						for( String s : getMessages() )
105 							buf.append( s ).append( "\r\n" );
106 
107 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStep.getName() + "] ran with status ["
108 								+ getStatus() + "]", buf.toString(), null );
109 					}
110 					else if( getError() != null )
111 					{
112 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStep.getName() + "] ran with status ["
113 								+ getStatus() + "]", getError().toString(), null );
114 					}
115 					else
116 					{
117 						UISupport.showInfoMessage( "Step [" + testStep.getName() + "] ran with status [" + getStatus() + "]",
118 								"TestStep Result" );
119 					}
120 				}
121 			} );
122 		}
123 
124 		return actionList;
125 	}
126 
127 	public void addAction( Action action, boolean isDefault )
128 	{
129 		if( isDiscarded() )
130 			return;
131 
132 		if( actionList == null )
133 		{
134 			actionList = new DefaultActionList( testStep.getName() );
135 		}
136 
137 		actionList.addAction( action );
138 		if( isDefault )
139 			actionList.setDefaultAction( action );
140 	}
141 
142 	public Throwable getError()
143 	{
144 		return error;
145 	}
146 
147 	public void setError( Throwable error )
148 	{
149 		this.error = error;
150 	}
151 
152 	public String[] getMessages()
153 	{
154 		return messages == null ? EMPTY_MESSAGES : messages.toArray( new String[messages.size()] );
155 	}
156 
157 	public void addMessage( String message )
158 	{
159 		if( messages != null )
160 			messages.add( message );
161 	}
162 
163 	public long getTimeTaken()
164 	{
165 		return timeTaken;
166 	}
167 
168 	public void setTimeTaken( long timeTaken )
169 	{
170 		this.timeTaken = timeTaken;
171 	}
172 
173 	public long getTimeStamp()
174 	{
175 		return timeStamp;
176 	}
177 
178 	public void setTimeStamp( long timeStamp )
179 	{
180 		this.timeStamp = timeStamp;
181 	}
182 
183 	public void setSize( long size )
184 	{
185 		this.size = size;
186 	}
187 
188 	public long getSize()
189 	{
190 		return size;
191 	}
192 
193 	public void writeTo( PrintWriter writer )
194 	{
195 		writer.println( "Status: " + getStatus() );
196 		writer.println( "Time Taken: " + getTimeTaken() );
197 		writer.println( "Size: " + getSize() );
198 		writer.println( "Timestamp: " + new Date( getTimeStamp() ).toString() );
199 		writer.println( "TestStep: " + getTestStep().getName() );
200 		if( error != null )
201 			writer.println( "Error:" + error.toString() );
202 
203 		if( messages != null )
204 		{
205 			writer.println( "\r\n----------------- Messages ------------------------------" );
206 			for( String message : messages )
207 				if( message != null )
208 					writer.println( message );
209 		}
210 
211 		if( isDiscarded() )
212 			writer.println( "Result has been Discarded!" );
213 	}
214 
215 	public void startTimer()
216 	{
217 		startTime = System.nanoTime();
218 	}
219 
220 	public void stopTimer()
221 	{
222 		timeTaken = ( ( System.nanoTime() - startTime ) / 1000000 );
223 	}
224 
225 	public void discard()
226 	{
227 		discarded = true;
228 
229 		messages = null;
230 		error = null;
231 		actionList = null;
232 	}
233 
234 	public boolean isDiscarded()
235 	{
236 		return discarded;
237 	}
238 
239 	public void addMessages( String[] messages )
240 	{
241 		if( this.messages != null )
242 			this.messages.addAll( Arrays.asList( messages ) );
243 	}
244 }