001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xml;
023:
024: import java.io.ByteArrayInputStream;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027:
028: import javax.xml.namespace.NamespaceContext;
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.xpath.XPath;
032: import javax.xml.xpath.XPathConstants;
033: import javax.xml.xpath.XPathExpression;
034: import javax.xml.xpath.XPathFactory;
035:
036: import junit.framework.TestCase;
037:
038: import org.w3c.dom.Document;
039: import org.w3c.dom.NodeList;
040:
041: /**
042: * A Simple class for jaxp XPath Test Cases
043: *
044: * @author <a href="mailto:a.walker@base2services.com">Aaron Walker</a>
045: * @version $Revision: 57211 $
046: */
047: public class JaxpXPathBaseTestCase extends TestCase {
048: protected static final String XML_STRING_SIMPLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
049: + "<employees>"
050: + " <employee>"
051: + " <name>e1</name>"
052: + " </employee>"
053: + " <employee>"
054: + " <name>e2</name>"
055: + " </employee>" + "</employees>";
056:
057: protected static final String XML_STRING_NS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
058: + "<foo:employees xmlns:foo=\"http://www.jboss.org/foobar\">"
059: + " <foo:employee>"
060: + " <name>e1</name>"
061: + " </foo:employee>"
062: + " <foo:employee>"
063: + " <name>e2</name>"
064: + " </foo:employee>"
065: + "</foo:employees>";
066:
067: protected void setUp() throws Exception {
068: super .setUp();
069: }
070:
071: protected void tearDown() throws Exception {
072: super .tearDown();
073: }
074:
075: public void testXPathDefaultFactoryCreate() {
076: assertNotNull(newXpathFactoryInstance());
077: }
078:
079: public void testSimpleXpathExpression() throws Exception {
080: XPathFactory xpathFactory = newXpathFactoryInstance();
081: XPath xpath = xpathFactory.newXPath();
082:
083: Document doc = parseXML(XML_STRING_SIMPLE);
084:
085: String xpe1 = "/employees/employee";
086: XPathExpression employeesXPath = xpath.compile(xpe1);
087:
088: NodeList nl = (NodeList) employeesXPath.evaluate(doc,
089: XPathConstants.NODESET);
090:
091: assertNotNull(nl);
092: assertEquals(nl.getLength(), 2);
093: assertEquals(nl.item(0).getTextContent().trim(), "e1");
094: assertEquals(nl.item(1).getTextContent().trim(), "e2");
095: }
096:
097: public void testNamespaceXpathExpression() throws Exception {
098: XPathFactory xpathFactory = newXpathFactoryInstance();
099: XPath xpath = xpathFactory.newXPath();
100: xpath.setNamespaceContext(new JBossFooBarNamespaceContext());
101:
102: Document doc = parseXML(XML_STRING_NS);
103:
104: String xpe1 = "/employees/employee";
105: XPathExpression badXPath = xpath.compile(xpe1);
106: NodeList nl = (NodeList) badXPath.evaluate(doc,
107: XPathConstants.NODESET);
108: assertNotNull(nl);
109: assertEquals(0, nl.getLength());
110:
111: String xpe2 = "//foo:employee";
112: XPathExpression empXPath = xpath.compile(xpe2);
113: NodeList nl2 = (NodeList) empXPath.evaluate(doc,
114: XPathConstants.NODESET);
115:
116: assertNotNull(nl2);
117: assertEquals(2, nl2.getLength());
118: assertEquals("e1", nl2.item(0).getTextContent().trim());
119: assertEquals("e2", nl2.item(1).getTextContent().trim());
120: }
121:
122: protected XPathFactory newXpathFactoryInstance() {
123: return XPathFactory.newInstance();
124: }
125:
126: protected Document parseXML(String xml) throws Exception {
127: DocumentBuilderFactory dbfactory = DocumentBuilderFactory
128: .newInstance();
129: dbfactory.setNamespaceAware(true);
130: dbfactory.setXIncludeAware(true);
131:
132: DocumentBuilder parser = dbfactory.newDocumentBuilder();
133:
134: ByteArrayInputStream is = new ByteArrayInputStream(xml
135: .getBytes());
136:
137: Document doc = parser.parse(is);
138:
139: return doc;
140: }
141:
142: protected class JBossFooBarNamespaceContext implements
143: NamespaceContext {
144: public String getNamespaceURI(String prefix) {
145: return "http://www.jboss.org/foobar";
146: }
147:
148: public String getPrefix(String namespaceURI) {
149: return "foo";
150: }
151:
152: public Iterator getPrefixes(String namespaceURI) {
153: ArrayList list = new ArrayList();
154: list.add("foo");
155: return list.iterator();
156: }
157: }
158: }
|