KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > TestTypedLiterals


1 /******************************************************************
2  * File: TestTypedLiterals.java
3  * Created by: Dave Reynolds
4  * Created on: 08-Dec-02
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestTypedLiterals.java,v 1.42 2005/04/08 13:51:30 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.graph.test;
11
12 import com.hp.hpl.jena.datatypes.*;
13 import com.hp.hpl.jena.datatypes.xsd.*;
14 import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
15 import com.hp.hpl.jena.graph.*;
16 import com.hp.hpl.jena.graph.impl.*;
17 import com.hp.hpl.jena.graph.query.*;
18 import com.hp.hpl.jena.rdf.model.*;
19 import com.hp.hpl.jena.shared.impl.JenaParameters;
20 import com.hp.hpl.jena.vocabulary.XSD;
21 import com.hp.hpl.jena.enhanced.EnhNode;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import java.math.*;
27 import java.util.*;
28 import java.io.*;
29
30 import org.apache.xerces.impl.dv.util.Base64;
31 import org.apache.xerces.impl.dv.util.HexBin;
32    
33 /**
34  * Unit test for the typed literal machinery - including RDFDatatype,
35  * TypeMapper and LiteralLabel.
36  *
37  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
38  * @version $Revision: 1.42 $ on $Date: 2005/04/08 13:51:30 $
39  */

