View Javadoc

1    /*
2    *  soapUI, copyright (C) 2004-2007 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.RequestAssertionConfig;
20  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
21  import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
22  import com.eviware.soapui.impl.wsdl.support.assertions.Assertable.AssertionStatus;
23  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
24  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionException;
25  import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
26  import com.eviware.soapui.model.iface.SubmitContext;
27  import com.eviware.soapui.model.settings.Settings;
28  import com.eviware.soapui.model.support.AbstractModelItem;
29  import com.eviware.soapui.support.UISupport;
30  
31  /***
32   * Base class for WsdlAssertions
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public abstract class WsdlMessageAssertion extends AbstractModelItem
38  {
39  	private RequestAssertionConfig assertionConfig;
40     private final Assertable assertable;
41     private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
42     private com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError [] assertionErrors;
43     private ImageIcon validIcon;
44     private ImageIcon failedIcon;
45     private ImageIcon unknownIcon;
46     
47     public final static String STATUS_PROPERTY = WsdlMessageAssertion.class.getName() + "@status";  
48     public final static String ERRORS_PROPERTY = WsdlMessageAssertion.class.getName() + "@errors";  
49     public final static String CONFIGURATION_PROPERTY = WsdlMessageAssertion.class.getName() + "@configuration";
50  	private final boolean cloneable;
51  	private final boolean configurable;
52  	private final boolean allowMultiple;  
53     
54     protected WsdlMessageAssertion(RequestAssertionConfig assertionConfig, Assertable modelItem, 
55     			boolean cloneable, boolean configurable, boolean multiple )
56     {
57        this.assertionConfig = assertionConfig;
58        this.assertable = modelItem;
59  		this.cloneable = cloneable;
60  		this.configurable = configurable;
61  		this.allowMultiple = multiple;
62        
63        validIcon = UISupport.createImageIcon("/valid_assertion.gif");
64        failedIcon = UISupport.createImageIcon("/failed_assertion.gif");
65        unknownIcon = UISupport.createImageIcon("/unknown_assertion.gif");      
66     }
67     
68     public XmlObject getConfiguration()
69     {
70        if( null == assertionConfig.getConfiguration())
71        {
72           assertionConfig.addNewConfiguration();
73        }
74        
75        return assertionConfig.getConfiguration();
76     }
77  
78     public void setConfiguration( XmlObject configuration )
79     {
80  	   XmlObject oldConfig = assertionConfig.getConfiguration();
81        assertionConfig.setConfiguration( configuration );
82        notifyPropertyChanged( WsdlMessageAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
83     }
84     
85     public String getName()
86     {
87        return assertionConfig.isSetName() ? assertionConfig.getName() : 
88        	WsdlAssertionRegistry.getInstance().getAssertionNameForType( assertionConfig.getType());
89     }
90  
91     public void setName(String name)
92     {
93        String old = getName();
94        assertionConfig.setName( name );
95        notifyPropertyChanged( NAME_PROPERTY, old, name );
96     }
97     
98     public AssertionStatus getStatus()
99     {
100       return assertionStatus;
101    }
102 
103    public AssertionError[] getErrors()
104    {
105       return assertionErrors;
106    }
107 
108    public boolean isAllowMultiple()
109    {
110    	return allowMultiple;
111    }
112    
113    public AssertionStatus assertResponse( WsdlMessageExchange messageExchange, SubmitContext context)
114    {
115       AssertionStatus oldStatus = assertionStatus;
116       AssertionError[] oldErrors = getErrors();
117       ImageIcon oldIcon = getIcon();
118       
119       if( !messageExchange.hasResponse() )
120       {
121       	if( messageExchange.getOperation().isOneWay() )
122       	{
123       		assertionStatus = AssertionStatus.VALID;
124 	         assertionErrors = null;
125       	}
126       	else
127       	{
128       		assertionStatus = AssertionStatus.FAILED;
129       		assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
130       	                    { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError("null/empty response") };
131       	}
132       }
133       else
134       {
135 	      try
136 	      {
137 	      	internalAssertResponse( messageExchange, context );
138 	         assertionStatus = AssertionStatus.VALID;
139 	         assertionErrors = null;
140 	      }
141 	      catch ( AssertionException e )
142 	      {
143 	      	assertionStatus = AssertionStatus.FAILED;
144 	      	assertionErrors = e.getErrors();
145 	      }
146 	      catch (Throwable e)
147 	      {
148 	         assertionStatus = AssertionStatus.FAILED;
149 	         assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
150                            { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError( e.getMessage() )};
151 	      }
152       }
153       
154       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
155       notifyPropertyChanged( ERRORS_PROPERTY, oldErrors, assertionErrors );
156       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
157       
158       return assertionStatus;
159    }
160 
161    protected abstract String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
162    
163    public AssertionStatus assertRequest( WsdlMessageExchange messageExchange, SubmitContext context)
164    {
165       AssertionStatus oldStatus = assertionStatus;
166       ImageIcon oldIcon = getIcon();
167       
168       if( !messageExchange.hasRequest( true ) )
169       {
170       	assertionStatus = AssertionStatus.FAILED;
171       	assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
172       	                    { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError("null/empty response") };
173       }
174       else
175       {
176 	      try
177 	      {
178 	      	internalAssertRequest( messageExchange, context );
179 	         assertionStatus = AssertionStatus.VALID;
180 	         assertionErrors = null;
181 	      }
182 	      catch ( AssertionException e )
183 	      {
184 	      	assertionStatus = AssertionStatus.FAILED;
185 	      	assertionErrors = e.getErrors();
186 	      }
187 	      catch (Throwable e)
188 	      {
189 	         assertionStatus = AssertionStatus.FAILED;
190 	         assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] 
191                            { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError( e.getMessage() )};
192 	      }
193       }
194       
195       notifyPropertyChanged( STATUS_PROPERTY, oldStatus, assertionStatus );
196       notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
197       
198       return assertionStatus;
199    }
200    
201    protected abstract String internalAssertRequest(WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException;
202 
203    public boolean isConfigurable()
204    {
205       return configurable;
206    }
207    
208    public boolean isClonable()
209    {
210       return cloneable;
211    }
212    
213    public boolean configure()
214    {
215    	return true;
216    }
217 
218    public String getDescription()
219 	{
220 		return getConfig().getDescription();
221 	}
222 
223 	public ImageIcon getIcon()
224    {
225       switch( getStatus() )
226       {
227          case FAILED : return failedIcon; 
228          case UNKNOWN : return unknownIcon; 
229          case VALID : return validIcon;
230       }
231       
232       return null;
233    }
234 
235 	public void updateConfig(RequestAssertionConfig config)
236 	{
237 		this.assertionConfig = config;
238 	}
239 
240 	public RequestAssertionConfig getConfig()
241 	{
242 		return assertionConfig;
243 	}
244 	
245 	public Settings getSettings()
246 	{
247 		return assertable.getTestStep().getSettings();
248 	}
249 
250 	public void release()
251 	{
252 	}
253 	
254 	public Assertable getAssertable()
255 	{
256 		return assertable;
257 	}
258 }
259 
260