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 org.apache.xmlbeans.XmlObject;
    16
    17  import com.eviware.soapui.config.LoadTestAssertionConfig;
    18  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
    19  import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
    20  import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
    21  import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLog;
    22  import com.eviware.soapui.impl.wsdl.support.Configurable;
    23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    24  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
    25  import com.eviware.soapui.model.testsuite.LoadTestRunner;
    26  import com.eviware.soapui.model.testsuite.TestRunContext;
    27  import com.eviware.soapui.model.testsuite.TestRunner;
    28  import com.eviware.soapui.model.testsuite.TestStep;
    29  import com.eviware.soapui.model.testsuite.TestStepResult;
    30  import com.eviware.soapui.support.UISupport;
    31  import com.eviware.soapui.support.types.StringToStringMap;
    32  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
    33  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
    34  import com.eviware.x.form.XForm;
    35  import com.eviware.x.form.XFormDialog;
    36  import com.eviware.x.form.XFormDialogBuilder;
    37  import com.eviware.x.form.XFormFactory;
    38  import com.eviware.x.form.XForm.FieldType;
    39
    40  /***
    41   * LoadTestAssertion for asserting the maximum number of total assertion errors
    42   *
    43   * @author Ole.Matzura
    44   */
    45
    46  public class MaxErrorsAssertion extends AbstractLoadTestAssertion implements Configurable
    47  {
    48  	private static final String NAME_FIELD = "Name";
    49  	private static final String NAME_ELEMENT = "name";
    50  	private static final String MAX_ABSOLUTE_ERRORS_ELEMENT = "max-absolute-errors";
    51  	private static final String MAX_ABSOLUTE_ERRORS_FIELD = "Max Absolute Errors";
    52  	private static final String MAX_RELATIVE_ERRORS_ELEMENT = "max-relative-errors";
    53  	private static final String MAX_RELATIVE_ERRORS_FIELD = "Max Relative Errors";
    54
    55  	private float maxRelativeErrors;
    56  	private int maxAbsoluteErrors;
    57  	private XFormDialog dialog;
    58  	public static final String MAX_ERRORS_TYPE = "Max Errors";
    59
    60  	public MaxErrorsAssertion(LoadTestAssertionConfig assertionConfig, WsdlLoadTest loadTest)
    61  	{
    62  		super(assertionConfig, loadTest);
    63
    64  		init(assertionConfig);
    65  		initIcon( "/errors_loadtest_assertion.gif" );
    66  	}
    67
    68  	private void init(LoadTestAssertionConfig assertionConfig)
    69  	{
    70  		XmlObject configuration = assertionConfig.getConfiguration();
    71  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( configuration );
    72
    73  		setName( reader.readString( MaxErrorsAssertion.NAME_ELEMENT, "Max Errors" ));
    74  		maxAbsoluteErrors = reader.readInt( MAX_ABSOLUTE_ERRORS_ELEMENT, 100 );
    75  		maxRelativeErrors = reader.readFloat( MAX_RELATIVE_ERRORS_ELEMENT, (float) 0.2 );
    76  		setTargetStep( reader.readString( TEST_STEP_ELEMENT, ALL_TEST_STEPS ));
    77  	}
    78
    79  	public String getDescription()
    80  	{
    81  		return "testStep: " + getTargetStep() + ", maxAbsoluteErrors: " + maxAbsoluteErrors + ", maxRelativeErrors; " + maxRelativeErrors;
    82  	}
    83
    84  	public String assertResult(LoadTestRunner loadTestRunner, LoadTestRunContext context, TestStepResult result, TestRunner testRunner, TestRunContext runContext)
    85  	{
    86  		TestStep step = result.getTestStep();
    87  		if( targetStepMatches( step ) )
    88  		{
    89  			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
    90  			LoadTestLog loadTestLog = loadTest.getLoadTestLog();
    91
    92  			int errorCount = loadTestLog.getErrorCount( step.getName() );
    93  			if( maxAbsoluteErrors >= 0 && errorCount > maxAbsoluteErrors )
    94  				loadTestRunner.fail( "Maximum number of errors [" + maxAbsoluteErrors + "] exceeded for step [" + step.getName() + "]" );
    95
    96  			int index = step.getTestCase().getIndexOfTestStep( step );
    97
    98  			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
    99  			long totalSteps = statisticsModel.getStatistic( index, Statistic.COUNT );
    100 			float relativeErrors = (float)errorCount / (float)totalSteps;
    101
    102 			if( maxRelativeErrors > 0 && relativeErrors > maxRelativeErrors )
    103 				loadTestRunner.fail( "Maximum relative number of errors [" + maxRelativeErrors + "] exceeded for step [" + step.getName() + "]" );
    104 		}
    105
    106 		return null;
    107 	}
    108
    109 	public String assertResults(LoadTestRunner loadTestRunner, LoadTestRunContext context, TestRunner testRunner, TestRunContext runContext)
    110 	{
    111 		if( ALL_TEST_STEPS.equals( getTargetStep() ) )
    112 		{
    113 			WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
    114 			LoadTestLog loadTestLog = loadTest.getLoadTestLog();
    115
    116 			int errorCount = loadTestLog.getErrorCount( null );
    117 			if( maxAbsoluteErrors >= 0 && errorCount > maxAbsoluteErrors )
    118 				loadTestRunner.fail( "Maximum number of errors [" + maxAbsoluteErrors + "] exceeded" );
    119
    120 			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
    121 			long totalSteps = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT );
    122 			float relativeErrors = (float)errorCount / (float)totalSteps;
    123
    124 			if( maxRelativeErrors > 0 && relativeErrors > maxRelativeErrors )
    125 				loadTestRunner.fail( "Maximum relative number of errors [" + maxRelativeErrors + "] exceeded" );
    126 		}
    127
    128 		return null;
    129 	}
    130
    131 	public boolean configure()
    132 	{
    133 		if( dialog == null )
    134 		{
    135 			buildDialog();
    136 		}
    137
    138 		StringToStringMap values = new StringToStringMap();
    139
    140 		values.put( NAME_FIELD, getName() );
    141 		values.put( MAX_ABSOLUTE_ERRORS_FIELD, String.valueOf( maxAbsoluteErrors ));
    142 		values.put( MAX_RELATIVE_ERRORS_FIELD, String.valueOf( maxRelativeErrors ));
    143 		values.put( TEST_STEP_FIELD, getTargetStep() );
    144
    145 		dialog.setOptions( TEST_STEP_FIELD, getTargetStepOptions( true ) );
    146 		values = dialog.show( values );
    147
    148 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
    149 		{
    150 			try
    151 			{
    152 				maxAbsoluteErrors = Integer.parseInt( values.get( MAX_ABSOLUTE_ERRORS_FIELD ));
    153 				maxRelativeErrors = Float.parseFloat( values.get( MAX_RELATIVE_ERRORS_FIELD ));
    154 				setTargetStep( values.get( TEST_STEP_FIELD ));
    155 				setName( values.get( NAME_FIELD ));
    156 			}
    157 			catch( Exception e )
    158 			{
    159 				UISupport.showErrorMessage( e.getMessage() );
    160 			}
    161
    162 			updateConfiguration();
    163
    164 			return true;
    165 		}
    166
    167 		return false;
    168 	}
    169
    170 	private void buildDialog()
    171 	{
    172 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Max Errors Assertion" );
    173 	   XForm form = builder.createForm( "Basic" );
    174
    175 	   form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
    176 	   form.addTextField( MAX_ABSOLUTE_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
    177 	   form.addTextField( MAX_RELATIVE_ERRORS_FIELD, "Relative maximum number of errors before failing (0-1)", FieldType.TEXT );
    178 	   form.addComboBox( TEST_STEP_FIELD, new String[0],  "TestStep to assert" );
    179
    180 	   dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.MAX_ERRORS_LOAD_TEST_ASSERTION_HELP_URL),
    181 	   		"Specify options for this Max Errors Assertion", UISupport.OPTIONS_ICON );
    182 	}
    183
    184 	protected void updateConfiguration()
    185 	{
    186 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    187
    188 		builder.add( NAME_ELEMENT, getName() );
    189 		builder.add( MAX_ABSOLUTE_ERRORS_ELEMENT, maxAbsoluteErrors );
    190 		builder.add( MAX_RELATIVE_ERRORS_ELEMENT, maxRelativeErrors );
    191 		builder.add( TEST_STEP_ELEMENT, getTargetStep() );
    192
    193 		setConfiguration( builder.finish() );
    194 	}
    195 }