KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > XPathFinderTest


1 package org.netbeans.modules.xml.xdm.visitor;
2 import java.util.List JavaDoc;
3 import junit.framework.*;
4 import org.netbeans.modules.xml.xdm.Util;
5 import org.netbeans.modules.xml.xdm.XDMModel;
6 import org.netbeans.modules.xml.xdm.nodes.Attribute;
7 import org.netbeans.modules.xml.xdm.nodes.Document;
8 import org.netbeans.modules.xml.xdm.nodes.Element;
9 import org.netbeans.modules.xml.xdm.nodes.Node;
10
11 /**
12  *
13  * @author nn136682
14  */

15 public class XPathFinderTest extends TestCase {
16     
17     public XPathFinderTest(String JavaDoc testName) {
18         super(testName);
19     }
20
21     private Document doc;
22     protected void setUp() throws Exception JavaDoc {
23         doc = Util.loadXdmDocument("visitor/address.xsd");
24     }
25
26     protected void tearDown() throws Exception JavaDoc {
27     }
28
29     public static Test suite() {
30         TestSuite suite = new TestSuite(XPathFinderTest.class);
31         
32         return suite;
33     }
34
35     public void testFindAndGetXpath() throws Exception JavaDoc {
36         String JavaDoc expr = "/company/employee[1]";
37         Document root = Util.loadXdmDocument("test.xml");
38         Node n = new XPathFinder().findNode(root, expr);
39         assertNotNull(n);
40         
41         String JavaDoc xpathString = XPathFinder.getXpath(root, n);
42         assertEquals("getXpath check round-trip", expr, xpathString);
43     }
44     
45     public void testFindNodeWithIndex() throws Exception JavaDoc {
46         String JavaDoc expr = "/schema/complexType[2]";
47         Node n = new XPathFinder().findNode(doc, expr);
48         assertNotNull(n);
49         assertEquals("attribute name=", "US-Address", n.getAttributes().getNamedItem("name").getNodeValue());
50         
51         String JavaDoc xpathString = XPathFinder.getXpath(doc, n);
52         assertEquals("checking round-trip", expr, xpathString);
53     }
54     
55     public void testSelectionByAttribute() throws Exception JavaDoc {
56         String JavaDoc expr = "/schema/simpleType[@name='US-State']/restriction/enumeration";
57         List JavaDoc<Node> nodes = new XPathFinder().findNodes(doc, expr);
58         assertEquals("testSelectionByAttribute", 54, nodes.size());
59         
60         String JavaDoc xpathString = XPathFinder.getXpath(doc, nodes.get(50));
61         assertEquals("testSelectionByAttribute.2", "/schema/simpleType[1]/restriction[1]/enumeration[51]", xpathString);
62     }
63
64     public void testFindAttributeNode() throws Exception JavaDoc {
65         String JavaDoc expr = "/schema/complexType[2]/complexContent[1]/extension[1]/@base";
66         Node n = new XPathFinder().findNode(doc, expr);
67         assertTrue("Attribute node", n instanceof Attribute);
68
69         Attribute attr = (Attribute) n;
70         assertEquals("Attribute value", "ipo:Address", attr.getNodeValue());
71         
72         String JavaDoc xpathString = XPathFinder.getXpath(doc, n);
73         assertEquals("checking round-trip", expr, xpathString);
74     }
75     
76     public void testWithDefaultNamespaceAndRootElementPrefix() throws Exception JavaDoc {
77         Document root = Util.loadXdmDocument("visitor/ipo.xml");
78         String JavaDoc expr = "/ipo:purchaseOrder/shipTo[1]/street[1]";
79         Node n = new XPathFinder().findNode(root, expr);
80         assertEquals("no default namespace", "street", n.getNodeName());
81         
82         String JavaDoc xpathString = XPathFinder.getXpath(root, n);
83         assertEquals("checking round-trip", expr, xpathString);
84     }
85      
86     public void testNonEmptyDefaultNamspacePrefix() throws Exception JavaDoc {
87         Document root = Util.loadXdmDocument("visitor/OrgChart.xsd");
88         String JavaDoc expr = "/xsd:schema/xsd:complexType[1]/xsd:sequence[1]/xsd:element[1]";
89         Node n = new XPathFinder().findNode(root, expr);
90
91         String JavaDoc xpathString = XPathFinder.getXpath(root, n);
92         assertEquals("checking round-trip", expr, xpathString);
93     }
94
95     public void testMixedPrefixesForSameNamespace() throws Exception JavaDoc {
96         Document root = Util.loadXdmDocument("visitor/Invoke1parent.bpel");
97         String JavaDoc expr = "/bpel_20:process[1]/bpel_20:sequence[1]/bpel_20:receive[1]";
98         Element n = (Element) new XPathFinder().findNode(root, expr);
99         assertEquals("MyRole.Invoke1parentOperation.Receive", n.getAttribute("name"));
100         String JavaDoc xpathString = XPathFinder.getXpath(root, n);
101         String JavaDoc expected = "/bpel_20:process/sequence[1]/bpel_20:receive[1]";
102         assertEquals("checking round-trip", expected, xpathString);
103     }
104
105     public void testMixedPrefixesForSameNamespace2() throws Exception JavaDoc {
106         Document root = Util.loadXdmDocument("visitor/Invoke1parent.bpel");
107         String JavaDoc expr = "/bpel_20:process[1]/sequence/receive[2]";
108         Element n = (Element) new XPathFinder().findNode(root, expr);
109         assertEquals("Invoke1parentOperation1", n.getAttribute("name"));
110         String JavaDoc xpathString = XPathFinder.getXpath(root, n);
111         String JavaDoc expected = "/bpel_20:process/sequence[1]/receive[2]";
112         assertEquals("checking round-trip", expected, xpathString);
113     }
114
115     private XDMModel xmlModel = null;
116 }
117
Popular Tags