RdfToRyaConversions { public static RyaType convertLiteral(final Literal literal) { if (literal == null) { return null; } if (literal.getDatatype() != null) { if (Literals.isLanguageLiteral(literal)) { final String language = literal.getLanguage().get(); if (Literals.isValidLanguageTag(language)) { return new RyaType(literal.getDatatype(), literal.stringValue(), language); } else { log.warn("Invalid language (" + LogUtils.clean(language) + ") found in Literal. Defaulting to: " + UNDETERMINED_LANGUAGE); return new RyaType(literal.getDatatype(), literal.stringValue(), UNDETERMINED_LANGUAGE); } } return new RyaType(literal.getDatatype(), literal.stringValue()); } return new RyaType(literal.stringValue()); }  static RyaIRI convertIRI(final IRI iri); static RyaType convertLiteral(final Literal literal); static RyaType convertValue(final Value value); static RyaIRI convertResource(final Resource resource); static RyaStatement convertStatement(final Statement statement);  }
@Test public void testConvertLiteral_null() { final RyaType ryaType = RdfToRyaConversions.convertLiteral(null); assertNull(ryaType); }
@Test public void testConvertLiteral_nullDataType() { final Literal literal = mock(SimpleLiteral.class); final String expectedData = "Ice Cream"; when(literal.getLabel()).thenReturn(expectedData); when(literal.stringValue()).thenReturn(expectedData); when(literal.getDatatype()).thenReturn(null); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); final RyaType expected = new RyaType(XMLSchema.STRING, expectedData); assertEquals(expected, ryaType); assertNull(ryaType.getLanguage()); }
@Test public void testConvertLiteral_validLanguage() { final String expectedData = "Hello"; for (final String language : LANGUAGE_CODES) { assertTrue(Literals.isValidLanguageTag(language)); final Literal literal = VF.createLiteral(expectedData, language); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(RDF.LANGSTRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); assertEquals(language, ryaType.getLanguage()); final RyaType expectedRyaType = new RyaType(RDF.LANGSTRING, expectedData, language); assertEquals(expectedRyaType, ryaType); } }
@Test public void testConvertLiteral_undeterminedLanguage() { final String expectedData = "Hello"; final String language = LiteralLanguageUtils.UNDETERMINED_LANGUAGE; assertTrue(Literals.isValidLanguageTag(language)); final Literal literal = VF.createLiteral(expectedData, language); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(RDF.LANGSTRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); final RyaType expectedRyaType = new RyaType(RDF.LANGSTRING, expectedData, language); assertEquals(expectedRyaType, ryaType); assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, ryaType.getLanguage()); }
@Test public void testConvertLiteral_invalidLanguage() { final String expectedData = "Hello"; final List<String> badLanguages = Lists.newArrayList( "bad language", "en-", "en-US-" ); for (final String badLanguage : badLanguages) { assertFalse(Literals.isValidLanguageTag(badLanguage)); final Literal literal = VF.createLiteral(expectedData, badLanguage); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(RDF.LANGSTRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, ryaType.getLanguage()); } }
@Test public void testConvertLiteral_normalString() { final String expectedData = "Hello"; final Literal literal = VF.createLiteral(expectedData); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(XMLSchema.STRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); final RyaType expectedRyaType = new RyaType(XMLSchema.STRING, expectedData); assertEquals(expectedRyaType, ryaType); assertNull(ryaType.getLanguage()); }