View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.testcase;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.log4j.Logger;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.config.LoadTestConfig;
28  import com.eviware.soapui.config.TestCaseConfig;
29  import com.eviware.soapui.config.TestStepConfig;
30  import com.eviware.soapui.config.WsrmVersionTypeConfig;
31  import com.eviware.soapui.impl.wsdl.AbstractTestPropertyHolderWsdlModelItem;
32  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
33  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
34  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
35  import com.eviware.soapui.impl.wsdl.loadtest.assertions.TestStepStatusAssertion;
36  import com.eviware.soapui.impl.wsdl.support.wsrm.WsrmTestRunListener;
37  import com.eviware.soapui.impl.wsdl.support.wsrm.WsrmUtils;
38  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
39  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepFactory;
40  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepRegistry;
41  import com.eviware.soapui.model.ModelItem;
42  import com.eviware.soapui.model.support.ModelSupport;
43  import com.eviware.soapui.model.testsuite.LoadTest;
44  import com.eviware.soapui.model.testsuite.TestCase;
45  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
46  import com.eviware.soapui.model.testsuite.TestCaseRunner;
47  import com.eviware.soapui.model.testsuite.TestRunListener;
48  import com.eviware.soapui.model.testsuite.TestStep;
49  import com.eviware.soapui.support.StringUtils;
50  import com.eviware.soapui.support.UISupport;
51  import com.eviware.soapui.support.action.swing.ActionList;
52  import com.eviware.soapui.support.action.swing.DefaultActionList;
53  import com.eviware.soapui.support.resolver.ResolveDialog;
54  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
55  import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
56  import com.eviware.soapui.support.types.StringToObjectMap;
57  
58  /***
59   * TestCase implementation for WSDL projects
60   * 
61   * @author Ole.Matzura
62   */
63  
64  public class WsdlTestCase extends AbstractTestPropertyHolderWsdlModelItem<TestCaseConfig> implements TestCase
65  {
66  	private final static Logger logger = Logger.getLogger( WsdlTestCase.class );
67  	public final static String KEEP_SESSION_PROPERTY = WsdlTestCase.class.getName() + "@keepSession";
68  	public final static String FAIL_ON_ERROR_PROPERTY = WsdlTestCase.class.getName() + "@failOnError";
69  	public final static String FAIL_ON_ERRORS_PROPERTY = WsdlTestCase.class.getName() + "@failOnErrors";
70  	public final static String DISCARD_OK_RESULTS = WsdlTestCase.class.getName() + "@discardOkResults";
71  	public final static String SETUP_SCRIPT_PROPERTY = WsdlTestCase.class.getName() + "@setupScript";
72  	public final static String TEARDOWN_SCRIPT_PROPERTY = WsdlTestCase.class.getName() + "@tearDownScript";
73  	public static final String TIMEOUT_PROPERTY = WsdlTestCase.class.getName() + "@timeout";
74  	public static final String SEARCH_PROPERTIES_PROPERTY = WsdlTestCase.class.getName() + "@searchProperties";
75  
76  	private final WsdlTestSuite testSuite;
77  	private List<WsdlTestStep> testSteps = new ArrayList<WsdlTestStep>();
78  	private List<WsdlLoadTest> loadTests = new ArrayList<WsdlLoadTest>();
79  	private Set<TestRunListener> testRunListeners = new HashSet<TestRunListener>();
80  	private DefaultActionList createActions;
81  	private final boolean forLoadTest;
82  	private SoapUIScriptEngine setupScriptEngine;
83  	private SoapUIScriptEngine tearDownScriptEngine;
84  
85  	public WsdlTestCase( WsdlTestSuite testSuite, TestCaseConfig config, boolean forLoadTest )
86  	{
87  		super( config, testSuite, "/testCase.gif" );
88  
89  		this.testSuite = testSuite;
90  		this.forLoadTest = forLoadTest;
91  
92  		List<TestStepConfig> testStepConfigs = config.getTestStepList();
93  		List<TestStepConfig> removed = new ArrayList<TestStepConfig>();
94  		for( TestStepConfig tsc : testStepConfigs )
95  		{
96  			WsdlTestStep testStep = createTestStepFromConfig( tsc );
97  			if( testStep != null )
98  			{
99  				ensureUniqueName( testStep );
100 				testSteps.add( testStep );
101 			}
102 			else
103 				removed.add( tsc );
104 		}
105 		if( removed.size() > 0 )
106 		{
107 			testStepConfigs.removeAll( removed );
108 			config.setTestStepArray( testStepConfigs.toArray( new TestStepConfig[] {} ) );
109 		}
110 
111 		if( !forLoadTest )
112 		{
113 			List<LoadTestConfig> loadTestConfigs = config.getLoadTestList();
114 			for( LoadTestConfig tsc : loadTestConfigs )
115 			{
116 				WsdlLoadTest loadTest = buildLoadTest( tsc );
117 				loadTests.add( loadTest );
118 			}
119 		}
120 
121 		// init default configs
122 		if( !config.isSetFailOnError() )
123 			config.setFailOnError( true );
124 
125 		if( !config.isSetFailTestCaseOnErrors() )
126 			config.setFailTestCaseOnErrors( true );
127 
128 		if( !config.isSetKeepSession() )
129 			config.setKeepSession( false );
130 
131 		if( !config.isSetMaxResults() )
132 			config.setMaxResults( 0 );
133 
134 		for( TestRunListener listener : SoapUI.getListenerRegistry().getListeners( TestRunListener.class ) )
135 		{
136 			addTestRunListener( listener );
137 		}
138 
139 		if( !getConfig().isSetProperties() )
140 			getConfig().addNewProperties();
141 
142 		setPropertiesConfig( getConfig().getProperties() );
143 
144 		WsrmTestRunListener wsrmListener = new WsrmTestRunListener();
145 
146 		addTestRunListener( wsrmListener );
147 	}
148 
149 	public WsdlLoadTest buildLoadTest( LoadTestConfig tsc )
150 	{
151 		return new WsdlLoadTest( this, tsc );
152 	}
153 
154 	public boolean getKeepSession()
155 	{
156 		return getConfig().getKeepSession();
157 	}
158 
159 	public void setKeepSession( boolean keepSession )
160 	{
161 		boolean old = getKeepSession();
162 		if( old != keepSession )
163 		{
164 			getConfig().setKeepSession( keepSession );
165 			notifyPropertyChanged( KEEP_SESSION_PROPERTY, old, keepSession );
166 		}
167 	}
168 
169 	public void setSetupScript( String script )
170 	{
171 		String oldScript = getSetupScript();
172 
173 		if( !getConfig().isSetSetupScript() )
174 			getConfig().addNewSetupScript();
175 
176 		getConfig().getSetupScript().setStringValue( script );
177 		if( setupScriptEngine != null )
178 			setupScriptEngine.setScript( script );
179 
180 		notifyPropertyChanged( SETUP_SCRIPT_PROPERTY, oldScript, script );
181 	}
182 
183 	public String getSetupScript()
184 	{
185 		return getConfig().isSetSetupScript() ? getConfig().getSetupScript().getStringValue() : null;
186 	}
187 
188 	public void setTearDownScript( String script )
189 	{
190 		String oldScript = getTearDownScript();
191 
192 		if( !getConfig().isSetTearDownScript() )
193 			getConfig().addNewTearDownScript();
194 
195 		getConfig().getTearDownScript().setStringValue( script );
196 		if( tearDownScriptEngine != null )
197 			tearDownScriptEngine.setScript( script );
198 
199 		notifyPropertyChanged( TEARDOWN_SCRIPT_PROPERTY, oldScript, script );
200 	}
201 
202 	public String getTearDownScript()
203 	{
204 		return getConfig().isSetTearDownScript() ? getConfig().getTearDownScript().getStringValue() : null;
205 	}
206 
207 	public boolean getFailOnError()
208 	{
209 		return getConfig().getFailOnError();
210 	}
211 
212 	public boolean getFailTestCaseOnErrors()
213 	{
214 		return getConfig().getFailTestCaseOnErrors();
215 	}
216 
217 	public void setFailOnError( boolean failOnError )
218 	{
219 		boolean old = getFailOnError();
220 		if( old != failOnError )
221 		{
222 			getConfig().setFailOnError( failOnError );
223 			notifyPropertyChanged( FAIL_ON_ERROR_PROPERTY, old, failOnError );
224 		}
225 	}
226 
227 	public void setFailTestCaseOnErrors( boolean failTestCaseOnErrors )
228 	{
229 		boolean old = getFailTestCaseOnErrors();
230 		if( old != failTestCaseOnErrors )
231 		{
232 			getConfig().setFailTestCaseOnErrors( failTestCaseOnErrors );
233 			notifyPropertyChanged( FAIL_ON_ERRORS_PROPERTY, old, failTestCaseOnErrors );
234 		}
235 	}
236 
237 	public boolean getSearchProperties()
238 	{
239 		return getConfig().getSearchProperties();
240 	}
241 
242 	public void setSearchProperties( boolean searchProperties )
243 	{
244 		boolean old = getSearchProperties();
245 		if( old != searchProperties )
246 		{
247 			getConfig().setSearchProperties( searchProperties );
248 			notifyPropertyChanged( SEARCH_PROPERTIES_PROPERTY, old, searchProperties );
249 		}
250 	}
251 
252 	public boolean getDiscardOkResults()
253 	{
254 		return getConfig().getDiscardOkResults();
255 	}
256 
257 	public void setDiscardOkResults( boolean discardOkResults )
258 	{
259 		boolean old = getDiscardOkResults();
260 		if( old != discardOkResults )
261 		{
262 			getConfig().setDiscardOkResults( discardOkResults );
263 			notifyPropertyChanged( DISCARD_OK_RESULTS, old, discardOkResults );
264 		}
265 	}
266 
267 	public int getMaxResults()
268 	{
269 		return getConfig().getMaxResults();
270 	}
271 
272 	public void setMaxResults( int maxResults )
273 	{
274 		int old = getMaxResults();
275 		if( old != maxResults )
276 		{
277 			getConfig().setMaxResults( maxResults );
278 			notifyPropertyChanged( "maxResults", old, maxResults );
279 		}
280 	}
281 
282 	private WsdlTestStep createTestStepFromConfig( TestStepConfig tsc )
283 	{
284 		WsdlTestStepFactory factory = WsdlTestStepRegistry.getInstance().getFactory( tsc.getType() );
285 		if( factory != null )
286 		{
287 			WsdlTestStep testStep = factory.buildTestStep( this, tsc, forLoadTest );
288 			return testStep;
289 		}
290 		else
291 		{
292 			logger.error( "Failed to create test step for [" + tsc.getName() + "]" );
293 			return null;
294 		}
295 	}
296 
297 	private boolean ensureUniqueName( WsdlTestStep testStep )
298 	{
299 		String name = testStep.getName();
300 		while( name == null || getTestStepByName( name ) != null )
301 		{
302 			if( name == null )
303 				name = testStep.getName();
304 			else
305 			{
306 				int cnt = 0;
307 
308 				while( getTestStepByName( name ) != null )
309 				{
310 					cnt++ ;
311 					name = testStep.getName() + " " + cnt;
312 				}
313 
314 				if( cnt == 0 )
315 					break;
316 			}
317 
318 			name = UISupport.prompt( "TestStep name must be unique, please specify new name for step\n" + "["
319 					+ testStep.getName() + "] in TestCase [" + getTestSuite().getProject().getName() + "->"
320 					+ getTestSuite().getName() + "->" + getName() + "]", "Change TestStep name", name );
321 
322 			if( name == null )
323 				return false;
324 		}
325 
326 		if( !name.equals( testStep.getName() ) )
327 			testStep.setName( name );
328 
329 		return true;
330 	}
331 
332 	public WsdlLoadTest addNewLoadTest( String name )
333 	{
334 		WsdlLoadTest loadTest = buildLoadTest( getConfig().addNewLoadTest() );
335 		loadTest.setStartDelay( 0 );
336 		loadTest.setName( name );
337 		loadTests.add( loadTest );
338 
339 		loadTest.addAssertion( TestStepStatusAssertion.STEP_STATUS_TYPE, LoadTestAssertion.ANY_TEST_STEP, false );
340 
341 		( getTestSuite() ).fireLoadTestAdded( loadTest );
342 
343 		return loadTest;
344 	}
345 
346 	public void removeLoadTest( WsdlLoadTest loadTest )
347 	{
348 		int ix = loadTests.indexOf( loadTest );
349 
350 		loadTests.remove( ix );
351 
352 		try
353 		{
354 			( getTestSuite() ).fireLoadTestRemoved( loadTest );
355 		}
356 		finally
357 		{
358 			loadTest.release();
359 			getConfig().removeLoadTest( ix );
360 		}
361 	}
362 
363 	public WsdlTestSuite getTestSuite()
364 	{
365 		return testSuite;
366 	}
367 
368 	public WsdlTestStep cloneStep( WsdlTestStep testStep, String name )
369 	{
370 		return testStep.clone( this, name );
371 	}
372 
373 	public WsdlTestStep getTestStepAt( int index )
374 	{
375 		return testSteps.get( index );
376 	}
377 
378 	public int getTestStepCount()
379 	{
380 		return testSteps.size();
381 	}
382 
383 	public WsdlLoadTest getLoadTestAt( int index )
384 	{
385 		return loadTests.get( index );
386 	}
387 
388 	public LoadTest getLoadTestByName( String loadTestName )
389 	{
390 		return ( LoadTest )getWsdlModelItemByName( loadTests, loadTestName );
391 	}
392 
393 	public int getLoadTestCount()
394 	{
395 		return loadTests.size();
396 	}
397 
398 	public WsdlTestStep addTestStep( TestStepConfig stepConfig )
399 	{
400 		return insertTestStep( stepConfig, -1, true );
401 	}
402 
403 	public WsdlTestStep addTestStep( String type, String name )
404 	{
405 		TestStepConfig newStepConfig = WsdlTestStepRegistry.getInstance().getFactory( type ).createNewTestStep( this,
406 				name );
407 		if( newStepConfig != null )
408 		{
409 			return addTestStep( newStepConfig );
410 		}
411 		else
412 			return null;
413 	}
414 
415 	public WsdlTestStep insertTestStep( String type, String name, int index )
416 	{
417 		TestStepConfig newStepConfig = WsdlTestStepRegistry.getInstance().getFactory( type ).createNewTestStep( this,
418 				name );
419 		if( newStepConfig != null )
420 		{
421 			return insertTestStep( newStepConfig, index, false );
422 		}
423 		else
424 			return null;
425 	}
426 
427 	public WsdlTestStep importTestStep( WsdlTestStep testStep, String name, int index, boolean createCopy )
428 	{
429 		testStep.beforeSave();
430 		TestStepConfig newStepConfig = ( TestStepConfig )testStep.getConfig().copy();
431 		newStepConfig.setName( name );
432 
433 		WsdlTestStep result = insertTestStep( newStepConfig, index, createCopy );
434       if( result == null )
435       	return null;
436       
437 		if( createCopy )
438 			ModelSupport.unsetIds( result );
439 
440 		resolveTestCase();
441 		return result;
442 	}
443 
444 	private void resolveTestCase()
445 	{
446 		ResolveDialog resolver = new ResolveDialog( "Validate TestCase", "Checks TestCase for inconsistencies", null );
447 		resolver.setShowOkMessage( false );
448 		resolver.resolve( this );
449 	}
450 
451 	public WsdlTestStep[] importTestSteps( WsdlTestStep[] testSteps, int index, boolean createCopies )
452 	{
453 		TestStepConfig[] newStepConfigs = new TestStepConfig[testSteps.length];
454 
455 		for( int c = 0; c < testSteps.length; c++ )
456 		{
457 			testSteps[c].beforeSave();
458 			newStepConfigs[c] = ( TestStepConfig )testSteps[c].getConfig().copy();
459 		}
460 
461 		WsdlTestStep[] result = insertTestSteps( newStepConfigs, index, createCopies );
462 
463 		resolveTestCase();
464 		return result;
465 	}
466 
467 	public WsdlTestStep insertTestStep( TestStepConfig stepConfig, int ix )
468 	{
469 		return insertTestStep( stepConfig, ix, true );
470 	}
471 
472 	public WsdlTestStep insertTestStep( TestStepConfig stepConfig, int ix, boolean clearIds )
473 	{
474 		TestStepConfig newStepConfig = ix == -1 ? getConfig().addNewTestStep() : getConfig().insertNewTestStep( ix );
475 		newStepConfig.set( stepConfig );
476 		WsdlTestStep testStep = createTestStepFromConfig( newStepConfig );
477 
478 		if( !ensureUniqueName( testStep ) )
479 		{
480 			testStep.release();
481 			getConfig().getTestStepList().remove( newStepConfig );
482 			return null;
483 		}
484 
485 		if( clearIds )
486 			ModelSupport.unsetIds( testStep );
487 
488 		if( ix == -1 )
489 			testSteps.add( testStep );
490 		else
491 			testSteps.add( ix, testStep );
492 
493 		testStep.afterLoad();
494 
495 		if( getTestSuite() != null )
496 			( getTestSuite() ).fireTestStepAdded( testStep, ix == -1 ? testSteps.size() - 1 : ix );
497 		
498 		notifyPropertyChanged( "testSteps", null, testStep );
499 
500 		return testStep;
501 	}
502 
503 	public WsdlTestStep[] insertTestSteps( TestStepConfig[] stepConfig, int ix, boolean clearIds )
504 	{
505 		WsdlTestStep[] result = new WsdlTestStep[stepConfig.length];
506 
507 		for( int c = 0; c < stepConfig.length; c++ )
508 		{
509 			TestStepConfig newStepConfig = ix == -1 ? getConfig().addNewTestStep() : getConfig()
510 					.insertNewTestStep( ix + c );
511 			newStepConfig.set( stepConfig[c] );
512 			WsdlTestStep testStep = createTestStepFromConfig( newStepConfig );
513 
514 			if( !ensureUniqueName( testStep ) )
515 				return null;
516 
517 			if( clearIds )
518 				ModelSupport.unsetIds( testStep );
519 
520 			if( ix == -1 )
521 				testSteps.add( testStep );
522 			else
523 				testSteps.add( ix + c, testStep );
524 
525 			result[c] = testStep;
526 		}
527 
528 		for( int c = 0; c < result.length; c++ )
529 		{
530 			result[c].afterLoad();
531 
532 			if( getTestSuite() != null )
533 				( getTestSuite() ).fireTestStepAdded( result[c], getIndexOfTestStep( result[c] ) );
534 			
535 			notifyPropertyChanged( "testSteps", null, result[c] );
536 		}
537 
538 		return result;
539 	}
540 
541 	public void removeTestStep( WsdlTestStep testStep )
542 	{
543 		int ix = testSteps.indexOf( testStep );
544 		if( ix == -1 )
545 		{
546 			logger.error( "TestStep [" + testStep.getName() + "] passed to removeTestStep in testCase [" + getName()
547 					+ "] not found" );
548 			return;
549 		}
550 
551 		testSteps.remove( ix );
552 
553 		try
554 		{
555 			( getTestSuite() ).fireTestStepRemoved( testStep, ix );
556 		}
557 		finally
558 		{
559 			notifyPropertyChanged( "testSteps", testStep, null );
560 			
561 			testStep.release();
562 
563 			for( int c = 0; c < getConfig().sizeOfTestStepArray(); c++ )
564 			{
565 				if( testStep.getConfig() == getConfig().getTestStepArray( c ) )
566 				{
567 					getConfig().removeTestStep( c );
568 					break;
569 				}
570 			}
571 		}
572 	}
573 
574 	public WsdlTestCaseRunner run( StringToObjectMap properties, boolean async )
575 	{
576 		WsdlTestCaseRunner runner = new WsdlTestCaseRunner( this, properties );
577 		runner.start( async );
578 		return runner;
579 	}
580 
581 	public void addTestRunListener( TestRunListener listener )
582 	{
583 		if( listener == null )
584 			throw new RuntimeException( "listener must not be null" );
585 
586 		testRunListeners.add( listener );
587 	}
588 
589 	public void removeTestRunListener( TestRunListener listener )
590 	{
591 		testRunListeners.remove( listener );
592 	}
593 
594 	public TestRunListener[] getTestRunListeners()
595 	{
596 		return testRunListeners.toArray( new TestRunListener[testRunListeners.size()] );
597 	}
598 
599 	public Map<String, TestStep> getTestSteps()
600 	{
601 		Map<String, TestStep> result = new HashMap<String, TestStep>();
602 		for( TestStep testStep : testSteps )
603 			result.put( testStep.getName(), testStep );
604 
605 		return result;
606 	}
607 
608 	public Map<String, LoadTest> getLoadTests()
609 	{
610 		Map<String, LoadTest> result = new HashMap<String, LoadTest>();
611 		for( LoadTest loadTest : loadTests )
612 			result.put( loadTest.getName(), loadTest );
613 
614 		return result;
615 	}
616 
617 	public int getIndexOfTestStep( TestStep step )
618 	{
619 		return testSteps.indexOf( step );
620 	}
621 
622 	/***
623 	 * Moves a step by the specified offset, a bit awkward since xmlbeans doesn't
624 	 * support reordering of arrays, we need to create copies of the contained
625 	 * XmlObjects
626 	 * 
627 	 * @param ix
628 	 * @param offset
629 	 */
630 
631 	public void moveTestStep( int ix, int offset )
632 	{
633 		if( offset == 0 )
634 			return;
635 		WsdlTestStep step = testSteps.get( ix );
636 
637 		if( ix + offset >= testSteps.size() )
638 			offset = testSteps.size() - ix - 1;
639 
640 		testSteps.remove( ix );
641 		testSteps.add( ix + offset, step );
642 
643 		TestStepConfig[] configs = new TestStepConfig[testSteps.size()];
644 
645 		TestCaseConfig conf = getConfig();
646 		for( int c = 0; c < testSteps.size(); c++ )
647 		{
648 			if( offset > 0 )
649 			{
650 				if( c < ix )
651 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c ).copy();
652 				else if( c < ( ix + offset ) )
653 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c + 1 ).copy();
654 				else if( c == ix + offset )
655 					configs[c] = ( TestStepConfig )conf.getTestStepArray( ix ).copy();
656 				else
657 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c ).copy();
658 			}
659 			else
660 			{
661 				if( c < ix + offset )
662 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c ).copy();
663 				else if( c == ix + offset )
664 					configs[c] = ( TestStepConfig )conf.getTestStepArray( ix ).copy();
665 				else if( c <= ix )
666 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c - 1 ).copy();
667 				else
668 					configs[c] = ( TestStepConfig )conf.getTestStepArray( c ).copy();
669 			}
670 		}
671 
672 		conf.setTestStepArray( configs );
673 		for( int c = 0; c < configs.length; c++ )
674 		{
675 			( testSteps.get( c ) ).resetConfigOnMove( conf.getTestStepArray( c ) );
676 		}
677 
678 		( getTestSuite() ).fireTestStepMoved( step, ix, offset );
679 	}
680 
681 	public int getIndexOfLoadTest( LoadTest loadTest )
682 	{
683 		return loadTests.indexOf( loadTest );
684 	}
685 
686 	public int getTestStepIndexByName( String stepName )
687 	{
688 		for( int c = 0; c < testSteps.size(); c++ )
689 		{
690 			if( testSteps.get( c ).getName().equals( stepName ) )
691 				return c;
692 		}
693 
694 		return -1;
695 	}
696 
697 	@SuppressWarnings( "unchecked" )
698 	public <T extends TestStep> T findPreviousStepOfType( TestStep referenceStep, Class<T> stepClass )
699 	{
700 		int currentStepIndex = getIndexOfTestStep( referenceStep );
701 		int ix = currentStepIndex - 1;
702 		while( ix >= 0 && !stepClass.isAssignableFrom( getTestStepAt( ix ).getClass()) )
703 		{
704 			ix-- ;
705 		}
706 
707 		return ( T )( ix < 0 ? null : getTestStepAt( ix ) );
708 	}
709 
710 	@SuppressWarnings( "unchecked" )
711 	public <T extends TestStep> T findNextStepOfType( TestStep referenceStep, Class<T> stepClass )
712 	{
713 		int currentStepIndex = getIndexOfTestStep( referenceStep );
714 		int ix = currentStepIndex + 1;
715 		while( ix < getTestStepCount() && !stepClass.isAssignableFrom( getTestStepAt( ix ).getClass()) )
716 		{
717 			ix++ ;
718 		}
719 
720 		return ( T )( ix >= getTestStepCount() ? null : getTestStepAt( ix ) );
721 	}
722 
723 	public List<TestStep> getTestStepList()
724 	{
725 		List<TestStep> result = new ArrayList<TestStep>();
726 		for( TestStep step : testSteps )
727 			result.add( step );
728 
729 		return result;
730 	}
731 
732 	@SuppressWarnings( "unchecked" )
733 	public <T extends TestStep> List<T> getTestStepsOfType( Class<T> stepType )
734 	{
735 		List<T> result = new ArrayList<T>();
736 		for( TestStep step : testSteps )
737 			if( step.getClass().isAssignableFrom( stepType ) )
738 				result.add( ( T )step );
739 
740 		return result;
741 	}
742 
743 	public WsdlTestStep getTestStepByName( String stepName )
744 	{
745 		return ( WsdlTestStep )getWsdlModelItemByName( testSteps, stepName );
746 	}
747 
748 	public WsdlLoadTest cloneLoadTest( WsdlLoadTest loadTest, String name )
749 	{
750 		loadTest.beforeSave();
751 
752 		LoadTestConfig loadTestConfig = getConfig().addNewLoadTest();
753 		loadTestConfig.set( loadTest.getConfig().copy() );
754 
755 		WsdlLoadTest newLoadTest = buildLoadTest( loadTestConfig );
756 		newLoadTest.setName( name );
757 		ModelSupport.unsetIds( newLoadTest );
758 		newLoadTest.afterLoad();
759 		loadTests.add( newLoadTest );
760 
761 		( getTestSuite() ).fireLoadTestAdded( newLoadTest );
762 
763 		return newLoadTest;
764 	}
765 
766 	@Override
767 	public void release()
768 	{
769 		super.release();
770 
771 		for( WsdlTestStep testStep : testSteps )
772 			testStep.release();
773 
774 		for( WsdlLoadTest loadTest : loadTests )
775 			loadTest.release();
776 
777 		testRunListeners.clear();
778 
779 		if( setupScriptEngine != null )
780 			setupScriptEngine.release();
781 
782 		if( tearDownScriptEngine != null )
783 			tearDownScriptEngine.release();
784 	}
785 
786 	public ActionList getCreateActions()
787 	{
788 		return createActions;
789 	}
790 
791 	public void resetConfigOnMove( TestCaseConfig testCaseConfig )
792 	{
793 		setConfig( testCaseConfig );
794 		int mod = 0;
795 
796 		List<TestStepConfig> configs = getConfig().getTestStepList();
797 		for( int c = 0; c < configs.size(); c++ )
798 		{
799 			if( WsdlTestStepRegistry.getInstance().hasFactory( configs.get( c ) ) )
800 			{
801 				( testSteps.get( c - mod ) ).resetConfigOnMove( configs.get( c ) );
802 			}
803 			else
804 				mod++ ;
805 		}
806 
807 		List<LoadTestConfig> loadTestConfigs = getConfig().getLoadTestList();
808 		for( int c = 0; c < loadTestConfigs.size(); c++ )
809 		{
810 			loadTests.get( c ).resetConfigOnMove( loadTestConfigs.get( c ) );
811 		}
812 
813 		setPropertiesConfig( testCaseConfig.getProperties() );
814 	}
815 
816 	public List<LoadTest> getLoadTestList()
817 	{
818 		List<LoadTest> result = new ArrayList<LoadTest>();
819 		for( LoadTest loadTest : loadTests )
820 			result.add( loadTest );
821 
822 		return result;
823 	}
824 
825 	public Object runSetupScript( TestCaseRunContext runContext, TestCaseRunner runner ) throws Exception
826 	{
827 		String script = getSetupScript();
828 		if( StringUtils.isNullOrEmpty( script ))
829 			return null;
830 
831 		if( setupScriptEngine == null )
832 		{
833 			setupScriptEngine = SoapUIScriptEngineRegistry.create( this );
834 			setupScriptEngine.setScript( script );
835 		}
836 
837 		setupScriptEngine.setVariable( "testCase", this );
838 		setupScriptEngine.setVariable( "context", runContext );
839 		setupScriptEngine.setVariable( "testRunner", runner );
840 		setupScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
841 		return setupScriptEngine.run();
842 	}
843 
844 	public Object runTearDownScript( TestCaseRunContext runContext, TestCaseRunner runner ) throws Exception
845 	{
846 		String script = getTearDownScript();
847 		if( StringUtils.isNullOrEmpty( script ) )
848 			return null;
849 
850 		if( tearDownScriptEngine == null )
851 		{
852 			tearDownScriptEngine = SoapUIScriptEngineRegistry.create( this );
853 			tearDownScriptEngine.setScript( script );
854 		}
855 
856 		tearDownScriptEngine.setVariable( "context", runContext );
857 		tearDownScriptEngine.setVariable( "testCase", this );
858 		tearDownScriptEngine.setVariable( "testRunner", runner );
859 		tearDownScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
860 		return tearDownScriptEngine.run();
861 	}
862 
863 	public List<? extends ModelItem> getChildren()
864 	{
865 		List<ModelItem> result = new ArrayList<ModelItem>();
866 		result.addAll( getTestStepList() );
867 		result.addAll( getLoadTestList() );
868 		return result;
869 	}
870 
871 	@Override
872 	public void setName( String name )
873 	{
874 		String oldLabel = getLabel();
875 
876 		super.setName( name );
877 
878 		String label = getLabel();
879 		if( oldLabel != null && !oldLabel.equals( label ) )
880 		{
881 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
882 		}
883 	}
884 
885 	public String getLabel()
886 	{
887 		String name = getName();
888 		if( isDisabled() )
889 			return name + " (disabled)";
890 		else
891 			return name;
892 	}
893 
894 	public boolean isDisabled()
895 	{
896 		return getConfig().getDisabled();
897 	}
898 
899 	public void setDisabled( boolean disabled )
900 	{
901 		String oldLabel = getLabel();
902 
903 		boolean oldDisabled = isDisabled();
904 		if( oldDisabled == disabled )
905 			return;
906 
907 		if( disabled )
908 			getConfig().setDisabled( disabled );
909 		else if( getConfig().isSetDisabled() )
910 			getConfig().unsetDisabled();
911 
912 		notifyPropertyChanged( DISABLED_PROPERTY, oldDisabled, disabled );
913 
914 		String label = getLabel();
915 		if( !oldLabel.equals( label ) )
916 			notifyPropertyChanged( LABEL_PROPERTY, oldLabel, label );
917 	}
918 
919 	public long getTimeout()
920 	{
921 		return getConfig().getTimeout();
922 	}
923 
924 	public void setTimeout( long timeout )
925 	{
926 		long old = getTimeout();
927 		getConfig().setTimeout( timeout );
928 		notifyPropertyChanged( TIMEOUT_PROPERTY, old, timeout );
929 	}
930 
931 	public void exportTestCase( File file )
932 	{
933 		try
934 		{
935 			this.getConfig().newCursor().save( file );
936 		}
937 		catch( IOException e )
938 		{
939 			e.printStackTrace();
940 		}
941 	}
942 
943 	public void afterCopy( WsdlTestSuite oldTestSuite, WsdlTestCase oldTestCase )
944 	{
945 		for( WsdlTestStep testStep : testSteps )
946 			testStep.afterCopy( oldTestSuite, oldTestCase );
947 	}
948 
949 	public void setWsrmEnabled( boolean enabled )
950 	{
951 		getConfig().setWsrmEnabled( enabled );
952 	}
953 
954 	public void setWsrmAckTo( String ackTo )
955 	{
956 		getConfig().setWsrmAckTo( ackTo );
957 	}
958 
959 	public void setWsrmExpires( Long expires )
960 	{
961 		getConfig().setWsrmExpires( expires );
962 	}
963 
964 	public void setWsrmVersion( String version )
965 	{
966 		getConfig().setWsrmVersion( WsrmVersionTypeConfig.Enum.forString( version ) );
967 	}
968 
969 	public boolean getWsrmEnabled()
970 	{
971 		return getConfig().getWsrmEnabled();
972 	}
973 
974 	public String getWsrmAckTo()
975 	{
976 		return getConfig().getWsrmAckTo();
977 	}
978 
979 	public long getWsrmExpires()
980 	{
981 		return getConfig().getWsrmExpires();
982 	}
983 
984 	public String getWsrmVersion()
985 	{
986 		if( getConfig().getWsrmVersion() == null )
987 			return WsrmVersionTypeConfig.X_1_0.toString();
988 		return getConfig().getWsrmVersion().toString();
989 	}
990 
991 	public String getWsrmVersionNamespace()
992 	{
993 		return WsrmUtils.getWsrmVersionNamespace( getConfig().getWsrmVersion() );
994 	}
995 
996 }