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             e.printStackTrace();
194 	         assertionStatus = AssertionStatus.FAILED;
195 	         assertionErrors = new com.eviware.soapui.model.testsuite.AssertionError[] 
196                            { new com.eviware.soapui.model.testsuite.AssertionError( e.getMessage() )};
197 	      }
198       }
199       
200       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
201       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
202       
203       return assertionStatus;
204    }
205    
206    protected abstract String internalAssertRequest( MessageExchange messageExchange, SubmitContext context ) throws AssertionException;
207 
208    /* (non-Javadoc)
209 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isConfigurable()
210 	 */
211    public boolean isConfigurable()
212    {
213       return configurable;
214    }
215    
216    /* (non-Javadoc)
217 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isClonable()
218 	 */
219    public boolean isClonable()
220    {
221       return cloneable;
222    }
223    
224    /* (non-Javadoc)
225 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#configure()
226 	 */
227    public boolean configure()
228    {
229    	return true;
230    }
231 
232    /* (non-Javadoc)
233 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getDescription()
234 	 */
235    public String getDescription()
236 	{
237 		return getConfig().getDescription();
238 	}
239 
240 	/* (non-Javadoc)
241 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getIcon()
242 	 */
243 	public ImageIcon getIcon()
244    {
245       switch( getStatus() )
246       {
247          case FAILED : return failedIcon; 
248          case UNKNOWN : return unknownIcon; 
249          case VALID : return validIcon;
250       }
251       
252       return null;
253    }
254 
255 	public void updateConfig(TestAssertionConfig config)
256 	{
257 		this.assertionConfig = config;
258 	}
259 
260 	public TestAssertionConfig getConfig()
261 	{
262 		return assertionConfig;
263 	}
264 	
265 	public Settings getSettings()
266 	{
267 		return assertable.getModelItem().getSettings();
268 	}
269 
270 	public void release()
271 	{
272 	}
273 	
274 	/* (non-Javadoc)
275 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getAssertable()
276 	 */
277 	public Assertable getAssertable()
278 	{
279 		return assertable;
280 	}
281 	
282 	public String getId()
283 	{
284 		if( !assertionConfig.isSetId())
285 			assertionConfig.setId( ModelSupport.generateModelItemID() );
286 		
287 		return assertionConfig.getId();
288 	}
289 	
290 	public PropertyExpansion[] getPropertyExpansions()
291 	{
292 		return null;
293 	}
294 	
295 	public void setName( String name )
296 	{
297 		String oldLabel = getLabel();
298    	
299 		String old = getName();
300       assertionConfig.setName( name );
301       notifyPropertyChanged( NAME_PROPERTY, old, name );
302 		
303 		String label = getLabel();
304 		if( !oldLabel.equals( label ))
305 		{
306 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
307 		}
308 	}
309 
310 	/* (non-Javadoc)
311 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#getLabel()
312 	 */
313 	public String getLabel()
314 	{
315 		String name = getName();
316 		if( isDisabled() )
317 			return name + " (disabled)";
318 		else
319 			return name;
320 	}
321 
322 	/* (non-Javadoc)
323 	 * @see com.eviware.soapui.impl.wsdl.teststeps.TestAssertion#isDisabled()
324 	 */
325 	public boolean isDisabled()
326 	{
327 		return getConfig().getDisabled();
328 	}
329 	
330 	public void setDisabled( boolean disabled )
331 	{
332 		String oldLabel = getLabel();
333 		
334 		boolean oldDisabled = isDisabled();
335 		if( oldDisabled == disabled )
336 			return;
337 		
338 		if( disabled )
339 		{
340 			getConfig().setDisabled( disabled );
341 		}
342 		else if( getConfig().isSetDisabled() )
343 		{
344 			getConfig().unsetDisabled();
345 		}
346 		
347 		String label = getLabel();
348 		if( !oldLabel.equals( label ))
349 		{
350 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
351 		}
352 		
353 		notifyPropertyChanged( DISABLED_PROPERTY, oldDisabled, disabled );
354 	}
355 
356 	public ModelItem getParent()
357 	{
358 		return assertable.getModelItem();
359 	}
360 	
361 	public boolean isValid()
362 	{
363 		return getStatus() == AssertionStatus.VALID;
364 	}
365 
366 	public boolean isFailed()
367 	{
368 		return getStatus() == AssertionStatus.FAILED;
369 	}
370 
371 	public void prepare( TestRunner testRunner, TestRunContext testRunContext ) throws Exception
372 	{
373 	}
374 
375    public void resolve( ResolveContext context )
376    {
377    }
378 }