diff --git a/src/main/java/cz/cvut/kbss/jsonld/exception/MissingTypeInfoException.java b/src/main/java/cz/cvut/kbss/jsonld/exception/MissingTypeInfoException.java new file mode 100644 index 0000000..5b8ec33 --- /dev/null +++ b/src/main/java/cz/cvut/kbss/jsonld/exception/MissingTypeInfoException.java @@ -0,0 +1,11 @@ +package cz.cvut.kbss.jsonld.exception; + +/** + * Indicates that no type info was found when serializing an object. + */ +public class MissingTypeInfoException extends JsonLdSerializationException { + + public MissingTypeInfoException(String message) { + super(message); + } +} diff --git a/src/main/java/cz/cvut/kbss/jsonld/serialization/traversal/InstanceTypeResolver.java b/src/main/java/cz/cvut/kbss/jsonld/serialization/traversal/InstanceTypeResolver.java index 455b4e2..ba0d93c 100644 --- a/src/main/java/cz/cvut/kbss/jsonld/serialization/traversal/InstanceTypeResolver.java +++ b/src/main/java/cz/cvut/kbss/jsonld/serialization/traversal/InstanceTypeResolver.java @@ -1,22 +1,21 @@ /** * Copyright (C) 2017 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jsonld.serialization.traversal; import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor; import cz.cvut.kbss.jsonld.common.BeanClassProcessor; import cz.cvut.kbss.jsonld.exception.BeanProcessingException; +import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException; import java.lang.reflect.Field; import java.util.Collection; @@ -54,6 +53,10 @@ Set resolveTypes(Object instance) { runtimeTypes.forEach(t -> declaredTypes.add(t.toString())); } }); + if (declaredTypes.isEmpty()) { + throw new MissingTypeInfoException("No type info found on instance " + instance + + ". Either annotate the class with @OWLClass or provide a non-empty @Types field."); + } return declaredTypes; } } diff --git a/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java b/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java index e835ea2..214b838 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java @@ -1,26 +1,27 @@ /** * Copyright (C) 2017 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jsonld.serialization.traversal; +import cz.cvut.kbss.jopa.model.annotations.Id; import cz.cvut.kbss.jopa.model.annotations.OWLClass; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; +import cz.cvut.kbss.jopa.model.annotations.Types; import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder; import cz.cvut.kbss.jsonld.environment.Generator; import cz.cvut.kbss.jsonld.environment.Vocabulary; import cz.cvut.kbss.jsonld.environment.model.*; import cz.cvut.kbss.jsonld.exception.MissingIdentifierException; +import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException; import org.hamcrest.core.StringStartsWith; import org.junit.Before; import org.junit.Rule; @@ -37,6 +38,7 @@ import java.util.Map; import java.util.Set; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; @@ -225,7 +227,7 @@ public void traversePutsVisitedInstanceTogetherWithIdentifierIntoKnownInstancesO @Test public void traverseGeneratesBlankNodeIdentifierWhenPuttingInstanceWithoutIdentifierIntoKnownInstances() throws - Exception { + Exception { final Person person = Generator.generatePerson(); person.setUri(null); traverser.traverse(person); @@ -252,7 +254,8 @@ public void traverseInvokesVisitWhenInstanceIsPlainIdentifierPropertyValue() thr traverser.traverse(employee); final InOrder inOrder = inOrder(visitor); inOrder.verify(visitor).openInstance(employee); - inOrder.verify(visitor).visitField(EmployeeWithUriEmployer.class.getDeclaredField("employer"), employee.employer); + inOrder.verify(visitor) + .visitField(EmployeeWithUriEmployer.class.getDeclaredField("employer"), employee.employer); inOrder.verify(visitor).openInstance(employee.employer); inOrder.verify(visitor).visitIdentifier(employee.employer.toString(), employee.employer); } @@ -275,4 +278,31 @@ public void traverseThrowsMissingIdentifierExceptionWhenIdentifierIsRequiredAndM traverser.setRequireId(true); traverser.traverse(person); } + + @Test + public void traverseThrowsMissingTypeInfoExceptionWhenObjectHasNoTypesAndIsNotOwlClass() { + final NoType instance = new NoType(); + instance.uri = Generator.generateUri(); + thrown.expect(MissingTypeInfoException.class); + thrown.expectMessage(containsString("@OWLClass")); + thrown.expectMessage(containsString("@Types")); + traverser.traverse(instance); + } + + @Test + public void traverseSupportsInstanceOfClassWithoutOWLClassAnnotationButWithNonEmptyTypes() { + final NoType instance = new NoType(); + instance.uri = Generator.generateUri(); + instance.types = Collections.singleton(Vocabulary.PERSON); + traverser.traverse(instance); + verify(visitor).visitTypes(instance.types, instance); + } + + private static class NoType { + @Id + private URI uri; + + @Types + private Set types; + } } \ No newline at end of file