View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.model.propertyexpansion.resolvers;
14  
15  import com.eviware.soapui.impl.support.AbstractHttpRequest;
16  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
17  import com.eviware.soapui.impl.wsdl.WsdlInterface;
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
20  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
22  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.TestRequest;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestMockService;
27  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
28  import com.eviware.soapui.model.ModelItem;
29  import com.eviware.soapui.model.project.Project;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
31  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
32  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
33  import com.eviware.soapui.model.testsuite.TestCase;
34  import com.eviware.soapui.model.testsuite.TestProperty;
35  import com.eviware.soapui.model.testsuite.TestStep;
36  import com.eviware.soapui.model.testsuite.TestSuite;
37  
38  public class ModelItemPropertyResolver implements PropertyResolver
39  {
40  	public String resolveProperty( PropertyExpansionContext context, String pe, boolean globalOverride )
41  	{
42  		if( pe.charAt( 0 ) == PropertyExpansion.SCOPE_PREFIX )
43  			return getScopedProperty( context, pe, globalOverride );
44  
45  		ModelItem modelItem = context.getModelItem();
46  		if( modelItem instanceof WsdlLoadTest )
47  			modelItem = ( ( WsdlLoadTest )modelItem ).getTestCase();
48  		else if( modelItem instanceof TestRequest )
49  			modelItem = ( ( TestRequest )modelItem ).getTestStep();
50  		else if( modelItem instanceof WsdlMockResponse
51  				&& ( ( WsdlMockResponse )modelItem ).getMockOperation().getMockService() instanceof WsdlTestMockService )
52  			modelItem = ( ( WsdlTestMockService )( ( WsdlMockResponse )modelItem ).getMockOperation().getMockService() )
53  					.getMockResponseStep();
54  
55  		if( modelItem instanceof WsdlTestStep || modelItem instanceof WsdlTestCase )
56  		{
57  			WsdlTestStep testStep = ( WsdlTestStep )( modelItem instanceof WsdlTestStep ? modelItem : null );
58  			WsdlTestCase testCase = ( WsdlTestCase )( testStep == null ? modelItem : testStep.getTestCase() );
59  
60  			int sepIx = pe.indexOf( PropertyExpansion.PROPERTY_SEPARATOR );
61  			Object property = null;
62  
63  			if( sepIx > 0 )
64  			{
65  				String step = pe.substring( 0, sepIx );
66  				String name = pe.substring( sepIx + 1 );
67  				String xpath = null;
68  
69  				sepIx = name.indexOf( PropertyExpansion.PROPERTY_SEPARATOR );
70  				WsdlTestStep ts = testCase.getTestStepByName( step );
71  
72  				if( sepIx != -1 )
73  				{
74  					xpath = name.substring( sepIx + 1 );
75  					name = name.substring( 0, sepIx );
76  				}
77  
78  				if( step != null )
79  				{
80  					if( ts != null )
81  					{
82  						TestProperty p = ts.getProperty( name );
83  						if( p != null )
84  							property = p.getValue();
85  					}
86  				}
87  				else
88  				{
89  					property = context.getProperty( name );
90  				}
91  
92  				if( property != null && xpath != null )
93  				{
94  					property = ResolverUtils.extractXPathPropertyValue( property, PropertyExpander.expandProperties(
95  							context, xpath ) );
96  				}
97  			}
98  
99  			if( property != null )
100 				return property.toString();
101 		}
102 
103 		return null;
104 	}
105 
106 	private String getScopedProperty( PropertyExpansionContext context, String pe, boolean globalOverride )
107 	{
108 		ModelItem modelItem = context.getModelItem();
109 
110 		TestStep testStep = null;
111 		TestCase testCase = null;
112 		TestSuite testSuite = null;
113 		Project project = null;
114 		WsdlMockService mockService = null;
115 		WsdlMockResponse mockResponse = null;
116 
117 		if( modelItem instanceof WsdlTestStep )
118 		{
119 			testStep = ( WsdlTestStep )modelItem;
120 			testCase = testStep.getTestCase();
121 			testSuite = testCase.getTestSuite();
122 			project = testSuite.getProject();
123 		}
124 		else if( modelItem instanceof WsdlTestCase )
125 		{
126 			testCase = ( WsdlTestCase )modelItem;
127 			testSuite = testCase.getTestSuite();
128 			project = testSuite.getProject();
129 		}
130 		else if( modelItem instanceof WsdlLoadTest )
131 		{
132 			testCase = ( ( WsdlLoadTest )modelItem ).getTestCase();
133 			testSuite = testCase.getTestSuite();
134 			project = testSuite.getProject();
135 		}
136 		else if( modelItem instanceof WsdlTestSuite )
137 		{
138 			testSuite = ( WsdlTestSuite )modelItem;
139 			project = testSuite.getProject();
140 		}
141 		else if( modelItem instanceof WsdlInterface )
142 		{
143 			project = ( ( WsdlInterface )modelItem ).getProject();
144 		}
145 		else if( modelItem instanceof WsdlProject )
146 		{
147 			project = ( WsdlProject )modelItem;
148 		}
149 		else if( modelItem instanceof WsdlMockService )
150 		{
151 			mockService = ( WsdlMockService )modelItem;
152 			project = mockService.getProject();
153 		}
154 		else if( modelItem instanceof TestRequest )
155 		{
156 			testStep = ( ( TestRequest )modelItem ).getTestStep();
157 			testCase = testStep.getTestCase();
158 			testSuite = testCase.getTestSuite();
159 			project = testSuite.getProject();
160 		}
161 		else if( modelItem instanceof AbstractHttpRequestInterface<?> )
162 		{
163 			project = ( ( AbstractHttpRequest<?> )modelItem ).getOperation().getInterface().getProject();
164 		}
165 		else if( modelItem instanceof WsdlMockOperation )
166 		{
167 			mockService = ( ( WsdlMockOperation )modelItem ).getMockService();
168 			project = mockService.getProject();
169 		}
170 		else if( modelItem instanceof WsdlMockResponse )
171 		{
172 			mockResponse = ( WsdlMockResponse )modelItem;
173 			mockService = mockResponse.getMockOperation().getMockService();
174 			project = mockService.getProject();
175 		}
176 
177 		// no project -> nothing
178 		if( project == null )
179 			return null;
180 
181 		// explicit item reference?
182 		String result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.PROJECT_REFERENCE, project,
183 				context, globalOverride );
184 		if( result != null )
185 			return result;
186 
187 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.TESTSUITE_REFERENCE, testSuite, context,
188 				globalOverride );
189 		if( result != null )
190 			return result;
191 
192 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.TESTCASE_REFERENCE, testCase, context,
193 				globalOverride );
194 		if( result != null )
195 			return result;
196 
197 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.MOCKSERVICE_REFERENCE, mockService,
198 				context, globalOverride );
199 		if( result != null )
200 			return result;
201 
202 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.MOCKRESPONSE_REFERENCE, mockResponse,
203 				context, globalOverride );
204 		if( result != null )
205 			return result;
206 
207 		return null;
208 	}
209 }