1   /*
2    *  soapUI, copyright (C) 2004-2009 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( "value", PropertyExpander.expandProperties( context, "${test}" ));
41     	assertEquals( "value", PropertyExpander.expandProperties( context, "${#test}" ));
42     	assertEquals( " value ", PropertyExpander.expandProperties( context, " ${test} " ));
43     	assertEquals( "", PropertyExpander.expandProperties( context, "${testa}" ));
44     	assertEquals( "valuevalue", PropertyExpander.expandProperties( context, "${test}${test}" ));
45  
46     	context.setProperty( "testa", "" );
47     	assertEquals( "", PropertyExpander.expandProperties( context, "${testa}" ));
48     }
49     
50     public void testRecursiveExpansion() throws Exception
51     {
52     	WsdlSubmitContext context = new WsdlSubmitContext( null );
53     	
54     	context.setProperty( "test", "value" );
55     	context.setProperty( "testexp", "${test}" );
56     	
57     	assertEquals( "value", PropertyExpander.expandProperties( context, "${testexp}" ));
58  
59     	context.setProperty( "exp", "${exp}" );
60     	assertEquals( "${exp}", PropertyExpander.expandProperties( context, "${exp}" ));
61     }
62     
63     public void testNestedExpansion() throws Exception
64     {
65     	WsdlSubmitContext context = new WsdlSubmitContext( null );
66     	
67     	context.setProperty( "test", "value" );
68     	context.setProperty( "testexp", "${test}" );
69     	context.setProperty( "exp", "exp" );
70     	
71     	assertEquals( "value", PropertyExpander.expandProperties( context, "${test${exp}}" ));
72     	
73     	context.setProperty( "id", "123" );
74     	context.setProperty( "testxml", "<test><value id=\"123\">hello</value></test>" );
75     	assertEquals( "hello", 
76     				PropertyExpander.expandProperties( context, "${#testxml#//value[@id=${id}]/text()}" ));
77  
78     	context.setProperty( "testxpath", "//value[@id=${id}]/text()" );
79     	assertEquals( "hello", 
80     				PropertyExpander.expandProperties( context, "${#testxml#${testxpath}}" ));
81     }
82     
83     public void testXPathExpansion() throws Exception 
84     {
85     	WsdlSubmitContext context = new WsdlSubmitContext( null );
86     	
87     	context.setProperty( "test", "<test><value>hello</value></test>" );
88     	assertEquals( "hello", PropertyExpander.expandProperties( context, "${#test#//value/text()}" ));
89     }
90     
91     public void testScopedPropertyExpansion() throws Exception
92     {
93     	WsdlProject project = new WsdlProject();
94     	project.addProperty( "projectId" ).setValue( "123" );
95     	WsdlTestSuite testSuite = project.addNewTestSuite( "TestSuite" );
96     	testSuite.addProperty( "testSuiteId" ).setValue( "234" );
97     	WsdlTestCase testCase = testSuite.addNewTestCase( "TestCase" );
98     	testCase.addProperty( "testCaseId" ).setValue( "345" );
99     	
100    	WsdlTestStep testStep = testCase.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "GroovyScript" );
101    	
102    	MockTestRunner mockTestRunner = new MockTestRunner( testCase, Logger.getLogger( "testing" ) );
103       MockTestRunContext context = new MockTestRunContext( mockTestRunner, testStep );
104       
105       PropertyExpansionUtils.getGlobalProperties().setPropertyValue( "testSuiteId", "testing" );
106       
107       assertEquals( "123", context.expand( "${#Project#projectId}" ) );
108       assertEquals( "234", context.expand( "${#TestSuite#testSuiteId}" ) );
109       assertEquals( "345", context.expand( "${#TestCase#testCaseId}" ) );
110    	
111       SoapUI.getSettings().setBoolean( GlobalPropertySettings.ENABLE_OVERRIDE, true );
112       
113       assertEquals( "testing", context.expand( "${#TestSuite#testSuiteId}" ) );
114    }
115 }