001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xdm.visitor;
043:
044: import java.util.List;
045: import junit.framework.*;
046: import org.netbeans.modules.xml.xdm.Util;
047: import org.netbeans.modules.xml.xdm.XDMModel;
048: import org.netbeans.modules.xml.xdm.nodes.Attribute;
049: import org.netbeans.modules.xml.xdm.nodes.Document;
050: import org.netbeans.modules.xml.xdm.nodes.Element;
051: import org.netbeans.modules.xml.xdm.nodes.Node;
052:
053: /**
054: *
055: * @author nn136682
056: */
057: public class XPathFinderTest extends TestCase {
058:
059: public XPathFinderTest(String testName) {
060: super (testName);
061: }
062:
063: private Document doc;
064:
065: protected void setUp() throws Exception {
066: doc = Util.loadXdmDocument("visitor/address.xsd");
067: }
068:
069: protected void tearDown() throws Exception {
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite(XPathFinderTest.class);
074:
075: return suite;
076: }
077:
078: public void testFindAndGetXpath() throws Exception {
079: String expr = "/company/employee[1]";
080: Document root = Util.loadXdmDocument("test.xml");
081: Node n = new XPathFinder().findNode(root, expr);
082: assertNotNull(n);
083:
084: String xpathString = XPathFinder.getXpath(root, n);
085: assertEquals("getXpath check round-trip", expr, xpathString);
086: }
087:
088: public void testFindNodeWithIndex() throws Exception {
089: String expr = "/schema/complexType[2]";
090: Node n = new XPathFinder().findNode(doc, expr);
091: assertNotNull(n);
092: assertEquals("attribute name=", "US-Address", n.getAttributes()
093: .getNamedItem("name").getNodeValue());
094:
095: String xpathString = XPathFinder.getXpath(doc, n);
096: assertEquals("checking round-trip", expr, xpathString);
097: }
098:
099: public void testSelectionByAttribute() throws Exception {
100: String expr = "/schema/simpleType[@name='US-State']/restriction/enumeration";
101: List<Node> nodes = new XPathFinder().findNodes(doc, expr);
102: assertEquals("testSelectionByAttribute", 54, nodes.size());
103:
104: String xpathString = XPathFinder.getXpath(doc, nodes.get(50));
105: assertEquals("testSelectionByAttribute.2",
106: "/schema/simpleType[1]/restriction[1]/enumeration[51]",
107: xpathString);
108: }
109:
110: public void testFindAttributeNode() throws Exception {
111: String expr = "/schema/complexType[2]/complexContent[1]/extension[1]/@base";
112: Node n = new XPathFinder().findNode(doc, expr);
113: assertTrue("Attribute node", n instanceof Attribute);
114:
115: Attribute attr = (Attribute) n;
116: assertEquals("Attribute value", "ipo:Address", attr
117: .getNodeValue());
118:
119: String xpathString = XPathFinder.getXpath(doc, n);
120: assertEquals("checking round-trip", expr, xpathString);
121: }
122:
123: public void testWithDefaultNamespaceAndRootElementPrefix()
124: throws Exception {
125: Document root = Util.loadXdmDocument("visitor/ipo.xml");
126: String expr = "/ipo:purchaseOrder/shipTo[1]/street[1]";
127: Node n = new XPathFinder().findNode(root, expr);
128: assertEquals("no default namespace", "street", n.getNodeName());
129:
130: String xpathString = XPathFinder.getXpath(root, n);
131: assertEquals("checking round-trip", expr, xpathString);
132: }
133:
134: public void testNonEmptyDefaultNamspacePrefix() throws Exception {
135: Document root = Util.loadXdmDocument("visitor/OrgChart.xsd");
136: String expr = "/xsd:schema/xsd:complexType[1]/xsd:sequence[1]/xsd:element[1]";
137: Node n = new XPathFinder().findNode(root, expr);
138:
139: String xpathString = XPathFinder.getXpath(root, n);
140: assertEquals("checking round-trip", expr, xpathString);
141: }
142:
143: public void testMixedPrefixesForSameNamespace() throws Exception {
144: Document root = Util
145: .loadXdmDocument("visitor/Invoke1parent.bpel");
146: String expr = "/bpel_20:process[1]/bpel_20:sequence[1]/bpel_20:receive[1]";
147: Element n = (Element) new XPathFinder().findNode(root, expr);
148: assertEquals("MyRole.Invoke1parentOperation.Receive", n
149: .getAttribute("name"));
150: String xpathString = XPathFinder.getXpath(root, n);
151: String expected = "/bpel_20:process/sequence[1]/bpel_20:receive[1]";
152: assertEquals("checking round-trip", expected, xpathString);
153: }
154:
155: public void testMixedPrefixesForSameNamespace2() throws Exception {
156: Document root = Util
157: .loadXdmDocument("visitor/Invoke1parent.bpel");
158: String expr = "/bpel_20:process[1]/sequence/receive[2]";
159: Element n = (Element) new XPathFinder().findNode(root, expr);
160: assertEquals("Invoke1parentOperation1", n.getAttribute("name"));
161: String xpathString = XPathFinder.getXpath(root, n);
162: String expected = "/bpel_20:process/sequence[1]/receive[2]";
163: assertEquals("checking round-trip", expected, xpathString);
164: }
165:
166: private XDMModel xmlModel = null;
167: }
|