📄 testtypedliterals.java
字号:
// gMonthDay
l1 = m.createTypedLiteral("--05-25", XSDDatatype.XSDgMonthDay);
assertEquals("dateTime data type", XSDDatatype.XSDgMonthDay, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime)l1.getValue();
assertEquals("dateTime value", 5, xdt.getMonths());
assertEquals("dateTime value", 25, xdt.getDays());
try {
xdt.getYears();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {}
// gDay
l1 = m.createTypedLiteral("---25", XSDDatatype.XSDgDay);
assertEquals("dateTime data type", XSDDatatype.XSDgDay, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime)l1.getValue();
assertEquals("dateTime value", 25, xdt.getDays());
try {
xdt.getMonths();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {}
// Creation of datetime from a date object
Calendar ncal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
ncal.set(2003, 11, 8, 10, 50, 42);
ncal.set(Calendar.MILLISECOND, 0);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2003-12-08T10:50:42Z", l1.getValue().toString());
// Thanks to Greg Shueler for DST patch and test case
//////some of below code from java.util.GregorianCalendar javadoc///////
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "America/Los_Angeles");
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
ncal = new GregorianCalendar(pdt);
ncal.set(2004, 02, 21, 12, 50, 42);//before daylight savings time
ncal.set(Calendar.MILLISECOND, 0);
//System.err.println("cal is: "+ncal);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2004-03-21T20:50:42Z", l1.getValue().toString());
//System.err.println("date is: "+ncal.getTime());
ncal = new GregorianCalendar(pdt);
ncal.set(2004, 03, 21, 12, 50, 42);//within daylight savings time
ncal.set(Calendar.MILLISECOND, 0);
//System.err.println("cal is: "+ncal);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2004-04-21T19:50:42Z", l1.getValue().toString());
//System.err.println("date is: "+ncal.getTime());
}
// Internal helper
private void doDateTimeTest(Calendar cal, String lex, double time) {
Literal lc4 = m.createTypedLiteral(cal);
assertEquals("serialization", lex, lc4.getValue().toString());
assertEquals("calendar ms test", m.createTypedLiteral(lex, XSDDatatype.XSDdateTime), lc4 );
XSDDateTime dt4 = (XSDDateTime)lc4.getValue();
assertTrue("Fraction time check", Math.abs(dt4.getSeconds() - time) < 0.0001);
assertEquals(dt4.asCalendar(), cal);
}
/**
* Test query applied to graphs containing typed values
*/
public void testTypedContains() {
Model model = ModelFactory.createDefaultModel();
Property p = model.createProperty("urn:x-eg/p");
Literal l1 = model.createTypedLiteral("10", "http://www.w3.org/2001/XMLSchema#integer");
Literal l2 = model.createTypedLiteral("010", "http://www.w3.org/2001/XMLSchema#integer");
assertSameValueAs( "sameas test", l1, l2 );
Resource a = model.createResource("urn:x-eg/a");
a.addProperty( p, l1 );
assertTrue( model.getGraph().contains( a.asNode(), p.asNode(), l1.asNode() ) );
assertTrue( model.getGraph().contains( a.asNode(), p.asNode(), l2.asNode() ) );
}
/**
* Test query applied to graphs containing typed values
*/
public void testTypedQueries() {
Model model = ModelFactory.createDefaultModel();
Property p = model.createProperty("urn:x-eg/p");
Literal l1 = model.createTypedLiteral("10", "http://www.w3.org/2001/XMLSchema#integer");
Literal l2 = model.createTypedLiteral("010", "http://www.w3.org/2001/XMLSchema#integer");
assertSameValueAs("sameas test", l1, l2);
Resource a = model.createResource("urn:x-eg/a");
a.addProperty(p, l1);
assertTrue(model.getGraph().find(null, p.asNode(), l1.asNode()).hasNext());
assertTrue(model.getGraph().find(null, p.asNode(), l2.asNode()).hasNext());
assertTrue(model.getGraph().find(a.asNode(), p.asNode(), l2.asNode()).hasNext());
assertTrue( model.getGraph().contains( a.asNode(), p.asNode(), l2.asNode() ) );
Query q = new Query();
q.addMatch(a.asNode(), p.asNode(), l2.asNode());
Iterator qi = model.getGraph().queryHandler().prepareBindings(q, new Node[] {}).executeBindings();
assertTrue(qi.hasNext());
// Similar tests at Model API level
// Selector s1 = new SimpleSelector(a, p, l2);
assertTrue(model.listStatements( a, p, l2 ).hasNext());
}
/**
* Test the isValidLiteral machinery
*/
public void testIsValidLiteral() {
Literal l = m.createTypedLiteral("1000", XSDDatatype.XSDinteger);
LiteralLabel ll = l.asNode().getLiteral();
assertTrue(XSDDatatype.XSDlong.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDint.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDshort.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDunsignedInt.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDunsignedLong.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDunsignedShort.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDpositiveInteger.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(ll));
assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(ll));
assertTrue( ! XSDDatatype.XSDbyte.isValidLiteral(ll));
assertTrue( ! XSDDatatype.XSDnegativeInteger.isValidLiteral(ll));
l = m.createTypedLiteral("-2", XSDDatatype.XSDinteger);
ll = l.asNode().getLiteral();
assertTrue(XSDDatatype.XSDlong.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDint.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDshort.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDunsignedInt.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDunsignedLong.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDunsignedShort.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDpositiveInteger.isValidLiteral(ll));
assertTrue( ! XSDDatatype.XSDstring.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDbyte.isValidLiteral(ll));
assertTrue(XSDDatatype.XSDnegativeInteger.isValidLiteral(ll));
l = m.createTypedLiteral("4.5", XSDDatatype.XSDfloat);
ll = l.asNode().getLiteral();
assertTrue(! XSDDatatype.XSDdouble.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDdecimal.isValidLiteral(ll));
Literal l2 = m.createTypedLiteral("foo", XSDDatatype.XSDstring);
assertTrue(XSDDatatype.XSDstring.isValidLiteral(l2.asNode().getLiteral()));
assertTrue(XSDDatatype.XSDnormalizedString.isValidLiteral(l2.asNode().getLiteral()));
assertTrue( ! XSDDatatype.XSDint.isValidLiteral(l2.asNode().getLiteral()));
l = m.createTypedLiteral("foo bar");
ll = l.asNode().getLiteral();
assertTrue(XSDDatatype.XSDstring.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDint.isValidLiteral(ll));
l = m.createTypedLiteral("12");
ll = l.asNode().getLiteral();
assertTrue(XSDDatatype.XSDstring.isValidLiteral(ll));
assertTrue(! XSDDatatype.XSDint.isValidLiteral(ll));
// Test the isValidValue form which had a problem with numbers
assertTrue(XSDDatatype.XSDnonNegativeInteger.isValidValue(new Integer(10)));
assertTrue(XSDDatatype.XSDnonNegativeInteger.isValidValue(new Integer(10)));
assertTrue(!XSDDatatype.XSDnonNegativeInteger.isValidValue(new Integer(-10)));
assertTrue(!XSDDatatype.XSDnonNegativeInteger.isValidValue("10"));
// The correct behaviour on float/double is unclear but will be clarified
// by the SWBP working group task force on XML schema.
// For now we leave that float, double and the decimal tree are all distinct
assertTrue(XSDDatatype.XSDfloat.isValidValue(new Float("2.3")));
assertTrue(XSDDatatype.XSDdouble.isValidValue(new Double("2.3")));
assertTrue( ! XSDDatatype.XSDfloat.isValidValue(new Integer("2")));
assertTrue( ! XSDDatatype.XSDfloat.isValidValue(new Double("2.3")));
}
/**
* Test binary types base64 and hexbinary
*/
public void testBinary() {
// Check byte[] maps onto a binary type
byte[] data = new byte[]{12, 42, 99};
Literal l = m.createTypedLiteral(data);
LiteralLabel ll = l.asNode().getLiteral();
assertEquals("binary test 1", ll.getDatatype(), XSDDatatype.XSDbase64Binary);
assertEquals("binary test 2", "DCpj", ll.getLexicalForm());
// Check round tripping from value
LiteralLabel l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDbase64Binary).asNode().getLiteral();
Object data2 = l2.getValue();
assertTrue("binary test 3", data2 instanceof byte[]);
byte[] data2b = (byte[])data2;
assertEquals("binary test 4", data2b[0], 12);
assertEquals("binary test 5", data2b[1], 42);
assertEquals("binary test 6", data2b[2], 99);
assertEquals(l2, ll);
l2 = m.createTypedLiteral("DCpj", XSDDatatype.XSDbase64Binary).asNode().getLiteral();
data2 = l2.getValue();
assertTrue("binary test 3", data2 instanceof byte[]);
data2b = ((byte[])data2);
assertEquals("binary test 4", data2b[0], 12);
assertEquals("binary test 5", data2b[1], 42);
assertEquals("binary test 6", data2b[2], 99);
// Check hexBinary
l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
ll = l.asNode().getLiteral();
assertEquals("binary test 1b", ll.getDatatype(), XSDDatatype.XSDhexBinary);
assertEquals("binary test 2b", HexBin.encode(data), ll.getLexicalForm());
// Check round tripping from value
l2 = m.createTypedLiteral(ll.getLexicalForm(), XSDDatatype.XSDhexBinary).asNode().getLiteral();
data2 = l2.getValue();
assertTrue("binary test 3b", data2 instanceof byte[]);
data2b = ((byte[])data2);
assertEquals("binary test 4b", data2b[0], 12);
assertEquals("binary test 5b", data2b[1], 42);
assertEquals("binary test 6b", data2b[2], 99);
assertEquals(l2, ll);
Literal la = m.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
Literal lb = m.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
assertTrue("equality test", la.sameValueAs(lb));
data = new byte[] {15, (byte)0xB7};
l = m.createTypedLiteral(data, XSDDatatype.XSDhexBinary);
assertEquals("hexBinary encoding", "0FB7", l.getLexicalForm());
}
/**
* Attempt to isolate a JDK-dependent bug that only appears under 1.4.1_*.
* This failed to provoke the bug.
*/
public void XXtestBinaryBug() throws IOException {
Model orig = ModelFactory.createDefaultModel();
Resource r = orig.createResource("http://jena.hpl.hp.com/test#r");
Property p = orig.createProperty("http://jena.hpl.hp.com/test#p");
Literal l = orig.createTypedLiteral("GpM7", XSDDatatype.XSDbase64Binary);
orig.add(r, p, l);
for (int i = 0; i < 150; i++) {
l = orig.createTypedLiteral(new byte[]{(byte)i, (byte)i, (byte)i});
orig.add(orig.createResource("urn:x-hp:" + i), p, l);
}
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
orig.write(out, "RDF/XML-ABBREV");
out.close();
InputStream ins = new ByteArrayInputStream(out.toByteArray());
Model m2 = ModelFactory.createDefaultModel();
m2.read(ins, null);
ins.close();
assertTrue(orig.isIsomorphicWith(m2));
}
/**
* Test a user error report concerning date/time literals
*/
public void testDateTimeBug() {
// Bug in serialization
String XSDDateURI = XSD.date.getURI();
TypeMapper typeMapper=TypeMapper.getInstance();
RDFDatatype dt = typeMapper.getSafeTypeByName(XSDDateURI);
Object obj = dt.parse("2003-05-21");
Literal literal = m.createTypedLiteral(obj, dt); String serialization = literal.toString();
Object value2 = dt.parse(obj.toString());
assertEquals(obj, value2);
// Check alternativ form doesn't provoke exceptions
RDFDatatype dateType = XSDDatatype.XSDdate;
Literal l = m.createTypedLiteral("2003-05-21", dateType);
// Check alt time times
checkSerialization("2003-05-21", XSDDatatype.XSDdate);
checkSerialization("2003-05-21T12:56:10Z", XSDDatatype.XSDdateTime);
checkSerialization("2003-05", XSDDatatype.XSDgYearMonth);
checkSerialization("2003", XSDDatatype.XSDgYear);
checkSerialization("--05", XSDDatatype.XSDgMonth);
checkSerialization("--05-12", XSDDatatype.XSDgMonthDay);
checkSerialization("---12", XSDDatatype.XSDgDay);
}
/**
* Test global parameter flags.
*/
public void testFlags() {
boolean originalFlag = JenaParameters.enableEagerLiteralValidation;
JenaParameters.enableEagerLiteralValidation = true;
boolean foundException = false;
try {
Literal l = m.createTypedLiteral("fool", XSDDatatype.XSDint);
} catch (DatatypeFormatException e1) {
foundException = true;
}
JenaParameters.enableEagerLiteralValidation = originalFlag;
assertTrue("Early datatype format exception", foundException);
JenaParameters.enableEagerLiteralValidation = false;
foundException = false;
Literal l = null;
try {
l = m.createTypedLiteral("fool", XSDDatatype.XSDint);
} catch (DatatypeFormatException e1) {
JenaParameters.enableEagerLiteralValidation = originalFlag;
assertTrue("Delayed datatype format validation", false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -