001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support;
014:
015: import junit.framework.TestCase;
016:
017: import org.apache.xmlbeans.XmlObject;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.NodeList;
021:
022: import com.eviware.soapui.support.xml.XmlUtils;
023:
024: public class XmlUtilsTestCase extends TestCase {
025: public void testGetElementIndex() throws Exception {
026: Document dom = XmlUtils
027: .parseXml("<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
028: NodeList nl = dom.getDocumentElement()
029: .getElementsByTagName("p");
030:
031: assertEquals(1, XmlUtils.getElementIndex((Element) nl.item(0)));
032: assertEquals(2, XmlUtils.getElementIndex((Element) nl.item(1)));
033: }
034:
035: public void testGetElementPath() throws Exception {
036: Document dom = XmlUtils
037: .parseXml("<h1><p>p1</p><h2>lkj</h2><p>p2</p></h1>");
038: NodeList nl = dom.getDocumentElement()
039: .getElementsByTagName("p");
040:
041: assertEquals("/h1[1]/p[1]", XmlUtils
042: .getElementPath((Element) nl.item(0)));
043: assertEquals("/h1[1]/p[2]", XmlUtils
044: .getElementPath((Element) nl.item(1)));
045: }
046:
047: public void testTransferValues() throws Exception {
048: String doc1 = "<h1><p>p1</p><h2 test=\"bil\">lkj</h2></h1>";
049: String doc2 = "<h1><p>string</p><h2>string</h2><p>p2</p></h1>";
050:
051: String result = XmlUtils.transferValues(doc1, doc2);
052: assertEquals(
053: "<h1><p>p1</p><h2 test=\"bil\">lkj</h2><p>p2</p></h1>",
054: result);
055: }
056:
057: public void testTransferValuesNS() throws Exception {
058: String doc1 = "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2></ns:h1>";
059: String doc2 = "<ns:h1 xmlns:ns=\"test\"><ns:p>string</ns:p><ns:h2>string</ns:h2><ns:p>p2</ns:p></ns:h1>";
060:
061: String result = XmlUtils.transferValues(doc1, doc2);
062: assertEquals(
063: "<ns:h1 xmlns:ns=\"test\"><ns:p>p1</ns:p><ns:h2 test=\"bil\">lkj</ns:h2><ns:p>p2</ns:p></ns:h1>",
064: result);
065: }
066:
067: public void testCreateXPath() throws Exception {
068: String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
069: + "xmlns:ord=\"http://www.example.org/OrderService/\">"
070: + "<soapenv:Header/><soapenv:Body><ord:purchaseOrder><productId>?</productId>"
071: + "</ord:purchaseOrder></soapenv:Body></soapenv:Envelope>";
072:
073: XmlObject xml = XmlObject.Factory.parse(str);
074: XmlObject xmlobj = xml.selectPath("//productId")[0];
075: String xpath = XmlUtils.createXPath(xmlobj.getDomNode());
076: assertEquals(xmlobj, xml.selectPath(xpath)[0]);
077:
078: System.out.println("before removal: " + xpath);
079: xpath = XmlUtils.removeXPathNamespaceDeclarations(xpath);
080: System.out.println("after removal:" + xpath);
081:
082: String ns = XmlUtils.declareXPathNamespaces(xml);
083: System.out.println("extracted namespaces:" + ns);
084:
085: assertEquals(xmlobj, xml.selectPath(ns + xpath)[0]);
086: }
087:
088: public void testCreateXPath2() throws Exception {
089: String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
090: + "xmlns:ord=\"http://www.example.org/OrderService/\">"
091: + "<soapenv:Header/><soapenv:Body><purchaseOrder xmlns=\"http://test\"><productId>?</productId>"
092: + "</purchaseOrder></soapenv:Body></soapenv:Envelope>";
093:
094: XmlObject xml = XmlObject.Factory.parse(str);
095: XmlObject xmlobj = xml
096: .selectPath("declare namespace ns='http://test';//ns:productId")[0];
097: String xpath = XmlUtils.createXPath(xmlobj.getDomNode());
098: System.out.println("created path: " + xpath);
099: assertEquals(xmlobj, xml.selectPath(xpath)[0]);
100: }
101: }
|