📄 basexpathtest.java
字号:
/* * $Header$ * $Revision$ * $Date$ * * ==================================================================== * * Copyright 2005 bob mcwhirter & James Strachan. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of the Jaxen Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ==================================================================== * This software consists of voluntary contributions made by many * individuals on behalf of the Jaxen Project and was originally * created by bob mcwhirter <bob@werken.com> and * James Strachan <jstrachan@apache.org>. For more information on the * Jaxen Project, please see <http://www.jaxen.org/>. * * $Id$ */package org.jaxen.test;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.ObjectOutputStream;import java.util.Iterator;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.jaxen.BaseXPath;import org.jaxen.JaxenException;import org.jaxen.NamespaceContext;import org.jaxen.SimpleNamespaceContext;import org.jaxen.XPath;import org.jaxen.dom.DOMXPath;import org.jaxen.dom.DocumentNavigator;import org.jaxen.dom.NamespaceNode;import org.jaxen.pattern.Pattern;import org.jaxen.saxpath.helpers.XPathReaderFactory;import org.w3c.dom.Attr;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.Text;import org.xml.sax.SAXException;import junit.framework.TestCase;/** * <p> * Tests for org.jaxen.BaseXPath. * </p> * * @author Elliotte Rusty Harold * @version 1.1b10 * */public class BaseXPathTest extends TestCase { private org.w3c.dom.Document doc; private DocumentBuilder builder; public BaseXPathTest(String name) { super(name); } protected void setUp() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); doc = factory.newDocumentBuilder().newDocument(); builder = factory.newDocumentBuilder(); } public void testSelectSingleNodeForContext() throws JaxenException { BaseXPath xpath = new DOMXPath("1 + 2"); String stringValue = xpath.stringValueOf(xpath); assertEquals("3", stringValue); Number numberValue = xpath.numberValueOf(xpath); assertEquals(3, numberValue.doubleValue(), 0.00001); } public void testParentOfSelection() throws JaxenException { /* html a img a <- return that node img <- select this node */ XPath xpath = new DOMXPath("(/html/a/img[contains(@src,'gif')])[2]/.."); org.w3c.dom.Element html = doc.createElementNS("", "html"); org.w3c.dom.Element a1 = doc.createElementNS("", "a"); org.w3c.dom.Element a2 = doc.createElementNS("", "a"); org.w3c.dom.Element img1 = doc.createElementNS("", "img"); org.w3c.dom.Attr img1_src = doc.createAttributeNS("", "src"); img1_src.setValue("1.gif"); org.w3c.dom.Element img2 = doc.createElementNS("", "img"); org.w3c.dom.Attr img2_src = doc.createAttributeNS("", "src"); img2_src.setValue("2.gif"); img1.setAttributeNode(img1_src); img2.setAttributeNode(img2_src); a1.appendChild(img1); a2.appendChild(img2); html.appendChild(a1); html.appendChild(a2); doc.appendChild(html); List result = xpath.selectNodes(doc); assertEquals(1, result.size()); assertEquals(a2, result.get(0)); } public void testEvaluateString() throws JaxenException { BaseXPath xpath = new DOMXPath("string(/*)"); doc.appendChild(doc.createElement("root")); String stringValue = (String) xpath.evaluate(doc); assertEquals("", stringValue); } public void testNumberValueOfEmptyNodeSetIsNaN() throws JaxenException { BaseXPath xpath = new DOMXPath("/x"); doc.appendChild(doc.createElement("root")); Double numberValue = (Double) xpath.numberValueOf(doc); assertTrue(numberValue.isNaN()); } public void testPathWithParentheses() throws JaxenException { BaseXPath xpath = new DOMXPath("(/root)/child"); Element root = doc.createElement("root"); doc.appendChild(root); Element child = doc.createElement("child"); root.appendChild(child); assertEquals(child, xpath.selectSingleNode(doc)); } public void testEvaluateWithMultiNodeAnswer() throws JaxenException { BaseXPath xpath = new DOMXPath("(/descendant-or-self::node())"); doc.appendChild(doc.createElement("root")); List result = (List) xpath.evaluate(doc); assertEquals(2, result.size()); } public void testValueOfEmptyListIsEmptyString() throws JaxenException { BaseXPath xpath = new DOMXPath("/element"); doc.appendChild(doc.createElement("root")); String stringValue = xpath.stringValueOf(doc); assertEquals("", stringValue); } public void testAllNodesQuery() throws JaxenException { BaseXPath xpath = new DOMXPath("//. | /"); org.w3c.dom.Element root = doc.createElementNS("http://www.example.org/", "root"); doc.appendChild(root); String stringValue = xpath.stringValueOf(doc); assertEquals("", stringValue); } public void testAncestorAxis() throws JaxenException { BaseXPath xpath = new DOMXPath("ancestor::*"); org.w3c.dom.Element root = doc.createElementNS("", "root"); org.w3c.dom.Element parent = doc.createElementNS("", "parent"); doc.appendChild(root); org.w3c.dom.Element child = doc.createElementNS("", "child"); root.appendChild(parent); parent.appendChild(child); List result = xpath.selectNodes(child); assertEquals(2, result.size()); assertEquals(root, result.get(0)); assertEquals(parent, result.get(1)); } public void testPrecedingSiblingAxisIsInDocumentOrder() throws JaxenException { BaseXPath xpath = new DOMXPath("preceding-sibling::*"); org.w3c.dom.Element root = doc.createElementNS("", "root"); doc.appendChild(root); org.w3c.dom.Element child1 = doc.createElementNS("", "child1"); root.appendChild(child1); org.w3c.dom.Element child2 = doc.createElementNS("", "child2"); root.appendChild(child2); org.w3c.dom.Element child3 = doc.createElementNS("", "child3"); root.appendChild(child3); List result = xpath.selectNodes(child3); assertEquals(2, result.size()); assertEquals(child1, result.get(0)); assertEquals(child2, result.get(1)); } public void testPrecedingAxisIsInDocumentOrder() throws JaxenException { BaseXPath xpath = new DOMXPath("preceding::*"); org.w3c.dom.Element root = doc.createElementNS("", "root"); doc.appendChild(root); org.w3c.dom.Element parent1 = doc.createElementNS("", "parent1"); root.appendChild(parent1); org.w3c.dom.Element parent2 = doc.createElementNS("", "parent2"); root.appendChild(parent2); org.w3c.dom.Element child1 = doc.createElementNS("", "child1"); parent2.appendChild(child1); org.w3c.dom.Element child2 = doc.createElementNS("", "child2"); parent2.appendChild(child2); org.w3c.dom.Element child3 = doc.createElementNS("", "child3"); parent2.appendChild(child3); List result = xpath.selectNodes(child3); assertEquals(3, result.size()); assertEquals(parent1, result.get(0)); assertEquals(child1, result.get(1)); assertEquals(child2, result.get(2)); } public void testPrecedingAxisWithPositionalPredicate() throws JaxenException { BaseXPath xpath = new DOMXPath("preceding::*[1]"); org.w3c.dom.Element root = doc.createElementNS("", "root"); doc.appendChild(root); org.w3c.dom.Element child1 = doc.createElementNS("", "child1"); root.appendChild(child1); org.w3c.dom.Element child2 = doc.createElementNS("", "child2"); root.appendChild(child2); org.w3c.dom.Element child3 = doc.createElementNS("", "child3"); root.appendChild(child3); List result = xpath.selectNodes(child3); assertEquals(1, result.size()); assertEquals(child2, result.get(0)); } public void testAncestorAxisWithPositionalPredicate() throws JaxenException { BaseXPath xpath = new DOMXPath("ancestor::*[1]"); org.w3c.dom.Element root = doc.createElementNS("", "root"); doc.appendChild(root); org.w3c.dom.Element child1 = doc.createElementNS("", "child1"); root.appendChild(child1); org.w3c.dom.Element child2 = doc.createElementNS("", "child2"); child1.appendChild(child2); org.w3c.dom.Element child3 = doc.createElementNS("", "child3"); child2.appendChild(child3); List result = xpath.selectNodes(child3); assertEquals(1, result.size()); assertEquals(child2, result.get(0)); } public void testAncestorOrSelfAxis() throws JaxenException { BaseXPath xpath = new DOMXPath("ancestor-or-self::*"); org.w3c.dom.Element root = doc.createElementNS("", "root"); org.w3c.dom.Element parent = doc.createElementNS("", "parent"); doc.appendChild(root); org.w3c.dom.Element child = doc.createElementNS("", "child"); root.appendChild(parent); parent.appendChild(child); List result = xpath.selectNodes(child); assertEquals(3, result.size()); assertEquals(root, result.get(0)); assertEquals(parent, result.get(1)); assertEquals(child, result.get(2)); } // test case for JAXEN-55 public void testAbbreviatedDoubleSlashAxis() throws JaxenException { BaseXPath xpath = new DOMXPath("//x"); 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(4, result.size()); assertEquals(x1, result.get(0)); assertEquals(x2, result.get(1)); assertEquals(x3, result.get(2)); assertEquals(x4, result.get(3)); } // test case for JAXEN-55 public void testAncestorFollowedByChildren() throws JaxenException { BaseXPath xpath = new DOMXPath("/a/b/x/ancestor::*/child::x"); 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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -