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