View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.ActionList;
28  import com.eviware.soapui.support.action.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  		return actionList;
90  	}
91  	
92  	public void addAction( Action action, boolean isDefault )
93  	{
94  		if( isDiscarded() )
95  			return;
96  		
97  		if( actionList == null )
98  			actionList = new DefaultActionList( testStep.getName() );
99  		
100 		actionList.addAction( action );
101 		if( isDefault )
102 			actionList.setDefaultAction( action );
103 	}
104 
105 	public Throwable getError()
106 	{
107 		return error;
108 	}
109 	
110 	public void setError( Throwable error )
111 	{
112 		this.error = error;
113 	}
114 
115 	public String[] getMessages()
116 	{
117 		return messages == null ? EMPTY_MESSAGES : messages.toArray( new String[messages.size()] );
118 	}
119 
120 	public void addMessage( String message )
121 	{
122 		if( messages != null )
123 			messages.add( message );
124 	}
125 	
126 	public long getTimeTaken()
127 	{
128 		return timeTaken;
129 	}
130 	
131 	public void setTimeTaken(long timeTaken)
132 	{
133 		this.timeTaken = timeTaken;
134 	}
135 
136 	public long getTimeStamp()
137 	{
138 		return timeStamp;
139 	}
140 
141 	public void setTimeStamp(long timeStamp)
142 	{
143 		this.timeStamp = timeStamp;
144 	}
145 	
146 	public void setSize( long size )
147 	{
148 		this.size = size;
149 	}
150 	
151 	public long getSize()
152 	{
153 		return size;
154 	}
155 
156 	public void writeTo(PrintWriter writer)
157 	{
158 		writer.println( "Status: " + getStatus() );
159 		writer.println( "Time Taken: " + getTimeTaken() );
160 		writer.println( "Size: " + getSize() );
161 		writer.println( "Timestamp: " + new Date( getTimeStamp() ).toString() );
162 		writer.println( "TestStep: " + getTestStep().getName() );
163 		if( error != null )
164 			writer.println( "Error:" + error.toString() );
165 		
166 		if( messages != null )
167 			for( String message : messages )
168 				if( message != null ) 
169 					writer.println( message );
170 
171 		if( isDiscarded() )
172 			writer.println( "Result has been Discarded!" );
173 	}
174 
175 	public void startTimer()
176 	{
177 		startTime = System.nanoTime();
178 	}
179 
180 	public void stopTimer()
181 	{
182 		timeTaken = ( (System.nanoTime()-startTime)/1000000 );
183 	}
184 	
185 	public void discard()
186 	{
187 		discarded = true;
188 		
189 		messages = null;
190 		error = null;
191 		actionList = null;
192 	}
193 	
194 	public boolean isDiscarded()
195 	{
196 		return discarded;
197 	}
198 }