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