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.teststeps;
14  
15  import javax.swing.ImageIcon;
16  
17  import org.apache.xmlbeans.XmlObject;
18  
19  import com.eviware.soapui.config.TestAssertionConfig;
20  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry;
21  import com.eviware.soapui.model.ModelItem;
22  import com.eviware.soapui.model.iface.MessageExchange;
23  import com.eviware.soapui.model.iface.SubmitContext;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
25  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
26  import com.eviware.soapui.model.settings.Settings;
27  import com.eviware.soapui.model.support.AbstractModelItem;
28  import com.eviware.soapui.model.support.ModelSupport;
29  import com.eviware.soapui.model.testsuite.Assertable;
30  import com.eviware.soapui.model.testsuite.AssertionError;
31  import com.eviware.soapui.model.testsuite.AssertionException;
32  import com.eviware.soapui.model.testsuite.TestAssertion;
33  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
34  import com.eviware.soapui.model.testsuite.TestCaseRunner;
35  import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus;
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.soapui.support.resolver.ResolveContext;
38  
39  /***
40   * Base class for WsdlAssertions
41   * 
42   * @author Ole.Matzura
43   */
44  
45  public abstract class WsdlMessageAssertion extends AbstractModelItem implements PropertyExpansionContainer,
46  		TestAssertion
47  {
48  	private TestAssertionConfig assertionConfig;
49  	private final Assertable assertable;
50  	private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
51  	private com.eviware.soapui.model.testsuite.AssertionError[] assertionErrors;
52  	private ImageIcon validIcon;
53  	private ImageIcon failedIcon;
54  	private ImageIcon unknownIcon;
55  
56  	private final boolean cloneable;
57  	private final boolean configurable;
58  	private final boolean allowMultiple;
59  	private final boolean requiresResponseContent;
60  
61  	protected WsdlMessageAssertion( TestAssertionConfig assertionConfig, Assertable modelItem, boolean cloneable,
62  			boolean configurable, boolean multiple, boolean requiresResponseContent )
63  	{
64  		this.assertionConfig = assertionConfig;
65  		this.assertable = modelItem;
66  		this.cloneable = cloneable;
67  		this.configurable = configurable;
68  		this.allowMultiple = multiple;
69  		this.requiresResponseContent = requiresResponseContent;
70  
71  		validIcon = UISupport.createImageIcon( "/valid_assertion.gif" );
72  		failedIcon = UISupport.createImageIcon( "/failed_assertion.gif" );
73  		unknownIcon = UISupport.createImageIcon( "/unknown_assertion.gif" );
74  	}
75  
76  	public XmlObject getConfiguration()
77  	{
78  		if( null == assertionConfig.getConfiguration() )
79  		{
80  			assertionConfig.addNewConfiguration();
81  		}
82  
83  		return assertionConfig.getConfiguration();
84  	}
85  
86  	public void setConfiguration( XmlObject configuration )
87  	{
88  		XmlObject oldConfig = assertionConfig.getConfiguration();
89  		assertionConfig.setConfiguration( configuration );
90  		notifyPropertyChanged( TestAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
91  	}
92  
93  	/*
94  	 * (non-Javadoc)
95  	 * 
96  	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getName()
97  	 */
98  	public String getName()
99  	{
100 		return assertionConfig.isSetName() ? assertionConfig.getName() : TestAssertionRegistry.getInstance()
101 				.getAssertionNameForType( assertionConfig.getType() );
102 	}
103 
104 	/*
105 	 * (non-Javadoc)
106 	 * 
107 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getStatus()
108 	 */
109 	public AssertionStatus getStatus()
110 	{
111 		return isDisabled() ? AssertionStatus.UNKNOWN : assertionStatus;
112 	}
113 
114 	/*
115 	 * (non-Javadoc)
116 	 * 
117 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getErrors()
118 	 */
119 	public AssertionError[] getErrors()
120 	{
121 		return isDisabled() ? null : assertionErrors;
122 	}
123 
124 	/*
125 	 * (non-Javadoc)
126 	 * 
127 	 * @see
128 	 * com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isAllowMultiple()
129 	 */
130 	public boolean isAllowMultiple()
131 	{
132 		return allowMultiple;
133 	}
134 
135 	public AssertionStatus assertResponse( MessageExchange messageExchange, SubmitContext context )
136 	{
137 		AssertionStatus oldStatus = assertionStatus;
138 		AssertionError[] oldErrors = getErrors();
139 		ImageIcon oldIcon = getIcon();
140 
141 		if( isDisabled() )
142 		{
143 			assertionStatus = AssertionStatus.UNKNOWN;
144 			assertionErrors = null;
145 		}
146 		else if( !messageExchange.hasResponse() && requiresResponseContent )
147 		{
148 			assertionStatus = AssertionStatus.FAILED;
149 			assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] { new com.eviware.soapui.model.testsuite.AssertionError(
150 					"null/empty response" ) };
151 		}
152 		else
153 		{
154 			try
155 			{
156 				internalAssertResponse( messageExchange, context );
157 				assertionStatus = AssertionStatus.VALID;
158 				assertionErrors = null;
159 			}
160 			catch( AssertionException e )
161 			{
162 				assertionStatus = AssertionStatus.FAILED;
163 				assertionErrors = e.getErrors();
164 			}
165 			catch( Throwable e )
166 			{
167 				assertionStatus = AssertionStatus.FAILED;
168 				assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] { new com.eviware.soapui.model.testsuite.AssertionError(
169 						e.getMessage() ) };
170 			}
171 		}
172 
173 		notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
174 		notifyPropertyChanged( ERRORS_PROPERTY, oldErrors, assertionErrors );
175 		notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
176 
177 		return assertionStatus;
178 	}
179 
180 	protected abstract String internalAssertResponse( MessageExchange messageExchange, SubmitContext context )
181 			throws AssertionException;
182 
183 	public AssertionStatus assertRequest( MessageExchange messageExchange, SubmitContext context )
184 	{
185 		AssertionStatus oldStatus = assertionStatus;
186 		ImageIcon oldIcon = getIcon();
187 
188 		if( !messageExchange.hasRequest( true ) )
189 		{
190 			assertionStatus = AssertionStatus.FAILED;
191 			assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] { new com.eviware.soapui.model.testsuite.AssertionError(
192 					"null/empty request" ) };
193 		}
194 		else
195 		{
196 			try
197 			{
198 				internalAssertRequest( messageExchange, context );
199 				assertionStatus = AssertionStatus.VALID;
200 				assertionErrors = null;
201 			}
202 			catch( AssertionException e )
203 			{
204 				assertionStatus = AssertionStatus.FAILED;
205 				assertionErrors = e.getErrors();
206 			}
207 			catch( Throwable e )
208 			{
209 				e.printStackTrace();
210 				assertionStatus = AssertionStatus.FAILED;
211 				assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] { new com.eviware.soapui.model.testsuite.AssertionError(
212 						e.getMessage() ) };
213 			}
214 		}
215 
216 		notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
217 		notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
218 
219 		return assertionStatus;
220 	}
221 
222 	protected abstract String internalAssertRequest( MessageExchange messageExchange, SubmitContext context )
223 			throws AssertionException;
224 
225 	/*
226 	 * (non-Javadoc)
227 	 * 
228 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isConfigurable()
229 	 */
230 	public boolean isConfigurable()
231 	{
232 		return configurable;
233 	}
234 
235 	/*
236 	 * (non-Javadoc)
237 	 * 
238 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isClonable()
239 	 */
240 	public boolean isClonable()
241 	{
242 		return cloneable;
243 	}
244 
245 	/*
246 	 * (non-Javadoc)
247 	 * 
248 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#configure()
249 	 */
250 	public boolean configure()
251 	{
252 		return true;
253 	}
254 
255 	/*
256 	 * (non-Javadoc)
257 	 * 
258 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getDescription()
259 	 */
260 	public String getDescription()
261 	{
262 		return getConfig().getDescription();
263 	}
264 
265 	/*
266 	 * (non-Javadoc)
267 	 * 
268 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getIcon()
269 	 */
270 	public ImageIcon getIcon()
271 	{
272 		switch( getStatus() )
273 		{
274 		case FAILED :
275 			return failedIcon;
276 		case UNKNOWN :
277 			return unknownIcon;
278 		case VALID :
279 			return validIcon;
280 		}
281 
282 		return null;
283 	}
284 
285 	public void updateConfig( TestAssertionConfig config )
286 	{
287 		this.assertionConfig = config;
288 	}
289 
290 	public TestAssertionConfig getConfig()
291 	{
292 		return assertionConfig;
293 	}
294 
295 	public Settings getSettings()
296 	{
297 		return assertable.getModelItem().getSettings();
298 	}
299 
300 	public void release()
301 	{
302 	}
303 
304 	/*
305 	 * (non-Javadoc)
306 	 * 
307 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getAssertable()
308 	 */
309 	public Assertable getAssertable()
310 	{
311 		return assertable;
312 	}
313 
314 	public String getId()
315 	{
316 		if( !assertionConfig.isSetId() )
317 			assertionConfig.setId( ModelSupport.generateModelItemID() );
318 
319 		return assertionConfig.getId();
320 	}
321 
322 	public PropertyExpansion[] getPropertyExpansions()
323 	{
324 		return null;
325 	}
326 
327 	public void setName( String name )
328 	{
329 		String oldLabel = getLabel();
330 
331 		String old = getName();
332 		assertionConfig.setName( name );
333 		notifyPropertyChanged( NAME_PROPERTY, old, name );
334 
335 		String label = getLabel();
336 		if( !oldLabel.equals( label ) )
337 		{
338 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
339 		}
340 	}
341 
342 	/*
343 	 * (non-Javadoc)
344 	 * 
345 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getLabel()
346 	 */
347 	public String getLabel()
348 	{
349 		String name = getName();
350 		if( isDisabled() )
351 			return name + " (disabled)";
352 		else
353 			return name;
354 	}
355 
356 	/*
357 	 * (non-Javadoc)
358 	 * 
359 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isDisabled()
360 	 */
361 	public boolean isDisabled()
362 	{
363 		return getConfig().getDisabled();
364 	}
365 
366 	public void setDisabled( boolean disabled )
367 	{
368 		String oldLabel = getLabel();
369 
370 		boolean oldDisabled = isDisabled();
371 		if( oldDisabled == disabled )
372 			return;
373 
374 		if( disabled )
375 		{
376 			getConfig().setDisabled( disabled );
377 		}
378 		else if( getConfig().isSetDisabled() )
379 		{
380 			getConfig().unsetDisabled();
381 		}
382 
383 		String label = getLabel();
384 		if( !oldLabel.equals( label ) )
385 		{
386 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
387 		}
388 
389 		notifyPropertyChanged( DISABLED_PROPERTY, oldDisabled, disabled );
390 	}
391 
392 	public ModelItem getParent()
393 	{
394 		return assertable.getModelItem();
395 	}
396 
397 	public boolean isValid()
398 	{
399 		return getStatus() == AssertionStatus.VALID;
400 	}
401 
402 	public boolean isFailed()
403 	{
404 		return getStatus() == AssertionStatus.FAILED;
405 	}
406 
407 	public void prepare( TestCaseRunner testRunner, TestCaseRunContext testRunContext ) throws Exception
408 	{
409 		assertionStatus = AssertionStatus.UNKNOWN;
410 	}
411 
412 	public void resolve( ResolveContext<?> context )
413 	{
414 	}
415 }