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