View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
14  
15  import java.util.ArrayList;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Set;
19  
20  import javax.swing.ImageIcon;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.config.TestCaseConfig;
26  import com.eviware.soapui.config.TestStepConfig;
27  import com.eviware.soapui.impl.wsdl.actions.testcase.RemoveTestCaseAction;
28  import com.eviware.soapui.impl.wsdl.actions.testcase.RenameTestCaseAction;
29  import com.eviware.soapui.impl.wsdl.panels.testcase.WsdlTestCasePanelBuilder;
30  import com.eviware.soapui.impl.wsdl.teststeps.TransferResponseValuesTestStep;
31  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
32  import com.eviware.soapui.model.PanelBuilder;
33  import com.eviware.soapui.model.testsuite.TestCase;
34  import com.eviware.soapui.model.testsuite.TestRunListener;
35  import com.eviware.soapui.model.testsuite.TestRunner;
36  import com.eviware.soapui.model.testsuite.TestStep;
37  import com.eviware.soapui.model.testsuite.TestSuite;
38  import com.eviware.soapui.model.tree.SoapUITreeNode;
39  import com.eviware.soapui.model.tree.nodes.TestCaseTreeNode;
40  
41  /***
42   * TestCase implementation for WSDL projects
43   *  
44   * @author Ole.Matzura
45   */
46  
47  public class WsdlTestCase extends AbstractWsdlModelItem implements TestCase
48  {
49  	private final WsdlTestSuite testSuite;
50     private final TestCaseConfig config;
51  
52     private List<WsdlTestStep> testSteps = new ArrayList<WsdlTestStep>();
53     private ImageIcon testCaseIcon;
54  
55     private final static Logger logger = Logger.getLogger( WsdlTestCase.class );
56     private PanelBuilder panelBuilder;
57     
58     private Set<TestRunListener> testRunListeners = new HashSet<TestRunListener>();
59     
60     public WsdlTestCase(WsdlTestSuite testSuite, TestCaseConfig config)
61     {
62        this.testSuite = testSuite;
63        this.config = config;
64        
65        TestStepConfig [] testStepConfigs = config.getTestStepArray();
66        for (int i = 0; i < testStepConfigs.length; i++)
67        {
68           TestStepConfig tsc = testStepConfigs[i];
69           addTestStepFromConfig(tsc);
70        }
71        
72        testCaseIcon = SoapUI.createImageIcon("/testCase.gif");
73        
74        addAction( new RenameTestCaseAction( this ) );
75        addAction( new RemoveTestCaseAction( this ) );
76     }
77  
78  	private WsdlTestStep addTestStepFromConfig(TestStepConfig tsc)
79  	{
80  		String type = tsc.getType();
81  		WsdlTestStep newStep = null;
82  		if( type.equals( "request" ))
83  		{
84  			newStep = new WsdlTestRequestStep( this, tsc );
85  		}
86  		else if( type.equals( "transfer") )
87  		{
88  			newStep = new TransferResponseValuesTestStep( this, tsc );
89  		}
90  		else logger.error( "Unknown request step type: " + type );
91  		
92  		if( newStep != null )
93  			testSteps.add( newStep );
94  		
95  		return newStep;
96  	}
97  
98     public TestSuite getTestSuite()
99     {
100       return testSuite;
101    }
102 
103    public WsdlTestStep cloneStep( WsdlTestStep testStep, String name )
104    {
105    	TestStepConfig testStepConfig = (TestStepConfig) config.addNewTestStep().set( testStep.getConfig().copy() );
106    	WsdlTestStep newStep = addTestStepFromConfig( testStepConfig );
107    	newStep.setName( name );
108       ((WsdlTestSuite)getTestSuite()).notifyTestStepAdded( newStep, testSteps.size()-1  );
109 		return newStep;
110    }
111    
112    public String getMessage()
113    {
114       return null;
115    }
116 
117    public TestStatus getStatus()
118    {
119       return TestStatus.UNKNOWN;
120    }
121 
122    public TestStep getTestStepAt(int index)
123    {
124       return testSteps.get( index );
125    }
126 
127    public int getTestStepCount()
128    {
129       return testSteps.size();
130    }
131 
132    public String getName()
133    {
134       return config.getName();
135    }
136 
137    public ImageIcon getIcon()
138    {
139      return testCaseIcon;
140    }
141 
142    public void setName(String name)
143    {
144       String old = getName();
145       config.setName( name );
146       notifyPropertyChanged(NAME_PROPERTY, old, name);
147    }
148 
149    public WsdlTestRequestStep addTestRequestStep(WsdlRequest request, String name )
150    {
151       TestStepConfig testStep = config.addNewTestStep();
152       testStep.setType( "request" );
153       WsdlTestRequestStep testRequestStep = new WsdlTestRequestStep( this, testStep, request );
154       testRequestStep.setName( name );
155       testSteps.add( testRequestStep );
156       
157       ((WsdlTestSuite)getTestSuite()).notifyTestStepAdded( testRequestStep, testSteps.size()-1 );
158       
159       return testRequestStep;
160    }
161 
162    public TransferResponseValuesTestStep addTransferResponseValuesStep(int ix)
163    {
164       TestStepConfig testStepConfig = ix == -1 ? config.addNewTestStep() : config.insertNewTestStep( ix );
165       testStepConfig.setType( "transfer" );
166       TransferResponseValuesTestStep testStep = new TransferResponseValuesTestStep( this, testStepConfig );
167       testStep.setName( "Transfer values" );
168       
169       if( ix == -1 )
170       	testSteps.add( testStep );
171       else
172       	testSteps.add( ix, testStep );
173       
174       ((WsdlTestSuite)getTestSuite()).notifyTestStepAdded( testStep, ix == -1 ? testSteps.size()-1 : ix  );
175       
176       return testStep;
177    }
178    
179    
180    public void removeTestStep(WsdlTestStep testStep)
181    {
182       int ix = testSteps.indexOf( testStep );
183       if( ix == -1 )
184       {
185          logger.error( "TestStep [" + testStep.getName() + "] passed to removeTestStep in testCase [" + 
186                getName() + "] not found" );
187          return;
188       }
189       
190       ((WsdlTestSuite)getTestSuite()).notifyTestStepRemoved( testStep, ix );
191       testSteps.remove( ix );
192       config.removeTestStep( ix );
193    }
194    
195    protected SoapUITreeNode createTreeNode()
196    {
197       return new TestCaseTreeNode( this );
198    }
199 
200    public PanelBuilder getPanelBuilder()
201    {
202       if( panelBuilder == null )
203          panelBuilder = new WsdlTestCasePanelBuilder( this );
204       
205       return panelBuilder;
206    }
207 
208    public TestRunner run() 
209    {
210    	TestRunner runner = new WsdlTestRunner( this );
211    	runner.start();
212    	return runner;
213 	}
214 
215 	public void addTestRunListener(TestRunListener listener) {
216 		testRunListeners.add( listener );
217 	}
218 
219 	public void removeTestRunListener(TestRunListener listener) {
220 		testRunListeners.remove( listener );
221 	}
222 	
223 	TestRunListener [] getTestRunListeners()
224 	{
225 		return testRunListeners.toArray( new TestRunListener[testRunListeners.size()]);
226 	}
227 
228 	WsdlTestStep[] getTestSteps() 
229 	{
230 		return testSteps.toArray( new WsdlTestStep[testSteps.size()] );
231  	}
232 
233 	public int getIndexOfTestStep(TestStep step)
234 	{
235 		return testSteps.indexOf( step );
236 	}
237 
238 	/***
239 	 * Moves a step by the specified offset - WONT WORK!
240 	 * 
241 	 * @param ix
242 	 * @param offset
243 	 */
244 	
245 	public void moveStep(int ix, int offset)
246 	{
247 		if( offset == 0 ) return;
248 		WsdlTestStep step = testSteps.get( ix );
249 
250 		((WsdlTestSuite)getTestSuite()).notifyTestStepRemoved(step, ix );
251 
252 		testSteps.remove( ix );
253 		testSteps.add( ix+offset, step );
254 
255 		TestStepConfig [] configs = new TestStepConfig[testSteps.size()];
256 		
257 		for( int c = 0; c < testSteps.size(); c++ )
258 		{
259 			if( offset > 0 )
260 			{
261 				if( c < ix )
262 					configs[c] = (TestStepConfig) config.getTestStepArray(c).copy();
263 				else if( c < (ix+offset))
264 					configs[c] = (TestStepConfig) config.getTestStepArray(c+1).copy();
265 				else if( c == ix+offset )
266 					configs[c] = (TestStepConfig) config.getTestStepArray(ix).copy();
267 				else
268 					configs[c] = (TestStepConfig) config.getTestStepArray(c).copy();
269 			}
270 			else
271 			{
272 				if( c < ix+offset )
273 					configs[c] = (TestStepConfig) config.getTestStepArray(c).copy();
274 				else if( c == ix+offset )
275 					configs[c] = (TestStepConfig) config.getTestStepArray(ix).copy();
276 				else if( c <= ix )
277 					configs[c] = (TestStepConfig) config.getTestStepArray(c-1).copy();
278 				else
279 					configs[c] = (TestStepConfig) config.getTestStepArray(c).copy();				
280 			}
281 		}
282 		
283 		config.setTestStepArray( configs );
284 		for( int c = 0; c < configs.length; c++ )
285 		{
286 			testSteps.get( c ).resetConfigOnMove( config.getTestStepArray( c ));
287 		}
288 		
289 		((WsdlTestSuite)getTestSuite()).notifyTestStepAdded(step, ix+offset );
290 //		((WsdlTestSuite)getTestSuite()).notifyTestStepMoved(step, offset );
291 	}
292 }