KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestUserData


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: TestUserData.java,v 1.1 2003/07/07 10:30:29 per_nyfelt 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.Attribute;
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.NodeFactory;
19 import org.dom4j.io.SAXReader;
20 import org.dom4j.util.UserDataAttribute;
21 import org.dom4j.util.UserDataDocumentFactory;
22 import org.dom4j.util.UserDataElement;
23
24 import java.io.File JavaDoc;
25
26 /** Tests the UserDataDocumentFactory
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.1 $
30   */

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

170
Popular Tags