40 public class TestTypedLiterals extends TestCase {
41               
42     /** dummy model used as a literal factory */
43     private Model m = ModelFactory.createDefaultModel();
44     
45     // Temporary for debug
46
/*
47     static {
48         Locale.setDefault(Locale.ITALY);
49         TimeZone.setDefault(TimeZone.getTimeZone("CEST"));
50     }
51     */

52     
53     /**
54      * Boilerplate for junit
55      */

56     public TestTypedLiterals( String JavaDoc name ) {
57         super( name );
58     }
59     
60     /**
61      * This is its own test suite
62      */

63     public static TestSuite suite() {
64         return new TestSuite( TestTypedLiterals.class );
65     }
66     
67     /**
68      * Test the base functioning of unknown datatypes
69      */

70     public void testUnknown() {
71         String JavaDoc typeURI = "urn:x-hp-dt:unknown";
72         String JavaDoc typeURI2 = "urn:x-hp-dt:unknown2";
73
74         boolean originalFlag = JenaParameters.enableSilentAcceptanceOfUnknownDatatypes;
75         JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = true;
76         Literal l1 = m.createTypedLiteral("foo", typeURI);
77         Literal l3 = m.createTypedLiteral("15", typeURI);
78         Literal l5 = m.createTypedLiteral("foo", typeURI2);
79         Literal l6 = m.createLiteral("foo", "lang1");
80         Literal l7 = m.createLiteral("foo");
81         JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
82         // Check for successful creation
83

84         assertNotNull(l1);
85         assertNotNull(l3);
86         assertNotNull(l5);
87         
88         // check equality function
89
assertDiffer("datatype sensitive", l1, l5);
90         assertDiffer("value sensitive", l1, l3);
91         assertDiffer("typed and plain differ", l1, l6);
92
93         // Check typed accessors
94
try {
95             int i = l3.getInt();
96             assertTrue("Allowed int conversion", false);
97         } catch (DatatypeFormatException e) {}
98         assertEquals("Extract value", l1.getValue(), "foo");
99         assertEquals("Extract xml tag", l1.getWellFormed(), false);
100         
101         JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = false;
102         boolean foundException = false;
103         try {
104             Literal l8 = m.createTypedLiteral("food", typeURI+"3");
105         } catch (DatatypeFormatException e2) {
106             foundException = true;
107         }
108         JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
109         assertTrue("Detected unknown datatype", foundException);
110     }
111     
112     /**
113      * Tests the base functioning of a user defined datatype
114      */

115     public void testUserDef() {
116         // Register the user defined type for rationals
117
RDFDatatype rtype = RationalType.theRationalType;
118         TypeMapper.getInstance().registerDatatype(rtype);
119
120
121         Literal l1 = m.createTypedLiteral("3/5", rtype);
122         Literal l3 = m.createTypedLiteral("7/5", rtype);
123         
124         // Check for successful creation
125
assertNotNull(l1);
126         assertNotNull(l3);
127         
128         // check equality function
129
assertDiffer("values should be tested!", l1, l3);
130
131         // Check typed accessors
132
assertSame("Datatype incorrect", l1.getDatatype(), rtype);
133         assertEquals("Datatype uri incorrect", l1.getDatatypeURI(), RationalType.theTypeURI);
134         Object JavaDoc val = l1.getValue();
135         assertTrue("Value space check", val instanceof Rational);
136         assertTrue("Value check", ((Rational)val).getNumerator() == 3);
137         assertTrue("Value check", ((Rational)val).getDenominator() == 5);
138         try {
139             int i = l1.getInt();
140             assertTrue("Allowed int conversion", false);
141         } catch (DatatypeFormatException e) {}
142         assertEquals("Extract xml tag", l1.getWellFormed(), false);
143     }
144
145     public void testXMLLiteral() {
146         Literal ll;
147         
148         ll = m.createLiteral("<bad",true);
149         
150         assertTrue("Error checking must be off.",((EnhNode)ll).asNode().getLiteral().isXML());
151         ll = m.createTypedLiteral("<bad/>",XMLLiteralType.theXMLLiteralType);
152         assertFalse("Error checking must be on.",((EnhNode)ll).asNode().getLiteral().isXML());
153         ll = m.createTypedLiteral("<good></good>",XMLLiteralType.theXMLLiteralType);
154         assertTrue("Well-formed XMLLiteral.",((EnhNode)ll).asNode().getLiteral().isXML());
155     
156     }
157
158     /**
159      * Tests basic XSD integer types()
160      */

161     public void testXSDbasics() {
162         String JavaDoc xsdIntURI = "http://www.w3.org/2001/XMLSchema#int";
163         
164         // Check int and basic equality processing
165
Literal l1 = m.createTypedLiteral(42); // default map
166
Literal l2 = m.createTypedLiteral("42", XSDDatatype.XSDint);
167         Literal l4 = m.createTypedLiteral("63"); // default map
168

169         assertSameValueAs("Default map failed", l1, l2);
170         assertEquals("Value wrong", l1.getValue(), new Integer JavaDoc(42));
171         assertEquals("class wrong", l1.getValue().getClass(), Integer JavaDoc.class);
172         assertEquals("Value accessor problem", l1.getInt(), 42);
173         assertEquals("wrong type name", l2.getDatatypeURI(), xsdIntURI);
174         assertEquals("wrong type", l2.getDatatype(), XSDDatatype.XSDint);
175         assertDiffer("Not value sensitive", l1, l4);
176         checkIllegalLiteral("zap", XSDDatatype.XSDint);
177         checkIllegalLiteral("42.1", XSDDatatype.XSDint);
178         
179         Literal l5 = m.createTypedLiteral("42", XSDDatatype.XSDnonNegativeInteger);
180         assertSameValueAs("type coercion", l2, l5);
181         
182         // Check float/double
183
l1 = m.createTypedLiteral(42.42); // default map
184
l2 = m.createTypedLiteral("42.42", XSDDatatype.XSDfloat);
185         Literal l3 = m.createTypedLiteral("42.42", XSDDatatype.XSDdouble);
186         
187         assertEquals("class wrong", l1.getValue().getClass(), Double JavaDoc.class);
188         assertFloatEquals("value wrong", ((Double JavaDoc)(l1.getValue())).floatValue(), 42.42);
189         assertEquals("class wrong", l2.getValue().getClass(), Float JavaDoc.class);
190         assertFloatEquals("value wrong", ((Float JavaDoc)(l2.getValue())).floatValue(), 42.42);
191         assertFloatEquals("Value accessor problem", l1.getFloat(), 42.42);
192         assertEquals("wrong type", l2.getDatatype(), XSDDatatype.XSDfloat);
193         assertSameValueAs("equality fn", l1, l3);
194         
195         // Minimal check on long, short, byte
196
checkLegalLiteral("12345", XSDDatatype.XSDlong, Long JavaDoc.class, new Long JavaDoc(12345));
197         checkLegalLiteral("-12345", XSDDatatype.XSDlong, Long JavaDoc.class, new Long JavaDoc(-12345));
198         checkIllegalLiteral("2.3", XSDDatatype.XSDlong);
199         
200         checkLegalLiteral("1234", XSDDatatype.XSDshort, Short JavaDoc.class, new Short JavaDoc((short)1234));
201         checkLegalLiteral("-1234", XSDDatatype.XSDshort, Short JavaDoc.class, new Short JavaDoc((short)-1234));
202         checkLegalLiteral("32767", XSDDatatype.XSDshort, Short JavaDoc.class, new Short JavaDoc((short)32767));
203         checkLegalLiteral("-32768", XSDDatatype.XSDshort, Short JavaDoc.class, new Short JavaDoc((short)-32768));
204         checkIllegalLiteral("32769", XSDDatatype.XSDshort);
205         checkIllegalLiteral("2.3", XSDDatatype.XSDshort);
206
207         checkLegalLiteral("42", XSDDatatype.XSDbyte, Byte JavaDoc.class, new Byte JavaDoc((byte)42));
208         checkLegalLiteral("-42", XSDDatatype.XSDbyte, Byte JavaDoc.class, new Byte JavaDoc((byte)-42));
209         checkLegalLiteral("127", XSDDatatype.XSDbyte, Byte JavaDoc.class, new Byte JavaDoc((byte)127));
210         checkLegalLiteral("-128", XSDDatatype.XSDbyte, Byte JavaDoc.class, new Byte JavaDoc((byte)-128));
211         checkIllegalLiteral("32769", XSDDatatype.XSDbyte);
212         checkIllegalLiteral("128", XSDDatatype.XSDbyte);
213         checkIllegalLiteral("2.3", XSDDatatype.XSDbyte);
214         
215         // Minimal check on unsigned normal types
216
checkLegalLiteral("12345", XSDDatatype.XSDunsignedLong, Long JavaDoc.class, new Long JavaDoc(12345));
217         checkLegalLiteral("+12345", XSDDatatype.XSDunsignedLong, Long JavaDoc.class, new Long JavaDoc(12345));
218         checkLegalLiteral("9223372036854775808", XSDDatatype.XSDunsignedLong, BigInteger.class, new BigInteger("9223372036854775808"));
219         checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedLong);
220         
221         checkLegalLiteral("12345", XSDDatatype.XSDunsignedInt, Long JavaDoc.class, new Long JavaDoc(12345));
222         checkLegalLiteral("2147483648", XSDDatatype.XSDunsignedInt, Long JavaDoc.class, new Long JavaDoc(2147483648l));
223         checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedInt);
224         
225         checkLegalLiteral("1234", XSDDatatype.XSDunsignedShort, Integer JavaDoc.class, new Integer JavaDoc(1234));
226         checkLegalLiteral("32679", XSDDatatype.XSDunsignedShort, Integer JavaDoc.class, new Integer JavaDoc(32679));
227         checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedShort);
228         
229         checkLegalLiteral("123", XSDDatatype.XSDunsignedByte, Short JavaDoc.class, new Short JavaDoc((short)123));
230         checkLegalLiteral("129", XSDDatatype.XSDunsignedByte, Short JavaDoc.class, new Short JavaDoc((short)129));
231         checkIllegalLiteral("-123", XSDDatatype.XSDunsignedByte);
232         
233         // Minimal check on the big num types
234
checkLegalLiteral("12345", XSDDatatype.XSDinteger, Long JavaDoc.class, new Long JavaDoc(12345));
235         checkLegalLiteral("0", XSDDatatype.XSDinteger, Long JavaDoc.class, new Long JavaDoc(0));
236         checkLegalLiteral("-12345", XSDDatatype.XSDinteger, Long JavaDoc.class, new Long JavaDoc(-12345));
237         checkLegalLiteral("9223372036854775808", XSDDatatype.XSDinteger, BigInteger.class, new BigInteger("9223372036854775808"));
238         
239         checkLegalLiteral("12345", XSDDatatype.XSDpositiveInteger, Long JavaDoc.class, new Long JavaDoc(12345));
240         checkIllegalLiteral("0", XSDDatatype.XSDpositiveInteger);
241         checkIllegalLiteral("-12345", XSDDatatype.XSDpositiveInteger);
242         checkLegalLiteral("9223372036854775808", XSDDatatype.XSDpositiveInteger, BigInteger.class, new BigInteger("9223372036854775808"));
243         
244         checkLegalLiteral("12345", XSDDatatype.XSDnonNegativeInteger, Long JavaDoc.class, new Long JavaDoc(12345));
245         checkLegalLiteral("0", XSDDatatype.XSDnonNegativeInteger, Long JavaDoc.class, new Long JavaDoc(0));
246         checkIllegalLiteral("-12345", XSDDatatype.XSDnonNegativeInteger);
247         checkLegalLiteral("9223372036854775808", XSDDatatype.XSDnonNegativeInteger, BigInteger.class, new BigInteger("9223372036854775808"));
248         
249         checkLegalLiteral("-12345", XSDDatatype.XSDnegativeInteger, Long JavaDoc.class, new Long JavaDoc(-12345));
250         checkIllegalLiteral("0", XSDDatatype.XSDnegativeInteger);
251         checkIllegalLiteral("12345", XSDDatatype.XSDnegativeInteger);
252         checkLegalLiteral("-9223372036854775808", XSDDatatype.XSDnegativeInteger, BigInteger.class, new BigInteger("-9223372036854775808"));
253         
254         checkLegalLiteral("-12345", XSDDatatype.XSDnonPositiveInteger, Long JavaDoc.class, new Long JavaDoc(-12345));
255         checkLegalLiteral("0", XSDDatatype.XSDnonPositiveInteger, Long JavaDoc.class, new Long JavaDoc(0));
256         checkIllegalLiteral("12345", XSDDatatype.XSDnonPositiveInteger);
257         checkLegalLiteral("-9223372036854775808", XSDDatatype.XSDnonPositiveInteger, BigInteger.class, new BigInteger("-9223372036854775808"));
258         
259         checkLegalLiteral("12345", XSDDatatype.XSDdecimal, Long JavaDoc.class, new Long JavaDoc("12345"));
260         checkLegalLiteral("0.0", XSDDatatype.XSDdecimal, Long JavaDoc.class, new Long JavaDoc("0"));
261         checkLegalLiteral("42.45", XSDDatatype.XSDdecimal, BigDecimal.class, new BigDecimal("42.45"));
262         checkLegalLiteral("9223372036854775808.1234", XSDDatatype.XSDdecimal, BigDecimal.class, new BigDecimal("9223372036854775808.1234"));
263         checkLegalLiteral("123.4", XSDDatatype.XSDdecimal, BigDecimal.class, new BigDecimal("123.4"));
264         checkIllegalLiteral("123,4", XSDDatatype.XSDdecimal);
265         
266         // Booleans
267
checkLegalLiteral("true", XSDDatatype.XSDboolean, Boolean JavaDoc.class, new Boolean JavaDoc(true));
268         checkLegalLiteral("false", XSDDatatype.XSDboolean, Boolean JavaDoc.class, new Boolean JavaDoc(false));
269         l1 = m.createTypedLiteral(true);
270         assertEquals("boolean mapping", XSDDatatype.XSDboolean, l1.getDatatype());
271         
272         // String types
273
checkLegalLiteral("hello world", XSDDatatype.XSDstring, String JavaDoc.class, "hello world");
274         l1 = m.createTypedLiteral("foo bar");
275         assertEquals("string mapping", XSDDatatype.XSDstring, l1.getDatatype());
276         
277     }
278     
279     /**
280      * Some selected equality tests which caused problems in WG tests
281      */

