XPathBugTest xref

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.util.List;
    13
    14  /***
    15   * A test harness to test XPath expression evaluation in DOM4J
    16   *
    17   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    18   * @version $Revision: 1.4 $
    19   */
    20  public class XPathBugTest extends AbstractTestCase {
    21      public static void main(String[] args) {
    22          TestRunner.run(XPathBugTest.class);
    23      }
    24
    25      // Test case(s)
    26      // -------------------------------------------------------------------------
    27      public void testXPaths() throws Exception {
    28          Document document = getDocument("/xml/rabo1ae.xml");
    29          Element root = (Element) document
    30                  .selectSingleNode("/m:Msg/m:Contents/m:Content");
    31
    32          assertTrue("root is not null", root != null);
    33
    34          Namespace ns = root.getNamespaceForPrefix("ab");
    35
    36          assertTrue("Found namespace", ns != null);
    37
    38          System.out.println("Found: " + ns.getURI());
    39
    40          Element element = (Element) root
    41                  .selectSingleNode("ab:RaboPayLoad[@id='1234123']");
    42
    43          assertTrue("element is not null", element != null);
    44
    45          String value = element.valueOf("ab:AccountingEntry/ab:RateType");
    46
    47          assertEquals("RateType is correct", "CRRNT", value);
    48      }
    49
    50      /***
    51       * A bug found by Rob Lebowitz
    52       *
    53       * @throws Exception
    54       *             DOCUMENT ME!
    55       */
    56      public void testRobLebowitz() throws Exception {
    57          String text = "<ul>" + "    <ul>" + "        <li/>"
    58                  + "            <ul>" + "                <li/>"
    59                  + "            </ul>" + "        <li/>" + "    </ul>" + "</ul>";
    60
    61          Document document = DocumentHelper.parseText(text);
    62          List lists = document.selectNodes("//ul | //ol");
    63
    64          int count = 0;
    65
    66          for (int i = 0; i < lists.size(); i++) {
    67              Element list = (Element) lists.get(i);
    68              List nodes = list.selectNodes("ancestor::ul");
    69
    70              if ((nodes != null) && (nodes.size() > 0)) {
    71                  continue;
    72              }
    73
    74              nodes = list.selectNodes("ancestor::ol");
    75
    76              if ((nodes != null) && (nodes.size() > 0)) {
    77                  continue;
    78              }
    79          }
    80      }
    81
    82      /***
    83       * A bug found by Stefan which results in IndexOutOfBoundsException for
    84       * empty results
    85       *
    86       * @throws Exception
    87       *             DOCUMENT ME!
    88       */
    89      public void testStefan() throws Exception {
    90          String text = "<foo>hello</foo>";
    91          Document document = DocumentHelper.parseText(text);
    92          XPath xpath = DocumentHelper.createXPath("/x");
    93          Object value = xpath.evaluate(document);
    94      }
    95
    96      /***
    97       * Test found by Mike Skells
    98       *
    99       * @throws Exception
    100      *             DOCUMENT ME!
    101      */
    102     public void testMikeSkells() throws Exception {
    103         Document top = DocumentFactory.getInstance().createDocument();
    104         Element root = top.addElement("root");
    105         root.addElement("child1").addElement("child11");
    106         root.addElement("child2").addElement("child21");
    107         System.out.println(top.asXML());
    108
    109         XPath test1 = top.createXPath("/root/child1/child11");
    110         XPath test2 = top.createXPath("/root/child2/child21");
    111         Node position1 = test1.selectSingleNode(root);
    112         Node position2 = test2.selectSingleNode(root);
    113
    114         System.out.println("test1= " + test1);
    115         System.out.println("test2= " + test2);
    116         System.out.println("Position1 Xpath = " + position1.getUniquePath());
    117         System.out.println("Position2 Xpath = " + position2.getUniquePath());
    118
    119         System.out.println("test2.matches(position1) : "
    120                 + test2.matches(position1));
    121
    122         assertTrue("test1.matches(position1)", test1.matches(position1));
    123         assertTrue("test2.matches(position2)", test2.matches(position2));
    124
    125         assertTrue("test2.matches(position1) should be false", !test2
    126                 .matches(position1));
    127     }
    128 }
    129
    130 /*
    131  * Redistribution and use of this software and associated documentation
    132  * ("Software"), with or without modification, are permitted provided that the
    133  * following conditions are met:
    134  *
    135  * 1. Redistributions of source code must retain copyright statements and
    136  * notices. Redistributions must also contain a copy of this document.
    137  *
    138  * 2. Redistributions in binary form must reproduce the above copyright notice,
    139  * this list of conditions and the following disclaimer in the documentation
    140  * and/or other materials provided with the distribution.
    141  *
    142  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    143  * from this Software without prior written permission of MetaStuff, Ltd. For
    144  * written permission, please contact dom4j-info@metastuff.com.
    145  *
    146  * 4. Products derived from this Software may not be called "DOM4J" nor may
    147  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    148  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    149  *
    150  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    151  *
    152  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    153  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    154  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    155  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    156  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    157  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    158  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    159  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    160  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    161  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    162  * POSSIBILITY OF SUCH DAMAGE.
    163  *
    164  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    165  */