KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > datatype > TestSetData


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: TestSetData.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
8  */

9
10 package test.dom4j.datatype;
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.QName;
19 import org.dom4j.datatype.DatatypeAttribute;
20 import org.dom4j.datatype.DatatypeDocumentFactory;
21 import org.dom4j.io.SAXReader;
22
23 import java.math.BigInteger JavaDoc;
24
25
26 /** Tests setting the value of datatype aware element or attribute value
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.2 $
30   */

31 public class TestSetData extends test.dom4j.AbstractTestCase {
32
33     // todo create an ozone version of the factory
34
private DatatypeDocumentFactory factory = new DatatypeDocumentFactory();
35
36
37     public static void main( String JavaDoc[] args ) {
38         TestRunner.run( suite() );
39     }
40
41     public static Test suite() {
42         return new TestSuite( TestSetData.class );
43     }
44
45     public TestSetData(String JavaDoc name) {
46         super(name);
47     }
48
49     // Test case(s)
50
//-------------------------------------------------------------------------
51
public void testAttribute() throws Exception JavaDoc {
52 /*
53         QName personName = QName.get( "person", "" );
54         QName ageName = QName.get( "age", "" );
55 */

56         QName personName = factory.createQName( "person" );
57         QName ageName = factory.createQName( "age" );
58
59         Element person = factory.createElement( personName );
60
61         person.addAttribute( ageName, "10" );
62         Attribute age = person.attribute( ageName );
63
64         assertTrue( "Created DatatypeAttribute", age instanceof DatatypeAttribute );
65
66         log( "Found attribute: " + age );
67
68
69         Object JavaDoc data = age.getData();
70         Object JavaDoc expected = new BigInteger JavaDoc( "10" );
71
72         assertEquals( "Data is correct type", BigInteger JavaDoc.class, data.getClass() );
73
74         assertEquals( "Set age correctly", expected, data );
75
76         age.setValue( "32" );
77         data = age.getData();
78         expected = new BigInteger JavaDoc( "32" );
79
80         assertEquals( "Set age correctly", expected, data );
81
82 /**
83  * not sure if numeric types should be round tripped back to BigDecimal (say)
84  *
85         age.setData( new Long( 21 ) );
86         data = age.getData();
87         expected = new BigInteger( "21" );
88
89         assertEquals( "Set age correctly", expected, data );
90 */

91         // now lets set an invalid value
92

93         try {
94             age.setValue( "abc" );
95             fail( "Appeared to set an invalid value" );
96         }
97         catch (IllegalArgumentException JavaDoc e) {
98         }
99     }
100
101     public void testElement() throws Exception JavaDoc {
102         QName personName = factory.createQName( "person" );
103         QName numberOfCarsName = factory.createQName( "numberOfCars" );
104
105         Element person = factory.createElement( personName );
106         //Element cars = factory.createElement( numberOfCarsName );
107
Element cars = person.addElement( numberOfCarsName );
108
109         //assertTrue( "Created DatatypeElement", cars instanceof DatatypeElement );
110

111         log( "Found element: " + cars );
112
113         Object JavaDoc expected = new Short JavaDoc( (short) 10 );
114         cars.setData( expected );
115         Object JavaDoc data = cars.getData();
116
117         assertEquals( "Data is correct type", Short JavaDoc.class, data.getClass() );
118         assertEquals( "Set cars correctly", expected, data );
119
120         cars.setData( new Short JavaDoc( (short) 32 ) );
121         data = cars.getData();
122         expected = new Short JavaDoc( (short) 32 );
123
124         assertEquals( "Set cars correctly", expected, data );
125
126         cars.setText( "34" );
127         data = cars.getData();
128         expected = new Short JavaDoc( (short) 34 );
129
130         assertEquals( "Set cars correctly", expected, data );
131
132         // now lets set an invalid value
133
try {
134             cars.setText( "abc" );
135             fail( "Appeared to set an invalid value" );
136         }
137         catch (IllegalArgumentException JavaDoc e) {
138         }
139     }
140
141
142     // Implementation methods
143
//-------------------------------------------------------------------------
144
protected void setUp() throws Exception JavaDoc {
145         SAXReader reader = new SAXReader();
146         Document schema = reader.read( "xml/test/schema/personal.xsd" );
147         factory.loadSchema( schema );
148     }
149 }
150
151
152
153
154 /*
155  * Redistribution and use of this software and associated documentation
156  * ("Software"), with or without modification, are permitted provided
157  * that the following conditions are met:
158  *
159  * 1. Redistributions of source code must retain copyright
160  * statements and notices. Redistributions must also contain a
161  * copy of this document.
162  *
163  * 2. Redistributions in binary form must reproduce the
164  * above copyright notice, this list of conditions and the
165  * following disclaimer in the documentation and/or other
166  * materials provided with the distribution.
167  *
168  * 3. The name "DOM4J" must not be used to endorse or promote
169  * products derived from this Software without prior written
170  * permission of MetaStuff, Ltd. For written permission,
171  * please contact dom4j-info@metastuff.com.
172  *
173  * 4. Products derived from this Software may not be called "DOM4J"
174  * nor may "DOM4J" appear in their names without prior written
175  * permission of MetaStuff, Ltd. DOM4J is a registered
176  * trademark of MetaStuff, Ltd.
177  *
178  * 5. Due credit should be given to the DOM4J Project
179  * (http://dom4j.org/).
180  *
181  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
182  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
183  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
184  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
185  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
186  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
187  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
188  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
189  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
190  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
191  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
192  * OF THE POSSIBILITY OF SUCH DAMAGE.
193  *
194  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
195  *
196  * $Id: TestSetData.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
197  */

198
Popular Tags