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