View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  				public void actionPerformed( ActionEvent e )
96  				{
97  					if( getMessages().length > 0 )
98  					{
99  						StringBuffer buf = new StringBuffer();
100 						if( getError() != null )
101 							buf.append( getError().toString() ).append( "\r\n" );
102 						
103 						for( String s : getMessages() )
104 							buf.append( s ).append(  "\r\n" );
105 						
106 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStep.getName() +
107 									"] ran with status [" + getStatus() + "]", buf.toString(), null );
108 					}
109 					else if( getError() != null )
110 					{
111 						UISupport.showExtendedInfo( "TestStep Result", "Step [" + testStep.getName() +
112 									"] ran with status [" + getStatus() + "]", getError().toString(), null );
113 					}
114 					else
115 					{
116 						UISupport.showInfoMessage( "Step [" + testStep.getName() +
117 									"] ran with status [" + getStatus() + "]", "TestStep Result");
118 					}
119 				}} );
120 		}
121 		
122 		return actionList;
123 	}
124 	
125 	public void addAction( Action action, boolean isDefault )
126 	{
127 		if( isDiscarded() )
128 			return;
129 		
130 		if( actionList == null )
131 		{
132 			actionList = new DefaultActionList( testStep.getName() );
133 		}
134 		
135 		actionList.addAction( action );
136 		if( isDefault )
137 			actionList.setDefaultAction( action );
138 	}
139 
140 	public Throwable getError()
141 	{
142 		return error;
143 	}
144 	
145 	public void setError( Throwable error )
146 	{
147 		this.error = error;
148 	}
149 
150 	public String[] getMessages()
151 	{
152 		return messages == null ? EMPTY_MESSAGES : messages.toArray( new String[messages.size()] );
153 	}
154 
155 	public void addMessage( String message )
156 	{
157 		if( messages != null )
158 			messages.add( message );
159 	}
160 	
161 	public long getTimeTaken()
162 	{
163 		return timeTaken;
164 	}
165 	
166 	public void setTimeTaken(long timeTaken)
167 	{
168 		this.timeTaken = timeTaken;
169 	}
170 
171 	public long getTimeStamp()
172 	{
173 		return timeStamp;
174 	}
175 
176 	public void setTimeStamp(long timeStamp)
177 	{
178 		this.timeStamp = timeStamp;
179 	}
180 	
181 	public void setSize( long size )
182 	{
183 		this.size = size;
184 	}
185 	
186 	public long getSize()
187 	{
188 		return size;
189 	}
190 
191 	public void writeTo(PrintWriter writer)
192 	{
193 		writer.println( "Status: " + getStatus() );
194 		writer.println( "Time Taken: " + getTimeTaken() );
195 		writer.println( "Size: " + getSize() );
196 		writer.println( "Timestamp: " + new Date( getTimeStamp() ).toString() );
197 		writer.println( "TestStep: " + getTestStep().getName() );
198 		if( error != null )
199 			writer.println( "Error:" + error.toString() );
200 		
201 		if( messages != null )
202 			for( String message : messages )
203 				if( message != null ) 
204 					writer.println( message );
205 
206 		if( isDiscarded() )
207 			writer.println( "Result has been Discarded!" );
208 	}
209 
210 	public void startTimer()
211 	{
212 		startTime = System.nanoTime();
213 	}
214 
215 	public void stopTimer()
216 	{
217 		timeTaken = ( (System.nanoTime()-startTime)/1000000 );
218 	}
219 	
220 	public void discard()
221 	{
222 		discarded = true;
223 		
224 		messages = null;
225 		error = null;
226 		actionList = null;
227 	}
228 	
229 	public boolean isDiscarded()
230 	{
231 		return discarded;
232 	}
233 	
234 	public void addMessages( String[] messages )
235 	{
236 		if( this.messages != null )
237 			this.messages.addAll( Arrays.asList( messages ) );
238 	}
239 }