| 1 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 40 public class TestTypedLiterals extends TestCase { 41 42 43 private Model m = ModelFactory.createDefaultModel(); 44 45 52 53 56 public TestTypedLiterals( String name ) { 57 super( name ); 58 } 59 60 63 public static TestSuite suite() { 64 return new TestSuite( TestTypedLiterals.class ); 65 } 66 67 70 public void testUnknown() { 71 String typeURI = "urn:x-hp-dt:unknown"; 72 String 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 84 assertNotNull(l1); 85 assertNotNull(l3); 86 assertNotNull(l5); 87 88 assertDiffer("datatype sensitive", l1, l5); 90 assertDiffer("value sensitive", l1, l3); 91 assertDiffer("typed and plain differ", l1, l6); 92 93 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 115 public void testUserDef() { 116 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 assertNotNull(l1); 126 assertNotNull(l3); 127 128 assertDiffer("values should be tested!", l1, l3); 130 131 assertSame("Datatype incorrect", l1.getDatatype(), rtype); 133 assertEquals("Datatype uri incorrect", l1.getDatatypeURI(), RationalType.theTypeURI); 134 Object 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 161 public void testXSDbasics() { 162 String xsdIntURI = "http://www.w3.org/2001/XMLSchema#int"; 163 164 Literal l1 = m.createTypedLiteral(42); Literal l2 = m.createTypedLiteral("42", XSDDatatype.XSDint); 167 Literal l4 = m.createTypedLiteral("63"); 169 assertSameValueAs("Default map failed", l1, l2); 170 assertEquals("Value wrong", l1.getValue(), new Integer (42)); 171 assertEquals("class wrong", l1.getValue().getClass(), Integer .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 l1 = m.createTypedLiteral(42.42); 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 .class); 188 assertFloatEquals("value wrong", ((Double )(l1.getValue())).floatValue(), 42.42); 189 assertEquals("class wrong", l2.getValue().getClass(), Float .class); 190 assertFloatEquals("value wrong", ((Float )(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 checkLegalLiteral("12345", XSDDatatype.XSDlong, Long .class, new Long (12345)); 197 checkLegalLiteral("-12345", XSDDatatype.XSDlong, Long .class, new Long (-12345)); 198 checkIllegalLiteral("2.3", XSDDatatype.XSDlong); 199 200 checkLegalLiteral("1234", XSDDatatype.XSDshort, Short .class, new Short ((short)1234)); 201 checkLegalLiteral("-1234", XSDDatatype.XSDshort, Short .class, new Short ((short)-1234)); 202 checkLegalLiteral("32767", XSDDatatype.XSDshort, Short .class, new Short ((short)32767)); 203 checkLegalLiteral("-32768", XSDDatatype.XSDshort, Short .class, new Short ((short)-32768)); 204 checkIllegalLiteral("32769", XSDDatatype.XSDshort); 205 checkIllegalLiteral("2.3", XSDDatatype.XSDshort); 206 207 checkLegalLiteral("42", XSDDatatype.XSDbyte, Byte .class, new Byte ((byte)42)); 208 checkLegalLiteral("-42", XSDDatatype.XSDbyte, Byte .class, new Byte ((byte)-42)); 209 checkLegalLiteral("127", XSDDatatype.XSDbyte, Byte .class, new Byte ((byte)127)); 210 checkLegalLiteral("-128", XSDDatatype.XSDbyte, Byte .class, new Byte ((byte)-128)); 211 checkIllegalLiteral("32769", XSDDatatype.XSDbyte); 212 checkIllegalLiteral("128", XSDDatatype.XSDbyte); 213 checkIllegalLiteral("2.3", XSDDatatype.XSDbyte); 214 215 checkLegalLiteral("12345", XSDDatatype.XSDunsignedLong, Long .class, new Long (12345)); 217 checkLegalLiteral("+12345", XSDDatatype.XSDunsignedLong, Long .class, new Long (12345)); 218 checkLegalLiteral("9223372036854775808", XSDDatatype.XSDunsignedLong, BigInteger.class, new BigInteger("9223372036854775808")); 219 checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedLong); 220 221 checkLegalLiteral("12345", XSDDatatype.XSDunsignedInt, Long .class, new Long (12345)); 222 checkLegalLiteral("2147483648", XSDDatatype.XSDunsignedInt, Long .class, new Long (2147483648l)); 223 checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedInt); 224 225 checkLegalLiteral("1234", XSDDatatype.XSDunsignedShort, Integer .class, new Integer (1234)); 226 checkLegalLiteral("32679", XSDDatatype.XSDunsignedShort, Integer .class, new Integer (32679)); 227 checkIllegalLiteral("-12345", XSDDatatype.XSDunsignedShort); 228 229 checkLegalLiteral("123", XSDDatatype.XSDunsignedByte, Short .class, new Short ((short)123)); 230 checkLegalLiteral("129", XSDDatatype.XSDunsignedByte, Short .class, new Short ((short)129)); 231 checkIllegalLiteral("-123", XSDDatatype.XSDunsignedByte); 232 233 checkLegalLiteral("12345", XSDDatatype.XSDinteger, Long .class, new Long (12345)); 235 checkLegalLiteral("0", XSDDatatype.XSDinteger, Long .class, new Long (0)); 236 checkLegalLiteral("-12345", XSDDatatype.XSDinteger, Long .class, new Long (-12345)); 237 checkLegalLiteral("9223372036854775808", XSDDatatype.XSDinteger, BigInteger.class, new BigInteger("9223372036854775808")); 238 239 checkLegalLiteral("12345", XSDDatatype.XSDpositiveInteger, Long .class, new Long (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 .class, new Long (12345)); 245 checkLegalLiteral("0", XSDDatatype.XSDnonNegativeInteger, Long .class, new Long (0)); 246 checkIllegalLiteral("-12345", XSDDatatype.XSDnonNegativeInteger); 247 checkLegalLiteral("9223372036854775808", XSDDatatype.XSDnonNegativeInteger, BigInteger.class, new BigInteger("9223372036854775808")); 248 249 checkLegalLiteral("-12345", XSDDatatype.XSDnegativeInteger, Long .class, new Long (-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 .class, new Long (-12345)); 255 checkLegalLiteral("0", XSDDatatype.XSDnonPositiveInteger, Long .class, new Long (0)); 256 checkIllegalLiteral("12345", XSDDatatype.XSDnonPositiveInteger); 257 checkLegalLiteral("-9223372036854775808", XSDDatatype.XSDnonPositiveInteger, BigInteger.class, new BigInteger("-9223372036854775808")); 258 259 checkLegalLiteral("12345", XSDDatatype.XSDdecimal, Long .class, new Long ("12345")); 260 checkLegalLiteral("0.0", XSDDatatype.XSDdecimal, Long .class, new Long ("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 checkLegalLiteral("true", XSDDatatype.XSDboolean, Boolean .class, new Boolean (true)); 268 checkLegalLiteral("false", XSDDatatype.XSDboolean, Boolean .class, new Boolean (false)); 269 l1 = m.createTypedLiteral(true); 270 assertEquals("boolean mapping", XSDDatatype.XSDboolean, l1.getDatatype()); 271 272 checkLegalLiteral("hello world", XSDDatatype.XSDstring, String .class, "hello world"); 274 l1 = m.createTypedLiteral("foo bar"); 275 assertEquals("string mapping", XSDDatatype.XSDstring, l1.getDatatype()); 276 277 } 278 279 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 297 public void testOverloads() { 298 boolean old = JenaParameters.enableEagerLiteralValidation; 300 try { 301 JenaParameters.enableEagerLiteralValidation = true; 302 303 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 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 Calendar testCal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 323 testCal.set(1999, 4, 30, 15, 9, 32); 324 testCal.set(Calendar.MILLISECOND, 0); Literal lc = m.createTypedLiteral((Object )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 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 )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 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 368 public void testUserDefined() throws IOException { 369 String uri = "http://www.daml.org/2001/03/daml+oil-ex-dt"; 370 String 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 [] { 374 uri + "#XSDEnumerationHeight", 375 uri + "#over12", 376 uri + "#over17", 377 uri + "#over59", 378 uri + "#clothingsize" }); 379 380 RDFDatatype heightType = tm.getSafeTypeByName(uri + "#XSDEnumerationHeight"); 382 checkLegalLiteral("short", heightType, String .class, "short"); 383 checkLegalLiteral("tall", heightType, String .class, "tall"); 384 checkIllegalLiteral("shortish", heightType); 385 386 RDFDatatype over12Type = tm.getSafeTypeByName(uri + "#over12"); 388 checkLegalLiteral("15", over12Type, Long .class, new Long (15)); 389 checkIllegalLiteral("12", over12Type); 390 391 RDFDatatype clothingsize = tm.getSafeTypeByName(uri + "#clothingsize"); 393 checkLegalLiteral("42", clothingsize, Long .class, new Long (42)); 394 checkLegalLiteral("short", clothingsize, String .class, "short"); 395 396 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 425 public void testDateTime() { 426 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 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 464 testCal.set(Calendar.MILLISECOND, 0); 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); 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 urib="rdf://test.com#"; 484 String uri1=urib+"1"; 485 String urip=urib+"prop"; 486 String testN3 = "<"+uri1+"> <"+urip+"> \""+lc.getLexicalForm()+"\"^^<"+lc.getDatatypeURI()+"> ."; 487 java.io.StringReader sr = new java.io.StringReader (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 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 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 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 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 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 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 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 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 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 SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "America/Los_Angeles"); 601 602 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 ncal = new GregorianCalendar(pdt); 608 ncal.set(2004, 02, 21, 12, 50, 42); ncal.set(Calendar.MILLISECOND, 0); 610 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 ncal = new GregorianCalendar(pdt); 617 ncal.set(2004, 03, 21, 12, 50, 42); ncal.set(Calendar.MILLISECOND, 0); 619 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 626 } 627 628 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 assertTrue(model.listStatements( a, p, l2 ).hasNext()); 649 } 650 651 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
|