KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > JaxpXPathBaseTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.xml;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.xml.namespace.NamespaceContext JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.xpath.XPath JavaDoc;
32 import javax.xml.xpath.XPathConstants JavaDoc;
33 import javax.xml.xpath.XPathExpression JavaDoc;
34 import javax.xml.xpath.XPathFactory JavaDoc;
35
36 import junit.framework.TestCase;
37
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  * A Simple class for jaxp XPath Test Cases
43  *
44  * @author <a HREF="mailto:a.walker@base2services.com">Aaron Walker</a>
45  * @version $Revision: 44765 $
46  */

47 public class JaxpXPathBaseTestCase extends TestCase
48 {
49    protected static final String JavaDoc XML_STRING_SIMPLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
50          + "<employees>"
51          + " <employee>"
52          + " <name>e1</name>"
53          + " </employee>"
54          + " <employee>"
55          + " <name>e2</name>"
56          + " </employee>" + "</employees>";
57
58    protected static final String JavaDoc XML_STRING_NS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
59          + "<foo:employees xmlns:foo=\"http://www.jboss.org/foobar\">"
60          + " <foo:employee>"
61          + " <name>e1</name>"
62          + " </foo:employee>"
63          + " <foo:employee>"
64          + " <name>e2</name>"
65          + " </foo:employee>"
66          + "</foo:employees>";
67
68    protected void setUp() throws Exception JavaDoc
69    {
70       super.setUp();
71    }
72
73    protected void tearDown() throws Exception JavaDoc
74    {
75       super.tearDown();
76    }
77
78    public void testXPathDefaultFactoryCreate()
79    {
80       assertNotNull(newXpathFactoryInstance());
81    }
82
83    public void testSimpleXpathExpression() throws Exception JavaDoc
84    {
85       XPathFactory JavaDoc xpathFactory = newXpathFactoryInstance();
86       XPath JavaDoc xpath = xpathFactory.newXPath();
87
88       Document JavaDoc doc = parseXML(XML_STRING_SIMPLE);
89
90       String JavaDoc xpe1 = "/employees/employee";
91       XPathExpression JavaDoc employeesXPath = xpath.compile(xpe1);
92
93       NodeList JavaDoc nl = (NodeList JavaDoc) employeesXPath.evaluate(doc,
94             XPathConstants.NODESET);
95
96       assertNotNull(nl);
97       assertEquals(nl.getLength(), 2);
98       assertEquals(nl.item(0).getTextContent().trim(), "e1");
99       assertEquals(nl.item(1).getTextContent().trim(), "e2");
100    }
101
102    public void testNamespaceXpathExpression() throws Exception JavaDoc
103    {
104       XPathFactory JavaDoc xpathFactory = newXpathFactoryInstance();
105       XPath JavaDoc xpath = xpathFactory.newXPath();
106       xpath.setNamespaceContext(new JBossFooBarNamespaceContext());
107
108       Document JavaDoc doc = parseXML(XML_STRING_NS);
109
110       String JavaDoc xpe1 = "/employees/employee";
111       XPathExpression JavaDoc badXPath = xpath.compile(xpe1);
112       NodeList JavaDoc nl = (NodeList JavaDoc) badXPath.evaluate(doc, XPathConstants.NODESET);
113       assertNotNull(nl);
114       assertEquals(0, nl.getLength());
115
116       String JavaDoc xpe2 = "//foo:employee";
117       XPathExpression JavaDoc empXPath = xpath.compile(xpe2);
118       NodeList JavaDoc nl2 = (NodeList JavaDoc) empXPath.evaluate(doc, XPathConstants.NODESET);
119
120       assertNotNull(nl2);
121       assertEquals(2, nl2.getLength());
122       assertEquals("e1", nl2.item(0).getTextContent().trim());
123       assertEquals("e2", nl2.item(1).getTextContent().trim());
124    }
125
126    protected XPathFactory JavaDoc newXpathFactoryInstance()
127    {
128       return XPathFactory.newInstance();
129    }
130
131    protected Document JavaDoc parseXML(String JavaDoc xml) throws Exception JavaDoc
132    {
133       DocumentBuilderFactory JavaDoc dbfactory = DocumentBuilderFactory.newInstance();
134       dbfactory.setNamespaceAware(true);
135       dbfactory.setXIncludeAware(true);
136
137       DocumentBuilder JavaDoc parser = dbfactory.newDocumentBuilder();
138
139       ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(xml.getBytes());
140
141       Document JavaDoc doc = parser.parse(is);
142
143       return doc;
144    }
145
146    protected class JBossFooBarNamespaceContext implements NamespaceContext JavaDoc
147    {
148       public String JavaDoc getNamespaceURI(String JavaDoc prefix)
149       {
150          return "http://www.jboss.org/foobar";
151       }
152
153       public String JavaDoc getPrefix(String JavaDoc namespaceURI)
154       {
155          return "foo";
156       }
157
158       public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI)
159       {
160          ArrayList JavaDoc list = new ArrayList JavaDoc();
161          list.add("foo");
162          return list.iterator();
163       }
164    }
165 }
Popular Tags