Index: src/main/org/jboss/test/jaxp/xpath/JaxpXPathBaseTestCase.java =================================================================== RCS file: src/main/org/jboss/test/jaxp/xpath/JaxpXPathBaseTestCase.java diff -N src/main/org/jboss/test/jaxp/xpath/JaxpXPathBaseTestCase.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/main/org/jboss/test/jaxp/xpath/JaxpXPathBaseTestCase.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,168 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2005, JBoss Inc., and individual contributors as indicated +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* This is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as +* published by the Free Software Foundation; either version 2.1 of +* the License, or (at your option) any later version. +* +* This software is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this software; if not, write to the Free +* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +* 02110-1301 USA, or see the FSF site: http://www.fsf.org. +*/ +package org.jboss.test.jaxp.xpath; + +import java.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathException; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import junit.framework.TestCase; +/** + * A Simple class for jaxp XPath Test Cases + * + * @author Aaron Walker + * + */ +public class JaxpXPathBaseTestCase extends TestCase +{ + protected static final String XML_STRING_SIMPLE = "" + + "" + + " " + + " e1" + + " " + + " " + + " e2" + + " " + + ""; + + protected static final String XML_STRING_NS = "" + + "" + + " " + + " e1" + + " " + + " " + + " e2" + + " " + + ""; + + protected void setUp() throws Exception + { + super.setUp(); + } + + protected void tearDown() throws Exception + { + super.tearDown(); + } + + public void testXPathDefaultFactoryCreate() + { + assertNotNull(newXpathFactoryInstance()); + } + + public void testSimpleXpathExpression() throws Exception + { + XPathFactory xpathFactory = newXpathFactoryInstance(); + XPath xpath = xpathFactory.newXPath(); + + Document doc = parseXML(XML_STRING_SIMPLE); + + String xpe1 = "/employees/employee"; + XPathExpression employeesXPath = xpath.compile(xpe1); + + NodeList nl = (NodeList)employeesXPath.evaluate(doc,XPathConstants.NODESET); + + assertNotNull(nl); + assertEquals(nl.getLength(),2); + assertEquals(nl.item(0).getTextContent().trim(),"e1"); + assertEquals(nl.item(1).getTextContent().trim(),"e2"); + } + + public void testNamespaceXpathExpression() throws Exception + { + XPathFactory xpathFactory = newXpathFactoryInstance(); + XPath xpath = xpathFactory.newXPath(); + xpath.setNamespaceContext(new JBossFooBarNamespaceContext()); + + Document doc = parseXML(XML_STRING_NS); + + String xpe1 = "/employees/employee"; + XPathExpression badXPath = xpath.compile(xpe1); + NodeList nl = (NodeList)badXPath.evaluate(doc,XPathConstants.NODESET); + assertNotNull(nl); + assertEquals(0,nl.getLength()); + + String xpe2 = "//foo:employee"; + XPathExpression empXPath = xpath.compile(xpe2); + NodeList nl2 = (NodeList)empXPath.evaluate(doc,XPathConstants.NODESET); + + assertNotNull(nl2); + assertEquals(2,nl2.getLength()); + assertEquals("e1",nl2.item(0).getTextContent().trim()); + assertEquals("e2",nl2.item(1).getTextContent().trim()); + } + + protected XPathFactory newXpathFactoryInstance() + { + return XPathFactory.newInstance(); + } + + protected Document parseXML(String xml) throws Exception + { + DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); + dbfactory.setNamespaceAware(true); + dbfactory.setXIncludeAware(true); + + DocumentBuilder parser = dbfactory.newDocumentBuilder(); + + ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); + + Document doc = parser.parse(is); + + return doc; + } + + protected class JBossFooBarNamespaceContext implements NamespaceContext + { + public String getNamespaceURI(String prefix) + { + return "http://www.jboss.org/foobar"; + } + + public String getPrefix(String namespaceURI) + { + return "foo"; + } + + public Iterator getPrefixes(String namespaceURI) + { + ArrayList list = new ArrayList(); + list.add("foo"); + return list.iterator(); + } + + } +}