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