View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
17  import com.eviware.soapui.model.settings.Settings;
18  import com.eviware.soapui.model.support.AbstractSubmitContext;
19  import com.eviware.soapui.model.testsuite.TestCase;
20  import com.eviware.soapui.model.testsuite.TestRunContext;
21  import com.eviware.soapui.model.testsuite.TestRunner;
22  import com.eviware.soapui.model.testsuite.TestStep;
23  import com.eviware.soapui.support.types.StringToObjectMap;
24  
25  /***
26   * TestRunContext for WsdlTestCase runners
27   * 
28   * @author Ole.Matzura
29   */
30  
31  public class WsdlTestRunContext extends AbstractSubmitContext implements TestRunContext
32  {
33  	private final TestRunner testRunner;
34  	private int currentStepIndex;
35  	private TestCase testCase;
36  
37  	public WsdlTestRunContext( TestRunner testRunner, StringToObjectMap properties )
38  	{
39  		super( testRunner.getTestCase(), properties );
40  		this.testRunner = testRunner;
41  	}
42  
43  	public WsdlTestRunContext( TestStep testStep )
44  	{
45  		super( testStep );
46  		
47  		testRunner = null;
48  		testCase = testStep.getTestCase();
49  		currentStepIndex = testCase.getIndexOfTestStep( testStep );
50  	}
51  
52  	public TestStep getCurrentStep()
53  	{
54  		if( currentStepIndex < 0 || currentStepIndex >= getTestCase().getTestStepCount() )
55  			return null;
56  		
57  		return getTestCase().getTestStepAt( currentStepIndex );
58  	}
59  
60  	@Override
61  	public void setProperty( String name, Object value )
62  	{
63  		super.setProperty( name, value, getTestCase() );
64  	}
65  
66  	public int getCurrentStepIndex()
67  	{
68  		return currentStepIndex;
69  	}
70  	
71  	public void setCurrentStep( int index )
72  	{
73  		currentStepIndex = index;
74  	}
75  
76  	public TestRunner getTestRunner()
77  	{
78  		return testRunner;
79  	}
80  
81  	public Object getProperty(String testStepName, String propertyName)
82  	{
83  		TestStep testStep = getTestCase().getTestStepByName( testStepName );
84  		return testStep == null ? null : testStep.getPropertyValue( propertyName );
85  	}
86  
87  	public TestCase getTestCase()
88  	{
89  		return testRunner == null ? testCase : testRunner.getTestCase();
90  	}
91  	
92  	@Override
93  	public Object get( Object key )
94  	{
95  		if( "currentStep".equals(key))
96  			return getCurrentStep();
97  
98  		if( "currentStepIndex".equals(key))
99  			return getCurrentStepIndex();
100 
101 		if( "settings".equals(key))
102 			return getSettings();
103 
104 		if( "testCase".equals(key))
105 			return getTestCase();
106 
107 		if( "testRunner".equals(key))
108 			return getTestRunner();
109 		
110 		Object result = getProperty( key.toString() );
111 		
112 		if( result == null )
113 		{
114 			result = super.get( key );
115 		}
116 		
117 		return result;
118 	}
119 	
120 	@Override
121 	public Object put( String key, Object value )
122 	{
123 		Object oldValue = get( key );
124 		setProperty( key, value );
125 		return oldValue;
126 	}
127 
128 	public Object getProperty(String name)
129 	{
130 		WsdlTestCase testCase = (WsdlTestCase) getTestCase();
131 		TestStep testStep = currentStepIndex >= 0 && currentStepIndex < testCase.getTestStepCount() ? 
132 				testCase.getTestStepAt( currentStepIndex ) : null;
133 		
134 		return getProperty( name, testStep, testCase);
135 	}
136 
137 	public void reset()
138 	{
139 		resetProperties();
140 		currentStepIndex = 0;
141 	}
142 	
143 	public String expand(String content)
144 	{
145 		return PropertyExpansionUtils.expandProperties( this, content );
146 	}
147 
148 	public Settings getSettings()
149 	{
150 		return testCase == null ? SoapUI.getSettings() : testCase.getSettings();
151 	}
152 }