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.teststeps;
14  
15  import com.eviware.soapui.config.PropertyTransferConfig;
16  import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
17  import com.eviware.soapui.model.support.DefaultTestStepProperty;
18  import junit.framework.TestCase;
19  
20  public class PropertyTransferTestCase extends TestCase
21  {
22     private PropertyTransfer transfer;
23  	private DefaultTestStepProperty sourceProperty;
24  	private DefaultTestStepProperty targetProperty;
25  
26  	protected void setUp() throws Exception
27  	{
28  		super.setUp();
29  		
30  		transfer = new PropertyTransfer( null, PropertyTransferConfig.Factory.newInstance() );
31  		sourceProperty = new DefaultTestStepProperty( "source", null );
32  		targetProperty = new DefaultTestStepProperty( "target", null );
33  	}
34  
35  	public void testStringToStringTransfer() throws Exception
36     {
37     	PropertyTransfer transfer = new PropertyTransfer( null, PropertyTransferConfig.Factory.newInstance() );
38     	DefaultTestStepProperty sourceProperty = new DefaultTestStepProperty( "source", null );
39     	sourceProperty.setValue( "Test" );
40     	
41     	DefaultTestStepProperty targetProperty = new DefaultTestStepProperty( "target", null );
42     	transfer.transferStringToString( sourceProperty, targetProperty );
43     	
44     	assertEquals( sourceProperty.getValue(), targetProperty.getValue() ); 
45     }
46     
47  	public void testStringToXmlTransfer() throws Exception
48     {
49     	sourceProperty.setValue( "audi" );
50     	targetProperty.setValue( "<bil><name>bmw</name></bil>" );
51     	
52     	transfer.setTargetPath( "//name/text()" );
53     	
54     	transfer.transferStringToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
55     	assertEquals("<bil><name>audi</name></bil>", targetProperty.getValue() ); 
56     	
57     	targetProperty.setValue( "<bil><name test=\"test\">bmw</name></bil>" );
58     	transfer.transferStringToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
59     	
60     	assertEquals( "<bil><name test=\"test\">audi</name></bil>", targetProperty.getValue() );
61     	
62     	transfer.setTargetPath( "//name/@test" );
63     	
64     	transfer.transferStringToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
65     	assertEquals( "<bil><name test=\"audi\">audi</name></bil>", targetProperty.getValue() );
66     }
67  	
68  	public void testXmlToStringTransfer() throws Exception
69     {
70     	sourceProperty.setValue( "<bil><name>audi</name></bil>" );
71     	targetProperty.setValue( "" );
72     	
73     	transfer.setSourcePath( "//name/text()" );
74     	
75     	transfer.transferXPathToString( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
76     	assertEquals("audi", targetProperty.getValue() ); 
77     }
78  	
79  	public void testXmlToStringNullTransfer() throws Exception
80     {
81     	sourceProperty.setValue( "<bil></bil>" );
82     	targetProperty.setValue( "" );
83     	
84     	transfer.setSourcePath( "//name/text()" );
85     	
86     	transfer.transferXPathToString( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
87     	assertEquals( null, targetProperty.getValue() ); 
88     }
89  	
90     public void testTextXmlToXmlTransfer() throws Exception
91     {
92     	sourceProperty.setValue( "<bil><name>audi</name></bil>" );
93     	targetProperty.setValue( "<bil><name>bmw</name></bil>" );
94     	
95     	transfer.setSourcePath( "//name/text()" );
96     	transfer.setTargetPath( "//name/text()" );
97     	
98     	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
99     	assertEquals( sourceProperty.getValue(), targetProperty.getValue() ); 
100    	
101    	targetProperty.setValue( "<bil><name test=\"test\">bmw</name></bil>" );
102    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
103    	
104    	assertEquals( "<bil><name test=\"test\">audi</name></bil>", targetProperty.getValue() );
105    }
106    
107    public void testTextContentXmlToXmlTransfer() throws Exception
108    {
109    	sourceProperty.setValue( "<bil><name>audi</name></bil>" );
110    	targetProperty.setValue( "<bil><name2>bmw</name2></bil>" );
111    	
112    	transfer.setTransferTextContent( true );
113    	transfer.setSourcePath( "//name" );
114    	transfer.setTargetPath( "//name2" );
115    	
116    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
117    	
118    	assertEquals( "<bil><name2>audi</name2></bil>", targetProperty.getValue() );
119    }
120    
121    public void testTextXmlToXmlNullTransfer() throws Exception
122    {
123    	sourceProperty.setValue( "<bil><name/></bil>" );
124    	targetProperty.setValue( "<bil><name>bmw</name></bil>" );
125    	
126    	transfer.setSourcePath( "//name/text()" );
127    	transfer.setTargetPath( "//name/text()" );
128    	
129    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
130    	
131    	assertEquals( "<bil><name/></bil>", targetProperty.getValue() );
132    }
133    
134    public void testAttributeXmlToXmlTransfer() throws Exception
135    {
136    	sourceProperty.setValue( "<bil><name value=\"fiat\" value2=\"volvo\">alfa</name></bil>" );
137    	targetProperty.setValue( "<bil><name test=\"test\">bmw</name></bil>" );
138    	
139    	transfer.setSourcePath( "//name/@value" );
140    	transfer.setTargetPath( "//name/text()" );
141    	
142    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
143    	
144    	assertEquals( "<bil><name test=\"test\">fiat</name></bil>", targetProperty.getValue() );
145    	
146    	transfer.setSourcePath( "//name/text()" );
147    	transfer.setTargetPath( "//name/@test" );
148    	
149    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
150    	
151    	assertEquals( "<bil><name test=\"alfa\">fiat</name></bil>", targetProperty.getValue() );
152    	
153    	transfer.setSourcePath( "//name/@value2" );
154    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
155    	assertEquals( "<bil><name test=\"volvo\">fiat</name></bil>", targetProperty.getValue() );
156    }
157 
158    
159    public void testElementXmlToXmlTransfer() throws Exception
160    {
161    	sourceProperty.setValue( "<bil><name>audi</name></bil>" );
162    	targetProperty.setValue( "<bil><test/></bil>" );
163    	
164    	transfer.setSourcePath( "//bil" );
165    	transfer.setTargetPath( "//bil" );
166    	
167    	transfer.setTransferTextContent( false );
168    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
169    	assertEquals( sourceProperty.getValue(), targetProperty.getValue() ); 
170   
171    	targetProperty.setValue( "<bil><name></name></bil>" );
172    	
173    	transfer.setSourcePath( "//bil/name/text()" );
174    	transfer.setTargetPath( "//bil/name" );
175    	
176    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
177    	assertEquals( sourceProperty.getValue(), targetProperty.getValue() ); 
178    }
179    
180    public void testElementWithNsXmlToXmlTransfer() throws Exception
181    {
182    	sourceProperty.setValue( "<ns1:bil xmlns:ns1=\"ns1\"><ns1:name>audi</ns1:name></ns1:bil>" );
183    	targetProperty.setValue( "<bil><name/></bil>" );
184    	
185    	transfer.setTransferTextContent( false );
186    	transfer.setSourcePath( "declare namespace ns='ns1';//ns:bil/ns:name" );
187    	transfer.setTargetPath( "//bil/name" );
188    	
189    	transfer.transferXPathToXml( sourceProperty, targetProperty, new WsdlSubmitContext( null ) );
190    	assertEquals( "<bil xmlns:ns1=\"ns1\"><ns1:name>audi</ns1:name></bil>", targetProperty.getValue() ); 
191    }
192 
193 }