ValueOfTest 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.xpath;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.util.List;
    13
    14  import org.dom4j.AbstractTestCase;
    15  import org.dom4j.Element;
    16  import org.dom4j.Node;
    17  import org.dom4j.XPath;
    18
    19  /***
    20   * Test harness for the valueOf() function
    21   *
    22   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    23   * @version $Revision: 1.3 $
    24   */
    25  public class ValueOfTest extends AbstractTestCase {
    26      protected static String[] paths = {"/root", "//author", "//author/@name",
    27              "/root/author[1]", "/root/author[1]/@name", "/root/author[2]",
    28              "/root/author[2]/@name", "/root/author[3]",
    29              "/root/author[3]/@name", "name()", "name(.)", "name(..)",
    30              "name(child::node())", "name(parent::*)", "name(../*)",
    31              "name(../child::node())", "local-name()", "local-name(..)",
    32              "local-name(parent::*)", "local-name(../*)", "parent::*",
    33              "name(/.)", "name(/child::node())", "name(/*)", ".", "..", "../*",
    34              "../child::node()", "/.", "/*", "*", "/child::node()"};
    35
    36      public static void main(String[] args) {
    37          TestRunner.run(ValueOfTest.class);
    38      }
    39
    40      // Test case(s)
    41      // -------------------------------------------------------------------------
    42      public void testXPaths() throws Exception {
    43          Element root = document.getRootElement();
    44          List children = root.elements("author");
    45          Element child1 = (Element) children.get(0);
    46
    47          testXPath(document);
    48          testXPath(root);
    49          testXPath(child1);
    50      }
    51
    52      protected void testXPath(Node node) throws Exception {
    53          log("Testing XPath on: " + node);
    54          log("===============================");
    55
    56          int size = paths.length;
    57
    58          for (int i = 0; i < size; i++) {
    59              testXPath(node, paths[i]);
    60          }
    61      }
    62
    63      protected void testXPath(Node node, String xpathExpr) throws Exception {
    64          try {
    65              XPath xpath = node.createXPath(xpathExpr);
    66              String value = xpath.valueOf(node);
    67
    68              log("valueOf: " + xpathExpr + " is: " + value);
    69          } catch (Throwable e) {
    70              e.printStackTrace();
    71              assertTrue("Failed with exception: " + e, false);
    72          }
    73      }
    74  }
    75
    76  /*
    77   * Redistribution and use of this software and associated documentation
    78   * ("Software"), with or without modification, are permitted provided that the
    79   * following conditions are met:
    80   *
    81   * 1. Redistributions of source code must retain copyright statements and
    82   * notices. Redistributions must also contain a copy of this document.
    83   *
    84   * 2. Redistributions in binary form must reproduce the above copyright notice,
    85   * this list of conditions and the following disclaimer in the documentation
    86   * and/or other materials provided with the distribution.
    87   *
    88   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    89   * from this Software without prior written permission of MetaStuff, Ltd. For
    90   * written permission, please contact dom4j-info@metastuff.com.
    91   *
    92   * 4. Products derived from this Software may not be called "DOM4J" nor may
    93   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    94   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    95   *
    96   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    97   *
    98   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    99   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    102  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    103  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    104  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    105  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    106  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    107  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    108  * POSSIBILITY OF SUCH DAMAGE.
    109  *
    110  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    111  */