282     public void testMiscEquality() {
283         Literal l1 = m.createTypedLiteral("10", "http://www.w3.org/2001/XMLSchema#integer");
284         Literal l3 = m.createTypedLiteral("010", "http://www.w3.org/2001/XMLSchema#integer");
285         assertSameValueAs("Int lex form", l1, l3);
286         
287         l1 = m.createTypedLiteral("1", XSDDatatype.XSDint);
288         l3 = m.createTypedLiteral("1", XSDDatatype.XSDnonNegativeInteger);
289         
290         assertSameValueAs("numeric comparisons", l1, l3);
291     }
292     
293     /**
294      * Check that creating a typed literal from an object traps the interesting
295      * special cases of String and Calendar.
296      */

297     public void testOverloads() {
298         // First case string overloads an explicit type
299
boolean old = JenaParameters.enableEagerLiteralValidation;
300         try {
301             JenaParameters.enableEagerLiteralValidation = true;
302             
303             // String overloading cases
304
boolean test1 = false;
305             try {
306                 Literal l1 = m.createTypedLiteral("foo", "http://www.w3.org/2001/XMLSchema#integer");
307             } catch (DatatypeFormatException e1 ) {
308                 test1 = true;
309             }
310             assertTrue("detected illegal string, direct", test1);
311             
312             boolean test2 = false;
313             try {
314                 Object JavaDoc foo = "foo";
315                 Literal l1 = m.createTypedLiteral(foo, "http://www.w3.org/2001/XMLSchema#integer");
316             } catch (DatatypeFormatException e2 ) {
317                 test2 = true;
318             }
319             assertTrue("detected illegal string, overloaded", test2);
320             
321             // Overloading of calendar convenience functions
322
Calendar testCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
323             testCal.set(1999, 4, 30, 15, 9, 32);
324             testCal.set(Calendar.MILLISECOND, 0); // ms field can be undefined on Linux
325
Literal lc = m.createTypedLiteral((Object JavaDoc)testCal);
326             assertEquals("calendar overloading test", m.createTypedLiteral("1999-05-30T15:09:32Z", XSDDatatype.XSDdateTime), lc );
327             
328         } finally {
329             JenaParameters.enableEagerLiteralValidation = old;
330         }
331     }
332     
333     /**
334      * Test plain literal/xsd:string/xsd:int equality operations
335      */

336     public void testPlainSameValueAs() {
337         Literal lString = m.createTypedLiteral("10", XSDDatatype.XSDstring );
338         Literal lPlain = m.createTypedLiteral("10", (RDFDatatype)null );
339         Literal lPlain3 = m.createTypedLiteral("10", (String JavaDoc)null );
340         Literal lPlain2 = m.createLiteral("10");
341         Literal lInt = m.createTypedLiteral("10", XSDDatatype.XSDint );
342         
343         assertSameValueAs("Null type = plain literal", lPlain, lPlain2);
344         assertSameValueAs("Null type = plain literal", lPlain, lPlain3);
345         assertSameValueAs("Null type = plain literal", lPlain2, lPlain3);
346         assertTrue("null type", lPlain3.getDatatype() == null);
347         assertDiffer("String != int", lString, lInt);
348         assertDiffer("Plain != int", lPlain, lInt);
349         assertDiffer("Plain != int", lPlain2, lInt);
350         
351         // The correct answer to this is currently up to us
352
if (JenaParameters.enablePlainLiteralSameAsString) {
353             assertSameValueAs("String != plain??", lString, lPlain);
354             assertSameValueAs("String != plain??", lString, lPlain2);
355         } else {
356             assertDiffer("String != plain??", lString, lPlain);
357             assertDiffer("String != plain??", lString, lPlain2);
358         }
359         
360     }
361     
362     /**
363      * Test user defined data types using the DAML+OIL standard example.
364      * N.B. The file on daml.org is not legal (wrong namespace for XMLSchema, missed
365      * qualifiers onsome restriction base types) so we actually load a locally cached
366      * correct version but pretend it is from the real URI.
367      */

