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