private static Literal<?> typedLiteral(Object value, String type) { return Literals.newTypedLiteral(value, URI.create("http://www.w3.org/2001/XMLSchema#"+type)); }
@Override public Value visitTypedLiteral(TypedLiteral<?> node, Value defaultResult) { return Literals.newTypedLiteral(serializable(node.getValue()),node.getType().toURI()); }
private void checkNewTypeLiteral(Object value) { for(URI datatype:Datatypes.temporalDatatypes()) { LOGGER.debug("Trying typed literal creation using {} ({}) with {}...",value,value.getClass().getName(),datatype); Literal<?> literal=Literals.newTypedLiteral(value, datatype); LOGGER.debug("Got: {}",literal); assertThat( temporalAssertionMessage(datatype, value), literal,instanceOf(DateTimeLiteral.class)); assertThat(((DateTimeLiteral)literal).get(),equalTo(this.dateTime.get())); } }
@Test public void testNewTypedLiteral$nonTemporal$notSerializable() { NotSerializableData data = new NotSerializableData(); URI datatype = URI.create("urn:my:datatype"); TypedLiteral<? extends Serializable> literal = Literals.newTypedLiteral(data, datatype); assertThat(literal.get(),equalTo((Serializable)data.toString())); assertThat(literal.type(),equalTo(Datatypes.STRING)); }
@Test(expected=DatatypeCohercionException.class) public void testNewTypedLiteral$duration$cohercionFailure$notString() throws DatatypeConfigurationException { checkDurationLiteral(Literals.newTypedLiteral(new Date(),Datatypes.DURATION)); }
@Test(expected=DatatypeCohercionException.class) public void testNewTypedLiteral$duration$cohercionFailure$string() throws DatatypeConfigurationException { checkDurationLiteral(Literals.newTypedLiteral("invalid",Datatypes.DURATION)); }
@Test public void testNewTypeLiteral$temporal$cohercionFailure$notString() { Object value = new Object(); for(URI datatype:Datatypes.temporalDatatypes()) { LOGGER.debug("Trying to coherce not temporal object using {} ({}) with {}...",value,value.getClass().getName(),datatype); try { Literals.newTypedLiteral(value, datatype); fail("Should fail when cohercion is not possible"); } catch (DatatypeCohercionException e) { LOGGER.debug("Failed as expected: {}",e.getMessage()); } } }
@Test public void testNewTypeLiteral$temporal$cohercionFailure$string() { Object value = "invalid"; for(URI datatype:Datatypes.temporalDatatypes()) { LOGGER.debug("Trying to coherce not temporal string using {} ({}) with {}...",value,value.getClass().getName(),datatype); try { Literals.newTypedLiteral(value, datatype); fail("Should fail when cohercion is not possible"); } catch (DatatypeCohercionException e) { LOGGER.debug("Failed as expected: {}",e.getMessage()); } } }
@Test public void testVisitLiteral$languageLiteral() throws Exception { LiteralValueExtractor<String> extractor = LiteralValueExtractor.newInstance(String.class); extractor.visitLiteral(Literals.newTypedLiteral("1",Datatypes.STRING)); assertThat(extractor.getValue(),equalTo(LITERAL_VALUE.toString())); assertThat(extractor.isAvailable(),equalTo(true)); }
@Test public void testNewTypedLiteral$nonTemporal$serializable() { SerializableData data = new SerializableData(); URI datatype = URI.create("urn:my:datatype"); TypedLiteral<? extends Serializable> literal = Literals.newTypedLiteral(data, datatype); assertThat(literal.get(),sameInstance((Serializable)data)); assertThat(literal.type(),equalTo(datatype)); }
@Test public void testVisitLiteral$typedLiteral() throws Exception { LiteralValueExtractor<Number> extractor = LiteralValueExtractor.newInstance(Number.class); extractor.visitLiteral(Literals.newTypedLiteral(1,Datatypes.STRING)); assertThat(extractor.getValue(),equalTo(LITERAL_VALUE)); assertThat(extractor.isAvailable(),equalTo(true)); }
@Test public void testVisitLiteral$typedLiteral$doesNotMatch() throws Exception { LiteralValueExtractor<Number> extractor = LiteralValueExtractor.newInstance(Number.class); extractor.visitLiteral(Literals.newTypedLiteral("test",Datatypes.STRING)); assertThat(extractor.getValue(),nullValue()); assertThat(extractor.isAvailable(),equalTo(false)); }
@Test public void testVisitTypedLiteral$valid() throws Exception { LiteralAdapter<Number> nAdapter = LiteralAdapter.newInstance(Number.class); nAdapter.visitTypedLiteral(Literals.newTypedLiteral(1,Datatypes.STRING)); assertThat(nAdapter.adaptedValue(),equalTo((Number)1)); }
@Test public void testNewTypedLiteral$duration$jodaDuration() { checkDurationLiteral(Literals.newTypedLiteral(jodaDuration(),Datatypes.DURATION)); }
@Test public void testNewTypedLiteral$duration$javaDuration() throws DatatypeConfigurationException { checkDurationLiteral(Literals.newTypedLiteral(javaDuration(),Datatypes.DURATION)); }
@Test public void testNewTypedLiteral$duration$string() throws DatatypeConfigurationException { checkDurationLiteral(Literals.newTypedLiteral(javaDuration().toString(),Datatypes.DURATION)); }