View Javadoc

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