View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  	@SuppressWarnings( "unused" )
48  	private final static Logger log = Logger.getLogger( 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  	public void updateConfiguration( LoadTestAssertionConfig configuration )
79  	{
80  		assertionConfig = configuration;
81  	}
82  
83  	protected void setConfiguration( XmlObject configuration )
84  	{
85  		XmlObject oldConfig = assertionConfig.getConfiguration();
86  		assertionConfig.setConfiguration( configuration );
87  		propertyChangeSupport.firePropertyChange( AbstractLoadTestAssertion.CONFIGURATION_PROPERTY, oldConfig,
88  				configuration );
89  	}
90  
91  	public String getName()
92  	{
93  		return assertionConfig.isSetName() ? assertionConfig.getName() : assertionConfig.getType();
94  	}
95  
96  	public void setName( String name )
97  	{
98  		String old = getName();
99  		assertionConfig.setName( name );
100 		propertyChangeSupport.firePropertyChange( NAME_PROPERTY, old, name );
101 	}
102 
103 	public WsdlLoadTest getLoadTest()
104 	{
105 		return loadTest;
106 	}
107 
108 	public class RenameAssertionAction extends AbstractAction
109 	{
110 		public RenameAssertionAction()
111 		{
112 			super( "Rename" );
113 			putValue( Action.SHORT_DESCRIPTION, "Renames this assertion" );
114 		}
115 
116 		public void actionPerformed( ActionEvent e )
117 		{
118 			String name = UISupport.prompt( "Specify name for this assertion", "Rename Assertion",
119 					AbstractLoadTestAssertion.this.getName() );
120 			if( name == null || name.equals( AbstractLoadTestAssertion.this.getName() ) )
121 				return;
122 
123 			setName( name );
124 		}
125 	}
126 
127 	public class ConfigureAssertionAction extends AbstractAction
128 	{
129 		public ConfigureAssertionAction()
130 		{
131 			super( "Configure" );
132 			putValue( Action.SHORT_DESCRIPTION, "Configures this assertion" );
133 		}
134 
135 		public void actionPerformed( ActionEvent e )
136 		{
137 			( ( Configurable )AbstractLoadTestAssertion.this ).configure();
138 		}
139 	}
140 
141 	public ImageIcon getIcon()
142 	{
143 		return icon;
144 	}
145 
146 	public void addPropertyChangeListener( PropertyChangeListener listener )
147 	{
148 		propertyChangeSupport.addPropertyChangeListener( listener );
149 	}
150 
151 	public void addPropertyChangeListener( String propertyName, PropertyChangeListener listener )
152 	{
153 		propertyChangeSupport.addPropertyChangeListener( propertyName, listener );
154 	}
155 
156 	public void removePropertyChangeListener( PropertyChangeListener listener )
157 	{
158 		propertyChangeSupport.removePropertyChangeListener( listener );
159 	}
160 
161 	public void removePropertyChangeListener( String propertyName, PropertyChangeListener listener )
162 	{
163 		propertyChangeSupport.removePropertyChangeListener( propertyName, listener );
164 	}
165 
166 	protected String returnErrorOrFail( String message, int maxErrors, LoadTestRunner testRunner,
167 			LoadTestRunContext context )
168 	{
169 		String propertyKey = getClass().getName() + hashCode();
170 		Long errorCount = ( Long )context.getProperty( propertyKey );
171 
172 		if( errorCount == null )
173 		{
174 			errorCount = 1L;
175 		}
176 		else
177 		{
178 			errorCount = new Long( errorCount.longValue() + 1 );
179 		}
180 
181 		if( maxErrors >= 0 && errorCount >= maxErrors )
182 		{
183 			testRunner.fail( "Maximum number of errors [" + maxErrors + "] for assertion [" + getName() + "] exceeded" );
184 		}
185 
186 		context.setProperty( propertyKey, errorCount );
187 
188 		return message;
189 	}
190 
191 	public String getTargetStep()
192 	{
193 		return testStepName;
194 	}
195 
196 	public void setTargetStep( String name )
197 	{
198 		testStepName = name;
199 		initTestStep();
200 	}
201 
202 	abstract protected void updateConfiguration();
203 
204 	protected boolean targetStepMatches( TestStep testStep )
205 	{
206 		return testStepName == null || testStepName.equals( ANY_TEST_STEP ) || testStep.getName().equals( testStepName );
207 	}
208 
209 	protected String[] getTargetStepOptions( boolean includeAll )
210 	{
211 		if( includeAll )
212 			return ModelSupport.getNames( new String[] { ANY_TEST_STEP, ALL_TEST_STEPS }, getLoadTest().getTestCase()
213 					.getTestStepList() );
214 		else
215 			return ModelSupport.getNames( new String[] { ANY_TEST_STEP }, getLoadTest().getTestCase().getTestStepList() );
216 	}
217 
218 	private void initTestStep()
219 	{
220 		if( testStep != null )
221 		{
222 			testStep.removePropertyChangeListener( testStepPropertyChangeListener );
223 		}
224 
225 		testStep = getLoadTest().getTestCase().getTestStepByName( testStepName );
226 		if( testStep != null )
227 		{
228 			testStep.addPropertyChangeListener( TestStep.NAME_PROPERTY, testStepPropertyChangeListener );
229 		}
230 	}
231 
232 	public void release()
233 	{
234 		if( testStep != null )
235 		{
236 			testStep.removePropertyChangeListener( testStepPropertyChangeListener );
237 		}
238 
239 		loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
240 	}
241 
242 	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
243 	{
244 		public void testStepRemoved( TestStep removedTestStep, int index )
245 		{
246 			if( removedTestStep.getName().equals( testStepName )
247 					&& removedTestStep.getTestCase() == testStep.getTestCase() )
248 			{
249 				testStepName = ANY_TEST_STEP;
250 				updateConfiguration();
251 			}
252 		}
253 	}
254 
255 	private final class TestStepPropertyChangeListener implements PropertyChangeListener
256 	{
257 		public void propertyChange( PropertyChangeEvent evt )
258 		{
259 			testStepName = evt.getNewValue().toString();
260 			updateConfiguration();
261 		}
262 	}
263 }