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.teststeps;
14  
15  import java.awt.event.ActionEvent;
16  import java.util.HashMap;
17  import java.util.HashSet;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.Action;
23  
24  import com.eviware.soapui.config.TestStepConfig;
25  import com.eviware.soapui.impl.actions.ShowDesktopPanelAction;
26  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
27  import com.eviware.soapui.impl.wsdl.actions.teststep.DeleteTestStepAction;
28  import com.eviware.soapui.impl.wsdl.actions.teststep.RenameTestStepAction;
29  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
30  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepFactory;
31  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepRegistry;
32  import com.eviware.soapui.model.PanelBuilder;
33  import com.eviware.soapui.model.testsuite.TestRunContext;
34  import com.eviware.soapui.model.testsuite.TestRunner;
35  import com.eviware.soapui.model.testsuite.TestStep;
36  import com.eviware.soapui.model.testsuite.TestStepProperty;
37  import com.eviware.soapui.support.UISupport;
38  import com.eviware.soapui.support.action.ActionSupport;
39  import com.eviware.soapui.support.action.DefaultActionList;
40  
41  /***
42   * Base class for WSDL project test steps.
43   *  
44   * @author Ole.Matzura
45   */
46  
47  abstract public class WsdlTestStep extends AbstractWsdlModelItem<TestStepConfig> implements TestStep
48  {
49  	private final WsdlTestCase testCase;
50  	private Map<String,TestStepProperty> properties;
51  	private Set<WsdlTestStepListener> listeners = new HashSet<WsdlTestStepListener>();
52     
53     protected WsdlTestStep( WsdlTestCase testCase, TestStepConfig config, boolean hasEditor )
54     {
55     	super( config, testCase, null );
56     	
57        this.testCase = testCase;
58        
59        if( hasEditor )
60        {
61        	addAction( new ShowDesktopPanelAction( "Open Editor", "Opens the Editor for this TestStep", this ));
62        }
63        
64        DefaultActionList createActions = new DefaultActionList( "Insert Step" );
65  
66  		WsdlTestStepRegistry registry = WsdlTestStepRegistry.getInstance();
67  		WsdlTestStepFactory[] factories = (WsdlTestStepFactory[]) registry.getFactories();
68  
69  		for (int c = 0; c < factories.length; c++)
70  		{
71  			if (factories[c].canCreate())
72  				createActions.addAction( new InsertTestStepAction(factories[c]));
73  		}
74  
75  		addAction( new ActionSupport.ActionListAction( createActions ));
76     	addAction( ActionSupport.SEPARATOR_ACTION );
77  
78        
79        addAction( new RenameTestStepAction( this ) );
80        addAction( new DeleteTestStepAction( this ) );
81     }
82     
83     /***
84      * Called after creation of all teststeps, should be used for inter-test-step initializations
85      * @param config 
86      */
87     
88     public void postInit(TestStepConfig config)
89     {}
90  
91  	protected PanelBuilder createPanelBuilder()
92  	{
93  		return null;
94  	}
95  
96  	public WsdlTestCase getTestCase()
97     {
98        return testCase;
99     }
100    
101    /***
102     * Called from WsdlTestCase when moving a teststep due to no move functionality
103     * in xmlbeans generated arrays. 
104     * 
105     * @param config the new config to use, will be a copy of the existing one. The current
106     * will be invalid
107     */
108    
109    public void resetConfigOnMove( TestStepConfig config )
110    {
111    	setConfig( config );
112    }
113 
114 	public boolean cancel()
115 	{
116 		return false;
117 	}
118 	
119    public String [] getPropertyNames()
120 	{
121    	if( properties == null )
122    		return new String[0];
123    	
124    	String [] result = new String[properties.size()];
125    	int ix = 0;
126    	for( TestStepProperty property : properties.values() )
127    		result[ix++] = property.getName();
128    	
129    	return result;
130 	}
131    
132 	public TestStepProperty getProperty(String name)
133 	{
134 		return properties == null || name == null ? null : properties.get( name.toUpperCase() );
135 	}
136 	
137 	public String getPropertyValue(String name)
138 	{
139 		if( properties == null )
140 			return null;
141 		
142 		TestStepProperty testStepProperty = properties.get( name.toUpperCase());
143 		return testStepProperty == null ? null : testStepProperty.getValue();
144 	}
145 
146 	public void setPropertyValue(String name, String value)
147 	{
148 		if( properties == null )
149 			return;
150 		
151 		TestStepProperty testStepProperty = properties.get( name.toUpperCase());
152 		if( testStepProperty != null )
153 		{
154 			testStepProperty.setValue( value );
155 		}
156 	}
157 
158 	protected void addProperty( TestStepProperty property )
159 	{
160 		if( properties == null )
161 			properties = new HashMap<String,TestStepProperty>();
162 			
163 		properties.put( property.getName().toUpperCase(), property );
164 	}
165 	
166 	protected void deleteProperty( String name )
167 	{
168 		if( properties != null )
169 			properties.remove( name.toUpperCase() );
170 	}
171 
172 	protected void propertyRenamed( String oldName )
173 	{
174 		if( properties == null )
175 			return;
176 		
177 		TestStepProperty testStepProperty = properties.get( oldName.toUpperCase() );
178 		if( testStepProperty == null )
179 			return;
180 		
181 		properties.remove( oldName.toUpperCase() );
182 		String newName = testStepProperty.getName();
183 		properties.put( newName.toUpperCase(), testStepProperty );
184 		
185 		firePropertyRenamed( oldName, newName );
186 	}
187 	
188 	public void addTestStepListener( WsdlTestStepListener listener )
189 	{
190 		listeners.add( listener );
191 	}
192 	
193 	public void removeTestStepListener( WsdlTestStepListener listener )
194 	{
195 		listeners.remove( listener );
196 	}
197 	
198 	protected void firePropertyAdded( String name )
199 	{
200 		WsdlTestStepListener[] array = listeners.toArray( new WsdlTestStepListener[listeners.size()]);
201 		for( WsdlTestStepListener listener : array )
202 		{
203 			listener.propertyAdded( name );
204 		}
205 	}
206 	
207 	protected void firePropertyRemoved( String name )
208 	{
209 		WsdlTestStepListener[] array = listeners.toArray( new WsdlTestStepListener[listeners.size()]);
210 		for( WsdlTestStepListener listener : array )
211 		{
212 			listener.propertyRemoved( name );
213 		}
214 	}
215 	
216 	protected void firePropertyRenamed( String oldName, String newName )
217 	{
218 		WsdlTestStepListener[] array = listeners.toArray( new WsdlTestStepListener[listeners.size()]);
219 		for( WsdlTestStepListener listener : array )
220 		{
221 			listener.propertyRenamed( oldName, newName );
222 		}
223 	}	
224 	
225 	protected void firePropertyValueChanged( String name, String oldValue, String newValue )
226 	{
227 		if( oldValue == null && newValue == null )
228 			return;
229 		
230 		if( oldValue != null && oldValue.equals( newValue ))
231 			return;
232 
233 		if( newValue != null && newValue.equals( oldValue ))
234 			return;
235 		
236 		WsdlTestStepListener[] array = listeners.toArray( new WsdlTestStepListener[listeners.size()]);
237 		for( WsdlTestStepListener listener : array )
238 		{
239 			listener.propertyValueChanged( name, oldValue, newValue );
240 		}
241 	}
242 	
243 	public boolean dependsOn( AbstractWsdlModelItem modelItem )
244 	{
245 		return false;
246 	}
247 	
248 	public String getTestStepTitle()
249 	{
250 		return getTestCase().getTestSuite().getName() + "#" + getTestCase().getName();
251 	}
252 
253 	/***
254 	 * Called after cloning for custom behaviour
255 	 * 
256 	 * @param sourceStep step we were cloned from
257 	 */
258 	
259 	public WsdlTestStep clone( WsdlTestCase targetTestCase, String name)
260 	{
261 		TestStepConfig newConfig = (TestStepConfig) getConfig().copy();
262 		newConfig.setName( name );
263 		return targetTestCase.addTestStep( newConfig );
264 	}
265 	
266 	public class InsertTestStepAction extends AbstractAction
267 	{
268 		private final WsdlTestStepFactory factory;
269 
270 		public InsertTestStepAction(WsdlTestStepFactory factory)
271 		{
272 			super(factory.getTestStepName());
273 			putValue(Action.SHORT_DESCRIPTION, factory.getTestStepDescription());
274 			putValue( Action.SMALL_ICON, factory.getTestStepIcon() );
275 			this.factory = factory;
276 		}
277 
278 		public void actionPerformed(ActionEvent e)
279 		{
280 			String name = UISupport.prompt( "Specify name for new step", "Insert Step", factory.getTestStepName());
281 			if( name != null )
282 			{
283 				TestStepConfig newTestStepConfig = factory.createNewTestStep(testCase, name);
284 				if( newTestStepConfig != null )
285 				{
286 					int ix = getTestCase().getIndexOfTestStep( WsdlTestStep.this );
287 					WsdlTestStep testStep = testCase.insertTestStep(newTestStepConfig, ix);
288 					UISupport.selectAndShow( testStep );
289 				}
290 			}
291 		}
292 	}
293 
294 	public void finish( TestRunner testRunner, TestRunContext testRunContext )
295 	{
296 	}
297 
298 	public void prepare( TestRunner testRunner, TestRunContext testRunContext )
299 	{
300 	}
301 }