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;
9   
10  import junit.textui.TestRunner;
11  
12  import org.dom4j.io.SAXReader;
13  import org.dom4j.util.UserDataAttribute;
14  import org.dom4j.util.UserDataDocumentFactory;
15  import org.dom4j.util.UserDataElement;
16  
17  /***
18   * Tests the UserDataDocumentFactory
19   * 
20   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
21   * @version $Revision: 1.4 $
22   */
23  public class UserDataTest extends AbstractTestCase {
24      /*** Input XML file to read */
25      private static final String INPUT_XML_FILE = "/xml/web.xml";
26  
27      private Object userData = new Double(1.23456);
28  
29      public static void main(String[] args) {
30          TestRunner.run(UserDataTest.class);
31      }
32  
33      // Test case(s)
34      // -------------------------------------------------------------------------
35      public void testSetData() throws Exception {
36          Element root = getRootElement();
37  
38          assertTrue("Element instanceof UserDataElement",
39                  root instanceof UserDataElement);
40  
41          root.setData(userData);
42  
43          assertTrue("Stored user data!", root.getData() == userData);
44  
45          log("root: " + root);
46  
47          assertUserData(root, userData);
48  
49          Element cloned = (Element) root.clone();
50          assertTrue("Cloned new instance", cloned != root);
51          assertUserData(cloned, userData);
52  
53          cloned = root.createCopy();
54          assertTrue("Cloned new instance", cloned != root);
55          assertUserData(cloned, userData);
56      }
57  
58      public void testCloneAttribute() throws Exception {
59          Element root = getRootElement();
60          root.addAttribute("name", "value");
61  
62          Attribute attribute = root.attribute("name");
63          assertTrue(attribute instanceof UserDataAttribute);
64  
65          Element cloned = (Element) root.clone();
66          Attribute clonedAttribute = cloned.attribute("name");
67          assertTrue(clonedAttribute instanceof UserDataAttribute);
68      }
69  
70      public void testNewAdditions() throws Exception {
71          Element root = getRootElement();
72  
73          Element newElement = root.addElement("foo1234");
74          assertTrue("New Element is a UserDataElement",
75                  newElement instanceof UserDataElement);
76  
77          root.addAttribute("bar456", "123");
78  
79          Attribute newAttribute = root.attribute("bar456");
80  
81          assertTrue("New Attribute is a UserDataAttribute",
82                  newAttribute instanceof UserDataAttribute);
83      }
84  
85      public void testNewDocument() throws Exception {
86          DocumentFactory factory = UserDataDocumentFactory.getInstance();
87          Document document = factory.createDocument();
88  
89          Element root = document.addElement("root");
90          assertTrue("Root Element is a UserDataElement",
91                  root instanceof UserDataElement);
92  
93          Element newElement = root.addElement("foo1234");
94          assertTrue("New Element is a UserDataElement",
95                  newElement instanceof UserDataElement);
96  
97          root.addAttribute("bar456", "123");
98  
99          Attribute newAttribute = root.attribute("bar456");
100 
101         assertTrue("New Attribute is a UserDataAttribute",
102                 newAttribute instanceof UserDataAttribute);
103     }
104 
105     // Implementation methods
106     // -------------------------------------------------------------------------
107     protected void assertUserData(Element root, Object data) throws Exception {
108         Object result = root.getData();
109 
110         assertTrue("No user data!", result != null);
111         assertTrue("Stored user data correctly", data.equals(result));
112     }
113 
114     protected void setUp() throws Exception {
115         super.setUp();
116 
117         SAXReader reader = new SAXReader();
118         reader.setDocumentFactory(UserDataDocumentFactory.getInstance());
119         document = getDocument(INPUT_XML_FILE, reader);
120     }
121 }
122 
123 /*
124  * Redistribution and use of this software and associated documentation
125  * ("Software"), with or without modification, are permitted provided that the
126  * following conditions are met:
127  * 
128  * 1. Redistributions of source code must retain copyright statements and
129  * notices. Redistributions must also contain a copy of this document.
130  * 
131  * 2. Redistributions in binary form must reproduce the above copyright notice,
132  * this list of conditions and the following disclaimer in the documentation
133  * and/or other materials provided with the distribution.
134  * 
135  * 3. The name "DOM4J" must not be used to endorse or promote products derived
136  * from this Software without prior written permission of MetaStuff, Ltd. For
137  * written permission, please contact dom4j-info@metastuff.com.
138  * 
139  * 4. Products derived from this Software may not be called "DOM4J" nor may
140  * "DOM4J" appear in their names without prior written permission of MetaStuff,
141  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
142  * 
143  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
144  * 
145  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
146  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
147  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
148  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
149  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
150  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
151  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
152  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
153  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
154  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
155  * POSSIBILITY OF SUCH DAMAGE.
156  * 
157  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
158  */