1 package org.netbeans.modules.xml.xdm.visitor; 2 import java.util.List ; 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 15 public class XPathFinderTest extends TestCase { 16 17 public XPathFinderTest(String testName) { 18 super(testName); 19 } 20 21 private Document doc; 22 protected void setUp() throws Exception { 23 doc = Util.loadXdmDocument("visitor/address.xsd"); 24 } 25 26 protected void tearDown() throws Exception { 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 { 36 String 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 xpathString = XPathFinder.getXpath(root, n); 42 assertEquals("getXpath check round-trip", expr, xpathString); 43 } 44 45 public void testFindNodeWithIndex() throws Exception { 46 String 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 xpathString = XPathFinder.getXpath(doc, n); 52 assertEquals("checking round-trip", expr, xpathString); 53 } 54 55 public void testSelectionByAttribute() throws Exception { 56 String expr = "/schema/simpleType[@name='US-State']/restriction/enumeration"; 57 List <Node> nodes = new XPathFinder().findNodes(doc, expr); 58 assertEquals("testSelectionByAttribute", 54, nodes.size()); 59 60 String 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 { 65 String 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 xpathString = XPathFinder.getXpath(doc, n); 73 assertEquals("checking round-trip", expr, xpathString); 74 } 75 76 public void testWithDefaultNamespaceAndRootElementPrefix() throws Exception { 77 Document root = Util.loadXdmDocument("visitor/ipo.xml"); 78 String 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 xpathString = XPathFinder.getXpath(root, n); 83 assertEquals("checking round-trip", expr, xpathString); 84 } 85 86 public void testNonEmptyDefaultNamspacePrefix() throws Exception { 87 Document root = Util.loadXdmDocument("visitor/OrgChart.xsd"); 88 String expr = "/xsd:schema/xsd:complexType[1]/xsd:sequence[1]/xsd:element[1]"; 89 Node n = new XPathFinder().findNode(root, expr); 90 91 String xpathString = XPathFinder.getXpath(root, n); 92 assertEquals("checking round-trip", expr, xpathString); 93 } 94 95 public void testMixedPrefixesForSameNamespace() throws Exception { 96 Document root = Util.loadXdmDocument("visitor/Invoke1parent.bpel"); 97 String 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 xpathString = XPathFinder.getXpath(root, n); 101 String 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 { 106 Document root = Util.loadXdmDocument("visitor/Invoke1parent.bpel"); 107 String 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 xpathString = XPathFinder.getXpath(root, n); 111 String 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 |