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