Search in sources :

Example 16 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class TestLiteralLabels method testDatatypeIsEqualsNotCalledIfSecondOperandIsNotTyped.

public void testDatatypeIsEqualsNotCalledIfSecondOperandIsNotTyped() {
    RDFDatatype d = new BaseDatatype("eh:/FakeDataType") {

        @Override
        public boolean isEqual(LiteralLabel A, LiteralLabel B) {
            fail("RDFDatatype::isEquals should not be called if B has no datatype");
            return false;
        }
    };
    LiteralLabel A = LiteralLabelFactory.createByValue("17", "", d);
    LiteralLabel B = LiteralLabelFactory.createByValue("17", "", null);
    assertFalse(A.sameValueAs(B));
}
Also used : BaseDatatype(org.apache.jena.datatypes.BaseDatatype) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 17 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class TestLiteralLabels method testHashCode.

public void testHashCode() {
    LiteralLabel ll = LiteralLabelFactory.createByValue("test", "", null);
    ll.hashCode();
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 18 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class TestLiteralLabels method testHashCode2.

public void testHashCode2() {
    LiteralLabel ll1 = LiteralLabelFactory.createByValue("test", "", null);
    LiteralLabel ll2 = LiteralLabelFactory.createByValue("test", "", null);
    assertEquals(ll1.hashCode(), ll2.hashCode());
}
Also used : LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 19 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class Rational method testIsValidLiteral.

/**
 * 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(Integer.valueOf(10)));
    assertTrue(XSDDatatype.XSDnonNegativeInteger.isValidValue(Integer.valueOf(10)));
    assertTrue(!XSDDatatype.XSDnonNegativeInteger.isValidValue(Integer.valueOf(-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(Float.valueOf("2.3")));
    assertTrue(XSDDatatype.XSDdouble.isValidValue(Double.valueOf("2.3")));
    assertTrue(!XSDDatatype.XSDfloat.isValidValue(Integer.valueOf("2")));
    assertTrue(!XSDDatatype.XSDfloat.isValidValue(Double.valueOf("2.3")));
}
Also used : NodeFactory.createLiteral(org.apache.jena.graph.NodeFactory.createLiteral) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 20 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class Rational method testUnknown.

/**
 * Test the base functioning of unknown datatypes
 */
public void testUnknown() {
    String typeURI = "urn:x-hp-dt:unknown";
    String typeURI2 = "urn:x-hp-dt:unknown2";
    boolean originalFlag = JenaParameters.enableSilentAcceptanceOfUnknownDatatypes;
    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = true;
    Literal l1 = m.createTypedLiteral("foo", typeURI);
    Literal l3 = m.createTypedLiteral("15", typeURI);
    Literal l5 = m.createTypedLiteral("foo", typeURI2);
    Literal l6 = m.createLiteral("foo", "lang1");
    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
    // Check for successful creation
    assertNotNull(l1);
    assertNotNull(l3);
    assertNotNull(l5);
    // check equality function
    assertDiffer("datatype sensitive", l1, l5);
    assertDiffer("value sensitive", l1, l3);
    assertDiffer("typed and plain differ", l1, l6);
    // Check typed accessors
    try {
        l3.getInt();
        assertTrue("Allowed int conversion", false);
    } catch (DatatypeFormatException e) {
    }
    assertEquals("Extract value", l1.getValue(), new BaseDatatype.TypedValue("foo", typeURI));
    assertEquals("Extract xml tag", l1.isWellFormedXML(), false);
    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = false;
    boolean foundException = false;
    try {
        m.createTypedLiteral("food", typeURI + "3");
    } catch (DatatypeFormatException e2) {
        foundException = true;
    }
    JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
    assertTrue("Detected unknown datatype", foundException);
    // Check we can create a literal of an unregistered java type without anything blowing up
    Object foo = new java.sql.Date(123456l);
    LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(foo);
    assertEquals(ll.getLexicalForm(), foo.toString());
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) NodeFactory.createLiteral(org.apache.jena.graph.NodeFactory.createLiteral) BaseDatatype(org.apache.jena.datatypes.BaseDatatype) RDFLangString(org.apache.jena.datatypes.xsd.impl.RDFLangString) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Aggregations

LiteralLabel (org.apache.jena.graph.impl.LiteralLabel)43 Node (org.apache.jena.graph.Node)8 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)7 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)5 NodeFactory.createLiteral (org.apache.jena.graph.NodeFactory.createLiteral)5 Test (org.junit.Test)5 RDFLangString (org.apache.jena.datatypes.xsd.impl.RDFLangString)4 BigDecimal (java.math.BigDecimal)3 BaseDatatype (org.apache.jena.datatypes.BaseDatatype)3 SpatialIndexException (org.apache.jena.query.spatial.SpatialIndexException)3 Var (org.apache.jena.sparql.core.Var)3 UUID (java.util.UUID)2 FrontsNode (org.apache.jena.graph.FrontsNode)2 Triple (org.apache.jena.graph.Triple)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 FileReader (java.io.FileReader)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Duration (javax.xml.datatype.Duration)1