View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.TestCaseRunContext;
27  import com.eviware.soapui.model.testsuite.TestCaseRunner;
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; "
82  				+ maxRelativeErrors;
83  	}
84  
85  	public String assertResult( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestStepResult result,
86  			TestCaseRunner testRunner, TestCaseRunContext runContext )
87  	{
88  		TestStep step = result.getTestStep();
89  		if( targetStepMatches( step ) )
90  		{
91  			WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
92  			LoadTestLog loadTestLog = loadTest.getLoadTestLog();
93  
94  			int errorCount = loadTestLog.getErrorCount( step.getName() );
95  			if( maxAbsoluteErrors >= 0 && errorCount > maxAbsoluteErrors )
96  				loadTestRunner.fail( "Maximum number of errors [" + maxAbsoluteErrors + "] exceeded for step ["
97  						+ step.getName() + "]" );
98  
99  			int index = step.getTestCase().getIndexOfTestStep( step );
100 
101 			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
102 			long totalSteps = statisticsModel.getStatistic( index, Statistic.COUNT );
103 			float relativeErrors = ( float )errorCount / ( float )totalSteps;
104 
105 			if( maxRelativeErrors > 0 && relativeErrors > maxRelativeErrors )
106 				loadTestRunner.fail( "Maximum relative number of errors [" + maxRelativeErrors + "] exceeded for step ["
107 						+ step.getName() + "]" );
108 		}
109 
110 		return null;
111 	}
112 
113 	public String assertResults( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
114 			TestCaseRunContext runContext )
115 	{
116 		if( ALL_TEST_STEPS.equals( getTargetStep() ) )
117 		{
118 			WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
119 			LoadTestLog loadTestLog = loadTest.getLoadTestLog();
120 
121 			int errorCount = loadTestLog.getErrorCount( null );
122 			if( maxAbsoluteErrors >= 0 && errorCount > maxAbsoluteErrors )
123 				loadTestRunner.fail( "Maximum number of errors [" + maxAbsoluteErrors + "] exceeded" );
124 
125 			LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
126 			long totalSteps = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT );
127 			float relativeErrors = ( float )errorCount / ( float )totalSteps;
128 
129 			if( maxRelativeErrors > 0 && relativeErrors > maxRelativeErrors )
130 				loadTestRunner.fail( "Maximum relative number of errors [" + maxRelativeErrors + "] exceeded" );
131 		}
132 
133 		return null;
134 	}
135 
136 	public boolean configure()
137 	{
138 		if( dialog == null )
139 		{
140 			buildDialog();
141 		}
142 
143 		StringToStringMap values = new StringToStringMap();
144 
145 		values.put( NAME_FIELD, getName() );
146 		values.put( MAX_ABSOLUTE_ERRORS_FIELD, String.valueOf( maxAbsoluteErrors ) );
147 		values.put( MAX_RELATIVE_ERRORS_FIELD, String.valueOf( maxRelativeErrors ) );
148 		values.put( TEST_STEP_FIELD, getTargetStep() );
149 
150 		dialog.setOptions( TEST_STEP_FIELD, getTargetStepOptions( true ) );
151 		values = dialog.show( values );
152 
153 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
154 		{
155 			try
156 			{
157 				maxAbsoluteErrors = Integer.parseInt( values.get( MAX_ABSOLUTE_ERRORS_FIELD ) );
158 				maxRelativeErrors = Float.parseFloat( values.get( MAX_RELATIVE_ERRORS_FIELD ) );
159 				setTargetStep( values.get( TEST_STEP_FIELD ) );
160 				setName( values.get( NAME_FIELD ) );
161 			}
162 			catch( Exception e )
163 			{
164 				UISupport.showErrorMessage( e.getMessage() );
165 			}
166 
167 			updateConfiguration();
168 
169 			return true;
170 		}
171 
172 		return false;
173 	}
174 
175 	private void buildDialog()
176 	{
177 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Max Errors Assertion" );
178 		XForm form = builder.createForm( "Basic" );
179 
180 		form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
181 		form.addTextField( MAX_ABSOLUTE_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
182 		form.addTextField( MAX_RELATIVE_ERRORS_FIELD, "Relative maximum number of errors before failing (0-1)",
183 				FieldType.TEXT );
184 		form.addComboBox( TEST_STEP_FIELD, new String[0], "TestStep to assert" );
185 
186 		dialog = builder.buildDialog(
187 				builder.buildOkCancelHelpActions( HelpUrls.MAX_ERRORS_LOAD_TEST_ASSERTION_HELP_URL ),
188 				"Specify options for this Max Errors Assertion", UISupport.OPTIONS_ICON );
189 	}
190 
191 	protected void updateConfiguration()
192 	{
193 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
194 
195 		builder.add( NAME_ELEMENT, getName() );
196 		builder.add( MAX_ABSOLUTE_ERRORS_ELEMENT, maxAbsoluteErrors );
197 		builder.add( MAX_RELATIVE_ERRORS_ELEMENT, maxRelativeErrors );
198 		builder.add( TEST_STEP_ELEMENT, getTargetStep() );
199 
200 		setConfiguration( builder.finish() );
201 	}
202 }