368     public void testUserDefined() throws IOException {
369         String JavaDoc uri = "http://www.daml.org/2001/03/daml+oil-ex-dt";
370         String JavaDoc filename = "testing/xsd/daml+oil-ex-dt.xsd";
371         TypeMapper tm = TypeMapper.getInstance();
372         List typenames = XSDDatatype.loadUserDefined(uri, new FileReader(filename), null, tm);
373         assertIteratorValues(typenames.iterator(), new Object JavaDoc[] {
374             uri + "#XSDEnumerationHeight",
375             uri + "#over12",
376             uri + "#over17",
377             uri + "#over59",
378             uri + "#clothingsize" });
379         
380         // Check the string restriction
381
RDFDatatype heightType = tm.getSafeTypeByName(uri + "#XSDEnumerationHeight");
382         checkLegalLiteral("short", heightType, String JavaDoc.class, "short");
383         checkLegalLiteral("tall", heightType, String JavaDoc.class, "tall");
384         checkIllegalLiteral("shortish", heightType);
385
386         // Check the numeric restriction
387
RDFDatatype over12Type = tm.getSafeTypeByName(uri + "#over12");
388         checkLegalLiteral("15", over12Type, Long JavaDoc.class, new Long JavaDoc(15));
389         checkIllegalLiteral("12", over12Type);
390         
391         // Check the union type
392
RDFDatatype clothingsize = tm.getSafeTypeByName(uri + "#clothingsize");
393         checkLegalLiteral("42", clothingsize, Long JavaDoc.class, new Long JavaDoc(42));
394         checkLegalLiteral("short", clothingsize, String JavaDoc.class, "short");
395         
396         // Check use of isValidLiteral for base versus derived combinations
397
LiteralLabel iOver12 = m.createTypedLiteral("13", over12Type).asNode().getLiteral();
398         LiteralLabel iDecimal14 = m.createTypedLiteral("14", XSDDatatype.XSDdecimal).asNode().getLiteral();
399         LiteralLabel iDecimal10 = m.createTypedLiteral("10", XSDDatatype.XSDdecimal).asNode().getLiteral();
400         LiteralLabel iString = m.createTypedLiteral("15", XSDDatatype.XSDstring).asNode().getLiteral();
401         LiteralLabel iPlain = m.createLiteral("foo").asNode().getLiteral();
402         
403         assertTrue(over12Type.isValidLiteral(iOver12));
404         assertTrue(over12Type.isValidLiteral(iDecimal14));
405         assertTrue( ! over12Type.isValidLiteral(iDecimal10));
406         assertTrue( ! over12Type.isValidLiteral(iString));
407         assertTrue( ! over12Type.isValidLiteral(iPlain));
408         
409         assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iOver12));
410         assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iDecimal14));
411         assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iDecimal10));
412         assertTrue( ! XSDDatatype.XSDdecimal.isValidLiteral(iString));
413         assertTrue( ! XSDDatatype.XSDdecimal.isValidLiteral(iPlain));
414         
415         assertTrue(XSDDatatype.XSDstring.isValidLiteral(iString));
416         assertTrue(XSDDatatype.XSDstring.isValidLiteral(iPlain));
417         assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(iOver12));
418         assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(iDecimal10));
419         assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(iDecimal14));
420     }
421
422     /**
423      * Test data/time wrappers
424      */

