1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2000-2003 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
46   */
47  
48  
49  package org.jaxen.test;
50  
51  import junit.framework.TestCase;
52  
53  import java.io.IOException;
54  import java.io.StringReader;
55  import java.util.Iterator;
56  import java.util.List;
57  
58  import org.jaxen.JaxenException;
59  import org.jaxen.XPath;
60  import org.jaxen.jdom.JDOMXPath;
61  import org.jdom.Attribute;
62  import org.jdom.Document;
63  import org.jdom.Element;
64  import org.jdom.JDOMException;
65  import org.jdom.Namespace;
66  import org.jdom.Text;
67  import org.jdom.input.SAXBuilder;
68  import org.xml.sax.InputSource;
69  
70  public class JDOMXPathTest extends TestCase
71  {
72  
73      private static final String BASIC_XML = "xml/basic.xml";
74  
75      public JDOMXPathTest(String name)
76      {
77          super( name );
78      }
79  
80      public void testConstruction() throws JaxenException
81      {
82          new JDOMXPath( "/foo/bar/baz" );
83      }
84  
85      public void testSelection() throws JaxenException, JDOMException, IOException
86      {
87          XPath xpath = new JDOMXPath( "/foo/bar/baz" );
88  
89          SAXBuilder builder = new SAXBuilder();
90  
91          Document doc = builder.build( BASIC_XML );
92  
93          List results = xpath.selectNodes( doc );
94  
95          assertEquals( 3,
96                        results.size() );
97  
98          Iterator iter = results.iterator();
99  
100         assertEquals( "baz",
101                       ((Element)iter.next()).getName() );
102 
103         assertEquals( "baz",
104                       ((Element)iter.next()).getName() );
105 
106         assertEquals( "baz",
107                       ((Element)iter.next()).getName() );
108 
109         assertTrue( ! iter.hasNext() );
110     }
111     
112     
113     public void testGetDocumentNode() throws JaxenException, JDOMException, IOException
114     {
115         XPath xpath = new JDOMXPath( "/" );
116 
117         SAXBuilder builder = new SAXBuilder();
118 
119         Document doc = builder.build( BASIC_XML );
120 
121         Element root = doc.getRootElement();
122         List results = xpath.selectNodes( root );
123 
124         assertEquals( 1,
125                       results.size() );
126 
127         Iterator iter = results.iterator();
128 
129         assertEquals( doc, iter.next());
130 
131     }
132     
133     public void testJaxen148() throws JaxenException, JDOMException, IOException  {
134         String xml = "<xml-document><nodes><node>" +
135         "\ntest\n" + 
136         "</node></nodes></xml-document>";
137 
138         SAXBuilder builder = new SAXBuilder();
139         Document document = builder.build( new InputSource( new StringReader(xml) ) );
140         
141         JDOMXPath x = new JDOMXPath("/xml-document/nodes/node/text()");
142         Text t = (Text) x.selectSingleNode(document);
143         
144         assertEquals( "\ntest\n" , t.getText() );
145         
146     }
147     
148     
149     public void testJaxen53Text() throws JaxenException, JDOMException, IOException
150     {
151         XPath xpath = new JDOMXPath( "//data/text() " );
152 
153         SAXBuilder builder = new SAXBuilder();
154 
155         Document doc = builder.build( new StringReader("<root>\n<data>1</data>\n</root>") );
156 
157         List results = xpath.selectNodes( doc );
158 
159         assertEquals( 1,
160                       results.size() );
161 
162         Iterator iter = results.iterator();
163 
164         Text result = (Text) iter.next();
165         assertEquals( "1", result.getValue());
166 
167     }
168     
169     public void testJaxen20AttributeNamespaceNodes() throws JaxenException
170     {
171         Namespace ns1 = Namespace.getNamespace("p1", "www.acme1.org");
172         Namespace ns2 = Namespace.getNamespace("p2", "www.acme2.org");
173         Element element = new Element("test", ns1);
174         Attribute attribute = new Attribute("foo", "bar", ns2);
175         element.setAttribute(attribute); 
176         Document doc = new Document(element);
177         
178         XPath xpath = new JDOMXPath( "//namespace::node()" );
179 
180         List results = xpath.selectNodes( doc );
181 
182         assertEquals( 3,
183                       results.size() );
184 
185     }
186     
187     public void testNamespaceNodesAreInherited() throws JaxenException
188     {
189         Namespace ns0 = Namespace.getNamespace("p0", "www.acme0.org");
190         Namespace ns1 = Namespace.getNamespace("p1", "www.acme1.org");
191         Namespace ns2 = Namespace.getNamespace("p2", "www.acme2.org");
192         Element element = new Element("test", ns1);
193         Attribute attribute = new Attribute("foo", "bar", ns2);
194         element.setAttribute(attribute);
195         Element root = new Element("root", ns0);
196         root.addContent(element);
197         Document doc = new Document(root);
198         
199         XPath xpath = new JDOMXPath( "/*/*/namespace::node()" );
200 
201         List results = xpath.selectNodes( doc );
202 
203         assertEquals( 4, results.size() );
204 
205     }
206     
207 }