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