📄 xpathtestbase.java
字号:
/* * $Header$ * $Revision$ * $Date$ * * ==================================================================== * * Copyright 2000-2002 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 junit.framework.TestCase;import org.jaxen.*;import org.jaxen.dom.DOMXPath;import org.jaxen.function.StringFunction;import org.jaxen.saxpath.helpers.XPathReaderFactory;import org.jaxen.pattern.Pattern;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public abstract class XPathTestBase extends TestCase{ protected static String VAR_URI = "http://jaxen.org/test-harness/var"; protected static String TESTS_XML = "xml/test/tests.xml"; protected static boolean verbose = false; protected static boolean debug = false; private ContextSupport contextSupport; protected XPathTestBase(String name) { super(name); } protected void setUp() throws Exception { this.contextSupport = null; System.setProperty(XPathReaderFactory.DRIVER_PROPERTY, ""); log("-----------------------------"); } public void log(String text) { log(verbose, text); } private void log(boolean actualVerbose, String text) { if (actualVerbose) System.out.println(text); } private void assertCountXPath(int expectedSize, Object context, String xpathStr) throws JaxenException { assertCountXPath2(expectedSize, context, xpathStr); } private Object assertCountXPath2(int expectedSize, Object context, String xpathStr) throws JaxenException { log(debug, " Select :: " + xpathStr); DOMXPath xpath = new DOMXPath(xpathStr); List results = xpath.selectNodes(getContext(context)); log(debug, " Expected Size :: " + expectedSize); log(debug, " Result Size :: " + results.size()); if (expectedSize != results.size()) { log(debug, " ## FAILED"); log(debug, " ## xpath: " + xpath + " = " + xpath.debug()); Iterator resultIter = results.iterator(); while (resultIter.hasNext()) { log(debug, " --> " + resultIter.next()); } } assertEquals(xpathStr, expectedSize, results.size()); if (expectedSize > 0) { return results.get(0); } return null; } private void assertInvalidXPath(Object context, String xpathStr) { try { log(debug, " Select :: " + xpathStr); DOMXPath xpath = new DOMXPath(xpathStr); List results = xpath.selectNodes(getContext(context)); log(debug, " Result Size :: " + results.size()); fail("An exception was expected."); } catch (JaxenException e) { log(debug, " Caught expected exception " + e.getMessage()); } } private void assertValueOfXPath(String expected, Object context, String xpathStr) throws JaxenException { DOMXPath xpath = new DOMXPath(xpathStr); Object node = xpath.evaluate(getContext(context)); String result = StringFunction.evaluate(node, getNavigator()); log(debug, " Select :: " + xpathStr); log(debug, " Expected :: " + expected); log(debug, " Result :: " + result); if (!expected.equals(result)) { log(debug, " ## FAILED"); log(debug, " ## xpath: " + xpath + " = " + xpath.debug()); } assertEquals(xpathStr, expected, result); } private Context getContext(Object contextNode) { Context context = new Context(getContextSupport()); List list = new ArrayList(1); list.add(contextNode); context.setNodeSet(list); return context; } private ContextSupport getContextSupport() { if (this.contextSupport == null) { this.contextSupport = new ContextSupport(new SimpleNamespaceContext(), XPathFunctionContext.getInstance(), new SimpleVariableContext(), getNavigator()); } return this.contextSupport; } protected abstract Navigator getNavigator(); protected abstract Object getDocument(String url) throws Exception; public void testGetNodeType() throws FunctionCallException, UnsupportedAxisException { Navigator nav = getNavigator(); Object document = nav.getDocument("xml/testNamespaces.xml"); int count = 0; Iterator descendantOrSelfAxisIterator = nav.getDescendantOrSelfAxisIterator(document); while (descendantOrSelfAxisIterator.hasNext()) { Object node = descendantOrSelfAxisIterator.next(); Iterator namespaceAxisIterator = nav.getNamespaceAxisIterator(node); while (namespaceAxisIterator.hasNext()) { count++; assertEquals("Node type mismatch", Pattern.NAMESPACE_NODE, nav.getNodeType(namespaceAxisIterator.next())); } } assertEquals(25, count); } /* test for jaxen-24 */ public void testJaxen24() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/jaxen24.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/body/div", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertCountXPath(1, context, "preceding::*[1]"); assertValueOfXPath("span", context, "local-name(preceding::*[1])"); } } /* jaxen-58 */ public void testJaxen58() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/jaxen24.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertCountXPath(0, context, "//preceding::x"); assertCountXPath(0, context, "//following::x"); assertCountXPath(0, context, "/descendant::*/preceding::x"); assertCountXPath(0, context, "/descendant::node()/preceding::x"); } } /* test for jaxen-3 */ public void testJaxen3() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("abd", context, "string()"); } } public void testStringFunction1() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/root", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("abd", context, "string()"); } } public void testStringFunction2() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml"; log("Document [" + url + "]"); Object document = nav.getDocument(url); XPath contextpath = new BaseXPath("/root/a", nav); log("Initial Context :: " + contextpath); List list = contextpath.selectNodes(document); Iterator iter = list.iterator(); while (iter.hasNext()) { Object context = iter.next(); assertValueOfXPath("a", context, "string()"); } } public void testStringFunction3() throws JaxenException { Navigator nav = getNavigator(); String url = "xml/simple.xml";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -