1 9 10 package test.dom4j.datatype; 11 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 import junit.textui.TestRunner; 16 import org.dom4j.Attribute; 17 import org.dom4j.Document; 18 import org.dom4j.Element; 19 import org.dom4j.datatype.DatatypeDocumentFactory; 20 import org.dom4j.io.SAXReader; 21 22 import java.io.StringReader ; 23 import java.util.Calendar ; 24 import java.util.GregorianCalendar ; 25 26 31 public class TestDatatype2 extends TestCase { 32 33 public static void main( String [] args ) { 34 TestRunner.run( suite() ); 35 } 36 37 public static Test suite() { 38 return new TestSuite(TestDatatype2.class); 39 } 40 41 public TestDatatype2(String name) { 42 super(name); 43 } 44 45 public void setUp() { 46 } 47 48 public void tearDown() { 49 } 50 51 public void testSchema() throws Exception { 52 Document schema=getSchema(); 53 validateDocumentWithSchema(schema); 54 } 55 56 public void testSchemaWithNamedComplexType() throws Exception { 57 Document schema=getSchemaWithNamedComplexType(); 58 validateDocumentWithSchema(schema); 59 } 60 61 public void testSchemaWithReference() throws Exception { 62 Document schema=getSchemaWithReference(); 63 validateDocumentWithSchema(schema); 64 } 65 66 public void testSchemaWithNamedSimpleType() throws Exception { 67 Document schema=getSchemaWithNamedSimpleType(); 68 validateDocumentWithSchema(schema); 69 } 70 71 private void validateDocumentWithSchema(Document schema) throws Exception { 72 Document doc=getSource(schema); 73 Element root=doc.getRootElement(); 74 validateLongAttribute(root); 75 validateFloatElement(root); 76 validateDateElement(root); 77 } 78 79 private void validateLongAttribute(Element root) throws Exception { 80 Attribute attr=root.attribute("longAttribute"); 81 Object attrData=attr.getData(); 82 validateData("testLongAttribute",attrData,new Long (123)); 83 System.out.println("retrieved attribute "+attrData); 84 } 85 86 private void validateFloatElement(Element root) throws Exception { 87 Element elem=root.element("floatElement"); 88 Object elemData=elem.getData(); 89 validateData("testFloatElement",elemData,new Float (1.23)); 90 System.out.println("retrieved element:"+elemData); 91 } 92 93 private void validateDateElement(Element root) throws Exception { 94 Element elem=root.element("dateElement"); 95 Object elemData=elem.getData(); 96 validateData("testFloatElement",elemData,getDate()); 97 System.out.println("retrieved element:"+getDate().getTime()); 98 } 99 100 private void validateData(String testName,Object retrieved,Object expected) 101 throws Exception { 102 Class retrievedClass=retrieved.getClass(); 103 Class expectedClass=expected.getClass(); 104 105 if (!expectedClass.equals(retrievedClass)) { 107 String msg="class mismatch in "+testName+ 108 ":expected "+expectedClass+ 109 ", retrieved "+retrievedClass; 110 throw new Exception (msg); 111 } 112 113 if (!expected.equals(retrieved)) { 115 String msg="value mismatch in "+testName+ 116 ":expected "+expected+ 117 ", retrieved "+retrieved; 118 throw new Exception (msg); 119 } 120 } 121 122 private Document getSource(Document schema) throws Exception { 123 StringBuffer buffer=new StringBuffer (); 124 buffer.append("<?xml version='1.0' ?>"); 125 buffer.append("<test xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"); 126 buffer.append(" xsi:noNamespaceSchemaLocation='long.xsd'"); 127 buffer.append(" longAttribute='123' >"); 128 buffer.append(" <floatElement>1.23</floatElement>"); 129 buffer.append(" <dateElement>"+getDateString()+"</dateElement>"); 130 buffer.append("</test>"); 131 132 StringReader in=new StringReader (buffer.toString()); 133 DatatypeDocumentFactory docFactory=new DatatypeDocumentFactory(); 135 docFactory.loadSchema(schema); 136 SAXReader parser=new SAXReader(docFactory); 137 return parser.read(in); 138 } 139 140 private Document getSchema() throws Exception { 141 StringBuffer buffer=new StringBuffer (); 142 buffer.append("<?xml version='1.0' encoding='UTF-8'?>"); 143 buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"); 144 buffer.append(" <xsd:element name='test'>"); 145 buffer.append(" <xsd:complexType>"); 146 buffer.append(" <xsd:sequence>"); 147 buffer.append(" <xsd:element name='floatElement' type='xsd:float' />"); 148 buffer.append(" <xsd:element name='dateElement' type='xsd:date' />"); 149 buffer.append(" </xsd:sequence>"); 150 buffer.append(" <xsd:attribute name='longAttribute' type='xsd:long' />"); 151 buffer.append(" </xsd:complexType>"); 152 buffer.append(" </xsd:element>"); 153 buffer.append("</xsd:schema>"); 154 155 StringReader in=new StringReader (buffer.toString()); 156 SAXReader parser=new SAXReader(); 157 return parser.read(in); 158 } 159 160 private Document getSchemaWithNamedComplexType() throws Exception { 161 StringBuffer buffer=new StringBuffer (); 162 buffer.append("<?xml version='1.0' encoding='UTF-8'?>"); 163 buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"); 164 buffer.append(" <xsd:element name='test' type='TimePeriodType' />"); 165 166 buffer.append(" <xsd:complexType name='TimePeriodType'>"); 167 buffer.append(" <xsd:sequence>"); 168 buffer.append(" <xsd:element name='floatElement' type='xsd:float' />"); 169 buffer.append(" <xsd:element name='dateElement' type='xsd:date' />"); 170 buffer.append(" </xsd:sequence>"); 171 buffer.append(" <xsd:attribute name='longAttribute' type='xsd:long' />"); 172 buffer.append(" </xsd:complexType>"); 173 buffer.append("</xsd:schema>"); 174 175 StringReader in=new StringReader (buffer.toString()); 176 SAXReader parser=new SAXReader(); 177 return parser.read(in); 178 } 179 180 private Document getSchemaWithReference() throws Exception { 181 StringBuffer buffer=new StringBuffer (); 182 buffer.append("<?xml version='1.0' encoding='UTF-8'?>"); 183 buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"); 184 buffer.append(" <xsd:element name='test' type='TimePeriodType' />"); 185 186 buffer.append(" <xsd:complexType name='TimePeriodType'>"); 187 buffer.append(" <xsd:sequence>"); 188 buffer.append(" <xsd:element name='floatElement' type='xsd:float' />"); 189 buffer.append(" <xsd:element ref='dateElement' />"); 190 buffer.append(" </xsd:sequence>"); 191 buffer.append(" <xsd:attribute name='longAttribute' type='xsd:long' />"); 192 buffer.append(" </xsd:complexType>"); 193 194 buffer.append(" <xsd:element name='dateElement' type='xsd:date' />"); 195 buffer.append("</xsd:schema>"); 196 197 StringReader in=new StringReader (buffer.toString()); 198 SAXReader parser=new SAXReader(); 199 return parser.read(in); 200 } 201 202 private Document getSchemaWithNamedSimpleType() throws Exception { 203 StringBuffer buffer=new StringBuffer (); 204 buffer.append("<?xml version='1.0' encoding='UTF-8'?>"); 205 buffer.append("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"); 206 buffer.append(" <xsd:element name='test'>"); 207 buffer.append(" <xsd:complexType>"); 208 buffer.append(" <xsd:sequence>"); 209 buffer.append(" <xsd:element name='floatElement' type='xsd:float' />"); 210 buffer.append(" <xsd:element name='dateElement' type='dateType' />"); 211 buffer.append(" </xsd:sequence>"); 212 buffer.append(" <xsd:attribute name='longAttribute' type='xsd:long' />"); 213 buffer.append(" </xsd:complexType>"); 214 buffer.append(" </xsd:element>"); 215 216 buffer.append(" <xsd:simpleType name='dateType'>"); 217 buffer.append(" <xsd:restriction base='xsd:date'>"); 218 buffer.append(" </xsd:restriction>"); 220 buffer.append(" </xsd:simpleType>"); 221 buffer.append("</xsd:schema>"); 222 223 StringReader in=new StringReader (buffer.toString()); 224 SAXReader parser=new SAXReader(); 225 return parser.read(in); 226 } 227 228 private static String getDateString() { 229 String yyyy=Integer.toString(year); 231 String mm=Integer.toString(month); 232 String dd=Integer.toString(date); 233 return yyyy+"-"+mm+"-"+dd; 234 } 235 236 private static Calendar getDate() { 237 Calendar calendar=new GregorianCalendar (); 238 calendar.clear(); 239 calendar.set( Calendar.YEAR, year ); 240 calendar.set( Calendar.MONTH, month-1 ); 241 calendar.set( Calendar.DAY_OF_MONTH, date ); 242 return calendar; 243 } 244 245 public static final int year=2001; 246 public static final int month=10; 247 public static final int date=31; 248 } 249 | Popular Tags |