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  					property = ts.getProperty( name ).getValue();
62  				}
63  				else
64  				{
65  					property = context.getProperty( name );
66  				}
67  				
68  				if( property != null && xpath != null )
69  				{
70  					property = ResolverUtils.extractXPathPropertyValue( property, PropertyExpansionUtils.expandProperties( context, xpath ) );
71  				}
72  			}
73  			
74  			if( property != null )
75  				return property.toString();
76  		}
77  		
78  		return null;
79  	}
80  
81  	private String getScopedProperty( PropertyExpansionContext context, String pe, boolean globalOverride )
82  	{
83  		ModelItem modelItem = context.getModelItem();
84  		
85  		WsdlTestStep testStep = null;
86  		WsdlTestCase testCase = null;
87  		WsdlTestSuite testSuite = null;
88  		WsdlProject project = null;
89  		WsdlMockService mockService = null;
90  		WsdlMockResponse mockResponse = null;
91  		
92  		if( modelItem instanceof WsdlTestStep )
93  		{
94  			testStep = ( WsdlTestStep ) modelItem;
95  			testCase = testStep.getTestCase();
96  			testSuite = testCase.getTestSuite();
97  			project = testSuite.getProject();
98  		}
99  		else if( modelItem instanceof WsdlTestCase )
100 		{
101 			testCase = ( WsdlTestCase ) modelItem;
102 			testSuite = testCase.getTestSuite();
103 			project = testSuite.getProject();
104 		}
105 		else if( modelItem instanceof WsdlTestSuite )
106 		{
107 			testSuite = ( WsdlTestSuite ) modelItem;
108 			project = testSuite.getProject();
109 		}
110 		else if( modelItem instanceof WsdlProject )
111 		{
112 			project = ( WsdlProject ) modelItem;
113 		}
114 		else if( modelItem instanceof WsdlMockService )
115 		{
116 			mockService = ( WsdlMockService ) modelItem;
117 			project = mockService.getProject();
118 		}	
119 		else if( modelItem instanceof WsdlRequest )
120 		{
121 			project = ((WsdlRequest)modelItem).getOperation().getInterface().getProject();
122 		}
123 		else if( modelItem instanceof WsdlMockOperation )
124 		{
125 			mockService = (( WsdlMockOperation ) modelItem).getMockService();
126 			project = mockService.getProject();
127 		}	
128 		else if( modelItem instanceof WsdlMockResponse )
129 		{
130 			mockResponse = ( WsdlMockResponse ) modelItem;
131 			mockService = mockResponse.getMockOperation().getMockService();
132 			project = mockService.getProject();
133 		}
134 		
135 		// no project -> nothing
136 		if( project == null )
137 			return null;
138 		
139 		// explicit item reference?
140 		String result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.PROJECT_REFERENCE, project, context, globalOverride );
141 		if( result != null )
142 			return result;
143 		
144 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.TESTSUITE_REFERENCE, testSuite, context, globalOverride );
145 		if( result != null )
146 			return result;
147 		
148 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.TESTCASE_REFERENCE, testCase, context, globalOverride );
149 		if( result != null )
150 			return result;
151 		
152 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.MOCKSERVICE_REFERENCE, mockService, context, globalOverride );
153 		if( result != null )
154 			return result;
155 		
156 		result = ResolverUtils.checkForExplicitReference( pe, PropertyExpansion.MOCKRESPONSE_REFERENCE, mockResponse, context, globalOverride );
157 		if( result != null )
158 			return result;
159 
160 		return null;
161 	}
162 }