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