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