425     public void testDateTime() {
426         // Duration
427
Literal l1 = m.createTypedLiteral("P1Y2M3DT5H6M7.5S", XSDDatatype.XSDduration);
428         assertEquals("duration data type", XSDDatatype.XSDduration, l1.getDatatype());
429         assertEquals("duration java type", XSDDuration.class, l1.getValue().getClass());
430         assertEquals("duration value", 1, ((XSDDuration)l1.getValue()).getYears());
431         assertEquals("duration value", 2, ((XSDDuration)l1.getValue()).getMonths());
432         assertEquals("duration value", 3, ((XSDDuration)l1.getValue()).getDays());
433         assertEquals("duration value", 5, ((XSDDuration)l1.getValue()).getHours());
434         assertEquals("duration value", 6, ((XSDDuration)l1.getValue()).getMinutes());
435         assertEquals("duration value", 7, ((XSDDuration)l1.getValue()).getFullSeconds());
436         assertFloatEquals("duration value", 18367.5, ((XSDDuration)l1.getValue()).getTimePart());
437         assertEquals("serialization", "P1Y2M3DT5H6M7.5S", l1.getValue().toString());
438         assertEquals("equality test", l1, m.createTypedLiteral("P1Y2M3DT5H6M7.5S", XSDDatatype.XSDduration));
439         assertTrue("inequality test", l1 != m.createTypedLiteral("P1Y2M2DT5H6M7.5S", XSDDatatype.XSDduration));
440         
441         // dateTime
442
l1 = m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime);
443         XSDDateTime xdt = (XSDDateTime)l1.getValue();
444         assertEquals("dateTime data type", XSDDatatype.XSDdateTime, l1.getDatatype());
445         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
446         assertEquals("dateTime value", 1999, xdt.getYears());
447         assertEquals("dateTime value", 5, xdt.getMonths());
448         assertEquals("dateTime value", 31, xdt.getDays());
449         assertEquals("dateTime value", 2, xdt.getHours());
450         assertEquals("dateTime value", 9, xdt.getMinutes());
451         assertEquals("dateTime value", 32, xdt.getFullSeconds());
452         assertEquals("serialization", "1999-05-31T02:09:32Z", l1.getValue().toString());
453         Calendar cal = xdt.asCalendar();
454         Calendar testCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
455         testCal.set(1999, 4, 31, 2, 9, 32);
456         /*
457         assertEquals("calendar value", cal.get(Calendar.YEAR), testCal.get(Calendar.YEAR) );
458         assertEquals("calendar value", cal.get(Calendar.MONTH), testCal.get(Calendar.MONTH) );
459         assertEquals("calendar value", cal.get(Calendar.DATE), testCal.get(Calendar.DATE) );
460         assertEquals("calendar value", cal.get(Calendar.HOUR), testCal.get(Calendar.HOUR) );
461         assertEquals("calendar value", cal.get(Calendar.MINUTE), testCal.get(Calendar.MINUTE) );
462         assertEquals("calendar value", cal.get(Calendar.SECOND), testCal.get(Calendar.SECOND) );
463         */

