Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Berezovskyi <[email protected]>
  • Loading branch information
berezovskyi committed Jun 30, 2023
1 parent c074c6d commit 4481832
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trim_trailing_whitespace = true
[*.{java,kt}]
indent_style = space
indent_size = 4
max_line_length = 120

# indent code easily
[*.md]
Expand All @@ -20,3 +21,4 @@ indent_size = 4
[*.{xml,html,js,yml,css}]
indent_style = space
indent_size = 2
max_line_length = 100
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ static boolean isCollectionType(final Class<?> returnType) {
return Collection.class.isAssignableFrom(returnType);
}

private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, final Method method, final OslcOccurs occursAnnotation) throws OslcCoreInvalidOccursException {
private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, final Method method,
final OslcOccurs occursAnnotation)
throws OslcCoreInvalidOccursException {
final Class<?> returnType = method.getReturnType();
final Occurs occurs = occursAnnotation.value();

Expand All @@ -434,18 +436,22 @@ private static void validateUserSpecifiedOccurs(final Class<?> resourceClass, fi
}
}

protected static void validateUserSpecifiedValueType(final Class<?> resourceClass, final Method method, final ValueType userSpecifiedValueType, final Representation userSpecifiedRepresentation, final Class<?> componentType) throws OslcCoreInvalidValueTypeException {
final ValueType calculatedValueType = CLASS_TO_VALUE_TYPE.get(componentType);
// ValueType is valid if ...
// user-specified value type matches calculated value type
// or
// user-specified value type is local resource (we will validate the local resource later)
protected static void validateUserSpecifiedValueType(final Class<?> resourceClass, final Method method,
final ValueType userSpecifiedValueType,
final Representation userSpecifiedRepresentation,
final Class<?> componentType)
throws OslcCoreInvalidValueTypeException {
final ValueType calculatedValueType = CLASS_TO_VALUE_TYPE.get(componentType);
// ValueType is valid if ...
// user-specified value type matches calculated value type
// or
// user-specified value type is local resource (we will validate the local resource later)
// or
// user-specified value type is non-local resource, and Representation is Inline (we will validate later)
// or
// user-specified value type is xml literal and calculated value type is string
// or
// user-specified value type is decimal and calculated value type is numeric
// or
// user-specified value type is xml literal and calculated value type is string
// or
// user-specified value type is decimal and calculated value type is numeric
if ((userSpecifiedValueType.equals(calculatedValueType))
||
(ValueType.LocalResource.equals(userSpecifiedValueType))
Expand All @@ -455,9 +461,7 @@ protected static void validateUserSpecifiedValueType(final Class<?> resourceClas
||
((ValueType.XMLLiteral.equals(userSpecifiedValueType))
&&
(ValueType.String.equals(calculatedValueType)
// || ValueType.XMLLiteral.equals(calculatedValueType)
)
(ValueType.String.equals(calculatedValueType))
)
||
((ValueType.Decimal.equals(userSpecifiedValueType))
Expand All @@ -470,33 +474,36 @@ protected static void validateUserSpecifiedValueType(final Class<?> resourceClas
)
)
) {
// We have a valid user-specified value type for our Java type
return;
}
// We have a valid user-specified value type for our Java type
return;
}

throw new OslcCoreInvalidValueTypeException(resourceClass, method, userSpecifiedValueType);
}
throw new OslcCoreInvalidValueTypeException(resourceClass, method, userSpecifiedValueType);
}

