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