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;
14  
15  import java.util.ArrayList;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Set;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.config.LoadTestConfig;
22  import com.eviware.soapui.config.TestCaseConfig;
23  import com.eviware.soapui.config.TestSuiteConfig;
24  import com.eviware.soapui.config.TestSuiteRunTypesConfig;
25  import com.eviware.soapui.config.TestSuiteRunTypesConfig.Enum;
26  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
27  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
28  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
29  import com.eviware.soapui.model.testsuite.TestCase;
30  import com.eviware.soapui.model.testsuite.TestSuite;
31  import com.eviware.soapui.model.testsuite.TestSuiteListener;
32  
33  /***
34   * TestSuite implementation for WSDL projects.
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public class WsdlTestSuite extends AbstractWsdlModelItem<TestSuiteConfig> implements TestSuite
40  {
41     private final WsdlProject project;
42     private List<WsdlTestCase> testCases = new ArrayList<WsdlTestCase>();
43     private Set<TestSuiteListener> testSuiteListeners = new HashSet<TestSuiteListener>();
44     
45     public WsdlTestSuite(WsdlProject project, TestSuiteConfig config)
46     {
47     	super( config, project, "/testSuite.gif" );
48        this.project = project;
49        
50        List<TestCaseConfig> testCaseConfigs = config.getTestCaseList();
51        for (int i = 0; i < testCaseConfigs.size(); i++)
52        {
53           testCases.add( new WsdlTestCase( this, testCaseConfigs.get(i), false ));
54        }
55        
56        if( !config.isSetRunType() )
57        	config.setRunType( TestSuiteRunTypesConfig.SEQUENTIAL );
58        
59     	for( TestSuiteListener listener : SoapUI.getListenerRegistry().getListeners( TestSuiteListener.class ) )
60  		{
61  			addTestSuiteListener( listener );
62  		}
63     }
64     
65  	public TestSuiteRunType getRunType()
66  	{
67  		Enum runType = getConfig().getRunType();
68  		
69  		if( runType.equals( TestSuiteRunTypesConfig.PARALLELL ))
70  			return TestSuiteRunType.PARALLEL;
71  		else
72  			return TestSuiteRunType.SEQUENTIAL;
73  	}
74  	
75  	public void setRunType( TestSuiteRunType runType )
76  	{
77  		TestSuiteRunType oldRunType = getRunType();
78  		
79  		if( runType == TestSuiteRunType.PARALLEL && oldRunType != TestSuiteRunType.PARALLEL )
80  		{
81  			getConfig().setRunType( TestSuiteRunTypesConfig.PARALLELL );
82  			notifyPropertyChanged( RUNTYPE_PROPERTY, oldRunType, runType );
83  		}
84  		else if( runType == TestSuiteRunType.SEQUENTIAL && oldRunType != TestSuiteRunType.SEQUENTIAL )
85  		{
86  			getConfig().setRunType( TestSuiteRunTypesConfig.SEQUENTIAL );
87  			notifyPropertyChanged( RUNTYPE_PROPERTY, oldRunType, runType );
88  		}
89  	}
90  
91  	public WsdlProject getProject()
92     {
93        return project;
94     }
95  
96     public int getTestCaseCount()
97     {
98        return testCases.size();
99     }
100 
101    public WsdlTestCase getTestCaseAt(int index)
102    {
103       return testCases.get( index );
104    }
105 
106    public WsdlTestCase getTestCaseByName(String testCaseName)
107 	{
108 		return ( WsdlTestCase ) getWsdlModelItemByName( testCases, testCaseName );
109 	}
110 
111 	public WsdlTestCase cloneTestCase( WsdlTestCase testCase, String name )
112    {
113 		testCase.onSave();
114    	TestCaseConfig newTestCase = getConfig().addNewTestCase();
115 		newTestCase.set( testCase.getConfig() );
116 		newTestCase.setName( name );
117 		WsdlTestCase newWsdlTestCase = new WsdlTestCase( this, newTestCase, false );
118 		
119       testCases.add( newWsdlTestCase );
120       fireTestCaseAdded( newWsdlTestCase );
121 
122       return newWsdlTestCase;
123    }
124    
125    public WsdlTestCase addNewTestCase( String name )
126    {
127       WsdlTestCase testCase = new WsdlTestCase( this, getConfig().addNewTestCase(), false );
128       testCase.setName( name );
129       testCase.setFailOnError( true );
130       testCase.setSearchProperties( true );
131       testCases.add( testCase );
132       fireTestCaseAdded( testCase );
133 
134       return testCase;
135    }
136    
137    public WsdlTestCase importTestCase( WsdlTestCase testCase, String name, int index, boolean includeLoadTests )
138    {
139    	testCase.onSave();
140    	 
141    	if( index >= testCases.size() )
142    		index = -1; //testCases.size()-1;
143    	
144       TestCaseConfig testCaseConfig = index == -1 ? 
145       	( TestCaseConfig ) getConfig().addNewTestCase().set( testCase.getConfig().copy() ) :
146          ( TestCaseConfig ) getConfig().insertNewTestCase( index ).set( testCase.getConfig().copy() );
147       testCaseConfig.setName( name );
148       
149       if( !includeLoadTests )
150       	testCaseConfig.setLoadTestArray( new LoadTestConfig[0] );
151      
152 		testCase = new WsdlTestCase( this, testCaseConfig, false );
153 		
154 		if( index == -1 )
155 			testCases.add( testCase );
156 		else
157 			testCases.add( index, testCase );
158 		
159       fireTestCaseAdded( testCase );
160 
161       return testCase;
162    }
163    
164    public void removeTestCase(WsdlTestCase testCase )
165    {
166       int ix = testCases.indexOf( testCase );
167 
168       testCases.remove( ix );
169       try
170       {
171       	fireTestCaseRemoved( testCase );
172       }
173       finally
174       {
175       	testCase.release();
176       	getConfig().removeTestCase( ix );
177       }
178    }
179 
180    public void fireTestCaseAdded( WsdlTestCase testCase )
181    {
182       TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
183       
184       for (int c = 0; c < a.length; c++ )
185       {
186          a[c].testCaseAdded( testCase );
187       }
188    }
189    
190    public void fireTestCaseRemoved( WsdlTestCase testCase )
191    {
192    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
193       
194       for (int c = 0; c < a.length; c++ )
195       {
196          a[c].testCaseRemoved( testCase );
197       }
198    } 
199    
200    private void fireTestCaseMoved( WsdlTestCase testCase, int ix, int offset )
201 	{
202    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
203       
204       for (int c = 0; c < a.length; c++ )
205       {
206          a[c].testCaseMoved( testCase, ix, offset );
207       }
208 	}
209 
210    public void fireTestStepAdded( WsdlTestStep testStep, int index )
211    {
212    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
213       
214       for (int c = 0; c < a.length; c++ )
215       {
216          a[c].testStepAdded( testStep, index );
217       }
218    }
219    
220    public void fireTestStepRemoved( WsdlTestStep testStep, int ix )
221    {
222    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
223       
224       for (int c = 0; c < a.length; c++ )
225       {
226          a[c].testStepRemoved( testStep, ix );
227       }
228    } 
229    
230    public void fireTestStepMoved( WsdlTestStep testStep, int ix, int offset )
231    {
232    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
233       
234       for (int c = 0; c < a.length; c++ )
235       {
236          a[c].testStepMoved( testStep, ix, offset );
237       }
238    } 
239 
240    public void fireLoadTestAdded( WsdlLoadTest loadTest )
241    {
242    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
243       
244       for (int c = 0; c < a.length; c++ )
245       {
246          a[c].loadTestAdded( loadTest );
247       }
248    }
249    
250    public void fireLoadTestRemoved( WsdlLoadTest loadTest )
251    {
252    	TestSuiteListener[] a = testSuiteListeners.toArray( new TestSuiteListener[testSuiteListeners.size()] );
253       
254       for (int c = 0; c < a.length; c++ )
255       {
256          a[c].loadTestRemoved( loadTest );
257       }
258    } 
259    
260 	public void addTestSuiteListener(TestSuiteListener listener)
261 	{
262 		testSuiteListeners.add( listener );
263 	}
264 
265 	public void removeTestSuiteListener(TestSuiteListener listener)
266 	{
267 		testSuiteListeners.remove( listener );
268 	}
269 
270 	public int getTestCaseIndex(TestCase testCase)
271 	{
272 		return testCases.indexOf( testCase );
273 	}
274 
275 	public void release()
276 	{
277 		super.release();
278 		
279 		for( WsdlTestCase testCase : testCases )
280 			testCase.release();
281 		
282 		testSuiteListeners.clear();
283 	}
284 
285 	public List<TestCase> getTestCaseList()
286 	{
287 		List<TestCase> result = new ArrayList<TestCase>();
288 		for( WsdlTestCase testCase : testCases )
289 			result.add( testCase );
290 		
291 		return result;
292 	} 
293 	
294 	/***
295 	 * Moves a testcase by the specified offset, a bit awkward since xmlbeans doesn't support reordering
296 	 * of arrays, we need to create copies of the contained XmlObjects
297 	 * 
298 	 * @param ix
299 	 * @param offset
300 	 */
301 	
302 	public WsdlTestCase moveTestCase(int ix, int offset)
303 	{
304 		WsdlTestCase testCase = testCases.get( ix );
305 
306 		if( offset == 0 ) 
307 			return testCase;
308 
309 		testCases.remove( ix );
310 		testCases.add( ix+offset, testCase );
311 
312 		TestCaseConfig [] configs = new TestCaseConfig[testCases.size()];
313 		
314 		for( int c = 0; c < testCases.size(); c++ )
315 		{
316 			if( offset > 0 )
317 			{
318 				if( c < ix )
319 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c).copy();
320 				else if( c < (ix+offset))
321 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c+1).copy();
322 				else if( c == ix+offset )
323 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(ix).copy();
324 				else
325 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c).copy();
326 			}
327 			else
328 			{
329 				if( c < ix+offset )
330 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c).copy();
331 				else if( c == ix+offset )
332 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(ix).copy();
333 				else if( c <= ix )
334 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c-1).copy();
335 				else
336 					configs[c] = (TestCaseConfig) getConfig().getTestCaseArray(c).copy();				
337 			}
338 		}
339 		
340 		getConfig().setTestCaseArray( configs );
341 		for( int c = 0; c < configs.length; c++ )
342 		{
343 			testCases.get( c ).resetConfigOnMove( getConfig().getTestCaseArray( c ) );
344 		}
345 		
346 		fireTestCaseMoved(testCase, ix, offset );
347 		return testCase;
348 	}
349 
350 	public int getIndexOfTestCase( TestCase testCase )
351 	{
352 		return testCases.indexOf( testCase );
353 	}
354 
355 	@Override
356 	public void onSave()
357 	{
358 		for( WsdlTestCase testCase : testCases )
359 			testCase.onSave();
360 	}
361 }