KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > datatype > SetDataTest


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.datatype;
9
10 import junit.textui.TestRunner;
11
12 import java.math.BigInteger JavaDoc;
13
14 import org.dom4j.AbstractTestCase;
15 import org.dom4j.Attribute;
16 import org.dom4j.Document;
17 import org.dom4j.Element;
18 import org.dom4j.Namespace;
19 import org.dom4j.QName;
20
21 /**
22  * Tests setting the value of datatype aware element or attribute value
23  *
24  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
25  * @version $Revision: 1.4 $
26  */

27 public class SetDataTest extends AbstractTestCase {
28     private DatatypeDocumentFactory factory = new DatatypeDocumentFactory();
29
30     public static void main(String JavaDoc[] args) {
31         TestRunner.run(SetDataTest.class);
32     }
33
34     // Test case(s)
35
// -------------------------------------------------------------------------
36
public void testAttribute() throws Exception JavaDoc {
37         QName personName = factory.createQName("person");
38         QName ageName = factory.createQName("age");
39
40         Element person = factory.createElement(personName);
41
42         person.addAttribute(ageName, "10");
43
44         Attribute age = person.attribute(ageName);
45
46         assertTrue("Created DatatypeAttribute not correct",
47                 age instanceof DatatypeAttribute);
48
49         log("Found attribute: " + age);
50
51         Object JavaDoc data = age.getData();
52         Object JavaDoc expected = new BigInteger JavaDoc("10");
53
54         assertEquals("Data is correct type", BigInteger JavaDoc.class, data.getClass());
55
56         assertEquals("Set age correctly", expected, data);
57
58         age.setValue("32");
59         data = age.getData();
60         expected = new BigInteger JavaDoc("32");
61
62         assertEquals("Set age correctly", expected, data);
63
64         /**
65          * not sure if numeric types should be round tripped back to BigDecimal
66          * (say) age.setData( new Long( 21 ) ); data = age.getData(); expected =
67          * new BigInteger( "21" ); assertEquals( "Set age correctly", expected,
68          * data );
69          */

70
71         // now lets set an invalid value
72
try {
73             age.setValue("abc");
74             fail("Appeared to set an invalid value");
75         } catch (IllegalArgumentException JavaDoc e) {
76         }
77     }
78
79     public void testAttributeWithNamespace() throws Exception JavaDoc {
80         QName personName = factory.createQName("person", "t", "urn://testing");
81         QName ageName = factory.createQName("age", "t", "urn://testing");
82
83         Element person = factory.createElement(personName);
84
85         person.addAttribute(ageName, "10");
86
87         Attribute age = person.attribute(ageName);
88
89         assertTrue("Created DatatypeAttribute not correct",
90                 age instanceof DatatypeAttribute);
91
92         log("Found attribute: " + age);
93
94         Object JavaDoc data = age.getData();
95         Object JavaDoc expected = new BigInteger JavaDoc("10");
96
97         assertEquals("Data is correct type", BigInteger JavaDoc.class, data.getClass());
98
99         assertEquals("Set age correctly", expected, data);
100
101         age.setValue("32");
102         data = age.getData();
103         expected = new BigInteger JavaDoc("32");
104
105         assertEquals("Set age correctly", expected, data);
106
107         try {
108             age.setValue("abc");
109             fail("Appeared to set an invalid value");
110         } catch (IllegalArgumentException JavaDoc e) {
111         }
112     }
113
114     public void testElement() throws Exception JavaDoc {
115         QName personName = factory.createQName("person");
116         QName numberOfCarsName = factory.createQName("numberOfCars");
117
118         Element person = factory.createElement(personName);
119
120         Element cars = person.addElement(numberOfCarsName);
121
122         log("Found element: " + cars);
123
124         Object JavaDoc expected = new Short JavaDoc((short) 10);
125         cars.setData(expected);
126
127         Object JavaDoc data = cars.getData();
128
129         assertEquals("Data is correct type", Short JavaDoc.class, data.getClass());
130         assertEquals("Set cars correctly", expected, data);
131
132         cars.setData(new Short JavaDoc((short) 32));
133         data = cars.getData();
134         expected = new Short JavaDoc((short) 32);
135
136         assertEquals("Set cars correctly", expected, data);
137
138         cars.setText("34");
139         data = cars.getData();
140         expected = new Short JavaDoc((short) 34);
141
142         assertEquals("Set cars correctly", expected, data);
143
144         // now lets set an invalid value
145
try {
146             cars.setText("abc");
147             fail("Appeared to set an invalid value");
148         } catch (IllegalArgumentException JavaDoc e) {
149         }
150     }
151
152     // Implementation methods
153
// -------------------------------------------------------------------------
154
protected void setUp() throws Exception JavaDoc {
155         super.setUp();
156
157         Document schema = getDocument("/xml/test/schema/personal.xsd");
158         factory.loadSchema(schema);
159
160         Namespace ns = new Namespace("t", "urn://testing");
161         factory.loadSchema(schema, ns);
162     }
163 }
164
165 /*
166  * Redistribution and use of this software and associated documentation
167  * ("Software"), with or without modification, are permitted provided that the
168  * following conditions are met:
169  *
170  * 1. Redistributions of source code must retain copyright statements and
171  * notices. Redistributions must also contain a copy of this document.
172  *
173  * 2. Redistributions in binary form must reproduce the above copyright notice,
174  * this list of conditions and the following disclaimer in the documentation
175  * and/or other materials provided with the distribution.
176  *
177  * 3. The name "DOM4J" must not be used to endorse or promote products derived
178  * from this Software without prior written permission of MetaStuff, Ltd. For
179  * written permission, please contact dom4j-info@metastuff.com.
180  *
181  * 4. Products derived from this Software may not be called "DOM4J" nor may
182  * "DOM4J" appear in their names without prior written permission of MetaStuff,
183  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
184  *
185  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
186  *
187  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
188  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
191  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
192  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
193  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
194  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
195  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
196  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
197  * POSSIBILITY OF SUCH DAMAGE.
198  *
199  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
200  */

201
Popular Tags