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.panels.teststeps;
14  
15  import java.util.HashSet;
16  import java.util.List;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import javax.swing.ImageIcon;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.config.ModelItemConfig;
26  import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
27  import com.eviware.soapui.impl.wsdl.teststeps.JdbcRequestTestStep;
28  import com.eviware.soapui.impl.wsdl.teststeps.TestRequest;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
30  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
31  import com.eviware.soapui.model.ModelItem;
32  import com.eviware.soapui.model.iface.Attachment;
33  import com.eviware.soapui.model.iface.Interface;
34  import com.eviware.soapui.model.iface.MessagePart;
35  import com.eviware.soapui.model.iface.Operation;
36  import com.eviware.soapui.model.iface.Submit;
37  import com.eviware.soapui.model.iface.SubmitContext;
38  import com.eviware.soapui.model.iface.SubmitListener;
39  import com.eviware.soapui.model.settings.Settings;
40  import com.eviware.soapui.model.support.AbstractAnimatableModelItem;
41  import com.eviware.soapui.model.support.ModelSupport;
42  import com.eviware.soapui.model.testsuite.Assertable;
43  import com.eviware.soapui.model.testsuite.AssertionsListener;
44  import com.eviware.soapui.model.testsuite.TestAssertion;
45  import com.eviware.soapui.monitor.TestMonitor;
46  import com.eviware.soapui.support.UISupport;
47  
48  public class JdbcRequest extends AbstractAnimatableModelItem<ModelItemConfig> implements Assertable, TestRequest
49  {
50  	private final JdbcRequestTestStep testStep;
51  	private Set<SubmitListener> submitListeners = new HashSet<SubmitListener>();
52  	private JdbcResponse response;
53  	final static Logger logger = Logger.getLogger( JdbcRequest.class );
54  	private ImageIcon validRequestIcon;
55  	private ImageIcon failedRequestIcon;
56  	private ImageIcon disabledRequestIcon;
57  	private ImageIcon unknownRequestIcon;
58  	private RequestIconAnimator<?> iconAnimator;
59  	private boolean forLoadTest;
60  	private AssertionStatus currentStatus;
61  
62  	public JdbcRequest( JdbcRequestTestStep testStep, boolean forLoadTest )
63  	{
64  		this.testStep = testStep;
65  
66  		if( !forLoadTest )
67  			initIcons();
68  	}
69  
70  	public void addSubmitListener( SubmitListener listener )
71  	{
72  		submitListeners.add( listener );
73  	}
74  
75  	public boolean dependsOn( ModelItem modelItem )
76  	{
77  		return ModelSupport.dependsOn( testStep, modelItem );
78  	}
79  
80  	public Attachment[] getAttachments()
81  	{
82  		return null;
83  	}
84  
85  	public String getEncoding()
86  	{
87  		return null;
88  	}
89  
90  	public String getEndpoint()
91  	{
92  		return null;
93  	}
94  
95  	public Operation getOperation()
96  	{
97  		return null;
98  	}
99  
100 	public String getRequestContent()
101 	{
102 		return ( ( JdbcRequestTestStep )testStep ).getQuery();
103 	}
104 
105 	public MessagePart[] getRequestParts()
106 	{
107 		return null;
108 	}
109 
110 	public MessagePart[] getResponseParts()
111 	{
112 		return null;
113 	}
114 
115 	public String getTimeout()
116 	{
117 		return testStep.getQueryTimeout();
118 	}
119 
120 	public void removeSubmitListener( SubmitListener listener )
121 	{
122 		submitListeners.remove( listener );
123 	}
124 
125 	public void setEncoding( String string )
126 	{
127 	}
128 
129 	public void setEndpoint( String string )
130 	{
131 	}
132 
133 	public JdbcSubmit submit( SubmitContext submitContext, boolean async ) throws SubmitException
134 	{
135 		return new JdbcSubmit( this, submitContext, async );
136 	}
137 
138 	public List<? extends ModelItem> getChildren()
139 	{
140 		return null;
141 	}
142 
143 	public String getDescription()
144 	{
145 		return testStep.getDescription();
146 	}
147 
148 	// public ImageIcon getIcon()
149 	// {
150 	// return testStep.getIcon();
151 	// }
152 	//
153 	public String getId()
154 	{
155 		return testStep.getId();
156 	}
157 
158 	public String getName()
159 	{
160 		return testStep.getName();
161 	}
162 
163 	public ModelItem getParent()
164 	{
165 		return testStep.getParent();
166 	}
167 
168 	public Settings getSettings()
169 	{
170 		return testStep.getSettings();
171 	}
172 
173 	public SubmitListener[] getSubmitListeners()
174 	{
175 		return submitListeners.toArray( new SubmitListener[submitListeners.size()] );
176 	}
177 
178 	public JdbcRequestTestStep getTestStep()
179 	{
180 		return testStep;
181 	}
182 
183 	public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy,
184 			String newName )
185 	{
186 		return testStep.importAssertion( source, overwrite, createCopy, newName );
187 	}
188 
189 	public TestAssertion addAssertion( String selection )
190 	{
191 		return testStep.addAssertion( selection );
192 	}
193 
194 	public void addAssertionsListener( AssertionsListener listener )
195 	{
196 		testStep.addAssertionsListener( listener );
197 	}
198 
199 	public TestAssertion cloneAssertion( TestAssertion source, String name )
200 	{
201 		return testStep.cloneAssertion( source, name );
202 	}
203 
204 	public String getAssertableContent()
205 	{
206 		return testStep.getAssertableContent();
207 	}
208 
209 	public AssertableType getAssertableType()
210 	{
211 		return testStep.getAssertableType();
212 	}
213 
214 	public TestAssertion getAssertionAt( int c )
215 	{
216 		return testStep.getAssertionAt( c );
217 	}
218 
219 	public TestAssertion getAssertionByName( String name )
220 	{
221 		return testStep.getAssertionByName( name );
222 	}
223 
224 	public int getAssertionCount()
225 	{
226 		return testStep.getAssertionCount();
227 	}
228 
229 	public List<TestAssertion> getAssertionList()
230 	{
231 		return testStep.getAssertionList();
232 	}
233 
234 	public AssertionStatus getAssertionStatus()
235 	{
236 		currentStatus = AssertionStatus.UNKNOWN;
237 
238 		if( getResponse() == null )
239 			return currentStatus;
240 
241 		int cnt = getAssertionCount();
242 		if( cnt == 0 )
243 			return currentStatus;
244 
245 		boolean hasEnabled = false;
246 
247 		for( int c = 0; c < cnt; c++ )
248 		{
249 			if( !getAssertionAt( c ).isDisabled() )
250 				hasEnabled = true;
251 
252 			if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
253 			{
254 				currentStatus = AssertionStatus.FAILED;
255 				break;
256 			}
257 		}
258 
259 		if( currentStatus == AssertionStatus.UNKNOWN && hasEnabled )
260 			currentStatus = AssertionStatus.VALID;
261 
262 		return currentStatus;
263 	}
264 
265 	public Map<String, TestAssertion> getAssertions()
266 	{
267 		return testStep.getAssertions();
268 	}
269 
270 	public String getDefaultAssertableContent()
271 	{
272 		return testStep.getDefaultAssertableContent();
273 	}
274 
275 	public Interface getInterface()
276 	{
277 		return testStep.getInterface();
278 	}
279 
280 	public ModelItem getModelItem()
281 	{
282 		return testStep.getModelItem();
283 	}
284 
285 	public TestAssertion moveAssertion( int ix, int offset )
286 	{
287 		return testStep.moveAssertion( ix, offset );
288 	}
289 
290 	public void removeAssertion( TestAssertion assertion )
291 	{
292 		testStep.removeAssertion( assertion );
293 	}
294 
295 	public void removeAssertionsListener( AssertionsListener listener )
296 	{
297 		testStep.removeAssertionsListener( listener );
298 	}
299 
300 	public void setResponse( JdbcResponse response )
301 	{
302 		this.response = response;
303 	}
304 
305 	public JdbcResponse getResponse()
306 	{
307 		return response;
308 	}
309 
310 	public void initIcons()
311 	{
312 		if( validRequestIcon == null )
313 			validRequestIcon = UISupport.createImageIcon( "/valid_jdbc_request.gif" );
314 
315 		if( failedRequestIcon == null )
316 			failedRequestIcon = UISupport.createImageIcon( "/invalid_jdbc_request.gif" );
317 
318 		if( unknownRequestIcon == null )
319 			unknownRequestIcon = UISupport.createImageIcon( "/unknown_jdbc_request.gif" );
320 
321 		if( disabledRequestIcon == null )
322 			disabledRequestIcon = UISupport.createImageIcon( "/disabled_jdbc_request.gif" );
323 
324 		setIconAnimator( new RequestIconAnimator<JdbcRequest>( this, "/jdbc_request.gif", "/exec_jdbc_request", 4, "gif" ) );
325 	}
326 
327 	protected RequestIconAnimator<?> initIconAnimator()
328 	{
329 		return new RequestIconAnimator<JdbcRequest>( this, "/jdbc_request.gif", "/exec_jdbc_request", 4, "gif" );
330 	}
331 
332 	public static class RequestIconAnimator<T extends JdbcRequest> extends ModelItemIconAnimator<T> implements
333 			SubmitListener
334 	{
335 		public RequestIconAnimator( T modelItem, String baseIcon, String animIconRoot, int iconCount, String iconExtension )
336 		{
337 			super( modelItem, baseIcon, animIconRoot, iconCount, iconExtension );
338 		}
339 
340 		public boolean beforeSubmit( Submit submit, SubmitContext context )
341 		{
342 			if( isEnabled() && submit.getRequest() == getTarget() )
343 				start();
344 			return true;
345 		}
346 
347 		public void afterSubmit( Submit submit, SubmitContext context )
348 		{
349 			if( submit.getRequest() == getTarget() )
350 				stop();
351 		}
352 	}
353 
354 	public RequestIconAnimator<?> getIconAnimator()
355 	{
356 		return iconAnimator;
357 	}
358 
359 	public void setIconAnimator( RequestIconAnimator<?> iconAnimator )
360 	{
361 		if( this.iconAnimator != null )
362 			removeSubmitListener( this.iconAnimator );
363 
364 		this.iconAnimator = iconAnimator;
365 		addSubmitListener( this.iconAnimator );
366 	}
367 
368 	public ImageIcon getIcon()
369 	{
370 		if( forLoadTest || UISupport.isHeadless() || getIconAnimator() == null )
371 			return null;
372 
373 		TestMonitor testMonitor = SoapUI.getTestMonitor();
374 		if( testMonitor != null && testMonitor.hasRunningLoadTest( getTestStep().getTestCase() ) )
375 			return disabledRequestIcon;
376 
377 		ImageIcon icon = getIconAnimator().getIcon();
378 		if( icon == getIconAnimator().getBaseIcon() )
379 		{
380 			AssertionStatus status = getAssertionStatus();
381 			if( status == AssertionStatus.VALID )
382 				return validRequestIcon;
383 			else if( status == AssertionStatus.FAILED )
384 				return failedRequestIcon;
385 			else if( status == AssertionStatus.UNKNOWN )
386 				return unknownRequestIcon;
387 		}
388 
389 		return icon;
390 	}
391 
392 	@Override
393 	public void setIcon( ImageIcon icon )
394 	{
395 		getTestStep().setIcon( icon );
396 	}
397 
398 	public String getPassword()
399 	{
400 		return null;
401 	}
402 
403 	public String getUsername()
404 	{
405 		return null;
406 	}
407 
408 	public boolean isDiscardResponse()
409 	{
410 		return getSettings().getBoolean( "discardResponse" );
411 	}
412 
413 	public void setDiscardResponse( boolean discardResponse )
414 	{
415 		getSettings().setBoolean( "discardResponse", discardResponse );
416 	}
417 }