📄 testtypedliterals.java
字号:
try {
l.getValue();
} catch (DatatypeFormatException e2) {
foundException = true;
}
JenaParameters.enableEagerLiteralValidation = originalFlag;
assertTrue("Early datatype format exception", foundException);
originalFlag = JenaParameters.enablePlainLiteralSameAsString;
Literal l1 = m.createLiteral("test string");
Literal l2 = m.createTypedLiteral("test string", XSDDatatype.XSDstring);
JenaParameters.enablePlainLiteralSameAsString = true;
boolean ok1 = l1.sameValueAs(l2);
JenaParameters.enablePlainLiteralSameAsString = false;
boolean ok2 = ! l1.sameValueAs(l2);
JenaParameters.enablePlainLiteralSameAsString = originalFlag;
assertTrue( ok1 );
assertTrue( ok2 );
}
/**
* Test that equality function takes lexical distinction into account.
*/
public void testLexicalDistinction() {
Literal l1 = m.createTypedLiteral("3.0", XSDDatatype.XSDdecimal);
Literal l2 = m.createTypedLiteral("3.00", XSDDatatype.XSDdecimal);
Literal l3 = m.createTypedLiteral("3.0", XSDDatatype.XSDdecimal);
assertSameValueAs("lexical form does not affect value", l1, l2);
assertSameValueAs("lexical form does not affect value", l3, l2);
assertTrue("lexical form affects equality", ! l1.equals(l2));
assertTrue("lexical form affects equality", l1.equals(l3));
// This version will become illegal in the future and will be removed then
l1 = m.createTypedLiteral("3", XSDDatatype.XSDint);
l2 = m.createTypedLiteral(" 3 ", XSDDatatype.XSDint);
l3 = m.createTypedLiteral("3", XSDDatatype.XSDint);
assertSameValueAs("lexical form does not affect value", l1, l2);
assertSameValueAs("lexical form does not affect value", l3, l2);
assertTrue("lexical form affects equality", ! l1.equals(l2));
assertTrue("lexical form affects equality", l1.equals(l3));
}
/**
* Test parse/unparse pairing for problem datatypes
*/
public void testRoundTrip() {
// Prior problem cases with unparsing
doTestRoundTrip("13:20:00.000", XSDDatatype.XSDtime, false);
doTestRoundTrip("GpM7", XSDDatatype.XSDbase64Binary, true);
doTestRoundTrip("0FB7", XSDDatatype.XSDhexBinary, true);
// check value round tripping
doTestValueRoundTrip("2005-06-27", XSDDatatype.XSDdate, true);
doTestValueRoundTrip("2005", XSDDatatype.XSDgYear, true);
doTestValueRoundTrip("2005-06", XSDDatatype.XSDgYearMonth, true);
doTestValueRoundTrip("13:20:00.000", XSDDatatype.XSDtime, true);
}
/**
* Check parse/unparse loop.
*/
public void doTestRoundTrip(String lex, RDFDatatype dt, boolean testeq) {
LiteralLabel ll = LiteralLabel.createLiteralLabel( lex, "", dt );
String lex2 = dt.unparse(ll.getValue());
if (testeq) {
assertEquals(lex, lex2);
}
LiteralLabel ll2 = LiteralLabel.createLiteralLabel( lex2, "", dt );
assertTrue( ll2.isWellFormed() );
}
/**
* Check getValue/rewrap loop.
*/
public void doTestValueRoundTrip(String lex, RDFDatatype dt, boolean testType) {
Literal l1 = m.createTypedLiteral(lex, dt);
Object o1 = l1.getValue();
Literal l2 = m.createTypedLiteral(o1);
assertTrue("value round trip", l1.sameValueAs(l2));
Object o2 = l2.getValue();
assertTrue("value round trip2", o1.equals(o2));
if (testType) {
assertEquals("Datatype round trip", dt, l2.getDatatype());
}
}
/**
* Test that two objects are not semantically the same
*/
private void assertDiffer( String title, Literal x, Literal y ) {
assertTrue( title, !x.sameValueAs( y ) );
}
/**
* Test that two objects are semantically the same
*/
private void assertSameValueAs( String title, Literal x, Literal y ) {
assertTrue( title, x.sameValueAs( y ) );
}
/**
* Test two doubles are equal to within 0.001
*/
private void assertFloatEquals(String title, double x, double y) {
assertTrue(title, Math.abs(x - y) < 0.001);
}
/**
* Check that constructing an illegal literal throws
* the right exception
*/
public void checkIllegalLiteral(String lex, RDFDatatype dtype) {
try {
Literal l = m.createTypedLiteral(lex, dtype);
l.getValue();
assertTrue("Failed to catch '" + lex + "' as an illegal " + dtype, false);
} catch (DatatypeFormatException e1) {
// OK this is what we expected
}
}
/**
* Check can legally construct a literal with given lex, value and dtype
*/
public void checkLegalLiteral(String lex, RDFDatatype dtype, Class jtype, Object value) {
Literal l = m.createTypedLiteral(lex, dtype);
assertEquals(l.getValue().getClass(), jtype);
assertEquals(l.getValue(), value);
assertEquals(l.getDatatype(), dtype);
}
/**
* Chek the serialization of the parse of a value.
*/
public void checkSerialization(String lex, RDFDatatype dtype) {
Literal l = m.createTypedLiteral(lex, dtype);
assertEquals(l.getValue().toString(), lex);
}
/** Helper function test an iterator against a list of objects - order dependent */
public void assertIteratorValues(Iterator it, Object[] vals) {
boolean[] found = new boolean[vals.length];
for (int i = 0; i < vals.length; i++) found[i] = false;
while (it.hasNext()) {
Object n = it.next();
boolean gotit = false;
for (int i = 0; i < vals.length; i++) {
if (n.equals(vals[i])) {
gotit = true;
found[i] = true;
}
}
assertTrue(gotit);
}
for (int i = 0; i < vals.length; i++) {
assertTrue(found[i]);
}
}
}
/**
* Datatype definition for the rational number representation
* defined below.
*/
class RationalType extends BaseDatatype {
public static final String theTypeURI = "urn:x-hp-dt:rational";
public static final RDFDatatype theRationalType = new RationalType();
/** private constructor - single global instance */
private RationalType() {
super(theTypeURI);
}
/**
* Convert a value of this datatype out
* to lexical form.
*/
public String unparse(Object value) {
Rational r = (Rational) value;
return Integer.toString(r.getNumerator()) + "/" + r.getDenominator();
}
/**
* Parse a lexical form of this datatype to a value
* @throws DatatypeFormatException if the lexical form is not legal
*/
public Object parse(String lexicalForm) throws DatatypeFormatException {
int index = lexicalForm.indexOf("/");
if (index == -1) {
throw new DatatypeFormatException(lexicalForm, theRationalType, "");
}
try {
int numerator = Integer.parseInt(lexicalForm.substring(0, index));
int denominator = Integer.parseInt(lexicalForm.substring(index+1));
return new Rational(numerator, denominator);
} catch (NumberFormatException e) {
throw new DatatypeFormatException(lexicalForm, theRationalType, "");
}
}
/**
* Compares two instances of values of the given datatype.
* This does not allow rationals to be compared to other number
* formats, lang tag is not significant.
*/
public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
return value1.getDatatype() == value2.getDatatype()
&& value1.getValue().equals(value2.getValue());
}
}
/**
* Representation of a rational number. Used for testing
* user defined datatypes
*/
class Rational {
private int numerator;
private int denominator;
Rational(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
/**
* Returns the denominator.
* @return int
*/
public int getDenominator() {
return denominator;
}
/**
* Returns the numerator.
* @return int
*/
public int getNumerator() {
return numerator;
}
/**
* Sets the denominator.
* @param denominator The denominator to set
*/
public void setDenominator(int denominator) {
this.denominator = denominator;
}
/**
* Sets the numerator.
* @param numerator The numerator to set
*/
public void setNumerator(int numerator) {
this.numerator = numerator;
}
/**
* Printable form - not parsable
*/
public String toString() {
return "rational[" + numerator + "/" + denominator + "]";
}
/**
* Equality check
*/
public boolean equals(Object o) {
if (o == null || !(o instanceof Rational)) return false;
Rational or = (Rational)o;
return (numerator == or.numerator && denominator == or.denominator);
}
}
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -