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: TestMakeElement.java,v 1.5 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 DocumentHelper.makeElement() methodt
17    *
18    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
19    * @version $Revision: 1.5 $
20    */
21  public class TestMakeElement extends AbstractTestCase {
22  
23      public static void main( String[] args ) {
24          TestRunner.run( suite() );
25      }
26      
27      public static Test suite() {
28          return new TestSuite( TestMakeElement.class );
29      }
30      
31      public TestMakeElement(String name) {
32          super(name);
33      }
34  
35      // Test case(s)
36      //-------------------------------------------------------------------------                    
37      public void testMakeElement() throws Exception {
38          Document doc = DocumentHelper.createDocument();
39          
40          Element c = DocumentHelper.makeElement( doc, "a/b/c" );
41          assertTrue( "Should return a valid element", c != null );
42          
43          Element c2 = DocumentHelper.makeElement( doc, "a/b/c" );
44          
45          assertTrue( "Found same element again", c == c2 );
46          
47          c.addAttribute( "x", "123" );
48          
49          Node found = doc.selectSingleNode( "/a/b/c[@x='123']" );
50          
51          assertEquals( "Found same node via XPath", c, found );
52          
53          Element b = c.getParent();
54          
55          Element e = DocumentHelper.makeElement( b, "c/d/e" );
56          
57          assertTrue( "Should return a valid element", e != null );
58          
59          Element e2 = DocumentHelper.makeElement( b, "c/d/e" );
60          
61          assertTrue( "Found same element again", e == e2 );
62          
63          e.addAttribute( "y", "456" );
64          
65          found = b.selectSingleNode( "c/d/e[@y='456']" );
66          
67          assertEquals( "Found same node via XPath", e, found );        
68      }
69      
70      public void testMakeQualifiedElement() throws Exception {
71          Document doc = DocumentHelper.createDocument();
72          Element root = doc.addElement( "root" );
73          root.addNamespace( "", "defaultURI" );
74          root.addNamespace( "foo", "fooURI" );
75          root.addNamespace( "bar", "barURI" );
76          
77          Element c = DocumentHelper.makeElement( doc, "root/foo:b/bar:c" );
78          assertTrue( "Should return a valid element", c != null );
79          
80          assertEquals( "c has a valid namespace", "barURI", c.getNamespaceURI() );
81          
82          Element b = c.getParent();
83          
84          assertEquals( "b has a valid namespace", "fooURI", b.getNamespaceURI() );
85          
86          log( "Created: " + c );
87          
88          Element c2 = DocumentHelper.makeElement( doc, "root/foo:b/bar:c" );
89          assertTrue( "Found same element again", c == c2 );
90      }
91      
92  }
93  
94  
95  
96  
97  /*
98   * Redistribution and use of this software and associated documentation
99   * ("Software"), with or without modification, are permitted provided
100  * that the following conditions are met:
101  *
102  * 1. Redistributions of source code must retain copyright
103  *    statements and notices.  Redistributions must also contain a
104  *    copy of this document.
105  *
106  * 2. Redistributions in binary form must reproduce the
107  *    above copyright notice, this list of conditions and the
108  *    following disclaimer in the documentation and/or other
109  *    materials provided with the distribution.
110  *
111  * 3. The name "DOM4J" must not be used to endorse or promote
112  *    products derived from this Software without prior written
113  *    permission of MetaStuff, Ltd.  For written permission,
114  *    please contact dom4j-info@metastuff.com.
115  *
116  * 4. Products derived from this Software may not be called "DOM4J"
117  *    nor may "DOM4J" appear in their names without prior written
118  *    permission of MetaStuff, Ltd. DOM4J is a registered
119  *    trademark of MetaStuff, Ltd.
120  *
121  * 5. Due credit should be given to the DOM4J Project - 
122  *    http://www.dom4j.org
123  *
124  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
125  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
126  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
127  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
128  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
129  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
130  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
131  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
132  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
133  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
134  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
135  * OF THE POSSIBILITY OF SUCH DAMAGE.
136  *
137  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
138  *
139  * $Id: TestMakeElement.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $
140  */