    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.util.List;
    65
    66  import javax.xml.parsers.DocumentBuilder;
    67  import javax.xml.parsers.DocumentBuilderFactory;
    68  import javax.xml.parsers.ParserConfigurationException;
    69
    70  import junit.framework.TestCase;
    71
    72  import org.jaxen.BaseXPath;
    73  import org.jaxen.FunctionCallException;
    74  import org.jaxen.JaxenException;
    75  import org.jaxen.dom.DOMXPath;
    76  import org.w3c.dom.Document;
    77  import org.w3c.dom.Element;
    78
    79  /***
    80   * @author Elliotte Rusty Harold
    81   *
    82   */
    83  public class LangTest extends TestCase {
    84
    85      private Document doc;
    86      private DocumentBuilder builder;
    87
    88      public void setUp() throws ParserConfigurationException
    89      {
    90          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    91          factory.setNamespaceAware(true);
    92          builder = factory.newDocumentBuilder();
    93          doc = builder.newDocument();
    94      }
    95
    96      public LangTest(String name) {
    97          super(name);
    98      }
    99
    100     public void testLangFunction()
    101       throws JaxenException {
    102
    103         BaseXPath xpath = new DOMXPath("//*[lang('en')]");
    104         Element a = doc.createElementNS("", "a");
    105         Element b = doc.createElementNS("", "b");
    106         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
    107         doc.appendChild(a);
    108         a.appendChild(b);
    109         Element x2 = doc.createElementNS("", "x");
    110         Element x3 = doc.createElementNS("", "x");
    111         Element x4 = doc.createElementNS("", "x");
    112         a.appendChild(x4);
    113         b.appendChild(x2);
    114         b.appendChild(x3);
    115         x2.appendChild(doc.createTextNode("2"));
    116         x3.appendChild(doc.createTextNode("3"));
    117         x4.appendChild(doc.createTextNode("4"));
    118
    119         List result = xpath.selectNodes(doc);
    120         assertEquals(3, result.size());
    121         assertEquals(b, result.get(0));
    122         assertEquals(x2, result.get(1));
    123         assertEquals(x3, result.get(2));
    124
    125     }
    126
    127     public void testLangFunctionSelectsNothing()
    128       throws JaxenException {
    129
    130         BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
    131         Element a = doc.createElementNS("", "a");
    132         Element b = doc.createElementNS("", "b");
    133         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "en");
    134         doc.appendChild(a);
    135         a.appendChild(b);
    136         Element x2 = doc.createElementNS("", "x");
    137         Element x3 = doc.createElementNS("", "x");
    138         Element x4 = doc.createElementNS("", "x");
    139         a.appendChild(x4);
    140         b.appendChild(x2);
    141         b.appendChild(x3);
    142         x2.appendChild(doc.createTextNode("2"));
    143         x3.appendChild(doc.createTextNode("3"));
    144         x4.appendChild(doc.createTextNode("4"));
    145
    146         List result = xpath.selectNodes(doc);
    147         assertEquals(0, result.size());
    148
    149     }
    150
    151     public void testLangFunctionSelectsSubcode()
    152       throws JaxenException {
    153
    154         BaseXPath xpath = new DOMXPath("//*[lang('fr')]");
    155         Element a = doc.createElementNS("", "a");
    156         Element b = doc.createElementNS("", "b");
    157         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    158         doc.appendChild(a);
    159         a.appendChild(b);
    160         Element x2 = doc.createElementNS("", "x");
    161         Element x3 = doc.createElementNS("", "x");
    162         Element x4 = doc.createElementNS("", "x");
    163         a.appendChild(x4);
    164         b.appendChild(x2);
    165         b.appendChild(x3);
    166         x2.appendChild(doc.createTextNode("2"));
    167         x3.appendChild(doc.createTextNode("3"));
    168         x4.appendChild(doc.createTextNode("4"));
    169
    170         List result = xpath.selectNodes(doc);
    171         assertEquals(3, result.size());
    172         assertEquals(b, result.get(0));
    173         assertEquals(x2, result.get(1));
    174         assertEquals(x3, result.get(2));
    175
    176     }
    177
    178     public void testHyphenRequiredAtEnd()
    179       throws JaxenException {
    180
    181         BaseXPath xpath = new DOMXPath("//*[lang('f')]");
    182         Element a = doc.createElementNS("", "a");
    183         Element b = doc.createElementNS("", "b");
    184         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    185         doc.appendChild(a);
    186         a.appendChild(b);
    187         Element x2 = doc.createElementNS("", "x");
    188         Element x3 = doc.createElementNS("", "x");
    189         Element x4 = doc.createElementNS("", "x");
    190         a.appendChild(x4);
    191         b.appendChild(x2);
    192         b.appendChild(x3);
    193         x2.appendChild(doc.createTextNode("2"));
    194         x3.appendChild(doc.createTextNode("3"));
    195         x4.appendChild(doc.createTextNode("4"));
    196
    197         List result = xpath.selectNodes(doc);
    198         assertEquals(0, result.size());
    199
    200     }
    201
    202     public void testLangFunctionSelectsEmptyNodeSet()
    203       throws JaxenException {
    204
    205         BaseXPath xpath = new DOMXPath("//*[lang(d)]");
    206         // This node-set will be empty. Therefore it's the same as the
    207         // empty string. Therefore it matches all languages.
    208         Element a = doc.createElementNS("", "a");
    209         Element b = doc.createElementNS("", "b");
    210         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    211         doc.appendChild(a);
    212         a.appendChild(b);
    213         Element x2 = doc.createElementNS("", "x");
    214         Element x3 = doc.createElementNS("", "x");
    215         Element x4 = doc.createElementNS("", "x");
    216         a.appendChild(x4);
    217         b.appendChild(x2);
    218         b.appendChild(x3);
    219         x2.appendChild(doc.createTextNode("2"));
    220         x3.appendChild(doc.createTextNode("3"));
    221         x4.appendChild(doc.createTextNode("4"));
    222
    223         List result = xpath.selectNodes(doc);
    224         assertEquals(0, result.size());
    225
    226     }
    227
    228     public void testLangFunctionSelectsNonEmptyNodeSet()
    229       throws JaxenException {
    230
    231         BaseXPath xpath = new DOMXPath("//*[lang(x)]");
    232         Element a = doc.createElementNS("", "a");
    233         Element b = doc.createElementNS("", "b");
    234         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    235         doc.appendChild(a);
    236         a.appendChild(b);
    237         Element x2 = doc.createElementNS("", "x");
    238         Element x3 = doc.createElementNS("", "x");
    239         Element x4 = doc.createElementNS("", "x");
    240         a.appendChild(x4);
    241         b.appendChild(x2);
    242         b.appendChild(x3);
    243         x2.appendChild(doc.createTextNode("fr"));
    244         x3.appendChild(doc.createTextNode("3"));
    245         x4.appendChild(doc.createTextNode("4"));
    246
    247         List result = xpath.selectNodes(doc);
    248         assertEquals(1, result.size());
    249         assertEquals(b, result.get(0));
    250
    251     }
    252
    253     public void testLangFunctionAppliedToNonElement()
    254       throws JaxenException {
    255
    256         BaseXPath xpath = new DOMXPath("//text()[lang('fr')]");
    257         Element a = doc.createElementNS("", "a");
    258         Element b = doc.createElementNS("", "b");
    259         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    260         doc.appendChild(a);
    261         a.appendChild(b);
    262         Element x2 = doc.createElementNS("", "x");
    263         Element x3 = doc.createElementNS("", "x");
    264         Element x4 = doc.createElementNS("", "x");
    265         a.appendChild(x4);
    266         b.appendChild(x2);
    267         b.appendChild(x3);
    268         x2.appendChild(doc.createTextNode("fr"));
    269         x3.appendChild(doc.createTextNode("3"));
    270         x4.appendChild(doc.createTextNode("4"));
    271
    272         List result = xpath.selectNodes(doc);
    273         assertEquals(2, result.size());
    274         assertEquals(x2.getFirstChild(), result.get(0));
    275         assertEquals(x3.getFirstChild(), result.get(1));
    276
    277     }
    278
    279     public void testLangFunctionAppliedToDocument()
    280       throws JaxenException {
    281
    282         BaseXPath xpath = new DOMXPath("lang('fr')");
    283         Element a = doc.createElementNS("", "a");
    284         Element b = doc.createElementNS("", "b");
    285         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    286         doc.appendChild(a);
    287         a.appendChild(b);
    288         Element x2 = doc.createElementNS("", "x");
    289         Element x3 = doc.createElementNS("", "x");
    290         Element x4 = doc.createElementNS("", "x");
    291         a.appendChild(x4);
    292         b.appendChild(x2);
    293         b.appendChild(x3);
    294         x2.appendChild(doc.createTextNode("fr"));
    295         x3.appendChild(doc.createTextNode("3"));
    296         x4.appendChild(doc.createTextNode("4"));
    297
    298         Boolean result = (Boolean) xpath.evaluate(doc);
    299         assertEquals(Boolean.FALSE, result);
    300
    301     }
    302
    303     public void testLangFunctionSelectsNumber()
    304       throws JaxenException {
    305
    306         BaseXPath xpath = new DOMXPath("//*[lang(3)]");
    307
    308         Element a = doc.createElementNS("", "a");
    309         Element b = doc.createElementNS("", "b");
    310         b.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang", "fr-CA");
    311         doc.appendChild(a);
    312         a.appendChild(b);
    313         Element x2 = doc.createElementNS("", "x");
    314         Element x3 = doc.createElementNS("", "x");
    315         Element x4 = doc.createElementNS("", "x");
    316         a.appendChild(x4);
    317         b.appendChild(x2);
    318         b.appendChild(x3);
    319         x2.appendChild(doc.createTextNode("2"));
    320         x3.appendChild(doc.createTextNode("3"));
    321         x4.appendChild(doc.createTextNode("4"));
    322
    323         List result = xpath.selectNodes(doc);
    324         assertEquals(0, result.size());
    325
    326     }
    327
    328     public void testLangFunctionRequiresOneArgument()
    329       throws JaxenException {
    330
    331         try {
    332             BaseXPath xpath = new DOMXPath("lang()");
    333             org.w3c.dom.Element a = doc.createElementNS("", "a");
    334             doc.appendChild(a);
    335             xpath.selectNodes(doc);
    336             fail("Allowed empty lang() function");
    337         }
    338         catch (FunctionCallException success) {
    339             assertNotNull(success.getMessage());
    340         }
    341
    342     }
    343
    344     public void testLangFunctionRequiresAtMostOneArgument()
    345       throws JaxenException {
    346
    347         try {
    348             BaseXPath xpath = new DOMXPath("lang('en', 'fr')");
    349             org.w3c.dom.Element a = doc.createElementNS("", "a");
    350             doc.appendChild(a);
    351             xpath.selectNodes(doc);
    352             fail("Allowed empty lang() function");
    353         }
    354         catch (FunctionCallException success) {
    355             assertNotNull(success.getMessage());
    356         }
    357
    358     }
    359
    360
    361 }