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.impl.wsdl.submit.filters;
14  
15  import junit.framework.TestCase;
16  
17  import org.apache.log4j.Logger;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
22  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
23  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext;
24  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner;
25  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
27  import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
30  import com.eviware.soapui.settings.GlobalPropertySettings;
31  
32  public class PropertyExpansionTestCase extends TestCase
33  {
34     public void testExpansion() throws Exception
35     {
36     	WsdlSubmitContext context = new WsdlSubmitContext( null );
37     	
38     	context.setProperty( "test", "value" );
39     	
40     	assertEquals( "${test}", PropertyExpander.expandProperties( context, "$${test}" ));
41     	assertEquals( "value${test}", PropertyExpander.expandProperties( context, "${test}$${test}" ));
42     	assertEquals( "${value", PropertyExpander.expandProperties( context, "$${${test}" ));
43     	assertEquals( "value", PropertyExpander.expandProperties( context, "${test}" ));
44     	assertEquals( "value", PropertyExpander.expandProperties( context, "${#test}" ));
45     	assertEquals( " value ", PropertyExpander.expandProperties( context, " ${test} " ));
46     	assertEquals( "", PropertyExpander.expandProperties( context, "${testa}" ));
47     	assertEquals( "valuevalue", PropertyExpander.expandProperties( context, "${test}${test}" ));
48  
49     	context.setProperty( "testa", "" );
50     	assertEquals( "", PropertyExpander.expandProperties( context, "${testa}" ));
51     }
52     
53     public void testRecursiveExpansion() throws Exception
54     {
55     	WsdlSubmitContext context = new WsdlSubmitContext( null );
56     	
57     	context.setProperty( "test", "value" );
58     	context.setProperty( "testexp", "${test}" );
59     	
60     	assertEquals( "value", PropertyExpander.expandProperties( context, "${testexp}" ));
61  
62     	context.setProperty( "exp", "${exp}" );
63     	assertEquals( "${exp}", PropertyExpander.expandProperties( context, "${exp}" ));
64     }
65     
66     public void testNestedExpansion() throws Exception
67     {
68     	WsdlSubmitContext context = new WsdlSubmitContext( null );
69     	
70     	context.setProperty( "test", "value" );
71     	context.setProperty( "testexp", "${test}" );
72     	context.setProperty( "exp", "exp" );
73     	
74     	assertEquals( "value", PropertyExpander.expandProperties( context, "${test${exp}}" ));
75     	
76     	context.setProperty( "id", "123" );
77     	context.setProperty( "testxml", "<test><value id=\"123\">hello</value></test>" );
78     	assertEquals( "hello", 
79     				PropertyExpander.expandProperties( context, "${#testxml#//value[@id=${id}]/text()}" ));
80  
81     	context.setProperty( "testxpath", "//value[@id=${id}]/text()" );
82     	assertEquals( "hello", 
83     				PropertyExpander.expandProperties( context, "${#testxml#${testxpath}}" ));
84     }
85     
86     public void testXPathExpansion() throws Exception 
87     {
88     	WsdlSubmitContext context = new WsdlSubmitContext( null );
89     	
90     	context.setProperty( "test", "<test><value>hello</value></test>" );
91     	assertEquals( "hello", PropertyExpander.expandProperties( context, "${#test#//value/text()}" ));
92     }
93     
94     public void testScopedPropertyExpansion() throws Exception
95     {
96     	WsdlProject project = new WsdlProject();
97     	project.addProperty( "projectId" ).setValue( "123" );
98     	WsdlTestSuite testSuite = project.addNewTestSuite( "TestSuite" );
99     	testSuite.addProperty( "testSuiteId" ).setValue( "234" );
100    	WsdlTestCase testCase = testSuite.addNewTestCase( "TestCase" );
101    	testCase.addProperty( "testCaseId" ).setValue( "345" );
102    	
103    	WsdlTestStep testStep = testCase.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "GroovyScript" );
104    	
105    	MockTestRunner mockTestRunner = new MockTestRunner( testCase, Logger.getLogger( "testing" ) );
106       MockTestRunContext context = new MockTestRunContext( mockTestRunner, testStep );
107       
108       PropertyExpansionUtils.getGlobalProperties().setPropertyValue( "testSuiteId", "testing" );
109       
110       assertEquals( "123", context.expand( "${#Project#projectId}" ) );
111       assertEquals( "234", context.expand( "${#TestSuite#testSuiteId}" ) );
112       assertEquals( "345", context.expand( "${#TestCase#testCaseId}" ) );
113    	
114       SoapUI.getSettings().setBoolean( GlobalPropertySettings.ENABLE_OVERRIDE, true );
115       
116       assertEquals( "testing", context.expand( "${#TestSuite#testSuiteId}" ) );
117    }
118 }