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: TestDefaultElement.java,v 1.4 2004/06/25 08:03:50 maartenc Exp $
8 */
9
10 package org.dom4j.tree;
11
12 import java.util.List;
13 import junit.framework.*;
14 import junit.textui.TestRunner;
15 import org.dom4j.*;
16
17 /***
18 *
19 * @author Maarten Coene
20 */
21 public class TestDefaultElement 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( TestDefaultElement.class );
29 }
30
31 public TestDefaultElement(String name) {
32 super(name);
33 }
34
35 // Test case(s)
36 //-------------------------------------------------------------------------
37 public void testBug894878() {
38 Element foo = DocumentFactory.getInstance().createElement("foo");
39 foo.addText("bla").addAttribute("foo", "bar");
40 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
41
42 foo = DocumentFactory.getInstance().createElement("foo");
43 foo.addAttribute("foo", "bar").addText("bla");
44 assertEquals("<foo foo=\"bar\">bla</foo>", foo.asXML());
45 }
46
47 public void testGetNamespacesForURI() throws Exception {
48 String xml =
49 "<schema targetNamespace='http://SharedTest.org/xsd' " +
50 " xmlns='http://www.w3.org/2001/XMLSchema' " +
51 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
52 " <complexType name='AllStruct'>" +
53 " <all>" +
54 " <element name='arString' type='xsd:string'/>" +
55 " <element name='varInt' type='xsd:int'/>" +
56 " </all>" +
57 " </complexType>" +
58 "</schema>";
59 Document doc = DocumentHelper.parseText(xml);
60 Element schema = doc.getRootElement();
61 List namespaces = schema.getNamespacesForURI("http://www.w3.org/2001/XMLSchema");
62
63 assertNotNull(namespaces);
64 assertEquals(2, namespaces.size());
65 }
66
67 public void testDeclaredNamespaces() throws Exception {
68 String xml = "<a xmlns:ns1=\"uri1\">" +
69 " <ns1:b/>" +
70 " <ns2:c xmlns:ns2=\"uri2\"/>" +
71 "</a>";
72 Document doc = DocumentHelper.parseText(xml);
73
74 Element a = doc.getRootElement();
75 List ns = a.declaredNamespaces();
76 assertEquals(1, ns.size());
77 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
78
79 Element b = a.element("b");
80 ns = b.declaredNamespaces();
81 assertEquals(0, ns.size());
82
83 Element c = a.element("c");
84 ns = c.declaredNamespaces();
85 assertEquals(1, ns.size());
86 assertSame(c.getNamespaceForPrefix("ns2"), ns.get(0));
87 }
88
89 public void testAdditionalNamespaces() throws Exception {
90 String xml = "<a xmlns:ns1=\"uri1\">" +
91 " <ns1:b/>" +
92 " <ns2:c xmlns:ns2=\"uri2\"/>" +
93 "</a>";
94 Document doc = DocumentHelper.parseText(xml);
95
96 Element a = doc.getRootElement();
97 List ns = a.additionalNamespaces();
98 assertEquals(1, ns.size());
99 assertSame(a.getNamespaceForPrefix("ns1"), ns.get(0));
100
101 Element b = a.element("b");
102 ns = b.additionalNamespaces();
103 assertEquals(0, ns.size());
104
105 Element c = a.element("c");
106 ns = c.additionalNamespaces();
107 assertEquals(0, ns.size());
108 }
109 }
110
111
112
113
114 /*
115 * Redistribution and use of this software and associated documentation
116 * ("Software"), with or without modification, are permitted provided
117 * that the following conditions are met:
118 *
119 * 1. Redistributions of source code must retain copyright
120 * statements and notices. Redistributions must also contain a
121 * copy of this document.
122 *
123 * 2. Redistributions in binary form must reproduce the
124 * above copyright notice, this list of conditions and the
125 * following disclaimer in the documentation and/or other
126 * materials provided with the distribution.
127 *
128 * 3. The name "DOM4J" must not be used to endorse or promote
129 * products derived from this Software without prior written
130 * permission of MetaStuff, Ltd. For written permission,
131 * please contact dom4j-info@metastuff.com.
132 *
133 * 4. Products derived from this Software may not be called "DOM4J"
134 * nor may "DOM4J" appear in their names without prior written
135 * permission of MetaStuff, Ltd. DOM4J is a registered
136 * trademark of MetaStuff, Ltd.
137 *
138 * 5. Due credit should be given to the DOM4J Project -
139 * http://www.dom4j.org
140 *
141 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
142 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
143 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
144 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
145 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
146 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
147 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
148 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
149 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
150 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
151 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
152 * OF THE POSSIBILITY OF SUCH DAMAGE.
153 *
154 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
155 *
156 * $Id: TestDefaultElement.java,v 1.4 2004/06/25 08:03:50 maartenc Exp $
157 */