1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright (C) 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
13   * are met:
14   * 
15   * 1. Redistributions of source code must retain the above copyright
16   *    notice, this list of conditions, and the following disclaimer.
17   *
18   * 2. Redistributions in binary form must reproduce the above copyright
19   *    notice, this list of conditions, and the disclaimer that follows 
20   *    these conditions in the documentation and/or other materials 
21   *    provided with the distribution.
22   *
23   * 3. The name "Jaxen" must not be used to endorse or promote products
24   *    derived from this software without prior written permission.  For
25   *    written permission, please contact license@jaxen.org.
26   * 
27   * 4. Products derived from this software may not be called "Jaxen", nor
28   *    may "Jaxen" appear in their name, without prior written permission
29   *    from the Jaxen Project Management (pm@jaxen.org).
30   * 
31   * In addition, we request (but do not require) that you include in the 
32   * end-user documentation provided with the redistribution and/or in the 
33   * software itself an acknowledgement equivalent to the following:
34   *     "This product includes software developed by the
35   *      Jaxen Project (http://www.jaxen.org/)."
36   * Alternatively, the acknowledgment may be graphical using the logos 
37   * available at http://www.jaxen.org/
38   *
39   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50   * SUCH DAMAGE.
51   *
52   * ====================================================================
53   * This software consists of voluntary contributions made by many 
54   * individuals on behalf of the Jaxen Project and was originally 
55   * created by bob mcwhirter <bob@werken.com> and 
56   * James Strachan <jstrachan@apache.org>.  For more information on the 
57   * Jaxen Project, please see <http://www.jaxen.org/>.
58   * 
59   * $Id$
60   */
61  
62  package org.jaxen.function;
63  
64  import java.io.IOException;
65  import java.util.List;
66  
67  import javax.xml.parsers.DocumentBuilder;
68  import javax.xml.parsers.DocumentBuilderFactory;
69  import javax.xml.parsers.ParserConfigurationException;
70  
71  import junit.framework.TestCase;
72  
73  import org.jaxen.BaseXPath;
74  import org.jaxen.FunctionCallException;
75  import org.jaxen.JaxenException;
76  import org.jaxen.dom.DOMXPath;
77  import org.w3c.dom.Document;
78  import org.w3c.dom.Element;
79  import org.xml.sax.SAXException;
80  
81  /***
82   * @author Elliotte Rusty Harold
83   *
84   */
85  public class LangTest extends TestCase {
86  
87      private Document doc;
88      private DocumentBuilder builder;
89      
90      public void setUp() throws ParserConfigurationException, SAXException, IOException
91      {
92          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
93          factory.setNamespaceAware(true);
94          builder = factory.newDocumentBuilder();
95          doc = builder.newDocument();
96      }
97  
98      public LangTest(String name) {
99          super(name);
100     }
101 
102     public void testLangFunction() 
103       throws JaxenException {
104         
105         BaseXPath xpath = new DOMXPath("//*[lang('en')]");
106         Element a = doc.createElementNS("", "a");
107         Element b = doc.createElementNS("", "b");
108         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
109         doc.appendChild(a);
110         a.appendChild(b);
111         Element x2 = doc.createElementNS("", "x");
112         Element x3 = doc.createElementNS("", "x");
113         Element x4 = doc.createElementNS("", "x");
114         a.appendChild(x4);
115         b.appendChild(x2);
116         b.appendChild(x3);
117         x2.appendChild(doc.createTextNode("2"));
118         x3.appendChild(doc.createTextNode("3"));
119         x4.appendChild(doc.createTextNode("4"));
120         
121         List result = xpath.selectNodes(doc);
122         assertEquals(3, result.size());
123         assertEquals(b, result.get(0));
124         assertEquals(x2, result.get(1));
125         assertEquals(x3, result.get(2));
126         
127     }    
128 
129     public void testLangFunctionSelectsNothing() 
130       throws JaxenException {
131         
132         BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
133         Element a = doc.createElementNS("", "a");
134         Element b = doc.createElementNS("", "b");
135         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
136         doc.appendChild(a);
137         a.appendChild(b);
138         Element x2 = doc.createElementNS("", "x");
139         Element x3 = doc.createElementNS("", "x");
140         Element x4 = doc.createElementNS("", "x");
141         a.appendChild(x4);
142         b.appendChild(x2);
143         b.appendChild(x3);
144         x2.appendChild(doc.createTextNode("2"));
145         x3.appendChild(doc.createTextNode("3"));
146         x4.appendChild(doc.createTextNode("4"));
147         
148         List result = xpath.selectNodes(doc);
149         assertEquals(0, result.size());
150         
151     }    
152 
153     public void testLangFunctionSelectsSubcode() 
154       throws JaxenException {
155         
156         BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
157         Element a = doc.createElementNS("", "a");
158         Element b = doc.createElementNS("", "b");
159         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
160         doc.appendChild(a);
161         a.appendChild(b);
162         Element x2 = doc.createElementNS("", "x");
163         Element x3 = doc.createElementNS("", "x");
164         Element x4 = doc.createElementNS("", "x");
165         a.appendChild(x4);
166         b.appendChild(x2);
167         b.appendChild(x3);
168         x2.appendChild(doc.createTextNode("2"));
169         x3.appendChild(doc.createTextNode("3"));
170         x4.appendChild(doc.createTextNode("4"));
171         
172         List result = xpath.selectNodes(doc);
173         assertEquals(3, result.size());
174         assertEquals(b, result.get(0));
175         assertEquals(x2, result.get(1));
176         assertEquals(x3, result.get(2));
177         
178     }    
179 
180     public void testHyphenRequiredAtEnd() 
181       throws JaxenException {
182         
183         BaseXPath xpath = new DOMXPath("//*[lang('f')]");
184         Element a = doc.createElementNS("", "a");
185         Element b = doc.createElementNS("", "b");
186         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
187         doc.appendChild(a);
188         a.appendChild(b);
189         Element x2 = doc.createElementNS("", "x");
190         Element x3 = doc.createElementNS("", "x");
191         Element x4 = doc.createElementNS("", "x");
192         a.appendChild(x4);
193         b.appendChild(x2);
194         b.appendChild(x3);
195         x2.appendChild(doc.createTextNode("2"));
196         x3.appendChild(doc.createTextNode("3"));
197         x4.appendChild(doc.createTextNode("4"));
198         
199         List result = xpath.selectNodes(doc);
200         assertEquals(0, result.size());
201         
202     }    
203 
204     public void testLangFunctionSelectsEmptyNodeSet() 
205       throws JaxenException {
206         
207         BaseXPath xpath = new DOMXPath("//*[lang(d)]");
208         // This node-set will be empty. Therefore it's the same as the
209         // empty string. Therefore it matches all languages.
210         Element a = doc.createElementNS("", "a");
211         Element b = doc.createElementNS("", "b");
212         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
213         doc.appendChild(a);
214         a.appendChild(b);
215         Element x2 = doc.createElementNS("", "x");
216         Element x3 = doc.createElementNS("", "x");
217         Element x4 = doc.createElementNS("", "x");
218         a.appendChild(x4);
219         b.appendChild(x2);
220         b.appendChild(x3);
221         x2.appendChild(doc.createTextNode("2"));
222         x3.appendChild(doc.createTextNode("3"));
223         x4.appendChild(doc.createTextNode("4"));
224         
225         List result = xpath.selectNodes(doc);
226         assertEquals(0, result.size());
227         
228     }    
229 
230     public void testLangFunctionSelectsNonEmptyNodeSet() 
231       throws JaxenException {
232         
233         BaseXPath xpath = new DOMXPath("//*[lang(x)]");
234         Element a = doc.createElementNS("", "a");
235         Element b = doc.createElementNS("", "b");
236         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
237         doc.appendChild(a);
238         a.appendChild(b);
239         Element x2 = doc.createElementNS("", "x");
240         Element x3 = doc.createElementNS("", "x");
241         Element x4 = doc.createElementNS("", "x");
242         a.appendChild(x4);
243         b.appendChild(x2);
244         b.appendChild(x3);
245         x2.appendChild(doc.createTextNode("fr"));
246         x3.appendChild(doc.createTextNode("3"));
247         x4.appendChild(doc.createTextNode("4"));
248         
249         List result = xpath.selectNodes(doc);
250         assertEquals(1, result.size());
251         assertEquals(b, result.get(0));
252         
253     }    
254 
255     public void testLangFunctionSelectsNumber() 
256       throws JaxenException {
257         
258         BaseXPath xpath = new DOMXPath("//*[lang(3)]");
259         
260         Element a = doc.createElementNS("", "a");
261         Element b = doc.createElementNS("", "b");
262         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
263         doc.appendChild(a);
264         a.appendChild(b);
265         Element x2 = doc.createElementNS("", "x");
266         Element x3 = doc.createElementNS("", "x");
267         Element x4 = doc.createElementNS("", "x");
268         a.appendChild(x4);
269         b.appendChild(x2);
270         b.appendChild(x3);
271         x2.appendChild(doc.createTextNode("2"));
272         x3.appendChild(doc.createTextNode("3"));
273         x4.appendChild(doc.createTextNode("4"));
274         
275         List result = xpath.selectNodes(doc);
276         assertEquals(0, result.size());
277         
278     }    
279 
280     public void testLangFunctionRequiresOneArgument() 
281       throws JaxenException {
282         
283         try {
284             BaseXPath xpath = new DOMXPath("lang()");
285             org.w3c.dom.Element a = doc.createElementNS("", "a");
286             doc.appendChild(a);
287             xpath.selectNodes(doc);
288             fail("Allowed empty lang() function");
289         }
290         catch (FunctionCallException success) {
291             assertNotNull(success.getMessage());
292         }
293         
294     }    
295 
296     public void testLangFunctionRequiresAtMostOneArgument() 
297       throws JaxenException {
298         
299         try {
300             BaseXPath xpath = new DOMXPath("lang('en', 'fr')");
301             org.w3c.dom.Element a = doc.createElementNS("", "a");
302             doc.appendChild(a);
303             xpath.selectNodes(doc);
304             fail("Allowed empty lang() function");
305         }
306         catch (FunctionCallException success) {
307             assertNotNull(success.getMessage());
308         }
309         
310     }    
311 
312 
313 }