    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 addNode() methods on node
    14   *
    15   * @author Philippe Ombredanne
    16   */
    17  public class AddNodeTest extends AbstractTestCase {
    18      public static void main(String[] args) {
    19          TestRunner.run(AddNodeTest.class);
    20      }
    21
    22      // Test case(s)
    23      // -------------------------------------------------------------------------
    24      public void testDom4jAddNodeClone() {
    25          Document maindoc = DocumentHelper.createDocument();
    26          Element docroot = maindoc.addElement("document");
    27          Element header = docroot.addElement("header").addText("Some Text");
    28
    29          Document subdoc = DocumentHelper.createDocument();
    30          Element docroot2 = subdoc.addElement("document");
    31
    32          docroot2.add((Element) maindoc.selectSingleNode("/document/header")
    33                  .clone());
    34          assertEquals(subdoc.asXML(), maindoc.asXML());
    35      }
    36
    37      public void testDom4jAddNodeCreateCopy() {
    38          Document maindoc = DocumentHelper.createDocument();
    39          Element docroot = maindoc.addElement("document");
    40          Element header = docroot.addElement("header").addText("Some Text");
    41
    42          Document subdoc = DocumentHelper.createDocument();
    43          Element docroot2 = subdoc.addElement("document");
    44
    45          docroot2.add(((Element) maindoc.selectSingleNode("/document/header"))
    46                  .createCopy());
    47          assertEquals(subdoc.asXML(), maindoc.asXML());
    48      }
    49
    50      public void testDom4jAddNodeBug() {
    51          Document maindoc = DocumentHelper.createDocument();
    52          Element docroot = maindoc.addElement("document");
    53          Element header = docroot.addElement("header").addText("Some Text");
    54
    55          Document subdoc = DocumentHelper.createDocument();
    56          Element subroot = subdoc.addElement("document");
    57
    58          try {
    59              // add the header node from maindoc without clone... or createCopy
    60              subroot.add((Element) maindoc.selectSingleNode("/document/header"));
    61              fail();
    62          } catch (IllegalAddException e) {
    63              // the header already has a parent, this exception is normal
    64          }
    65      }
    66  }
    67
    68  /*
    69   * Redistribution and use of this software and associated documentation
    70   * ("Software"), with or without modification, are permitted provided that the
    71   * following conditions are met:
    72   *
    73   * 1. Redistributions of source code must retain copyright statements and
    74   * notices. Redistributions must also contain a copy of this document.
    75   *
    76   * 2. Redistributions in binary form must reproduce the above copyright notice,
    77   * this list of conditions and the following disclaimer in the documentation
    78   * and/or other materials provided with the distribution.
    79   *
    80   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    81   * from this Software without prior written permission of MetaStuff, Ltd. For
    82   * written permission, please contact dom4j-info@metastuff.com.
    83   *
    84   * 4. Products derived from this Software may not be called "DOM4J" nor may
    85   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    86   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    87   *
    88   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    89   *
    90   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    91   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    92   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    93   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    94   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    95   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    96   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    97   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    98   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    99   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    100  * POSSIBILITY OF SUCH DAMAGE.
    101  *
    102  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    103  */