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