1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import java.awt.event.ActionEvent;
16
17 import javax.swing.AbstractAction;
18 import javax.swing.Action;
19 import javax.swing.ImageIcon;
20 import javax.swing.JOptionPane;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.xmlbeans.XmlObject;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.config.RequestAssertionConfig;
28 import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionException;
29 import com.eviware.soapui.model.iface.Request;
30 import com.eviware.soapui.model.support.AbstractModelItem;
31 import com.eviware.soapui.model.tree.SoapUITreeNode;
32
33 /***
34 * Base class for WsdlAssertions
35 *
36 * @author Ole.Matzura
37 */
38
39 public abstract class WsdlAssertion extends AbstractModelItem
40 {
41 private RequestAssertionConfig assertionConfig;
42 private final WsdlTestRequest request;
43 private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
44 private com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError [] assertionErrors;
45 private final static Log log = LogFactory.getLog( WsdlAssertion.class );
46 private ImageIcon validIcon;
47 private ImageIcon failedIcon;
48 private ImageIcon unknownIcon;
49
50 public final static String STATUS_PROPERTY = WsdlAssertion.class.getName() + "@status";
51 public final static String CONFIGURATION_PROPERTY = WsdlAssertion.class.getName() + "@configuration";
52
53 public enum AssertionStatus { UNKNOWN, VALID, FAILED, NONE };
54
55 public WsdlAssertion(RequestAssertionConfig assertionConfig, WsdlTestRequest request)
56 {
57 this.assertionConfig = assertionConfig;
58 this.request = request;
59
60 addAction( new RenameAssertionAction() );
61 addAction( new RemoveAssertionAction() );
62 if( isConfigurable() )
63 addAction( new ConfigureAssertionAction() );
64
65 validIcon = SoapUI.createImageIcon("/valid_assertion.gif");
66 failedIcon = SoapUI.createImageIcon("/failed_assertion.gif");
67 unknownIcon = SoapUI.createImageIcon("/unknown_assertion.gif");
68 }
69
70 protected XmlObject getConfiguration()
71 {
72 if( null == assertionConfig.getConfiguration())
73 {
74 assertionConfig.addNewConfiguration();
75 }
76
77 return assertionConfig.getConfiguration();
78 }
79
80 protected void setConfiguration( XmlObject configuration )
81 {
82 XmlObject oldConfig = assertionConfig.getConfiguration();
83 assertionConfig.setConfiguration( configuration );
84 notifyPropertyChanged( WsdlAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
85 }
86
87 public String getName()
88 {
89 return assertionConfig.isSetName() ? assertionConfig.getName() : assertionConfig.getType();
90 }
91
92 public void setName(String name)
93 {
94 String old = getName();
95 assertionConfig.setName( name );
96 notifyPropertyChanged( NAME_PROPERTY, old, name );
97 }
98
99 public class RenameAssertionAction extends AbstractAction
100 {
101 public RenameAssertionAction()
102 {
103 super( "Rename" );
104 putValue( Action.SHORT_DESCRIPTION, "Renames this assertion" );
105 }
106
107 public void actionPerformed(ActionEvent e)
108 {
109 String name = (String) JOptionPane.showInputDialog( SoapUI.getInstance().getFrame(),
110 "Specify name for this assertion", "Rename Assertion", JOptionPane.QUESTION_MESSAGE, null,
111 null, WsdlAssertion.this.getName() );
112 if( name == null || name.equals( WsdlAssertion.this.getName() )) return;
113
114 setName( name );
115 }
116 }
117
118 public class RemoveAssertionAction extends AbstractAction
119 {
120 public RemoveAssertionAction()
121 {
122 super( "Remove" );
123 putValue( Action.SHORT_DESCRIPTION, "Removes this assertion from its request" );
124 }
125
126 public void actionPerformed(ActionEvent e)
127 {
128 if( JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog( SoapUI.getInstance().getFrame(),
129 "Remove assertion [" + WsdlAssertion.this.getName() + "] from request [" +
130 getRequest().getName() + "]", "Remove Assertion", JOptionPane.OK_CANCEL_OPTION ))
131 {
132 return;
133 }
134
135 request.removeAssertion( WsdlAssertion.this );
136 }
137 }
138
139 public class ConfigureAssertionAction extends AbstractAction
140 {
141 public ConfigureAssertionAction()
142 {
143 super( "Configure" );
144 putValue( Action.SHORT_DESCRIPTION, "Configures this assertion" );
145 }
146
147 public void actionPerformed(ActionEvent e)
148 {
149 configure();
150 }
151 }
152
153 public AssertionStatus getStatus()
154 {
155 return assertionStatus;
156 }
157
158 public Request getRequest()
159 {
160 return request;
161 }
162
163 public com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError [] getErrors()
164 {
165 return assertionErrors;
166 }
167
168 public AssertionStatus assertResponse(WsdlTestRequest request)
169 {
170 AssertionStatus oldStatus = assertionStatus;
171 ImageIcon oldIcon = getIcon();
172
173 if( request.getResponseContent() == null || request.getResponseContent().trim().length() == 0 )
174 {
175 assertionStatus = AssertionStatus.FAILED;
176 assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[]
177 { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError("null/empty response") };
178 }
179 else
180 {
181 try
182 {
183 assertRequest( request );
184 assertionStatus = AssertionStatus.VALID;
185 assertionErrors = null;
186 }
187 catch ( AssertionException e )
188 {
189 assertionStatus = AssertionStatus.FAILED;
190 assertionErrors = e.getErrors();
191 }
192 catch (Throwable e)
193 {
194 assertionStatus = AssertionStatus.FAILED;
195 assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[]
196 { new com.eviware.soapui.impl.wsdl.teststeps.assertions.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 assertRequest(WsdlTestRequest request) throws AssertionException;
207
208 public boolean isConfigurable()
209 {
210 return false;
211 }
212
213 public void configure()
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 protected SoapUITreeNode createTreeNode()
229 {
230 return null;
231 }
232
233 public void updateConfig(RequestAssertionConfig config)
234 {
235 this.assertionConfig = config;
236 }
237 }
238
239