View Javadoc

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