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  
14  package com.eviware.soapui.support;
15  
16  import junit.framework.TestCase;
17  
18  import org.apache.xmlbeans.XmlObject;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.NodeList;
22  
23  import com.eviware.soapui.support.xml.XmlUtils;
24  
25  public class XmlUtilsTestCase extends TestCase
26  {
27  	public void testGetElementIndex() throws Exception
28  	{
29  		Document dom = XmlUtils.parseXml( "<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
30  		NodeList nl = dom.getDocumentElement().getElementsByTagName( "p" );
31  		
32  		assertEquals( 1, XmlUtils.getElementIndex( (Element) nl.item(0)));
33  		assertEquals( 2, XmlUtils.getElementIndex( (Element) nl.item(1)));
34  	}
35  	
36  	public void testGetElementPath() throws Exception
37  	{
38  		Document dom = XmlUtils.parseXml( "<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
39  		NodeList nl = dom.getDocumentElement().getElementsByTagName( "p" );
40  		
41  		assertEquals( "/h1[1]/p[1]", XmlUtils.getElementPath( (Element) nl.item(0)) );
42  		assertEquals( "/h1[1]/p[2]", XmlUtils.getElementPath( (Element) nl.item(1)) );
43  	}
44  	
45  	public void testTransferValues() throws Exception
46  	{
47  		String doc1 = "<h1><p>p1</p><h2 test=\"bil\">lkj</h2></h1>";
48  		String doc2 = "<h1><p>string</p><h2>string</h2><p>p2</p></h1>";
49  		
50  		String result = XmlUtils.transferValues( doc1, doc2 );
51  		assertEquals( "<h1><p>p1</p><h2 test=\"bil\">lkj</h2><p>p2</p></h1>", result );
52  	}
53  	
54  	public void testTransferValuesNS() throws Exception
55  	{
56  		String doc1 = "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2></ns:h1>";
57  		String doc2 = "<ns:h1 xmlns:ns=\"test\"><ns:p>string</ns:p><ns:h2>string</ns:h2><ns:p>p2</ns:p></ns:h1>";
58  		
59  		String result = XmlUtils.transferValues( doc1, doc2 );
60  		assertEquals( "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2><ns:p>p2</ns:p></ns:h1>", result );
61  	}
62  	
63  	public void testCreateXPath() throws Exception 
64  	{
65  		String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
66  				"xmlns:ord=\"http://www.example.org/OrderService/\">" +
67  				"<soapenv:Header/><soapenv:Body><ord:purchaseOrder><productId>?</productId>" +
68  				"</ord:purchaseOrder></soapenv:Body></soapenv:Envelope>";
69  		
70  		XmlObject xml = XmlObject.Factory.parse( str );
71  		XmlObject xmlobj = xml.selectPath( "//productId" )[0];
72  		String xpath = XmlUtils.createXPath( xmlobj.getDomNode() );
73  		assertEquals( xmlobj, xml.selectPath( xpath )[0] );
74  		
75  		System.out.println( "before removal: " + xpath );
76  		xpath = XmlUtils.removeXPathNamespaceDeclarations( xpath );
77  		System.out.println( "after removal:" + xpath );
78  		
79  		String ns = XmlUtils.declareXPathNamespaces( xml );
80  		System.out.println( "extracted namespaces:" + ns );
81  		
82  		assertEquals( xmlobj, xml.selectPath( ns + xpath )[0] );
83  	}
84  	
85  	public void testCreateXPath2() throws Exception 
86  	{
87  		String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
88  				"xmlns:ord=\"http://www.example.org/OrderService/\">" +
89  				"<soapenv:Header/><soapenv:Body><purchaseOrder xmlns=\"http://test\"><productId>?</productId>" +
90  				"</purchaseOrder></soapenv:Body></soapenv:Envelope>";
91  		
92  		XmlObject xml = XmlObject.Factory.parse( str );
93  		XmlObject xmlobj = xml.selectPath( "declare namespace ns='http://test';//ns:productId" )[0];
94  		String xpath = XmlUtils.createXPath( xmlobj.getDomNode() );
95  		System.out.println( "created path: " + xpath );
96  		assertEquals( xmlobj, xml.selectPath( xpath )[0] );
97  	}
98  }