View Javadoc

1    /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.loadtest.assertions;
14  
15  import java.awt.event.ActionEvent;
16  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;
18  import java.beans.PropertyChangeSupport;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.ImageIcon;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.xmlbeans.XmlObject;
27  
28  import com.eviware.soapui.config.LoadTestAssertionConfig;
29  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
30  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
31  import com.eviware.soapui.impl.wsdl.support.Configurable;
32  import com.eviware.soapui.model.support.ModelSupport;
33  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
34  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
35  import com.eviware.soapui.model.testsuite.LoadTestRunner;
36  import com.eviware.soapui.model.testsuite.TestStep;
37  import com.eviware.soapui.support.UISupport;
38  
39  /***
40   * Base class for LoadTestAssertions
41   * 
42   * @author Ole.Matzura
43   */
44  
45  public abstract class AbstractLoadTestAssertion implements LoadTestAssertion
46  {
47     private LoadTestAssertionConfig assertionConfig;
48     private final static Log log = LogFactory.getLog( AbstractLoadTestAssertion.class );
49     private ImageIcon icon;
50  	private final WsdlLoadTest loadTest;
51     private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( this );
52  	private String testStepName;
53  	private TestStep testStep;
54  	private TestStepPropertyChangeListener testStepPropertyChangeListener = new TestStepPropertyChangeListener();
55  	private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
56     
57     protected static final String TEST_STEP_ELEMENT = "test-step";
58     protected static final String TEST_STEP_FIELD = "TestStep";
59     
60     public AbstractLoadTestAssertion(LoadTestAssertionConfig assertionConfig, WsdlLoadTest loadTest)
61     {
62        this.assertionConfig = assertionConfig;
63  		this.loadTest = loadTest;
64  		
65  		loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener );
66     }
67     
68     public void initIcon( String url )
69     {
70        icon = UISupport.createImageIcon( url );      
71     }
72     
73     public LoadTestAssertionConfig getConfiguration()
74     {
75        return assertionConfig;
76     }
77  
78     protected void setConfiguration( XmlObject configuration )
79     {
80  	   XmlObject oldConfig = assertionConfig.getConfiguration();
81        assertionConfig.setConfiguration( configuration );
82        propertyChangeSupport.firePropertyChange( AbstractLoadTestAssertion.CONFIGURATION_PROPERTY, oldConfig, configuration );
83     }
84     
85     public String getName()
86     {
87        return assertionConfig.isSetName() ? assertionConfig.getName() : assertionConfig.getType();
88     }
89  
90     public void setName(String name)
91     {
92        String old = getName();
93        assertionConfig.setName( name );
94        propertyChangeSupport.firePropertyChange( NAME_PROPERTY, old, name );
95     }
96     
97     public WsdlLoadTest getLoadTest()
98     {
99     	return loadTest;
100    }
101    
102    public class RenameAssertionAction extends AbstractAction
103    {
104       public RenameAssertionAction()
105       {
106          super( "Rename" );
107          putValue( Action.SHORT_DESCRIPTION, "Renames this assertion" );
108       }
109       
110       public void actionPerformed(ActionEvent e)
111 		{
112          String name = UISupport.prompt("Specify name for this assertion", "Rename Assertion", AbstractLoadTestAssertion.this.getName() );
113          if( name == null || name.equals( AbstractLoadTestAssertion.this.getName() )) return;
114          
115          setName( name );
116       }
117    }
118    
119    public class ConfigureAssertionAction extends AbstractAction
120    {
121       public ConfigureAssertionAction()
122       {
123          super( "Configure" );
124          putValue( Action.SHORT_DESCRIPTION, "Configures this assertion" );
125       }
126       
127       public void actionPerformed(ActionEvent e)
128 		{
129          ((Configurable)AbstractLoadTestAssertion.this).configure();
130       }
131    }
132 
133    public ImageIcon getIcon()
134    {
135       return icon;
136    }
137 
138 	public void addPropertyChangeListener(PropertyChangeListener listener)
139 	{
140 		propertyChangeSupport.addPropertyChangeListener( listener );
141 	}
142 
143 	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
144 	{
145 		propertyChangeSupport.addPropertyChangeListener( propertyName, listener );
146 	}
147 
148 	public void removePropertyChangeListener(PropertyChangeListener listener)
149 	{
150 		propertyChangeSupport.removePropertyChangeListener( listener );
151 	}
152 
153 	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
154 	{
155 		propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
156 	}
157 	
158 	protected String returnErrorOrFail(String message, int maxErrors, LoadTestRunner testRunner, LoadTestRunContext context)
159 	{
160 		String propertyKey = getClass().getName() + hashCode();
161 		Long errorCount = (Long) context.getProperty( propertyKey );
162 		
163 		if( errorCount == null )
164 		{
165 			errorCount = 1L;
166 		}
167 		else
168 		{
169 			errorCount = new Long( errorCount.longValue() + 1 );
170 		}
171 		
172 		if( maxErrors >= 0 && errorCount >= maxErrors )
173 		{
174 			testRunner.fail( "Maximum number of errors [" + maxErrors + "] for assertion [" + getName() + "] exceeded" );
175 		}
176 				
177 		context.setProperty( propertyKey, errorCount );	
178 		
179 		return message;
180 	}
181 	
182 	public String getTargetStep()
183 	{
184 		return testStepName;
185 	}
186 	
187 	public void setTargetStep(String name)
188 	{
189 		testStepName = name;
190 		initTestStep();
191 	}
192 	
193 	abstract protected void updateConfiguration();
194 
195 	protected boolean targetStepMatches( TestStep testStep )
196 	{
197 		return testStepName == null || testStepName.equals( ANY_TEST_STEP ) || testStep.getName().equals( testStepName );
198 	}
199 
200 	protected String[] getTargetStepOptions( boolean includeAll )
201 	{
202 		if( includeAll )
203 			return ModelSupport.getNames( new String []{ ANY_TEST_STEP, ALL_TEST_STEPS }, 
204 					getLoadTest().getTestCase().getTestStepList() );
205 		else 
206 			return ModelSupport.getNames( new String []{ ANY_TEST_STEP }, 
207 					getLoadTest().getTestCase().getTestStepList() );
208 	}
209 	
210 	private void initTestStep()
211 	{
212 		if( testStep != null )
213 		{
214 			testStep.removePropertyChangeListener( testStepPropertyChangeListener );
215 		}
216 		
217 		testStep = getLoadTest().getTestCase().getTestStepByName( testStepName );
218 		if( testStep != null )
219 		{
220 			testStep.addPropertyChangeListener( TestStep.NAME_PROPERTY, testStepPropertyChangeListener );
221 		}
222 	}
223 
224 	public void release()
225 	{
226 		if( testStep != null )
227 		{
228 			testStep.removePropertyChangeListener( testStepPropertyChangeListener );
229 			loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
230 		}
231 	}
232 	
233 	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
234 	{
235 		public void testStepRemoved(TestStep removedTestStep, int index)
236 		{
237 			if( removedTestStep.getName().equals( testStepName ) && 
238 				 removedTestStep.getTestCase() == testStep.getTestCase() )
239 			{
240 				testStepName = ANY_TEST_STEP;
241 				updateConfiguration();
242 			}
243 		}
244 	}
245 
246 	private final class TestStepPropertyChangeListener implements PropertyChangeListener
247 	{
248 		public void propertyChange(PropertyChangeEvent evt)
249 		{
250 			testStepName = evt.getNewValue().toString();
251 			updateConfiguration();
252 		}
253 	}
254 }
255 
256