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