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.teststeps;
14  
15  import java.awt.Dialog.ModalityType;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.apache.log4j.Logger;
20  
21  import com.eviware.soapui.SoapUI;
22  import com.eviware.soapui.config.ManualTestStepConfig;
23  import com.eviware.soapui.config.TestStepConfig;
24  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
25  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
26  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
27  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
29  import com.eviware.soapui.model.support.DefaultTestStepProperty;
30  import com.eviware.soapui.model.support.TestStepBeanProperty;
31  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
32  import com.eviware.soapui.model.testsuite.TestCaseRunner;
33  import com.eviware.soapui.model.testsuite.TestStepResult;
34  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
35  import com.eviware.soapui.support.StringUtils;
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.x.form.XFormDialog;
38  import com.eviware.x.form.XFormOptionsField;
39  import com.eviware.x.form.support.ADialogBuilder;
40  import com.eviware.x.form.support.AField;
41  import com.eviware.x.form.support.AForm;
42  import com.eviware.x.form.support.AField.AFieldType;
43  import com.eviware.x.impl.swing.JFormDialog;
44  
45  /***
46   * 
47   * @author nebojsa.tasic
48   */
49  
50  public class ManualTestStep extends WsdlTestStepWithProperties implements PropertyExpansionContainer
51  
52  {
53  	@SuppressWarnings( "unused" )
54  	private final static Logger log = Logger.getLogger( WsdlTestRequestStep.class );
55  	protected ManualTestStepConfig manualTestStepConfig;
56  	private ManualTestStepResult testStepResult;
57  	public final static String MANUAL_STEP = ManualTestStep.class.getName() + "@manualstep";
58  	public static final String STATUS_PROPERTY = WsdlTestRequest.class.getName() + "@status";
59  	private final boolean forLoadTest;
60  
61  	public ManualTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
62  	{
63  		super( testCase, config, true, forLoadTest );
64  		this.forLoadTest = forLoadTest;
65  
66  		if( !forLoadTest )
67  		{
68  			setIcon( UISupport.createImageIcon( "/manualteststep.gif" ) );
69  		}
70  
71  		if( getConfig().getConfig() != null )
72  		{
73  			manualTestStepConfig = ( ManualTestStepConfig )getConfig().getConfig().changeType( ManualTestStepConfig.type );
74  		}
75  		else
76  		{
77  			manualTestStepConfig = ( ManualTestStepConfig )getConfig().addNewConfig().changeType(
78  					ManualTestStepConfig.type );
79  		}
80  
81  		addProperty( new DefaultTestStepProperty( "Result", true, new DefaultTestStepProperty.PropertyHandlerAdapter()
82  		{
83  			@Override
84  			public String getValue( DefaultTestStepProperty property )
85  			{
86  				return getLastResult() == null ? null : getLastResult().getResult();
87  			}
88  		}, this ) );
89  
90  		addProperty( new TestStepBeanProperty( "ExpectedResult", false, this, "expectedResult", this ) );
91  	}
92  
93  	protected ManualTestStepResult getLastResult()
94  	{
95  		return testStepResult;
96  	}
97  
98  	public ManualTestStepConfig getManualTestStepConfig()
99  	{
100 		return manualTestStepConfig;
101 	}
102 
103 	@Override
104 	public WsdlTestStep clone( WsdlTestCase targetTestCase, String name )
105 	{
106 		beforeSave();
107 
108 		TestStepConfig config = ( TestStepConfig )getConfig().copy();
109 		ManualTestStep result = ( ManualTestStep )targetTestCase.addTestStep( config );
110 
111 		return result;
112 	}
113 
114 	@Override
115 	public void release()
116 	{
117 		super.release();
118 	}
119 
120 	public TestStepResult run( TestCaseRunner runner, TestCaseRunContext runContext )
121 	{
122 		testStepResult = new ManualTestStepResult( this );
123 		testStepResult.startTimer();
124 
125 		if( !forLoadTest && !UISupport.isHeadless() && !SoapUI.isCommandLine() )
126 		{
127 			XFormDialog dialog = ADialogBuilder.buildDialog( Form.class );
128 			dialog.setSize( 450, 550 );
129 			( ( JFormDialog )dialog ).getDialog().setModalityType( ModalityType.MODELESS );
130 
131 			dialog.setValue( Form.DESCRIPTION, runContext.expand( getDescription() ) );
132 			dialog.setValue( Form.EXPECTED_DESULT, runContext.expand( getExpectedResult() ) );
133 			dialog.setValue( Form.STATUS, "Unknown" );
134 
135 			UISupport.select( this );
136 
137 			while( !dialog.show() )
138 			{
139 				if( UISupport.confirm( "Are you sure? This will stop the entire test", "Cancel TestStep" ) )
140 				{
141 					testStepResult.setStatus( TestStepStatus.CANCELED );
142 					runner.cancel( "Canceled by user" );
143 					break;
144 				}
145 			}
146 
147 			if( dialog.getValue( Form.STATUS ).equals( "Pass" ) )
148 				testStepResult.setStatus( TestStepStatus.OK );
149 			else if( dialog.getValue( Form.STATUS ).equals( "Fail" ) )
150 				testStepResult.setStatus( TestStepStatus.FAILED );
151 
152 			String result = dialog.getValue( Form.RESULT );
153 			if( StringUtils.hasContent( result ) )
154 				testStepResult.setResult( result );
155 
156 			testStepResult.setUrls( ( ( XFormOptionsField )dialog.getFormField( Form.URLS ) ).getOptions() );
157 
158 			dialog.release();
159 		}
160 
161 		testStepResult.stopTimer();
162 		return testStepResult;
163 	}
164 
165 	@Override
166 	public boolean cancel()
167 	{
168 		return true;
169 	}
170 
171 	@Override
172 	public String getDefaultSourcePropertyName()
173 	{
174 		return "Result";
175 	}
176 
177 	@Override
178 	public String getDefaultTargetPropertyName()
179 	{
180 		return "ExpectedResult";
181 	}
182 
183 	public PropertyExpansion[] getPropertyExpansions()
184 	{
185 		List<PropertyExpansion> result = new ArrayList<PropertyExpansion>();
186 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "description" ) );
187 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( this, this, "expectedResult" ) );
188 		return result.toArray( new PropertyExpansion[result.size()] );
189 	}
190 
191 	public String getExpectedResult()
192 	{
193 		return manualTestStepConfig.getExpectedResult();
194 	}
195 
196 	public void setExpectedResult( String expectedResult )
197 	{
198 		String old = getExpectedResult();
199 		if( String.valueOf( old ).equals( expectedResult ) )
200 			return;
201 
202 		manualTestStepConfig.setExpectedResult( expectedResult );
203 		notifyPropertyChanged( "expectedResult", old, expectedResult );
204 	}
205 
206 	public void resetConfigOnMove( TestStepConfig config )
207 	{
208 		super.resetConfigOnMove( config );
209 		manualTestStepConfig = ( ManualTestStepConfig )config.getConfig().changeType( ManualTestStepConfig.type );
210 	}
211 
212 	@AForm( description = "", name = "Run Manual TestStep", helpUrl = HelpUrls.MANUALTESTSTEP_HELP_URL )
213 	protected interface Form
214 	{
215 		@AField( name = "Description", description = "Describes the actions to perform", type = AFieldType.INFORMATION )
216 		public final static String DESCRIPTION = "Description";
217 
218 		@AField( name = "Expected Result", description = "Describes the actions to perform", type = AFieldType.INFORMATION )
219 		public final static String EXPECTED_DESULT = "Expected Result";
220 
221 		@AField( name = "Result", description = "an optional result description or value", type = AFieldType.STRINGAREA )
222 		public final static String RESULT = "Result";
223 
224 		@AField( name = "URLs", description = "A list of URLs related to the result", type = AFieldType.STRINGLIST )
225 		public final static String URLS = "URLs";
226 
227 		@AField( name = "Result Status", description = "The result status", type = AFieldType.ENUMERATION, values = {
228 				"Pass", "Fail", "Unknown" } )
229 		public final static String STATUS = "Result Status";
230 	}
231 }