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  
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 testTransferValuesWithList() throws Exception
55  	{
56  		String doc1 = "<h1><p>p1</p><p>p2</p><h2 test=\"bil\">lkj</h2></h1>";
57  		String doc2 = "<h1><p>string</p><h2>string</h2><p>p2</p></h1>";
58  		
59  		String result = XmlUtils.transferValues( doc1, doc2 );
60  		assertEquals( "<h1><p>p1</p><p>p2</p><h2 test=\"bil\">lkj</h2><p>p2</p></h1>", result );
61  	}
62  	
63  	public void testTransferValuesNS() throws Exception
64  	{
65  		String doc1 = "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2></ns:h1>";
66  		String doc2 = "<ns:h1 xmlns:ns=\"test\"><ns:p>string</ns:p><ns:h2>string</ns:h2><ns:p>p2</ns:p></ns:h1>";
67  		
68  		String result = XmlUtils.transferValues( doc1, doc2 );
69  		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 );
70  	}
71  	
72  	public void testCreateXPath() throws Exception 
73  	{
74  		String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
75  				"xmlns:ord=\"http://www.example.org/OrderService/\">" +
76  				"<soapenv:Header/><soapenv:Body><ord:purchaseOrder><productId>?</productId>" +
77  				"</ord:purchaseOrder></soapenv:Body></soapenv:Envelope>";
78  		
79  		XmlObject xml = XmlObject.Factory.parse( str );
80  		XmlObject xmlobj = xml.selectPath( "//productId" )[0];
81  		String xpath = XmlUtils.createXPath( xmlobj.getDomNode() );
82  		assertEquals( xmlobj, xml.selectPath( xpath )[0] );
83  		
84  		System.out.println( "before removal: " + xpath );
85  		xpath = XmlUtils.removeXPathNamespaceDeclarations( xpath );
86  		System.out.println( "after removal:" + xpath );
87  		
88  		String ns = XmlUtils.declareXPathNamespaces( xml );
89  		System.out.println( "extracted namespaces:" + ns );
90  		
91  		assertEquals( xmlobj, xml.selectPath( ns + xpath )[0] );
92  	}
93  	
94  	public void testCreateXPath2() throws Exception 
95  	{
96  		String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
97  				"xmlns:ord=\"http://www.example.org/OrderService/\">" +
98  				"<soapenv:Header/><soapenv:Body><purchaseOrder xmlns=\"http://test\"><productId>?</productId>" +
99  				"</purchaseOrder></soapenv:Body></soapenv:Envelope>";
100 		
101 		XmlObject xml = XmlObject.Factory.parse( str );
102 		XmlObject xmlobj = xml.selectPath( "declare namespace ns='http://test';//ns:productId" )[0];
103 		String xpath = XmlUtils.createXPath( xmlobj.getDomNode() );
104 		System.out.println( "created path: " + xpath );
105 		assertEquals( xmlobj, xml.selectPath( xpath )[0] );
106 	}
107 }