    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.Comparator;
    13
    14  import org.dom4j.dom.DOMDocument;
    15  import org.dom4j.dom.DOMDocumentFactory;
    16  import org.dom4j.util.NodeComparator;
    17
    18  /***
    19   * A test harness to test the clone() methods on Nodes
    20   *
    21   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    22   * @version $Revision: 1.6 $
    23   */
    24  public class CloneTest extends AbstractTestCase {
    25      private Comparator comparator = new NodeComparator();
    26
    27      public static void main(String[] args) {
    28          TestRunner.run(CloneTest.class);
    29      }
    30
    31      // Test case(s)
    32      // -------------------------------------------------------------------------
    33      public void testBug1148333() {
    34          DOMDocumentFactory factory = (DOMDocumentFactory) DOMDocumentFactory
    35                  .getInstance();
    36          DOMDocument doc = (DOMDocument) factory.createDocument();
    37          Element el = doc.addElement("root");
    38          el.addNamespace("pref2", "uri2");
    39
    40          DOMDocument clone = (DOMDocument) doc.cloneNode(true);
    41
    42          assertNotSame(doc, clone);
    43          assertNodesEqual(doc, clone);
    44      }
    45
    46      public void testElementWithNamespaceClone() {
    47          Element element = DocumentFactory.getInstance()
    48                  .createElement("element");
    49          element.addNamespace("prefix", "uri");
    50          Element clone = (Element) element.clone();
    51
    52          assertNotSame(element, clone);
    53          assertNodesEqual(element, clone);
    54      }
    55
    56      public void testDocumentClone() throws Exception {
    57          document.setName("doc1");
    58
    59          Document doc2 = (Document) document.clone();
    60
    61          assertNotSame(document, doc2);
    62          assertNodesEqual(document, doc2);
    63      }
    64
    65      public void testAddCloneToOtherElement() {
    66          DocumentFactory factory = DocumentFactory.getInstance();
    67          Document doc = factory.createDocument();
    68          Element root = doc.addElement("root");
    69          Element parent1 = root.addElement("parent");
    70          Element child1 = parent1.addElement("child");
    71
    72          Element parent2 = (Element) parent1.clone();
    73          root.add(parent2);
    74
    75          assertSame("parent not correct", root, parent2.getParent());
    76          assertSame("document not correct", doc, parent2.getDocument());
    77
    78          Element child2 = parent2.element("child");
    79
    80          assertNotSame("child not cloned", child1, child2);
    81          assertSame("parent not correct", parent2, child2.getParent());
    82          assertSame("document not correct", doc, child2.getDocument());
    83      }
    84
    85      public void testRootElementClone() throws Exception {
    86          testElementClone(document.getRootElement());
    87      }
    88
    89      public void testAuthorElementClone() throws Exception {
    90          testElementClone((Element) document.selectSingleNode("//author"));
    91      }
    92
    93      public void testRootCompare1() throws Exception {
    94          Document doc2 = (Document) document.clone();
    95          Element author = doc2.getRootElement();
    96          author.addAttribute("foo", "bar");
    97
    98          assertTrue("Documents are not equal", comparator
    99                  .compare(document, doc2) != 0);
    100     }
    101
    102     public void testRootCompare2() throws Exception {
    103         Document doc2 = (Document) document.clone();
    104         Element author = doc2.getRootElement();
    105
    106         author.addText("foo");
    107
    108         assertTrue("Documents are not equal", comparator
    109                 .compare(document, doc2) != 0);
    110     }
    111
    112     public void testAuthorCompare1() throws Exception {
    113         Document doc2 = (Document) document.clone();
    114         Element author = (Element) doc2.selectSingleNode("//author");
    115         author.addAttribute("name", "James Strachan");
    116
    117         assertTrue("Documents are not equal", comparator
    118                 .compare(document, doc2) != 0);
    119     }
    120
    121     public void testAuthorCompare2() throws Exception {
    122         Document doc2 = (Document) document.clone();
    123         Element author = (Element) doc2.selectSingleNode("//author");
    124
    125         author.addText("foo");
    126
    127         assertTrue("Documents are not equal", comparator
    128                 .compare(document, doc2) != 0);
    129     }
    130
    131     protected void testElementClone(Element element) throws Exception {
    132         Element element2 = (Element) element.clone();
    133
    134         assertTrue("Returned a new Element", element2 != element);
    135         assertNull("New element has no parent", element2.getParent());
    136         assertNull("New element has no Document", element2.getDocument());
    137
    138         assertTrue("Element fragments are equal", comparator.compare(element,
    139                 element2) == 0);
    140     }
    141 }
    142
    143 /*
    144  * Redistribution and use of this software and associated documentation
    145  * ("Software"), with or without modification, are permitted provided that the
    146  * following conditions are met:
    147  *
    148  * 1. Redistributions of source code must retain copyright statements and
    149  * notices. Redistributions must also contain a copy of this document.
    150  *
    151  * 2. Redistributions in binary form must reproduce the above copyright notice,
    152  * this list of conditions and the following disclaimer in the documentation
    153  * and/or other materials provided with the distribution.
    154  *
    155  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    156  * from this Software without prior written permission of MetaStuff, Ltd. For
    157  * written permission, please contact dom4j-info@metastuff.com.
    158  *
    159  * 4. Products derived from this Software may not be called "DOM4J" nor may
    160  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    161  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    162  *
    163  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    164  *
    165  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    166  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    167  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    168  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    169  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    170  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    171  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    172  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    173  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    174  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    175  * POSSIBILITY OF SUCH DAMAGE.
    176  *
    177  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    178  */