464         testCal.set(Calendar.MILLISECOND, 0); // ms field can be undefined on Linux
465
assertEquals("calendar value", cal, testCal);
466         assertEquals("equality test", l1, m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime));
467         assertTrue("inequality test", l1 != m.createTypedLiteral("1999-04-31T02:09:32Z", XSDDatatype.XSDdateTime));
468         
469         Calendar testCal2 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
470         testCal2.set(1999, 4, 30, 15, 9, 32);
471         testCal2.set(Calendar.MILLISECOND, 0); // ms field can be undefined on Linux
472
Literal lc = m.createTypedLiteral(testCal2);
473         assertEquals("calendar 24 hour test", m.createTypedLiteral("1999-05-30T15:09:32Z", XSDDatatype.XSDdateTime), lc );
474         
475         assertEquals("calendar value", cal, testCal);
476         assertEquals("equality test", l1, m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime));
477
478         Calendar testCal3 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
479         testCal3.clear();
480         testCal3.set(1999, Calendar.JANUARY, 30, 15, 9, 32);
481         lc = m.createTypedLiteral(testCal3);
482         assertEquals("1999-01-30T15:09:32Z", lc.getLexicalForm());
483         String JavaDoc urib="rdf://test.com#";
484         String JavaDoc uri1=urib+"1";
485         String JavaDoc urip=urib+"prop";
486         String JavaDoc testN3 = "<"+uri1+"> <"+urip+"> \""+lc.getLexicalForm()+"\"^^<"+lc.getDatatypeURI()+"> .";
487         java.io.StringReader JavaDoc sr = new java.io.StringReader JavaDoc(testN3);
488         m.read(sr, urib, "N3");
489         assertTrue(m.contains(m.getResource(uri1),m.getProperty(urip)));
490         Resource r1 = m.getResource(uri1);
491         Property p = m.getProperty(urip);
492         XSDDateTime returnedDateTime = (XSDDateTime) r1.getProperty(p).getLiteral().getValue();
493         assertEquals("deserialized calendar value", testCal3, returnedDateTime.asCalendar());
494
495         // dateTime to calendar with milliseconds
496
Calendar testCal4 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
497         testCal4.set(1999, 4, 30, 15, 9, 32);
498         testCal4.set(Calendar.MILLISECOND, 25);
499         Literal lc4 = m.createTypedLiteral(testCal4);
500         assertEquals("serialization", "1999-05-30T15:09:32.25Z", lc4.getValue().toString());
501         assertEquals("calendar ms test", m.createTypedLiteral("1999-05-30T15:09:32.25Z", XSDDatatype.XSDdateTime), lc4 );
502         XSDDateTime dt4 = (XSDDateTime)lc4.getValue();
503         assertEquals(dt4.asCalendar(), testCal4);
504         
505         // date
506
l1 = m.createTypedLiteral("1999-05-31", XSDDatatype.XSDdate);
507         assertEquals("dateTime data type", XSDDatatype.XSDdate, l1.getDatatype());
508         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
509         xdt = (XSDDateTime)l1.getValue();
510         assertEquals("dateTime value", 1999, xdt.getYears());
511         assertEquals("dateTime value", 5, xdt.getMonths());
512         assertEquals("dateTime value", 31, xdt.getDays());
513         try {
514             xdt.getHours();
515             assertTrue("Failed to prevent illegal access", false);
516         } catch (IllegalDateTimeFieldException e) {}
517         
518         // time
519
l1 = m.createTypedLiteral("12:56:32", XSDDatatype.XSDtime);
520         assertEquals("dateTime data type", XSDDatatype.XSDtime, l1.getDatatype());
521         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
522         xdt = (XSDDateTime)l1.getValue();
523         assertEquals("dateTime value", 12, xdt.getHours());
524         assertEquals("dateTime value", 56, xdt.getMinutes());
525         assertEquals("dateTime value", 32, xdt.getFullSeconds());
526         try {
527             xdt.getDays();
528             assertTrue("Failed to prevent illegal access", false);
529         } catch (IllegalDateTimeFieldException e) {}
530         
531         // gYearMonth
532
l1 = m.createTypedLiteral("1999-05", XSDDatatype.XSDgYearMonth);
533         assertEquals("dateTime data type", XSDDatatype.XSDgYearMonth, l1.getDatatype());
534         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
535         xdt = (XSDDateTime)l1.getValue();
536         assertEquals("dateTime value", 1999, xdt.getYears());
537         assertEquals("dateTime value", 5, xdt.getMonths());
538         try {
539             xdt.getDays();
540             assertTrue("Failed to prevent illegal access", false);
541         } catch (IllegalDateTimeFieldException e) {}
542         
543         // gYear
544
l1 = m.createTypedLiteral("1999", XSDDatatype.XSDgYear);
545         assertEquals("dateTime data type", XSDDatatype.XSDgYear, l1.getDatatype());
546         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
547         xdt = (XSDDateTime)l1.getValue();
548         assertEquals("dateTime value", 1999, xdt.getYears());
549         try {
550             xdt.getMonths();
551             assertTrue("Failed to prevent illegal access", false);
552         } catch (IllegalDateTimeFieldException e) {}
553         
554         // gMonth
555
l1 = m.createTypedLiteral("--05--", XSDDatatype.XSDgMonth);
556         assertEquals("dateTime data type", XSDDatatype.XSDgMonth, l1.getDatatype());
557         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
558         xdt = (XSDDateTime)l1.getValue();
559         assertEquals("dateTime value", 5, xdt.getMonths());
560         try {
561             xdt.getYears();
562             assertTrue("Failed to prevent illegal access", false);
563         } catch (IllegalDateTimeFieldException e) {}
564         
565         // gMonthDay
566
l1 = m.createTypedLiteral("--05-25", XSDDatatype.XSDgMonthDay);
567         assertEquals("dateTime data type", XSDDatatype.XSDgMonthDay, l1.getDatatype());
568         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
569         xdt = (XSDDateTime)l1.getValue();
570         assertEquals("dateTime value", 5, xdt.getMonths());
571         assertEquals("dateTime value", 25, xdt.getDays());
572         try {
573             xdt.getYears();
574             assertTrue("Failed to prevent illegal access", false);
575         } catch (IllegalDateTimeFieldException e) {}
576         
577         // gDay
578
l1 = m.createTypedLiteral("---25", XSDDatatype.XSDgDay);
579         assertEquals("dateTime data type", XSDDatatype.XSDgDay, l1.getDatatype());
580         assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
581         xdt = (XSDDateTime)l1.getValue();
582         assertEquals("dateTime value", 25, xdt.getDays());
583         try {
584             xdt.getMonths();
585             assertTrue("Failed to prevent illegal access", false);
586         } catch (IllegalDateTimeFieldException e) {}
587         
588         // Creation of datetime from a date object
589
Calendar ncal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
590         ncal.set(2003, 11, 8, 10, 50, 42);
591         ncal.set(Calendar.MILLISECOND, 0);
592         l1 = m.createTypedLiteral(ncal);
593         assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
594         assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
595         assertEquals("DateTime from date", "2003-12-08T10:50:42Z", l1.getValue().toString());
596         
597         // Thanks to Greg Shueler for DST patch and test case
598
//////some of below code from java.util.GregorianCalendar javadoc///////
599
// create a Pacific Standard Time time zone
600
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "America/Los_Angeles");
601
602         // set up rules for daylight savings time
603
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
604         pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
605
606         // create a GregorianCalendar with the Pacific Daylight time zone
607
ncal = new GregorianCalendar(pdt);
608         ncal.set(2004, 02, 21, 12, 50, 42);//before daylight savings time
609
ncal.set(Calendar.MILLISECOND, 0);
610         //System.err.println("cal is: "+ncal);
611
l1 = m.createTypedLiteral(ncal);
612         assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
613         assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
614         assertEquals("DateTime from date", "2004-03-21T20:50:42Z", l1.getValue().toString());
615         //System.err.println("date is: "+ncal.getTime());
616
ncal = new GregorianCalendar(pdt);
617         ncal.set(2004, 03, 21, 12, 50, 42);//within daylight savings time
618
ncal.set(Calendar.MILLISECOND, 0);
619         //System.err.println("cal is: "+ncal);
620
l1 = m.createTypedLiteral(ncal);
621         assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
622         assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
623         assertEquals("DateTime from date", "2004-04-21T19:50:42Z", l1.getValue().toString());
624         //System.err.println("date is: "+ncal.getTime());
625

