1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2005 Elliotte Rusty Harold
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  package org.jaxen.test;
49  
50  import java.io.IOException;
51  import java.io.StringReader;
52  import java.util.List;
53  
54  import javax.xml.parsers.DocumentBuilder;
55  import javax.xml.parsers.DocumentBuilderFactory;
56  import javax.xml.parsers.ParserConfigurationException;
57  
58  import junit.framework.TestCase;
59  
60  import org.jaxen.BaseXPath;
61  import org.jaxen.FunctionCallException;
62  import org.jaxen.JaxenException;
63  import org.jaxen.dom.DOMXPath;
64  import org.w3c.dom.Attr;
65  import org.w3c.dom.Document;
66  import org.w3c.dom.Element;
67  import org.xml.sax.InputSource;
68  import org.xml.sax.SAXException;
69  
70  /***
71   * @author Elliotte Rusty Harold
72   *
73   */
74  public class IdTest extends TestCase {
75  
76      private Document doc;
77      private DocumentBuilder builder;
78      
79      public void setUp() throws ParserConfigurationException
80      {
81          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
82          factory.setNamespaceAware(true);
83          builder = factory.newDocumentBuilder();
84          doc = builder.newDocument();
85      }
86  
87      public IdTest(String name) {
88          super(name);
89      }
90  
91      public void testIDFunctionSelectsNothingInDocumentWithNoIds() 
92        throws JaxenException {
93          
94          BaseXPath xpath = new DOMXPath("id('p1')");
95          org.w3c.dom.Element a = doc.createElementNS("", "a");
96          org.w3c.dom.Element b = doc.createElementNS("", "b");
97          doc.appendChild(a);
98          a.appendChild(b);
99          org.w3c.dom.Element x2 = doc.createElementNS("", "x");
100         org.w3c.dom.Element x3 = doc.createElementNS("", "x");
101         org.w3c.dom.Element x4 = doc.createElementNS("", "x");
102         a.appendChild(x4);
103         b.appendChild(x2);
104         b.appendChild(x3);
105         x2.appendChild(doc.createTextNode("2"));
106         x3.appendChild(doc.createTextNode("3"));
107         x4.appendChild(doc.createTextNode("4"));
108         Attr id = doc.createAttribute("id");
109         id.setNodeValue("p1");
110         x2.setAttributeNode(id);
111         
112         List result = xpath.selectNodes(doc);
113         assertEquals(0, result.size());
114         
115     }    
116 
117     public void testIDFunctionRequiresAtLeastOneArgument() 
118       throws JaxenException {
119         
120         try {
121             BaseXPath xpath = new DOMXPath("id()");
122             org.w3c.dom.Element a = doc.createElementNS("", "a");
123             doc.appendChild(a);
124             xpath.selectNodes(doc);
125             fail("Allowed empty id() function");
126         }
127         catch (FunctionCallException success) {
128             assertNotNull(success.getMessage());
129         }
130         
131     }    
132 
133     public void testIDFunctionRequiresAtMostOneArgument() 
134       throws JaxenException {
135         
136         try {
137             BaseXPath xpath = new DOMXPath("id('p', 'q')");
138             org.w3c.dom.Element a = doc.createElementNS("", "a");
139             doc.appendChild(a);
140             xpath.selectNodes(doc);
141             fail("Allowed two-argument id() function");
142         }
143         catch (FunctionCallException success) {
144             assertNotNull(success.getMessage());
145         }
146         
147     }    
148 
149     public void testFindElementById() 
150       throws JaxenException, SAXException, IOException {
151         
152         BaseXPath xpath = new DOMXPath("id('p1')");
153         String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>]><root><a id='p1'/></root>";
154         StringReader reader = new StringReader(text);
155         InputSource in = new InputSource(reader);
156         Document doc = builder.parse(in);
157         List result = xpath.selectNodes(doc);
158         assertEquals(1, result.size());
159         Element a = (Element) result.get(0);
160         assertEquals("a", a.getNodeName());
161         
162     }    
163 
164     /* public void testFindElementByXMLId() 
165       throws JaxenException, SAXException, IOException {
166         
167         BaseXPath xpath = new DOMXPath("id('p1')");
168         String text = "<root><a xml:id='p1'/></root>";
169         StringReader reader = new StringReader(text);
170         InputSource in = new InputSource(reader);
171         Document doc = builder.parse(in);
172         List result = xpath.selectNodes(doc);
173         assertEquals(1, result.size());
174         Element a = (Element) result.get(0);
175         assertEquals("a", a.getNodeName());
176         
177     }    */
178 
179     public void testFindMultipleElementsByMultipleIDs() 
180       throws JaxenException, SAXException, IOException {
181         
182         BaseXPath xpath = new DOMXPath("id(//id)");
183         String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>]><root><id>p1</id><id>p2</id><id>p3</id><a id='p1'/><a id='p2'/></root>";
184         StringReader reader = new StringReader(text);
185         InputSource in = new InputSource(reader);
186         Document doc = builder.parse(in);
187         List result = xpath.selectNodes(doc);
188         assertEquals(2, result.size());
189         Element a1 = (Element) result.get(0);
190         Element a2 = (Element) result.get(1);
191         assertEquals("a", a1.getNodeName());
192         assertEquals("a", a2.getNodeName());
193         
194     }    
195 
196     public void testIdReturnsFirstElementWithMatchingId() 
197       throws JaxenException, SAXException, IOException {
198         
199         BaseXPath xpath = new DOMXPath("id('p1')");
200         String text = "<!DOCTYPE root [<!ATTLIST a id ID #REQUIRED>" +
201                 "<!ATTLIST b id ID #REQUIRED>]><root><a id='p1'/><b id='p1'/></root>";
202         StringReader reader = new StringReader(text);
203         InputSource in = new InputSource(reader);
204         Document doc = builder.parse(in);
205         List result = xpath.selectNodes(doc);
206         assertEquals(1, result.size());
207         Element a = (Element) result.get(0);
208         assertEquals("a", a.getNodeName());
209         
210     }    
211 
212 }