private static void validateUserSpecifiedRepresentation(final Class<?> resourceClass, final Method method, final Representation userSpecifiedRepresentation, final Class<?> componentType) throws OslcCoreInvalidRepresentationException {
private static void validateUserSpecifiedRepresentation(final Class<?> resourceClass, final Method method,
final Representation userSpecifiedRepresentation,
final Class<?> componentType)
throws OslcCoreInvalidRepresentationException {
// Throw an Exception if ...
// User-specified representation is reference and component is not URI
// or
// user-specified representation is inline and component is a standard class

if (null == userSpecifiedRepresentation) {
return;
}
if (((Representation.Reference.equals(userSpecifiedRepresentation))
&&
(!URI.class.equals(componentType))
)
||
((Representation.Inline.equals(userSpecifiedRepresentation))
&&
(CLASS_TO_VALUE_TYPE.containsKey(componentType))
)
) {
throw new OslcCoreInvalidRepresentationException(resourceClass, method, userSpecifiedRepresentation);
}
if (null == userSpecifiedRepresentation) {
return;
}
if (((Representation.Reference.equals(userSpecifiedRepresentation))
&&
(!URI.class.equals(componentType))
)
||
((Representation.Inline.equals(userSpecifiedRepresentation))
&&
(CLASS_TO_VALUE_TYPE.containsKey(componentType))
)
) {
throw new OslcCoreInvalidRepresentationException(resourceClass, method, userSpecifiedRepresentation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,11 @@ else if (Collection.class.isAssignableFrom(setMethodComponentParameterClass))
final Literal literal = o.asLiteral();
final String stringValue = literal.getString();

if (String.class == setMethodComponentParameterClass)
{
if (String.class == setMethodComponentParameterClass) {
parameter = stringValue;
}
else if (XMLLiteral.class == setMethodComponentParameterClass) {
//TODO: handle the same way as the extended property
} else if (XMLLiteral.class == setMethodComponentParameterClass) {
parameter = new XMLLiteral(literal.getString());
}
else if ((Boolean.class == setMethodComponentParameterClass) ||
} else if ((Boolean.class == setMethodComponentParameterClass) ||
(Boolean.TYPE == setMethodComponentParameterClass))
{
// XML supports both 'true' and '1' for a true Boolean.
Expand Down Expand Up @@ -1916,24 +1912,18 @@ private static void handleLocalResource(final Class<?> resourceClass,
return;
}

if (value instanceof String)
{
if (onlyNested)
{
if (value instanceof String) {
if (onlyNested) {
return;
}

if (xmlLiteral)
{
if (xmlLiteral) {
nestedNode = model.createTypedLiteral(value.toString(),
XMLLiteralType.theXMLLiteralType);
}
else
{
XMLLiteralType.theXMLLiteralType);
} else {
nestedNode = model.createLiteral(value.toString());
}
}
else if (value instanceof XMLLiteral) {
} else if (value instanceof XMLLiteral) {
if (xmlLiteral) {
nestedNode = model.createTypedLiteral(((XMLLiteral) value).getValue(),
XMLLiteralType.theXMLLiteralType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.lyo.oslc4j.provider.jena.AbstractOslcRdfXmlProvider;
import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
import org.eclipse.lyo.oslc4j.provider.jena.test.resources.TestResourceWithLiterals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -45,69 +44,32 @@ public class XmlLiteralTest {
@Before
public void before() {
System.getProperties().remove(AbstractOslcRdfXmlProvider.OSLC4J_STRICT_DATATYPES);
// System.setProperty(AbstractOslcRdfXmlProvider.OSLC4J_STRICT_DATATYPES, "false");
}

@After
public void after() {
System.getProperties().remove(AbstractOslcRdfXmlProvider.OSLC4J_STRICT_DATATYPES);
}


/* ROUND-TRIPPING TESTS */

@Test
public void roundtripTestXmlDatatypeRDF() throws DatatypeConfigurationException, OslcCoreApplicationException,
InvocationTargetException, IllegalAccessException {
final Model diskModel = readModel("/xml_literals/datatype_rdf.rdf", RDFLanguages.strLangRDFXML);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue(diskModel.isIsomorphicWith(lyoModel));
assertModelUnmarshalsAndBackIsomorphically(diskModel, TestResourceWithLiterals.class);
}

@Test
public void roundtripTestXmlParsetypeWithAnnotatedLiteralProperty() throws DatatypeConfigurationException,
OslcCoreApplicationException, InvocationTargetException, IllegalAccessException {
final Model diskModel = readModel("/xml_literals/parsetype_annot.rdf", RDFLanguages.strLangRDFXML);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue("Models should match: " + showModels(diskModel, lyoModel), diskModel.isIsomorphicWith(lyoModel));
}

private String showModels(Model expected, Model resulting) {
return String.format("EXPECTED model:\n\n%s\n" +
"=======================================================\n\n"
+ "RESULTING model:\n\n%s\n", prettyPrintModel(expected), prettyPrintModel(resulting));
}

private String prettyPrintModel(Model model) {
StringWriter writer = new StringWriter(4096);
RDFDataMgr.write(writer, model, RDFFormat.TRIG_PRETTY);
return writer.toString();
assertModelUnmarshalsAndBackIsomorphically(diskModel, TestResourceWithLiterals.class);
}

@Test
public void roundtripTestXmlParsetypeWithAnnotatedStringProperty() throws DatatypeConfigurationException,
OslcCoreApplicationException, InvocationTargetException, IllegalAccessException {
final Model diskModel = readModel("/xml_literals/parsetype_annot_string.rdf", RDFLanguages.strLangRDFXML);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue(diskModel.isIsomorphicWith(lyoModel));
assertModelUnmarshalsAndBackIsomorphically(diskModel, TestResourceWithLiterals.class);
}

/* INVALID LITERAL TESTS */
Expand All @@ -121,13 +83,7 @@ public void invalidLiteralTest_NoRoot() throws DatatypeConfigurationException,
// final Model diskModel = readModel("/xml_literals/invalid_noroot_parsetype.rdf", RDFLanguages.strLangRDFXML);
final Model diskModel = readModel("/xml_literals/invalid_noroot.ttl", RDFLanguages.strLangTurtle);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue(diskModel.isIsomorphicWith(lyoModel));
assertModelUnmarshalsAndBackIsomorphically(diskModel, TestResourceWithLiterals.class);
}

/**
Expand All @@ -136,34 +92,21 @@ public void invalidLiteralTest_NoRoot() throws DatatypeConfigurationException,
@Test
public void invalidLiteralTest_NoEscape() throws DatatypeConfigurationException,
OslcCoreApplicationException, InvocationTargetException, IllegalAccessException {
// final Model diskModel = readModel("/xml_literals/invalid_noroot_parsetype.rdf", RDFLanguages.strLangRDFXML);
final Model diskModel = readModel("/xml_literals/invalid_noescape.ttl", RDFLanguages.strLangTurtle);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue(diskModel.isIsomorphicWith(lyoModel));
assertModelUnmarshalsAndBackIsomorphically(diskModel, TestResourceWithLiterals.class);
}

/**
* Not escaping & turns XML invalid. NB! Unescaped & in Turtle will get escaped when you use libs to convert to RDF/XML.
* Not escaping & turns XML invalid. NB! Unescaped & in Turtle will get escaped when you use libs to convert to
* RDF/XML.
*/
@Test(expected = RiotException.class)
public void invalidLiteralTest_BadXml() throws DatatypeConfigurationException,
OslcCoreApplicationException, InvocationTargetException, IllegalAccessException {
// final Model diskModel = readModel("/xml_literals/invalid_noroot_parsetype.rdf", RDFLanguages.strLangRDFXML);
public void invalidLiteralTest_BadXml() {
final Model diskModel = readModel("/xml_literals/invalid_badxml.rdf", RDFLanguages.strLangTurtle);

final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, TestResourceWithLiterals.class);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue(diskModel.isIsomorphicWith(lyoModel));
final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel,
TestResourceWithLiterals.class);
}


Expand Down Expand Up @@ -244,4 +187,31 @@ private Model readModel(String filePath, String lang) {
m.read(is, null, lang);
return m;
}

/*HELPERS*/

private void assertModelUnmarshalsAndBackIsomorphically(Model diskModel, Class<TestResourceWithLiterals> klass)
throws DatatypeConfigurationException, IllegalAccessException, InvocationTargetException,
OslcCoreApplicationException {
final TestResourceWithLiterals[] testResources = JenaModelHelper.unmarshal(diskModel, klass);
assertEquals("Expected only one TestResource resource", 1, testResources.length);
final TestResourceWithLiterals resource = testResources[0];

Model lyoModel = JenaModelHelper.createJenaModel(new Object[]{resource});

assertTrue("Models should match: " + showModels(diskModel, lyoModel), diskModel.isIsomorphicWith(lyoModel));
}

private String showModels(Model expected, Model resulting) {
return String.format("EXPECTED model:\n\n%s\n" +
"=======================================================\n\n"
+ "RESULTING model:\n\n%s\n", prettyPrintModel(expected), prettyPrintModel(resulting));
}

private String prettyPrintModel(Model model) {
StringWriter writer = new StringWriter(4096);
RDFDataMgr.write(writer, model, RDFFormat.TRIG_PRETTY);
return writer.toString();
}

}

0 comments on commit 4481832

Please sign in to comment.