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: TestDatatype2.java,v 1.11 2004/06/25 08:03:48 maartenc Exp $
8    */
9   
10  package org.dom4j.datatype;
11  
12  import java.io.StringReader;
13  import java.text.DateFormat;
14  import java.text.SimpleDateFormat;
15  import java.util.Calendar;
16  import java.util.GregorianCalendar;
17  import java.util.SimpleTimeZone;
18  import java.util.TimeZone;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  import org.dom4j.Attribute;
26  import org.dom4j.Document;
27  import org.dom4j.Element;
28  import org.dom4j.io.SAXReader;
29  
30  /*** Test harness for XML Schema Datatypes support
31    *
32    * @author Yuxin Ruan
33    * @version $Revision: 1.11 $
34    */
35  public class TestDatatype2 extends TestCase {
36  
37      public static void main( String[] args ) {
38          TestRunner.run( suite() );
39      }
40  
41      public static Test suite() {
42          return new TestSuite(TestDatatype2.class);
43      }
44  
45      public TestDatatype2(String name) {
46          super(name);
47      }
48  
49      public void setUp() {
50      }
51  
52      public void tearDown() {
53      }
54  
55      public void testSchema() throws Exception {
56          Document schema=getSchema();
57          validateDocumentWithSchema(schema);
58      }
59  
60      public void testSchemaWithNamedComplexType() throws Exception {
61          Document schema=getSchemaWithNamedComplexType();
62          validateDocumentWithSchema(schema);
63      }
64  
65      public void testSchemaWithReference() throws Exception {
66          Document schema=getSchemaWithReference();
67          validateDocumentWithSchema(schema);
68      }
69  
70      public void testSchemaWithNamedSimpleType() throws Exception {
71          Document schema=getSchemaWithNamedSimpleType();
72          validateDocumentWithSchema(schema);
73      }
74  
75      private void validateDocumentWithSchema(Document schema) throws Exception {
76          Document doc=getSource(schema);
77          Element root=doc.getRootElement();
78          validateLongAttribute(root);
79          validateFloatElement(root);
80          validateDateElement(root);
81      }
82  
83      private void validateLongAttribute(Element root) throws Exception {
84          Attribute attr=root.attribute("longAttribute");
85          Object attrData=attr.getData();
86          validateData("testLongAttribute",attrData,new Long(123));
87          System.out.println("retrieved attribute "+attrData);
88      }
89  
90      private void validateFloatElement(Element root) throws Exception {
91          Element elem=root.element("floatElement");
92          Object elemData=elem.getData();
93          validateData("testFloatElement",elemData,new Float(1.23));
94          System.out.println("retrieved element:"+elemData);
95      }
96  
97      private void validateDateElement(Element root) throws Exception {
98          Element elem = root.element("dateElement");
99          Object elemData = elem.getData();
100         Calendar expected = getDate();
101         
102         System.out.println("retrieved element:" + elemData);
103         
104         // don't compare the Calendar instances, compare their strings instead!
105         assertTrue(elemData instanceof Calendar);
106         Calendar elemCal = (Calendar) elemData;
107 
108         DateFormat format = new SimpleDateFormat("MM/dd/yyyyZ");
109         format.setTimeZone(elemCal.getTimeZone());
110         String elemStr = format.format(elemCal.getTime());
111         
112         format.setTimeZone(expected.getTimeZone());
113         String expectedStr = format.format(expected.getTime());
114 
115         assertEquals("testDateElement", expectedStr, elemStr);
116     }
117 
118     private void validateData(String testName, Object retrieved, Object expected)
119             throws Exception {
120         Class retrievedClass=retrieved.getClass();
121         Class expectedClass=expected.getClass();
122 
123         //compare class
124         if (!expectedClass.equals(retrievedClass)) {
125             String msg="class mismatch in "+testName+
126                 ":expected "+expectedClass+
127                 ", retrieved "+retrievedClass;
128             throw new Exception(msg);
129         }
130 
131         //compare value
132         if (!expected.equals(retrieved)) {
133             String msg="value mismatch in "+testName+
134                 ":expected "+expected+
135                 ", retrieved "+retrieved;
136             throw new Exception(msg);
137         }
138     }
139 
140     private Document getSource(Document schema) throws Exception {
141         StringBuffer buffer=new StringBuffer();
142         buffer.append("<?xml version='1.0' ?>");
143         buffer.append("<test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
144         buffer.append("         xsi:noNamespaceSchemaLocation='long.xsd'");
145         buffer.append("         longAttribute='123' >");
146         buffer.append("     <floatElement>1.23</floatElement>");
147         buffer.append("     <dateElement>"+getDateString()+"</dateElement>");
148         buffer.append("</test>");
149 
150         StringReader in=new StringReader(buffer.toString());
151         DatatypeDocumentFactory docFactory=new DatatypeDocumentFactory();
152         docFactory.loadSchema(schema);
153         SAXReader parser=new SAXReader(docFactory);
154         return parser.read(in);
155     }
156 
157     private Document getSchema() throws Exception {
158         StringBuffer buffer=new StringBuffer();
159         buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
160         buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
161         buffer.append("     <xsd:element name='test'>");
162         buffer.append("         <xsd:complexType>");
163         buffer.append("             <xsd:sequence>");
164         buffer.append("                 <xsd:element name='floatElement' type='xsd:float' />");
165         buffer.append("                 <xsd:element name='dateElement' type='xsd:date' />");
166         buffer.append("             </xsd:sequence>");
167         buffer.append("             <xsd:attribute name='longAttribute' type='xsd:long' />");
168         buffer.append("         </xsd:complexType>");
169         buffer.append("     </xsd:element>");
170         buffer.append("</xsd:schema>");
171 
172         StringReader in=new StringReader(buffer.toString());
173         SAXReader parser=new SAXReader();
174         return parser.read(in);
175     }
176 
177     private Document getSchemaWithNamedComplexType() throws Exception {
178         StringBuffer buffer=new StringBuffer();
179         buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
180         buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
181         buffer.append("     <xsd:element name='test' type='TimePeriodType' />");
182 
183         buffer.append("     <xsd:complexType name='TimePeriodType'>");
184         buffer.append("         <xsd:sequence>");
185         buffer.append("             <xsd:element name='floatElement' type='xsd:float' />");
186         buffer.append("             <xsd:element name='dateElement' type='xsd:date' />");
187         buffer.append("         </xsd:sequence>");
188         buffer.append("         <xsd:attribute name='longAttribute' type='xsd:long' />");
189         buffer.append("     </xsd:complexType>");
190         buffer.append("</xsd:schema>");
191 
192         StringReader in=new StringReader(buffer.toString());
193         SAXReader parser=new SAXReader();
194         return parser.read(in);
195     }
196 
197     private Document getSchemaWithReference() throws Exception {
198         StringBuffer buffer=new StringBuffer();
199         buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
200         buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
201         buffer.append("     <xsd:element name='test' type='TimePeriodType' />");
202 
203         buffer.append("     <xsd:complexType name='TimePeriodType'>");
204         buffer.append("         <xsd:sequence>");
205         buffer.append("             <xsd:element name='floatElement' type='xsd:float' />");
206         buffer.append("             <xsd:element ref='dateElement' />");
207         buffer.append("         </xsd:sequence>");
208         buffer.append("         <xsd:attribute name='longAttribute' type='xsd:long' />");
209         buffer.append("     </xsd:complexType>");
210 
211         buffer.append("     <xsd:element name='dateElement' type='xsd:date' />");
212         buffer.append("</xsd:schema>");
213 
214         StringReader in=new StringReader(buffer.toString());
215         SAXReader parser=new SAXReader();
216         return parser.read(in);
217     }
218 
219     private Document getSchemaWithNamedSimpleType() throws Exception {
220         StringBuffer buffer=new StringBuffer();
221         buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
222         buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
223         buffer.append("     <xsd:element name='test'>");
224         buffer.append("         <xsd:complexType>");
225         buffer.append("             <xsd:sequence>");
226         buffer.append("                 <xsd:element name='floatElement' type='xsd:float' />");
227         buffer.append("                 <xsd:element name='dateElement' type='dateType' />");
228         buffer.append("             </xsd:sequence>");
229         buffer.append("             <xsd:attribute name='longAttribute' type='xsd:long' />");
230         buffer.append("         </xsd:complexType>");
231         buffer.append("     </xsd:element>");
232 
233         buffer.append("     <xsd:simpleType name='dateType'>");
234         buffer.append("         <xsd:restriction base='xsd:date'>");
235         //buffer.append("             <whiteSpace value='collapse' />");
236         buffer.append("         </xsd:restriction>");
237         buffer.append("     </xsd:simpleType>");
238         buffer.append("</xsd:schema>");
239 
240         StringReader in=new StringReader(buffer.toString());
241         SAXReader parser=new SAXReader();
242         return parser.read(in);
243     }
244 
245     private static String getDateString() {
246         //return dateTime in ISO8601 format
247         String yyyy=Integer.toString(year);
248         String mm=Integer.toString(month);
249         String dd=Integer.toString(date);
250         return yyyy+"-"+mm+"-"+dd+"Z";
251     }
252 
253     private static Calendar getDate() {
254         Calendar calendar=new GregorianCalendar();
255         calendar.clear();
256         calendar.set(Calendar.YEAR, year);
257         calendar.set(Calendar.MONTH, month - 1);
258         calendar.set(Calendar.DAY_OF_MONTH, date);
259         calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
260         calendar.setTimeZone(new SimpleTimeZone(0, "XSD 'Z' timezone"));
261         return calendar;
262     }
263 
264     public static final int year=2001;
265     public static final int month=10;
266     public static final int date=31;
267 }
268 
269 
270 
271 
272 /*
273  * Redistribution and use of this software and associated documentation
274  * ("Software"), with or without modification, are permitted provided
275  * that the following conditions are met:
276  *
277  * 1. Redistributions of source code must retain copyright
278  *    statements and notices.  Redistributions must also contain a
279  *    copy of this document.
280  *
281  * 2. Redistributions in binary form must reproduce the
282  *    above copyright notice, this list of conditions and the
283  *    following disclaimer in the documentation and/or other
284  *    materials provided with the distribution.
285  *
286  * 3. The name "DOM4J" must not be used to endorse or promote
287  *    products derived from this Software without prior written
288  *    permission of MetaStuff, Ltd.  For written permission,
289  *    please contact dom4j-info@metastuff.com.
290  *
291  * 4. Products derived from this Software may not be called "DOM4J"
292  *    nor may "DOM4J" appear in their names without prior written
293  *    permission of MetaStuff, Ltd. DOM4J is a registered
294  *    trademark of MetaStuff, Ltd.
295  *
296  * 5. Due credit should be given to the DOM4J Project - 
297  *    http://www.dom4j.org
298  *
299  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
300  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
301  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
302  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
303  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
304  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
305  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
306  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
308  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
309  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
310  * OF THE POSSIBILITY OF SUCH DAMAGE.
311  *
312  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
313  *
314  * $Id: TestDatatype2.java,v 1.11 2004/06/25 08:03:48 maartenc Exp $
315  */