626     }
627       
628     /**
629      * Test query applied to graphs containing typed values
630      */

631     public void testTypedQueries() {
632         Model model = ModelFactory.createDefaultModel();
633         Property p = model.createProperty("urn:x-eg/p");
634         Literal l1 = model.createTypedLiteral("10", "http://www.w3.org/2001/XMLSchema#integer");
635         Literal l2 = model.createTypedLiteral("010", "http://www.w3.org/2001/XMLSchema#integer");
636         assertSameValueAs("sameas test", l1, l2);
637         Resource a = model.createResource("urn:x-eg/a");
638         a.addProperty(p, l1);
639         assertTrue(model.getGraph().find(null, p.asNode(), l1.asNode()).hasNext());
640         assertTrue(model.getGraph().find(null, p.asNode(), l2.asNode()).hasNext());
641         assertTrue(model.getGraph().find(a.asNode(), p.asNode(), l2.asNode()).hasNext());
642         Query q = new Query();
643         q.addMatch(a.asNode(), p.asNode(), l2.asNode());
644         Iterator qi = model.getGraph().queryHandler().prepareBindings(q, new Node[] {}).executeBindings();
645         assertTrue(qi.hasNext());
646         // Similar tests at Model API level
647
// Selector s1 = new SimpleSelector(a, p, l2);
648
assertTrue(model.listStatements( a, p, l2 ).hasNext());
649     }
650     
651     /**
652      * Test the isValidLiteral machinery
653      */

654     public void testIsValidLiteral() {
655         Literal l = m.createTypedLiteral("1000", XSDDatatype.XSDinteger);
656         LiteralLabel ll = l.asNode().getLiteral();
657         assertTrue(XSDDatatype.XSDlong.isValidLiteral(ll));
658         assertTrue(XSDDatatype.XSDint.isValidLiteral(ll));
659         assertTrue(XSDDatatype.XSDshort.isValidLiteral(ll));
660         assertTrue(XSDDatatype.XSDunsignedInt.isValidLiteral(ll));
661         assertTrue(XSDDatatype.XSDunsignedLong.isValidLiteral(ll));
662         assertTrue(XSDDatatype.XSDunsignedShort.isValidLiteral(ll));
663         assertTrue(XSDDatatype.XSDpositiveInteger.isValidLiteral(ll));
664         assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(ll));
665         assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(ll));
666         assertTrue( ! XSDDatatype.XSDbyte.isValidLiteral(ll));
667         assertTrue( ! XSDDatatype.XSDnegativeInteger.isValidLiteral(ll));
668         
669         l = m.createTypedLiteral("-2", XSDDatatype.XSDinteger);
670         ll = l.asNode().getLiteral();
671         assertTrue(XSDDatatype.XSDlong.isValidLiteral(ll));
672         assertTrue(XSDDatatype.XSDint.isValidLiteral(ll));
673         assertTrue(XSDDatatype.XSDshort.isValidLiteral(ll));
674         assertTrue(! XSDDatatype.XSDunsignedInt.isValidLiteral(ll));
675 &nbs