📄 basexpathtest.java
字号:
xpath.selectNodes(doc); fail("Allowed union with non-node-set"); } catch (JaxenException ex) { assertNotNull(ex.getMessage()); } } public void testSelectSingleNodeSelectsNothing() throws JaxenException { BaseXPath xpath = new DOMXPath("id('p1')"); org.w3c.dom.Element a = doc.createElementNS("", "a"); doc.appendChild(a); Object result = xpath.selectSingleNode(doc); assertNull(result); } public void testSAXPathExceptionThrownFromConstructor() { System.setProperty( XPathReaderFactory.DRIVER_PROPERTY, "java.lang.String" ); try { new DOMXPath("id('p1')"); } catch (JaxenException e) { assertNotNull(e.getMessage()); } finally { System.setProperty( XPathReaderFactory.DRIVER_PROPERTY, "" ); } } public void testBooleanValueOfEmptyNodeSetIsFalse() throws JaxenException { BaseXPath xpath = new DOMXPath("/b/c"); org.w3c.dom.Element a = doc.createElementNS("", "a"); doc.appendChild(a); List result = xpath.selectNodes(doc); assertTrue(! xpath.booleanValueOf(result)); } public void testAddNamespaceWithNonSimpleNamespaceContext() throws JaxenException { BaseXPath xpath = new DOMXPath("/b/c"); xpath.setNamespaceContext(new NamespaceContext() { public String translateNamespacePrefixToUri(String prefix) { return prefix; } }); try { xpath.addNamespace("pre", "foo"); fail("Added namespace"); } catch (JaxenException ex) { assertNotNull(ex.getMessage()); } } public void testDebug() throws JaxenException { BaseXPath xpath = new DOMXPath("/b/c"); assertEquals( "[(DefaultXPath): [(DefaultAbsoluteLocationPath): [(DefaultNameStep): b]/[(DefaultNameStep): c]]]", xpath.debug() ); } public void testGetRootExpr() throws JaxenException { BaseXPath xpath = new DOMXPath("/b/c"); assertTrue(xpath.getRootExpr() instanceof org.jaxen.expr.LocationPath); } public void testUnionUsesDocumentOrder() throws JaxenException { BaseXPath xpath = new DOMXPath("/descendant::x | /a | /a/b"); org.w3c.dom.Element a = doc.createElementNS("", "a"); org.w3c.dom.Element b = doc.createElementNS("", "b"); doc.appendChild(a); org.w3c.dom.Element x1 = doc.createElementNS("", "x"); x1.appendChild(doc.createTextNode("1")); a.appendChild(x1); a.appendChild(b); org.w3c.dom.Element x2 = doc.createElementNS("", "x"); org.w3c.dom.Element x3 = doc.createElementNS("", "x"); org.w3c.dom.Element x4 = doc.createElementNS("", "x"); a.appendChild(x4); b.appendChild(x2); b.appendChild(x3); x2.appendChild(doc.createTextNode("2")); x3.appendChild(doc.createTextNode("3")); x4.appendChild(doc.createTextNode("4")); List result = xpath.selectNodes(doc); assertEquals(6, result.size()); assertEquals(a, result.get(0)); assertEquals(x1, result.get(1)); assertEquals(b, result.get(2)); assertEquals(x2, result.get(3)); assertEquals(x3, result.get(4)); assertEquals(x4, result.get(5)); } public void testArithmeticAssociativity() throws JaxenException { XPath xpath = new DOMXPath("2+1-1+1"); Double result = (Double) xpath.evaluate(doc); assertEquals(3, result.intValue()); } public void testLogicalAssociativity() throws JaxenException { XPath xpath = new DOMXPath("false() or true() and true() and false()"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testRelationalAssociativity3() throws JaxenException { XPath xpath = new DOMXPath("3 > 2 > 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testRelationalAssociativity4() throws JaxenException { XPath xpath = new DOMXPath("4 > 3 > 2 > 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testRelationalGTAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("5 > 4 > 3 > 2 > 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testRelationalLTAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("1 < 2 < 3 < 4 < 5"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testRelationalLEAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("1 <= 2 <= 3 <= 4 <= 5"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testRelationalGEAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("5 >= 4 >= 3 >= 2 >= 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testRelationalGEAssociativity3() throws JaxenException { XPath xpath = new DOMXPath("3 >= 2 >= 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testRelationalGEAssociativity2() throws JaxenException { XPath xpath = new DOMXPath("2 >= 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testRelationalGEAssociativity4() throws JaxenException { XPath xpath = new DOMXPath("4 >= 3 >= 2 >= 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } // This is the same test but with parentheses to make explicit // how the previous test should be evaluated. public void testRelationalAssociativity5P() throws JaxenException { XPath xpath = new DOMXPath("((((5 > 4) > 3) > 2) > 1)"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testInequalityAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("2 != 3 != 1 != 4 != 0"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } // This is the same test but with parentheses to make explicit // how the previous test should be evaluated. public void testInequalityAssociativity5P() throws JaxenException { XPath xpath = new DOMXPath("(((2 != 3) != 1) != 4) != 0"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testInequalityAssociativity5B() throws JaxenException { XPath xpath = new DOMXPath("2 != 3 != 1 != 4 != 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } // This is the same test but with parentheses to make explicit // how the previous test should be evaluated. public void testInequalityAssociativity5BP() throws JaxenException { XPath xpath = new DOMXPath("(((2 != 3) != 1) != 4) != 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testEqualityAssociativity5() throws JaxenException { XPath xpath = new DOMXPath("2 = 3 = 1 = 4 = 0"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } // This is the same test but with parentheses to make explicit // how the previous test should be evaluated. public void testEqualityAssociativity5P() throws JaxenException { XPath xpath = new DOMXPath("(((2 = 3) = 1) = 4) = 0"); Boolean result = (Boolean) xpath.evaluate(doc); assertTrue(result.booleanValue()); } public void testEqualityAssociativity5B() throws JaxenException { XPath xpath = new DOMXPath("2 = 3 = 1 = 4 = 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } // This is the same test but with parentheses to make explicit // how the previous test should be evaluated. public void testEqualityAssociativity5BP() throws JaxenException { XPath xpath = new DOMXPath("(((2 = 3) = 1) = 4) = 1"); Boolean result = (Boolean) xpath.evaluate(doc); assertFalse(result.booleanValue()); } public void testMoreComplexArithmeticAssociativity() throws JaxenException { XPath xpath = new DOMXPath("1+2+1-1+1"); Double result = (Double) xpath.evaluate(doc); assertEquals(4, result.intValue()); } public void testMostComplexArithmeticAssociativity() throws JaxenException { XPath xpath = new DOMXPath("1+1+2+1-1+1"); Double result = (Double) xpath.evaluate(doc); assertEquals(5, result.intValue()); } public void testSimplerArithmeticAssociativity() throws JaxenException { XPath xpath = new DOMXPath("1-1+1"); Double result = (Double) xpath.evaluate(doc); assertEquals(1, result.intValue()); } public void testNamespaceNodesComeBeforeAttributeNodesInDocumentOrder() throws JaxenException { org.w3c.dom.Element root = doc.createElementNS("http://www.example.org", "pre:b"); doc.appendChild(root); root.setAttribute("name", "value"); XPath xpath = new DOMXPath("/*/attribute::* | /*/namespace::node()"); List result = xpath.selectNodes(doc); assertTrue(((org.w3c.dom.Node) result.get(0)).getNodeType() == Pattern.NAMESPACE_NODE); assertTrue(((org.w3c.dom.Node) result.get(1)).getNodeType() == Pattern.NAMESPACE_NODE); assertTrue(((org.w3c.dom.Node) result.get(2)).getNodeType() == Node.ATTRIBUTE_NODE); // now flip the order of the statement and retest xpath = new DOMXPath("/*/namespace::node() | /*/attribute::* "); result = xpath.selectNodes(doc); assertTrue(((org.w3c.dom.Node) result.get(0)).getNodeType() == Pattern.NAMESPACE_NODE); assertTrue(((org.w3c.dom.Node) result.get(1)).getNodeType() == Pattern.NAMESPACE_NODE); assertTrue(((org.w3c.dom.Node) result.get(2)).getNodeType() == Node.ATTRIBUTE_NODE); } public void testJaxen97() throws JaxenException { // jaxen 97 claims this expression throws an exception. new DOMXPath("/aaa:element/text()"); } public void testAttributeNodesOnParentComeBeforeNamespaceNodesInChildInDocumentOrder() throws JaxenException { org.w3c.dom.Element root = doc.createElement("root"); doc.appendChild(root); root.setAttribute("name", "value"); Element child = doc.createElementNS("http://www.example.org", "pre:child"); root.appendChild(child); XPath xpath = new DOMXPath("/*/*/namespace::node() | //attribute::* "); List result = xpath.selectNodes(doc); assertEquals(3, result.size()); assertTrue(((org.w3c.dom.Node) result.get(0)).getNodeType() == Node.ATTRIBUTE_NODE); assertTrue(((org.w3c.dom.Node) result.get(1)).getNodeType() == Pattern.NAMESPACE_NODE); } public void testJaxen107() throws JaxenException { org.w3c.dom.Element a = doc.createElementNS("http://www.a.com/", "a:foo"); doc.appendChild(a); Element b = doc.createElementNS("http://www.b.com/", "b:bar"); a.appendChild(b); XPath xpath = new DOMXPath("/a:foo/b:bar/namespace::*/parent::*"); SimpleNamespaceContext context1 = new SimpleNamespaceContext(); context1.addNamespace("a", "http://www.a.com/"); context1.addNamespace("b", "http://www.b.com/"); xpath.setNamespaceContext(context1); List result = xpath.selectNodes(doc); assertEquals(1, result.size()); assertEquals(b, result.get(0)); } public void testJaxen107FromFile() throws JaxenException, SAXException, IOException { doc = builder.parse(new File("xml/testNamespaces.xml")); XPath xpath = new DOMXPath("/Template/Application2/namespace::*/parent::*"); List result = xpath.selectNodes(doc); assertEquals(1, result.size()); } public void testSelectNodesReturnsANonNodeSet() throws JaxenException { XPath xpath = new DOMXPath("1 + 2 + 3"); List result = xpath.selectNodes(doc); assertEquals(1, result.size()); } public void testNonElementContextNode() throws JaxenException { org.w3c.dom.Element a = doc.createElementNS("http://www.a.com/", "a:foo"); doc.appendChild(a); Text b = doc.createTextNode("ready"); a.appendChild(b); XPath xpath = new DOMXPath(".."); List result = (List) xpath.evaluate(b); assertEquals(1, result.size()); assertEquals(a, result.get(0)); } public void testNonNodeContext() throws JaxenException { org.w3c.dom.Element a = doc.createElementNS("http://www.a.com/", "a:foo"); doc.appendChild(a); Text b = doc.createTextNode("ready"); a.appendChild(b); XPath xpath = new DOMXPath(".."); try { xpath.evaluate("String"); fail("Allowed String as context"); } catch (ClassCastException ex) { // success } } public void testIsSerializable() throws JaxenException, IOException { BaseXPath path = new BaseXPath("//foo", new DocumentNavigator()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(path); oos.close(); assertTrue(out.toByteArray().length > 0); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -