    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 org.dom4j.AbstractTestCase;
    13  import org.dom4j.Document;
    14  import org.dom4j.Element;
    15  import org.dom4j.Node;
    16
    17  /***
    18   * Tests the selectSingleNode method
    19   *
    20   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    21   * @version $Revision: 1.4 $
    22   */
    23  public class SelectSingleNodeTest extends AbstractTestCase {
    24      public static void main(String[] args) {
    25          TestRunner.run(SelectSingleNodeTest.class);
    26      }
    27
    28      // Test case(s)
    29      // -------------------------------------------------------------------------
    30      public void testSelectSingleNode() throws Exception {
    31          Document document = getDocument("/xml/test/jimBrain.xml");
    32          Node node = document.selectSingleNode("/properties/client/threadsafe");
    33          assertTrue("Found a valid node", node != null);
    34
    35          Element server = (Element) document
    36                  .selectSingleNode("/properties/server");
    37          assertTrue("Found a valid server", server != null);
    38
    39          Element root = document.getRootElement();
    40          server = (Element) root.selectSingleNode("/properties/server");
    41          assertTrue("Found a valid server", server != null);
    42
    43          // try finding it via a relative path
    44          server = (Element) document.selectSingleNode("properties/server");
    45          assertTrue("Found a valid server", server != null);
    46
    47          // now lets use a relative path
    48          Element connection = (Element) server.selectSingleNode("db/connection");
    49          assertTrue("Found a valid connection", connection != null);
    50      }
    51
    52      /***
    53       * Test out Steen's bug
    54       *
    55       * @throws Exception
    56       *             DOCUMENT ME!
    57       */
    58      public void testSteensBug() throws Exception {
    59          Document document = getDocument("/xml/schema/personal.xsd");
    60
    61          String xpath = "/xs:schema/xs:element[@name='person']";
    62          assertNotNull("element is null", document.selectSingleNode(xpath));
    63
    64          Element root = document.getRootElement();
    65
    66          assertNotNull("element is null", root.selectSingleNode(xpath));
    67      }
    68  }
    69
    70  /*
    71   * Redistribution and use of this software and associated documentation
    72   * ("Software"), with or without modification, are permitted provided that the
    73   * following conditions are met:
    74   *
    75   * 1. Redistributions of source code must retain copyright statements and
    76   * notices. Redistributions must also contain a copy of this document.
    77   *
    78   * 2. Redistributions in binary form must reproduce the above copyright notice,
    79   * this list of conditions and the following disclaimer in the documentation
    80   * and/or other materials provided with the distribution.
    81   *
    82   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    83   * from this Software without prior written permission of MetaStuff, Ltd. For
    84   * written permission, please contact dom4j-info@metastuff.com.
    85   *
    86   * 4. Products derived from this Software may not be called "DOM4J" nor may
    87   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    88   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    89   *
    90   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    91   *
    92   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    93   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    94   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    95   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    96   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    97   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    98   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    99   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    100  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    102  * POSSIBILITY OF SUCH DAMAGE.
    103  *
    104  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    105  */