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