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