KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestMakeElement


1 /*
2  * Copyright 2001 (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.3 2003/04/07 22:24:24 jstrachan Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
19
20 /** A test harness to test the O3DocumentHelper.makeElement() methodt
21   *
22   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
23   * @version $Revision: 1.3 $
24   */

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

145
Popular Tags