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: TestSetData.java,v 1.8 2004/06/25 08:03:48 maartenc Exp $
8    */
9   
10  package org.dom4j.datatype;
11  
12  import java.math.BigInteger;
13  import java.net.URL;
14  
15  import junit.framework.Test;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  
19  import org.dom4j.AbstractTestCase;
20  import org.dom4j.Attribute;
21  import org.dom4j.Document;
22  import org.dom4j.Element;
23  import org.dom4j.Namespace;
24  import org.dom4j.QName;
25  import org.dom4j.io.SAXReader;
26  
27  
28  /*** Tests setting the value of datatype aware element or attribute value
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 1.8 $
32    */
33  public class TestSetData extends AbstractTestCase {
34  
35      private DatatypeDocumentFactory factory = new DatatypeDocumentFactory();
36      
37      public static void main( String[] args ) {
38          TestRunner.run( suite() );
39      }
40      
41      public static Test suite() {
42          return new TestSuite( TestSetData.class );
43      }
44      
45      public TestSetData(String name) {
46          super(name);
47      }
48  
49      // Test case(s)
50      //-------------------------------------------------------------------------                    
51      public void testAttribute() throws Exception {        
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 data = age.getData();
70          Object expected = new BigInteger( "10" );
71          
72          assertEquals( "Data is correct type", BigInteger.class, data.getClass() );
73          
74          assertEquals( "Set age correctly", expected, data );
75          
76          age.setValue( "32" );
77          data = age.getData();
78          expected = new BigInteger( "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 e) {
98          }
99      }
100     
101     public void testAttributeWithNamespace() throws Exception {        
102 /*        
103         QName personName = QName.get( "person", "" );
104         QName ageName = QName.get( "age", "" );
105 */
106         QName personName = factory.createQName( "person" , "t", "urn://testing");
107         QName ageName = factory.createQName( "age", "t", "urn://testing" );
108         
109         Element person = factory.createElement( personName );
110         
111         person.addAttribute( ageName, "10" );
112         Attribute age = person.attribute( ageName );
113         
114         assertTrue( "Created DatatypeAttribute", age instanceof DatatypeAttribute );
115         
116         log( "Found attribute: " + age );
117         
118 
119         Object data = age.getData();
120         Object expected = new BigInteger( "10" );
121         
122         assertEquals( "Data is correct type", BigInteger.class, data.getClass() );
123         
124         assertEquals( "Set age correctly", expected, data );
125         
126         age.setValue( "32" );
127         data = age.getData();
128         expected = new BigInteger( "32" );
129         
130         assertEquals( "Set age correctly", expected, data );
131 
132         try {
133             age.setValue( "abc" );
134             fail( "Appeared to set an invalid value" );
135         }
136         catch (IllegalArgumentException e) {
137         }
138     }    
139     
140     public void testElement() throws Exception {        
141         QName personName = factory.createQName( "person" );
142         QName numberOfCarsName = factory.createQName( "numberOfCars" );
143         
144         Element person = factory.createElement( personName );
145         //Element cars = factory.createElement( numberOfCarsName );
146         Element cars = person.addElement( numberOfCarsName );
147         
148         //assertTrue( "Created DatatypeElement", cars instanceof DatatypeElement );
149         
150         log( "Found element: " + cars );
151         
152         Object expected = new Short( (short) 10 );
153         cars.setData( expected );
154         Object data = cars.getData();
155         
156         assertEquals( "Data is correct type", Short.class, data.getClass() );
157         assertEquals( "Set cars correctly", expected, data );
158         
159         cars.setData( new Short( (short) 32 ) );
160         data = cars.getData();
161         expected = new Short( (short) 32 );
162         
163         assertEquals( "Set cars correctly", expected, data );
164 
165         cars.setText( "34" );
166         data = cars.getData();
167         expected = new Short( (short) 34 );
168         
169         assertEquals( "Set cars correctly", expected, data );
170 
171         // now lets set an invalid value        
172         try {
173             cars.setText( "abc" );
174             fail( "Appeared to set an invalid value" );
175         }
176         catch (IllegalArgumentException e) {
177         }
178     }    
179     
180     
181     // Implementation methods
182     //-------------------------------------------------------------------------                    
183     protected void setUp() throws Exception {
184         SAXReader reader = new SAXReader();
185         
186         URL url = getClass().getResource("/xml/test/schema/personal.xsd");
187         Document schema = reader.read(url);
188         factory.loadSchema(schema);
189         Namespace ns = new Namespace( "t", "urn://testing" );
190         factory.loadSchema( schema, ns );
191     }
192 }
193 
194 
195 
196 
197 /*
198  * Redistribution and use of this software and associated documentation
199  * ("Software"), with or without modification, are permitted provided
200  * that the following conditions are met:
201  *
202  * 1. Redistributions of source code must retain copyright
203  *    statements and notices.  Redistributions must also contain a
204  *    copy of this document.
205  *
206  * 2. Redistributions in binary form must reproduce the
207  *    above copyright notice, this list of conditions and the
208  *    following disclaimer in the documentation and/or other
209  *    materials provided with the distribution.
210  *
211  * 3. The name "DOM4J" must not be used to endorse or promote
212  *    products derived from this Software without prior written
213  *    permission of MetaStuff, Ltd.  For written permission,
214  *    please contact dom4j-info@metastuff.com.
215  *
216  * 4. Products derived from this Software may not be called "DOM4J"
217  *    nor may "DOM4J" appear in their names without prior written
218  *    permission of MetaStuff, Ltd. DOM4J is a registered
219  *    trademark of MetaStuff, Ltd.
220  *
221  * 5. Due credit should be given to the DOM4J Project - 
222  *    http://www.dom4j.org
223  *
224  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
225  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
226  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
227  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
228  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
229  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
230  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
231  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
233  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
234  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
235  * OF THE POSSIBILITY OF SUCH DAMAGE.
236  *
237  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
238  *
239  * $Id: TestSetData.java,v 1.8 2004/06/25 08:03:48 maartenc Exp $
240  */