From c48191d1430c5c62908180286bbd763a1cc81004 Mon Sep 17 00:00:00 2001 From: luxbe Date: Mon, 16 Dec 2024 15:13:14 +0100 Subject: [PATCH 01/17] [Enhancement #294] Add missing Short and Byte references to OWL DatatypeTransformer --- .../java/cz/cvut/kbss/jopa/owlapi/DatatypeTransformer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jopa-owlapi-utils/src/main/java/cz/cvut/kbss/jopa/owlapi/DatatypeTransformer.java b/jopa-owlapi-utils/src/main/java/cz/cvut/kbss/jopa/owlapi/DatatypeTransformer.java index f55c53a29..7b797faa8 100644 --- a/jopa-owlapi-utils/src/main/java/cz/cvut/kbss/jopa/owlapi/DatatypeTransformer.java +++ b/jopa-owlapi-utils/src/main/java/cz/cvut/kbss/jopa/owlapi/DatatypeTransformer.java @@ -122,6 +122,10 @@ public static OWLLiteral transform(Object value, String lang) { return DATA_FACTORY.getOWLLiteral(value.toString(), OWL2Datatype.XSD_INTEGER); } else if (value instanceof Long) { return DATA_FACTORY.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG); + } else if (value instanceof Short) { + return DATA_FACTORY.getOWLLiteral(value.toString(), OWL2Datatype.XSD_SHORT); + } else if (value instanceof Byte) { + return DATA_FACTORY.getOWLLiteral(value.toString(), OWL2Datatype.XSD_BYTE); } else if (value instanceof Boolean) { return DATA_FACTORY.getOWLLiteral((Boolean) value); } else if (value instanceof Double) { From 0666a34a58432592a84bfcfc3d9c4735059b26af Mon Sep 17 00:00:00 2001 From: luxbe Date: Mon, 16 Dec 2024 15:43:33 +0100 Subject: [PATCH 02/17] [Enhancement #294] Add DatatypeTransformer wrapperToPrimitive method - converts Wrapper classes (e.g. Integer.class) to their corresponding primitive counterparts (e.g. int.class) --- .../jopa/datatype/DatatypeTransformer.java | 27 +++++++++++++++++++ .../datatype/DatatypeTransformerTest.java | 23 ++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java index cecd5a84a..d476df538 100644 --- a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java +++ b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java @@ -41,10 +41,24 @@ public class DatatypeTransformer { private static final Map> TRANSFORMERS = initTransformers(); + private static final Map, Class> WRAPPER_TO_PRIMITIVES = initWrapperToPrimitives(); + private DatatypeTransformer() { throw new AssertionError(); } + private static Map, Class> initWrapperToPrimitives() { + final Map, Class> map = new HashMap<>(); + map.put(Integer.class, int.class); + map.put(Boolean.class, boolean.class); + map.put(Byte.class, byte.class); + map.put(Short.class, short.class); + map.put(Long.class, long.class); + map.put(Float.class, float.class); + map.put(Double.class, double.class); + return map; + } + private static Map> initTransformers() { final Map> map = new HashMap<>(); map.put(new Pair(Short.class, Integer.class), value -> ((Short) value).intValue()); @@ -79,6 +93,19 @@ private DatatypeTransformer() { return map; } + /** + * Converts the specified wrapper class to its corresponding primitive class. + * + * If the class parameter is a wrapper type, the equivalent primitive type will be returned (e.g. int.class for Integer.class) + * In all other cases, the return value is null. + * + * @param cls - the class to convert + * @return + */ + public static Class wrapperToPrimitive( Class cls ) { + return WRAPPER_TO_PRIMITIVES.get(cls); + } + /** * Maps the specified value to the target type (if possible). * diff --git a/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java b/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java index 8bd776885..f31b8ad88 100644 --- a/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java +++ b/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java @@ -32,6 +32,29 @@ class DatatypeTransformerTest { + @Test + void wrapperToPrimitiveReturnsNullForNullInput() { + assertNull(DatatypeTransformer.wrapperToPrimitive(null)); + } + + @ParameterizedTest + @MethodSource("wrapperToPrimitiveTestValues") + void wrapperToPrimitive(Class wrapper, Class primitive) { + assertEquals(primitive, DatatypeTransformer.wrapperToPrimitive(wrapper)); + } + + private static Stream wrapperToPrimitiveTestValues() { + return Stream.of( + Arguments.arguments(Integer.class, int.class), + Arguments.arguments(Boolean.class, boolean.class), + Arguments.arguments(Byte.class, byte.class), + Arguments.arguments(Short.class, short.class), + Arguments.arguments(Long.class, long.class), + Arguments.arguments(Float.class, float.class), + Arguments.arguments(Double.class, double.class) + ); + } + @Test void transformReturnsNullForNullInput() { assertNull(DatatypeTransformer.transform(null, String.class)); From 85548f1b0e1a31ab84af0567d9b9e3f4098e4201 Mon Sep 17 00:00:00 2001 From: luxbe Date: Mon, 16 Dec 2024 16:14:39 +0100 Subject: [PATCH 03/17] [Enhancement #294] Add support for primitive datatypes as fields - remove MetamodelInitializationException thrown in ClassFieldMetamodelProcessor - check if Wrapper type can be converted to primitive type in DataPropertyFieldStrategy --- .../ClassFieldMetamodelProcessor.java | 3 - .../jopa/oom/DataPropertyFieldStrategy.java | 9 ++ .../cz/cvut/kbss/jopa/test/OWLClassBB.java | 137 ++++++++++++++++++ .../cz/cvut/kbss/jopa/test/Vocabulary.java | 10 ++ .../kbss/jopa/test/runner/BaseRunner.java | 12 ++ .../test/runner/CreateOperationsRunner.java | 18 +++ .../test/runner/RetrieveOperationsRunner.java | 44 ++++++ .../test/runner/UpdateOperationsRunner.java | 51 +++++++ 8 files changed, 281 insertions(+), 3 deletions(-) create mode 100644 jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/ClassFieldMetamodelProcessor.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/ClassFieldMetamodelProcessor.java index d19036d6e..9469d65ee 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/ClassFieldMetamodelProcessor.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/ClassFieldMetamodelProcessor.java @@ -82,9 +82,6 @@ void processField(Field field) { } return; } - if (field.getType().isPrimitive()) { - throw new MetamodelInitializationException("Primitive types cannot be used for entity fields. Field " + field + " in class " + cls); - } final Class fieldValueCls = getFieldValueType(field); field.setAccessible(true); diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java index 311947d8c..4a50047a6 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java @@ -17,6 +17,7 @@ */ package cz.cvut.kbss.jopa.oom; +import cz.cvut.kbss.jopa.datatype.DatatypeTransformer; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.model.metamodel.AbstractAttribute; import cz.cvut.kbss.jopa.model.metamodel.EntityType; @@ -43,6 +44,14 @@ boolean isValidRange(Object value) { } boolean canBeConverted(Object value) { + if(attribute.getJavaType() != null && attribute.getJavaType().isPrimitive()) { + // if the value is a wrapper for the primitive attribute type, it can be converted automatically + Class primitiveClass = DatatypeTransformer.wrapperToPrimitive(value.getClass()); + if (primitiveClass != null && primitiveClass.equals(attribute.getJavaType())) { + return true; + } + } + return converter.supportsAxiomValueType(value.getClass()); } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java new file mode 100644 index 000000000..3abce702d --- /dev/null +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java @@ -0,0 +1,137 @@ +/* + * JOPA + * Copyright (C) 2024 Czech Technical University in Prague + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package cz.cvut.kbss.jopa.test; + +import cz.cvut.kbss.jopa.model.annotations.Id; +import cz.cvut.kbss.jopa.model.annotations.OWLClass; +import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; + +import java.net.URI; + +@OWLClass(iri = Vocabulary.C_OWL_CLASS_BB) +public class OWLClassBB implements HasUri { + @Id + private URI uri; + + @OWLDataProperty(iri = Vocabulary.P_BB_INT_ATTRIBUTE) + private int intAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_BOOLEAN_ATTRIBUTE) + private boolean booleanAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_BYTE_ATTRIBUTE) + private byte byteAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_SHORT_ATTRIBUTE) + private short shortAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_LONG_ATTRIBUTE) + private long longAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_FLOAT_ATTRIBUTE) + private float floatAttribute; + + @OWLDataProperty(iri = Vocabulary.P_BB_DOUBLE_ATTRIBUTE) + private double doubleAttribute; + + public OWLClassBB() { + } + + public OWLClassBB(URI uri) { + this.uri = uri; + } + + public void setUri(URI uri) { + this.uri = uri; + } + + @Override + public URI getUri() { + return uri; + } + + public int getIntAttribute() { + return intAttribute; + } + + public void setIntAttribute(int intAttribute) { + this.intAttribute = intAttribute; + } + + public byte getByteAttribute() { + return byteAttribute; + } + + public void setByteAttribute(byte byteAttribute) { + this.byteAttribute = byteAttribute; + } + + public double getDoubleAttribute() { + return doubleAttribute; + } + + public void setDoubleAttribute(double doubleAttribute) { + this.doubleAttribute = doubleAttribute; + } + + public float getFloatAttribute() { + return floatAttribute; + } + + public void setFloatAttribute(float floatAttribute) { + this.floatAttribute = floatAttribute; + } + + public long getLongAttribute() { + return longAttribute; + } + + public void setLongAttribute(long longAttribute) { + this.longAttribute = longAttribute; + } + + public short getShortAttribute() { + return shortAttribute; + } + + public void setShortAttribute(short shortAttribute) { + this.shortAttribute = shortAttribute; + } + + public boolean getBooleanAttribute() { + return booleanAttribute; + } + + public void setBooleanAttribute(boolean booleanAttribute) { + this.booleanAttribute = booleanAttribute; + } + + @Override + public String toString() { + return "OWLClassBB{" + + "uri=" + uri + + ", intAttribute=" + intAttribute + + ", booleanAttribute=" + booleanAttribute + + ", byteAttribute=" + byteAttribute + + ", shortAttribute=" + shortAttribute + + ", longAttribute=" + longAttribute + + ", floatAttribute=" + floatAttribute + + ", doubleAttribute=" + doubleAttribute + + '}'; + } +} diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java index 5a5bfb403..8cc5195c1 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java @@ -61,6 +61,7 @@ public class Vocabulary { public static final String C_OWL_CLASS_Y = CLASS_IRI_BASE + "OWLClassY"; public static final String C_OWL_CLASS_Z = CLASS_IRI_BASE + "OWLClassZ"; public static final String C_OWL_CLASS_PART_CONSTR_IN_PARENT = CLASS_IRI_BASE + "OWLClassWithPartConstraintsInInterfaceParent"; + public static final String C_OWL_CLASS_BB = CLASS_IRI_BASE + "OWLClassBB"; public static final String C_OWL_CLASS_Z_CHILD = CLASS_IRI_BASE + "OWLClassZChild"; public static final String C_OwlClassWithQueryAttr = CLASS_IRI_BASE + "OWLClassWithQueryAttr"; public static final String C_OwlClassWithQueryAttr2 = CLASS_IRI_BASE + "OWLClassWithQueryAttr2"; @@ -149,6 +150,15 @@ public class Vocabulary { public static final String P_Y_PLURAL_MULTILINGUAL_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "yPluralMultilingual"; public static final String P_AA_DYNAMIC_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "aaDynamicAttribute"; + public static final String P_BB_INT_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbIntAttribute"; + public static final String P_BB_BOOLEAN_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbBooleanAttribute"; + public static final String P_BB_BYTE_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbByteAttribute"; + public static final String P_BB_SHORT_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbShortAttribute"; + public static final String P_BB_LONG_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbLongAttribute"; + public static final String P_BB_FLOAT_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbFloatAttribute"; + public static final String P_BB_DOUBLE_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbDoubleAttribute"; + public static final String P_BB_CHAR_ATTRIBUTE = ATTRIBUTE_IRI_BASE + "bbCharAttribute"; + public static final String P_HAS_H = ATTRIBUTE_IRI_BASE + "hasH"; diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java index ff6142491..3b07fdbe8 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java @@ -23,6 +23,7 @@ import cz.cvut.kbss.jopa.test.OWLClassA; import cz.cvut.kbss.jopa.test.OWLClassAA; import cz.cvut.kbss.jopa.test.OWLClassB; +import cz.cvut.kbss.jopa.test.OWLClassBB; import cz.cvut.kbss.jopa.test.OWLClassC; import cz.cvut.kbss.jopa.test.OWLClassD; import cz.cvut.kbss.jopa.test.OWLClassE; @@ -95,6 +96,8 @@ public abstract class BaseRunner { protected OWLClassWithQueryAttr7 entityWithQueryAttr7; // Dynamic attributes protected OWLClassAA entityAA; + // Primitive attributes + protected OWLClassBB entityBB; protected final DataAccessor dataAccessor; protected final PersistenceFactory persistenceFactory; @@ -179,6 +182,15 @@ private void init() { entityWithQueryAttr7.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityWithQueryAttr7")); this.entityAA = new OWLClassAA(); this.entityAA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAA")); + this.entityBB = new OWLClassBB(); + this.entityBB.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityBB")); + entityBB.setIntAttribute(15); + entityBB.setBooleanAttribute(true); + entityBB.setByteAttribute((byte) 5); + entityBB.setShortAttribute((short) 10); + entityBB.setLongAttribute(20L); + entityBB.setFloatAttribute(25.5f); + entityBB.setDoubleAttribute(30.7d); } @AfterEach diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java index 78fdf989b..3000ba832 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java @@ -25,6 +25,7 @@ import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import cz.cvut.kbss.jopa.test.OWLClassA; import cz.cvut.kbss.jopa.test.OWLClassB; +import cz.cvut.kbss.jopa.test.OWLClassBB; import cz.cvut.kbss.jopa.test.OWLClassD; import cz.cvut.kbss.jopa.test.OWLClassE; import cz.cvut.kbss.jopa.test.OWLClassG; @@ -244,6 +245,23 @@ void testPersistEntityWithBasicTypeAttributes() { assertEquals(entityM.getDateAttribute(), res.getDateAttribute()); } + @Test + void testPersistEntityWithBasicPrimitiveTypeAttributes() { + this.em = getEntityManager("PersistEntityWithBasicPrimitiveTypeAttributes", false); + persist(entityBB); + em.clear(); + + final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); + assertEquals(entityBB.getUri(), res.getUri()); + assertEquals(entityBB.getIntAttribute(), res.getIntAttribute()); + assertEquals(entityBB.getBooleanAttribute(), res.getBooleanAttribute()); + assertEquals(entityBB.getByteAttribute(), res.getByteAttribute()); + assertEquals(entityBB.getShortAttribute(), res.getShortAttribute()); + assertEquals(entityBB.getLongAttribute(), res.getLongAttribute()); + assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute()); + assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute()); + } + @Test void testPersistAndUpdateAttributeBeforeCommit() { this.em = getEntityManager("PersistAndUpdateBeforeCommit", false); diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java index a9f51fdc8..fb1ea0f23 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java @@ -18,6 +18,7 @@ package cz.cvut.kbss.jopa.test.runner; import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties; +import cz.cvut.kbss.jopa.model.SequencesVocabulary; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import cz.cvut.kbss.jopa.model.query.TypedQuery; @@ -25,6 +26,7 @@ import cz.cvut.kbss.jopa.test.OWLClassA; import cz.cvut.kbss.jopa.test.OWLClassAA; import cz.cvut.kbss.jopa.test.OWLClassB; +import cz.cvut.kbss.jopa.test.OWLClassBB; import cz.cvut.kbss.jopa.test.OWLClassC; import cz.cvut.kbss.jopa.test.OWLClassD; import cz.cvut.kbss.jopa.test.OWLClassE; @@ -69,6 +71,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -106,6 +109,47 @@ void testRetrieveSimple() { assertTrue(em.contains(res)); } + @Test + void testRetrievePrimitive() { + this.em = getEntityManager("RetrieveSimplePrimitive", false); + persist(entityBB); + + em.getEntityManagerFactory().getCache().evictAll(); + final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); + assertEquals(entityBB.getUri(), res.getUri()); + assertEquals(entityBB.getIntAttribute(), res.getIntAttribute()); + assertEquals(entityBB.getBooleanAttribute(), res.getBooleanAttribute()); + assertEquals(entityBB.getByteAttribute(), res.getByteAttribute()); + assertEquals(entityBB.getShortAttribute(), res.getShortAttribute()); + assertEquals(entityBB.getLongAttribute(), res.getLongAttribute()); + assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute()); + assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute()); + assertTrue(em.contains(res)); + } + + @Test + void testRetrieveMissingPrimitive() throws Exception { + this.em = getEntityManager("RetrieveSimplePrimitive", false); + final List data = new ArrayList<>(List.of( + new Quad(entityBB.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_BB)) + )); + persistTestData(data, em); + + em.getEntityManagerFactory().getCache().evictAll(); + final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); + + // if primitives are not set, they should fall back to their default values + assertEquals(entityBB.getUri(), res.getUri()); + assertEquals(0, res.getIntAttribute()); + assertEquals(false, res.getBooleanAttribute()); + assertEquals(0, res.getByteAttribute()); + assertEquals((short) 0, res.getShortAttribute()); + assertEquals(0L, res.getLongAttribute()); + assertEquals(0.0f, res.getFloatAttribute()); + assertEquals(0.0d, res.getDoubleAttribute()); + assertTrue(em.contains(res)); + } + @Test void testRetrieveWithLazyAttribute() { this.em = getEntityManager("RetrieveLazy", false); diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java index 4767e5d5f..3e7a7fc6b 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java @@ -27,6 +27,7 @@ import cz.cvut.kbss.jopa.test.OWLClassA; import cz.cvut.kbss.jopa.test.OWLClassAA; import cz.cvut.kbss.jopa.test.OWLClassB; +import cz.cvut.kbss.jopa.test.OWLClassBB; import cz.cvut.kbss.jopa.test.OWLClassD; import cz.cvut.kbss.jopa.test.OWLClassE; import cz.cvut.kbss.jopa.test.OWLClassG; @@ -1050,6 +1051,56 @@ void updateSupportsUpdatingSimpleLiteralValue() { updateSimpleLiteralAndVerify(); } + @Test + void updateSupportsUpdatingPrimitiveLiteralValue() { + this.em = getEntityManager("updateSupportsUpdatingSimpleLiteralValue", true); + entityBB.setIntAttribute(15); + entityBB.setBooleanAttribute(true); + entityBB.setByteAttribute((byte) 5); + entityBB.setShortAttribute((short) 10); + entityBB.setLongAttribute(20L); + entityBB.setFloatAttribute(25.5f); + entityBB.setDoubleAttribute(30.7); + persist(entityBB); + + final int newIntValue = 20; + final boolean newBooleanValue = false; + final byte newByteValue = (byte) 8; + final short newShortValue = (short) 7; + final long newLongValue = 9L; + final float newFloatValue = 3.2f; + final double newDoubleValue = 8.9d; + + em.getTransaction().begin(); + final OWLClassBB toUpdate = findRequired(OWLClassBB.class, entityBB.getUri()); + toUpdate.setIntAttribute(newIntValue); + toUpdate.setBooleanAttribute(newBooleanValue); + toUpdate.setByteAttribute(newByteValue); + toUpdate.setShortAttribute(newShortValue); + toUpdate.setLongAttribute(newLongValue); + toUpdate.setFloatAttribute(newFloatValue); + toUpdate.setDoubleAttribute(newDoubleValue); + em.getTransaction().commit(); + + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_INT_ATTRIBUTE, XSD.INT); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_BOOLEAN_ATTRIBUTE, XSD.BOOLEAN); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_BYTE_ATTRIBUTE, XSD.BYTE); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_SHORT_ATTRIBUTE, XSD.SHORT); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_LONG_ATTRIBUTE, XSD.LONG); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_FLOAT_ATTRIBUTE, XSD.FLOAT); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_DOUBLE_ATTRIBUTE, XSD.DOUBLE); + final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); + assertEquals(entityBB.getUri(), res.getUri()); + assertEquals(newIntValue, res.getIntAttribute()); + assertEquals(newBooleanValue, res.getBooleanAttribute()); + assertEquals(newByteValue, res.getByteAttribute()); + assertEquals(newShortValue, res.getShortAttribute()); + assertEquals(newLongValue, res.getLongAttribute()); + assertEquals(newFloatValue, res.getFloatAttribute()); + assertEquals(newDoubleValue, res.getDoubleAttribute()); + } + + private void updateSimpleLiteralAndVerify() { em.getTransaction().begin(); final String newValue = "new test value"; From c6fb3e89333c926448e3d1991b46d3ca3bd3cf38 Mon Sep 17 00:00:00 2001 From: luxbe Date: Tue, 17 Dec 2024 10:12:05 +0100 Subject: [PATCH 04/17] [Enhancement #294] Add support for Character Java type --- .../kbss/jopa/model/metamodel/Converters.java | 4 +- .../model/metamodel/PropertyAttributes.java | 2 +- .../oom/converter/CharacterConverter.java | 50 +++++++++++++++++++ .../cvut/kbss/jopa/environment/OWLClassM.java | 24 ++++++++- .../kbss/jopa/environment/Vocabulary.java | 1 + .../environment/utils/MetamodelFactory.java | 21 ++++++-- .../environment/utils/MetamodelMocks.java | 8 ++- .../kbss/jopa/model/MetamodelImplTest.java | 6 +++ .../metamodel/ConverterResolverTest.java | 14 ++++++ .../kbss/jopa/oom/EntityConstructorTest.java | 14 ++++-- .../jopa/oom/EntityDeconstructorTest.java | 1 + .../oom/converter/CharacterConverterTest.java | 32 ++++++++++++ .../kbss/jopa/sessions/CloneBuilderTest.java | 4 ++ .../cz/cvut/kbss/jopa/test/OWLClassM.java | 14 ++++++ .../cz/cvut/kbss/jopa/test/Vocabulary.java | 1 + .../test/runner/CreateOperationsRunner.java | 1 + .../test/runner/RetrieveOperationsRunner.java | 1 + .../UpdateOperationsOnGetReferenceRunner.java | 1 + .../test/runner/UpdateOperationsRunner.java | 2 + 19 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java create mode 100644 jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java index cd5e72524..e69a763da 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java @@ -29,6 +29,7 @@ import cz.cvut.kbss.jopa.oom.converter.ToStringConverter; import cz.cvut.kbss.jopa.oom.converter.ToURIConverter; import cz.cvut.kbss.jopa.oom.converter.ToURLConverter; +import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.DateConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.InstantConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.LocalDateTimeConverter; @@ -91,7 +92,8 @@ void registerConverter(Class attributeType, ConverterWrapper converter) Map.entry(String.class, new ToStringConverter()), Map.entry(LangString.class, new ToLangStringConverter()), Map.entry(URI.class, new ToURIConverter()), - Map.entry(URL.class, new ToURLConverter())); + Map.entry(URL.class, new ToURLConverter()), + Map.entry(Character.class, new CharacterConverter())); } /** diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java index b29bb7c45..dfaf3da13 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java @@ -133,7 +133,7 @@ private void resolveEnumType(PropertyInfo propertyInfo, Class fieldValueCls) } String resolveLanguage(Class fieldValueCls) { - return MultilingualString.class.equals(fieldValueCls) ? null : typeBuilderContext.getPuLanguage(); + return MultilingualString.class.equals(fieldValueCls) || Character.class.equals(fieldValueCls) ? null : typeBuilderContext.getPuLanguage(); } static PropertyAttributes create(PropertyInfo field, FieldMappingValidator validator, TypeBuilderContext context) { diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java new file mode 100644 index 000000000..af5eca7a1 --- /dev/null +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java @@ -0,0 +1,50 @@ +/* + * JOPA + * Copyright (C) 2024 Czech Technical University in Prague + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package cz.cvut.kbss.jopa.oom.converter; + +import cz.cvut.kbss.ontodriver.model.LangString; + +/** + * Converts between {@link Character} and a xsd:string representation. + *

+ * This converter supports {@link String} and {@link LangString} as character representations. + */ +public class CharacterConverter implements ConverterWrapper { + + @Override + public Object convertToAxiomValue(Character value) { + assert value != null; + return value.toString(); + } + + @Override + public Character convertToAttribute(Object value) { + assert value != null; + if (value instanceof LangString ls) { + value = ls.getValue(); + } + + assert value instanceof String && ((String) value).length() == 1; + return ((String) value).charAt(0); + } + + @Override + public boolean supportsAxiomValueType(Class type) { + return String.class.isAssignableFrom(type); + } +} diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassM.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassM.java index 1e7da7806..6386b8267 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassM.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassM.java @@ -54,6 +54,9 @@ public class OWLClassM { @OWLDataProperty(iri = Vocabulary.p_m_dateAttribute) private Date dateAttribute; + @OWLDataProperty(iri = Vocabulary.p_m_characterAttribute) + private Character characterAttribute; + @OWLDataProperty(iri = Vocabulary.p_m_enumAttribute) private Severity enumAttribute; @@ -133,6 +136,14 @@ public void setDateAttribute(Date dateAttribute) { this.dateAttribute = dateAttribute; } + public Character getCharacterAttribute() { + return characterAttribute; + } + + public void setCharacterAttribute(Character characterAttribute) { + this.characterAttribute = characterAttribute; + } + public Severity getEnumAttribute() { return enumAttribute; } @@ -191,12 +202,14 @@ public void setObjectOneOfEnumAttribute(OneOfEnum objectOneOfEnumAttribute) { @Override public String toString() { - return "OWLCLassM{" + + return "OWLClassM{" + "key='" + key + '\'' + ", booleanAttribute=" + booleanAttribute + ", intAttribute=" + intAttribute + ", longAttribute=" + longAttribute + ", doubleAttribute=" + doubleAttribute + + ", dateAttribute=" + dateAttribute + + ", characterAttribute=" + characterAttribute + ", enumAttribute=" + enumAttribute + ", ordinalEnumAttribute=" + ordinalEnumAttribute + ", integerSet=" + integerSet + @@ -217,6 +230,7 @@ public void initializeTestValues(boolean includingKey) { this.longAttribute = 365L; this.doubleAttribute = 3.14D; this.dateAttribute = new Date(); + this.characterAttribute = 'j'; this.enumAttribute = Severity.MEDIUM; this.ordinalEnumAttribute = enumAttribute; this.integerSet = IntStream.generate(Generators::randomInt).limit(10).boxed().collect(Collectors.toSet()); @@ -263,6 +277,14 @@ public static PropertyInfo getDateAttributeFieldPropertyInfo() throws Exception return PropertyInfo.from(OWLClassM.getDateAttributeField()); } + public static Field getCharacterAttributeField() throws Exception { + return OWLClassM.class.getDeclaredField("characterAttribute"); + } + + public static PropertyInfo getCharacterAttributeFieldPropertyInfo() throws Exception { + return PropertyInfo.from(OWLClassM.getCharacterAttributeField()); + } + public static Field getEnumAttributeField() throws Exception { return OWLClassM.class.getDeclaredField("enumAttribute"); } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/Vocabulary.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/Vocabulary.java index c124963d7..84c01497f 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/Vocabulary.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/Vocabulary.java @@ -61,6 +61,7 @@ public class Vocabulary { public static final String p_m_longAttribute = ATTRIBUTE_BASE + "m-longAttribute"; public static final String p_m_doubleAttribute = ATTRIBUTE_BASE + "m-doubleAttribute"; public static final String p_m_dateAttribute = ATTRIBUTE_BASE + "m-dateAttribute"; + public static final String p_m_characterAttribute = ATTRIBUTE_BASE + "m-characterAttribute"; public static final String p_m_enumAttribute = ATTRIBUTE_BASE + "m-enumAttribute"; public static final String p_m_ordinalEnumAttribute = ATTRIBUTE_BASE + "m-ordinalEnumAttribute"; public static final String p_m_IntegerSet = ATTRIBUTE_BASE + "m-pluralIntAttribute"; diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index fbc4e1583..9868a39f9 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -95,6 +95,7 @@ import cz.cvut.kbss.jopa.oom.converter.ToIntegerConverter; import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter; import cz.cvut.kbss.jopa.oom.converter.ToLongConverter; +import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.LocalDateTimeConverter; import cz.cvut.kbss.jopa.utils.Configuration; import cz.cvut.kbss.jopa.vocabulary.DC; @@ -735,7 +736,7 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, public static void initOWLClassMMock(IdentifiableEntityType etMock, AbstractAttribute booleanAtt, AbstractAttribute intAtt, SingularAttributeImpl longAtt, - AbstractAttribute doubleAtt, AbstractAttribute dateAtt, + AbstractAttribute doubleAtt, AbstractAttribute dateAtt, AbstractAttribute characterAtt, AbstractAttribute enumAtt, AbstractAttribute ordinalEnumAtt, AbstractPluralAttribute intSetAtt, SingularAttributeImpl lexicalFormAtt, SingularAttributeImpl simpleLiteralAtt, @@ -755,13 +756,13 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, A when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock); when(etMock.getAttributes()).thenReturn( new HashSet<>(Arrays.>asList(booleanAtt, intAtt, longAtt, doubleAtt, - dateAtt, enumAtt, ordinalEnumAtt, + dateAtt, characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, simpleLiteralAtt, explicitDatatypeAtt, mObjectOneOfEnumAttribute))); when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>( Arrays.>asList(booleanAtt, intAtt, longAtt, doubleAtt, dateAtt, - enumAtt, ordinalEnumAtt, intSetAtt, + characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, simpleLiteralAtt, explicitDatatypeAtt, mObjectOneOfEnumAttribute, idMock))); @@ -836,6 +837,20 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, A when(etMock.getFieldSpecification(OWLClassM.getDateAttributeField().getName())).thenReturn(dateAtt); when(etMock.getAttribute(OWLClassM.getDateAttributeField().getName())).thenReturn(dateAtt); + when(characterAtt.getJavaField()).thenReturn(OWLClassM.getCharacterAttributeField()); + when(characterAtt.getName()).thenReturn(OWLClassM.getCharacterAttributeField().getName()); + when(characterAtt.getJavaType()).thenReturn(OWLClassM.getCharacterAttributeField().getType()); + when(characterAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_characterAttribute)); + when(characterAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); + when(characterAtt.isCollection()).thenReturn(false); + when(characterAtt.getDeclaringType()).thenReturn(etMock); + when(characterAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); + when(characterAtt.getConverter()).thenReturn(new CharacterConverter()); + when(characterAtt.hasLanguage()).thenReturn(true); + when(characterAtt.getLanguage()).thenReturn(Generators.LANG); + when(etMock.getFieldSpecification(OWLClassM.getCharacterAttributeField().getName())).thenReturn(characterAtt); + when(etMock.getAttribute(OWLClassM.getCharacterAttributeField().getName())).thenReturn(characterAtt); + when(enumAtt.getJavaField()).thenReturn(OWLClassM.getEnumAttributeField()); when(enumAtt.getName()).thenReturn(OWLClassM.getEnumAttributeField().getName()); when(enumAtt.getJavaType()).thenReturn(OWLClassM.getEnumAttributeField().getType()); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java index 8413678a2..b5a037f1b 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java @@ -206,6 +206,8 @@ public class MetamodelMocks { @Mock private SingularAttributeImpl mDateAtt; @Mock + private SingularAttributeImpl mCharacterAtt; + @Mock private SingularAttributeImpl mEnumAtt; @Mock private SingularAttributeImpl mOrdinalEnumAtt; @@ -388,7 +390,7 @@ public MetamodelMocks() throws Exception { MetamodelFactory.initOWLClassJMocks(etJ, jSetAtt, idJ); MetamodelFactory.initOWLClassKMocks(etK, kOwlClassEAtt, idK); MetamodelFactory.initOWLClassLMocks(etL, lReferencedList, lSimpleList, lSetAtt, lOwlClassAAtt, idL); - MetamodelFactory.initOWLClassMMock(etM, mBooleanAtt, mIntegerAtt, mLongAtt, mDoubleAtt, mDateAtt, mEnumAtt, + MetamodelFactory.initOWLClassMMock(etM, mBooleanAtt, mIntegerAtt, mLongAtt, mDoubleAtt, mDateAtt, mCharacterAtt, mEnumAtt, mOrdinalEnumAtt, mIntegerSetAtt, mLexicalFormAtt, mSimpleLiteralAtt, mExplicitDatatypeAtt, mWithConverterAtt, mObjectOneOfEnumAttribute, idM); MetamodelFactory.initOWLClassNMock(etN, nAnnotationAtt, nAnnotationUriAtt, nStringAtt, nPluralAnnotationAtt, @@ -786,6 +788,10 @@ public AbstractAttribute dateAttribute() { return MetamodelMocks.this.mDateAtt; } + public AbstractAttribute characterAttribute() { + return MetamodelMocks.this.mCharacterAtt; + } + public AbstractAttribute enumAttribute() { return MetamodelMocks.this.mEnumAtt; } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/MetamodelImplTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/MetamodelImplTest.java index 3e0d8c272..8eefb2114 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/MetamodelImplTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/MetamodelImplTest.java @@ -181,6 +181,12 @@ void buildsSingleEntityWithMultipleSingularDataProperties() throws Exception { FetchType.EAGER, false, dateField.getAnnotation(OWLDataProperty.class).iri(), dateField.getType(), new CascadeType[]{}); + final Field characterField = OWLClassM.getCharacterAttributeField(); + final FieldSpecification characterAtt = et.getFieldSpecification(characterField.getName()); + checkSingularAttribute(characterAtt, et, characterField.getName(), Attribute.PersistentAttributeType.DATA, characterField, + FetchType.EAGER, false, characterField.getAnnotation(OWLDataProperty.class).iri(), + characterField.getType(), + new CascadeType[]{}); final Field enumField = OWLClassM.getEnumAttributeField(); final FieldSpecification enumAtt = et.getFieldSpecification(enumField.getName()); checkSingularAttribute(enumAtt, et, enumField.getName(), Attribute.PersistentAttributeType.DATA, enumField, diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/metamodel/ConverterResolverTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/metamodel/ConverterResolverTest.java index d76b34d25..fb9114c2d 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/metamodel/ConverterResolverTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/model/metamodel/ConverterResolverTest.java @@ -45,10 +45,12 @@ import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter; import cz.cvut.kbss.jopa.oom.converter.ToMultilingualStringConverter; import cz.cvut.kbss.jopa.oom.converter.ToRdfLiteralConverter; +import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.DateConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.InstantConverter; import cz.cvut.kbss.jopa.utils.Configuration; import cz.cvut.kbss.jopa.vocabulary.XSD; +import cz.cvut.kbss.ontodriver.model.LangString; import cz.cvut.kbss.ontodriver.model.Literal; import cz.cvut.kbss.ontodriver.model.NamedResource; import org.junit.jupiter.api.Test; @@ -110,6 +112,18 @@ void resolveConverterReturnsBuiltInDateConverterForDataPropertyWithDateTarget() assertThat(result.get(), instanceOf(DateConverter.class)); } + @Test + void resolveConverterReturnsBuiltInCharacterConverterForDataPropertyWithCharacterTarget() throws Exception { + final PropertyInfo propertyInfo = OWLClassM.getCharacterAttributeFieldPropertyInfo(); + final PropertyAttributes pa = mock(PropertyAttributes.class); + when(pa.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); + doReturn(BasicTypeImpl.get(Character.class)).when(pa).getType(); + final Optional> result = sut.resolveConverter(propertyInfo, pa); + assertTrue(result.isPresent()); + assertTrue(result.get().supportsAxiomValueType(String.class)); + assertThat(result.get(), instanceOf(CharacterConverter.class)); + } + @Test void resolveConverterReturnsBuiltInInstantConverterForInstantDataPropertyField() throws Exception { final PropertyInfo propertyInfo = OWLClassM.getDateAttributeFieldPropertyInfo(); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java index 64b3dea2d..e905f537b 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java @@ -500,7 +500,8 @@ void reconstructsEntityWithDataPropertiesOfBasicTypesAndStringIdentifier() throw final Long lng = 365L; final Double d = 3.14; final Date date = new Date(); - axioms.addAll(createAxiomsForValues(true, i, lng, d, date, null)); + final Character character = 'j'; + axioms.addAll(createAxiomsForValues(true, i, lng, d, date, character, null)); final OWLClassM res = constructor.reconstructEntity(constructionConfig(ID, mocks.forOwlClassM() .entityType(), descriptor), axioms); @@ -509,9 +510,10 @@ void reconstructsEntityWithDataPropertiesOfBasicTypesAndStringIdentifier() throw assertEquals(lng, res.getLongAttribute()); assertEquals(d, res.getDoubleAttribute()); assertEquals(date, res.getDateAttribute()); + assertEquals(character, res.getCharacterAttribute()); } - private Collection> createAxiomsForValues(Boolean b, Integer i, Long lng, Double d, Date date, + private Collection> createAxiomsForValues(Boolean b, Integer i, Long lng, Double d, Date date, Character character, OWLClassM.Severity severity) throws Exception { final Collection> axioms = new ArrayList<>(); @@ -543,6 +545,12 @@ private Collection> createAxiomsForValues(Boolean b, Integer i, Long ln new AxiomImpl<>(ID_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(dateAttIri), false), new Value<>(date))); } + if (character != null) { + final String characterAttIri = OWLClassM.getCharacterAttributeField().getAnnotation(OWLDataProperty.class).iri(); + axioms.add( + new AxiomImpl<>(ID_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(characterAttIri), false), + new Value<>(character.toString()))); + } if (severity != null) { final String enumAttIri = OWLClassM.getEnumAttributeField().getAnnotation(OWLDataProperty.class).iri(); axioms.add( @@ -557,7 +565,7 @@ void reconstructsEntityWithEnumDataProperty() throws Exception { final Set> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(ID, OWLClassM.getClassIri())); final OWLClassM.Severity enumValue = OWLClassM.Severity.HIGH; - axioms.addAll(createAxiomsForValues(null, null, null, null, null, enumValue)); + axioms.addAll(createAxiomsForValues(null, null, null, null, null, null, enumValue)); final OWLClassM result = constructor .reconstructEntity(constructionConfig(ID, mocks.forOwlClassM().entityType(), descriptor), axioms); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java index 83336b345..d24aeac0e 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java @@ -372,6 +372,7 @@ void mapsEntityWithStringKeyAndBasicDataAttributes() throws Exception { assertTrue(containsDPAssertion(res, OWLClassM.getDoubleAttributeField(), entityM.getDoubleAttribute())); assertTrue(containsDPAssertion(res, OWLClassM.getLongAttributeField(), entityM.getLongAttribute())); assertTrue(containsDPAssertion(res, OWLClassM.getDateAttributeField(), entityM.getDateAttribute())); + assertTrue(containsDPAssertion(res, OWLClassM.getCharacterAttributeField(), entityM.getCharacterAttribute().toString())); } private boolean containsInstanceClassAssertion(AxiomValueDescriptor descriptor, String classIri) { diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java new file mode 100644 index 000000000..201a40b83 --- /dev/null +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java @@ -0,0 +1,32 @@ +/* + * JOPA + * Copyright (C) 2024 Czech Technical University in Prague + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package cz.cvut.kbss.jopa.oom.converter; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CharacterConverterTest { + + private final CharacterConverter converter = new CharacterConverter(); + + @Test + public void toAttributeSupportsIdentityConversion() { + assertEquals(Character.valueOf('j'), converter.convertToAttribute("j")); + } +} diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/sessions/CloneBuilderTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/sessions/CloneBuilderTest.java index 1ba23140d..a6db116c1 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/sessions/CloneBuilderTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/sessions/CloneBuilderTest.java @@ -536,6 +536,9 @@ public void testMergeChangesWithMultipleWrapperTypesAndStringKey() { m.setDateAttribute(new Date(System.currentTimeMillis() + 10000L)); changeSet.addChangeRecord( new ChangeRecord(metamodelMocks.forOwlClassM().dateAttribute(), m.getDateAttribute())); + m.setCharacterAttribute('w'); + changeSet.addChangeRecord( + new ChangeRecord(metamodelMocks.forOwlClassM().characterAttribute(), m.getCharacterAttribute())); builder.mergeChanges(changeSet); assertEquals(m.getBooleanAttribute(), entityM.getBooleanAttribute()); @@ -543,6 +546,7 @@ public void testMergeChangesWithMultipleWrapperTypesAndStringKey() { assertEquals(m.getLongAttribute(), entityM.getLongAttribute()); assertEquals(m.getDoubleAttribute(), entityM.getDoubleAttribute()); assertEquals(m.getDateAttribute(), entityM.getDateAttribute()); + assertEquals(m.getCharacterAttribute(), entityM.getCharacterAttribute()); } @Test diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassM.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassM.java index 4aa8a7d48..9e5bad11c 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassM.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassM.java @@ -58,6 +58,9 @@ public class OWLClassM { @OWLDataProperty(iri = Vocabulary.p_m_dateAttribute) private Date dateAttribute; + @OWLDataProperty(iri = Vocabulary.p_m_characterAttribute) + private Character characterAttribute; + @OWLDataProperty(iri = Vocabulary.p_m_enumAttribute) private Severity enumAttribute; @@ -164,6 +167,14 @@ public void setDateAttribute(Date dateAttribute) { this.dateAttribute = dateAttribute; } + public Character getCharacterAttribute() { + return characterAttribute; + } + + public void setCharacterAttribute(Character characterAttribute) { + this.characterAttribute = characterAttribute; + } + public Severity getEnumAttribute() { return enumAttribute; } @@ -286,6 +297,8 @@ public String toString() { ", longAttribute=" + longAttribute + ", floatAttribute=" + floatAttribute + ", doubleAttribute=" + doubleAttribute + + ", dateAttribute=" + dateAttribute + + ", characterAttribute" + characterAttribute + ", enumAttribute=" + enumAttribute + ", ordinalEnumAttribute=" + ordinalEnumAttribute + ", integerSet=" + integerSet + @@ -313,6 +326,7 @@ public void initializeTestValues(boolean includingKey) { this.floatAttribute = 3.14F; this.doubleAttribute = 3.14D; this.dateAttribute = new Date(); + this.characterAttribute = 'j'; this.enumAttribute = Severity.MEDIUM; this.ordinalEnumAttribute = enumAttribute; this.integerSet = IntStream.generate(Generators::randomInt).limit(10).boxed().collect(Collectors.toSet()); diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java index 8cc5195c1..e3554051f 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/Vocabulary.java @@ -88,6 +88,7 @@ public class Vocabulary { public static final String p_m_doubleAttribute = ATTRIBUTE_IRI_BASE + "m-doubleAttribute"; public static final String p_m_floatAttribute = ATTRIBUTE_IRI_BASE + "m-floatAttribute"; public static final String p_m_dateAttribute = ATTRIBUTE_IRI_BASE + "m-dateAttribute"; + public static final String p_m_characterAttribute = ATTRIBUTE_IRI_BASE + "m-characterAttribute"; public static final String p_m_enumAttribute = ATTRIBUTE_IRI_BASE + "m-enumAttribute"; public static final String p_m_ordinalEnumAttribute = ATTRIBUTE_IRI_BASE + "m-ordinalEnumAttribute"; public static final String p_m_enumSimpleLiteralAttribute = ATTRIBUTE_IRI_BASE + "m-enumSimpleLiteralAttribute"; diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java index 3000ba832..c4d914938 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java @@ -243,6 +243,7 @@ void testPersistEntityWithBasicTypeAttributes() { assertEquals(entityM.getFloatAttribute(), res.getFloatAttribute()); assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute()); assertEquals(entityM.getDateAttribute(), res.getDateAttribute()); + assertEquals(entityM.getCharacterAttribute(), res.getCharacterAttribute()); } @Test diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java index fb1ea0f23..c09bb0814 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java @@ -446,6 +446,7 @@ void getReferenceRetrievesReferenceToInstanceWithDataPropertiesWhoseAttributesAr assertEquals(entityM.getIntAttribute(), result.getIntAttribute()); assertEquals(entityM.getIntegerSet(), result.getIntegerSet()); assertEquals(entityM.getDateAttribute(), result.getDateAttribute()); + assertEquals(entityM.getCharacterAttribute(), result.getCharacterAttribute()); assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute()); } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsOnGetReferenceRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsOnGetReferenceRunner.java index 88ffbdbc2..fc1881cd1 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsOnGetReferenceRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsOnGetReferenceRunner.java @@ -79,6 +79,7 @@ void getReferenceResultDataAttributesCanBeAssignedNewValuesInUpdate() { assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute()); assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute()); assertEquals(entityM.getDateAttribute(), result.getDateAttribute()); + assertEquals(entityM.getCharacterAttribute(), result.getCharacterAttribute()); assertEquals(entityM.getDoubleAttribute(), result.getDoubleAttribute()); } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java index 3e7a7fc6b..1fc2635b7 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java @@ -407,6 +407,7 @@ void testModifyAttributesOfBasicTypes() { m.setDoubleAttribute(m.getDoubleAttribute() - 100.0); m.setLongAttribute(m.getLongAttribute() + 100L); m.setDateAttribute(new Date(System.currentTimeMillis() + 10000)); + m.setCharacterAttribute('o'); em.getTransaction().commit(); final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey()); @@ -415,6 +416,7 @@ void testModifyAttributesOfBasicTypes() { assertEquals(m.getFloatAttribute(), res.getFloatAttribute()); assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute()); assertEquals(m.getDateAttribute(), res.getDateAttribute()); + assertEquals(m.getCharacterAttribute(), res.getCharacterAttribute()); } @Test From 60937911165e6826e22d077d731af42e67a8d4b1 Mon Sep 17 00:00:00 2001 From: luxbe Date: Tue, 17 Dec 2024 14:17:35 +0100 Subject: [PATCH 05/17] [Enhancement #294] Add support for primitive char Java type --- .../cvut/kbss/jopa/model/metamodel/Converters.java | 3 ++- .../jopa/model/metamodel/PropertyAttributes.java | 5 ++++- .../main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java | 12 ++++++++++++ .../cz/cvut/kbss/jopa/test/runner/BaseRunner.java | 1 + .../jopa/test/runner/CreateOperationsRunner.java | 1 + .../jopa/test/runner/RetrieveOperationsRunner.java | 2 ++ .../jopa/test/runner/UpdateOperationsRunner.java | 6 ++++++ .../cvut/kbss/ontodriver/rdf4j/util/Rdf4jUtils.java | 2 ++ 8 files changed, 30 insertions(+), 2 deletions(-) diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java index e69a763da..894b0d40c 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/Converters.java @@ -93,7 +93,8 @@ void registerConverter(Class attributeType, ConverterWrapper converter) Map.entry(LangString.class, new ToLangStringConverter()), Map.entry(URI.class, new ToURIConverter()), Map.entry(URL.class, new ToURLConverter()), - Map.entry(Character.class, new CharacterConverter())); + Map.entry(Character.class, new CharacterConverter()), + Map.entry(char.class, new CharacterConverter())); } /** diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java index dfaf3da13..16c813a96 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/metamodel/PropertyAttributes.java @@ -17,6 +17,7 @@ */ package cz.cvut.kbss.jopa.model.metamodel; +import cz.cvut.kbss.jopa.datatype.DatatypeTransformer; import cz.cvut.kbss.jopa.model.IRI; import cz.cvut.kbss.jopa.model.MultilingualString; import cz.cvut.kbss.jopa.model.annotations.CascadeType; @@ -133,7 +134,9 @@ private void resolveEnumType(PropertyInfo propertyInfo, Class fieldValueCls) } String resolveLanguage(Class fieldValueCls) { - return MultilingualString.class.equals(fieldValueCls) || Character.class.equals(fieldValueCls) ? null : typeBuilderContext.getPuLanguage(); + return MultilingualString.class.equals(fieldValueCls) || Character.class.equals(fieldValueCls) || char.class.equals(fieldValueCls) + ? null + : typeBuilderContext.getPuLanguage(); } static PropertyAttributes create(PropertyInfo field, FieldMappingValidator validator, TypeBuilderContext context) { diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java index 3abce702d..2d83f1713 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/OWLClassBB.java @@ -49,6 +49,9 @@ public class OWLClassBB implements HasUri { @OWLDataProperty(iri = Vocabulary.P_BB_DOUBLE_ATTRIBUTE) private double doubleAttribute; + @OWLDataProperty(iri = Vocabulary.P_BB_CHAR_ATTRIBUTE) + private char charAttribute; + public OWLClassBB() { } @@ -121,6 +124,14 @@ public void setBooleanAttribute(boolean booleanAttribute) { this.booleanAttribute = booleanAttribute; } + public char getCharAttribute() { + return charAttribute; + } + + public void setCharAttribute(char charAttribute) { + this.charAttribute = charAttribute; + } + @Override public String toString() { return "OWLClassBB{" + @@ -132,6 +143,7 @@ public String toString() { ", longAttribute=" + longAttribute + ", floatAttribute=" + floatAttribute + ", doubleAttribute=" + doubleAttribute + + ", charAttribute=" + charAttribute + '}'; } } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java index 3b07fdbe8..a075d515d 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/BaseRunner.java @@ -191,6 +191,7 @@ private void init() { entityBB.setLongAttribute(20L); entityBB.setFloatAttribute(25.5f); entityBB.setDoubleAttribute(30.7d); + entityBB.setCharAttribute('j'); } @AfterEach diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java index c4d914938..3423c2574 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/CreateOperationsRunner.java @@ -261,6 +261,7 @@ void testPersistEntityWithBasicPrimitiveTypeAttributes() { assertEquals(entityBB.getLongAttribute(), res.getLongAttribute()); assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute()); assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute()); + assertEquals(entityBB.getCharAttribute(), res.getCharAttribute()); } @Test diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java index c09bb0814..ec18b0d0a 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java @@ -124,6 +124,7 @@ void testRetrievePrimitive() { assertEquals(entityBB.getLongAttribute(), res.getLongAttribute()); assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute()); assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute()); + assertEquals(entityBB.getCharAttribute(), res.getCharAttribute()); assertTrue(em.contains(res)); } @@ -147,6 +148,7 @@ void testRetrieveMissingPrimitive() throws Exception { assertEquals(0L, res.getLongAttribute()); assertEquals(0.0f, res.getFloatAttribute()); assertEquals(0.0d, res.getDoubleAttribute()); + assertEquals('\u0000', res.getCharAttribute()); assertTrue(em.contains(res)); } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java index 1fc2635b7..c205063f1 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/UpdateOperationsRunner.java @@ -49,6 +49,7 @@ import cz.cvut.kbss.jopa.test.environment.PersistenceFactory; import cz.cvut.kbss.jopa.test.environment.Quad; import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils; +import cz.cvut.kbss.jopa.vocabulary.RDF; import cz.cvut.kbss.jopa.vocabulary.XSD; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -1063,6 +1064,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() { entityBB.setLongAttribute(20L); entityBB.setFloatAttribute(25.5f); entityBB.setDoubleAttribute(30.7); + entityBB.setCharAttribute('c'); persist(entityBB); final int newIntValue = 20; @@ -1072,6 +1074,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() { final long newLongValue = 9L; final float newFloatValue = 3.2f; final double newDoubleValue = 8.9d; + final char newCharValue = 'o'; em.getTransaction().begin(); final OWLClassBB toUpdate = findRequired(OWLClassBB.class, entityBB.getUri()); @@ -1082,6 +1085,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() { toUpdate.setLongAttribute(newLongValue); toUpdate.setFloatAttribute(newFloatValue); toUpdate.setDoubleAttribute(newDoubleValue); + toUpdate.setCharAttribute(newCharValue); em.getTransaction().commit(); verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_INT_ATTRIBUTE, XSD.INT); @@ -1091,6 +1095,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() { verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_LONG_ATTRIBUTE, XSD.LONG); verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_FLOAT_ATTRIBUTE, XSD.FLOAT); verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_DOUBLE_ATTRIBUTE, XSD.DOUBLE); + verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_CHAR_ATTRIBUTE, XSD.STRING); final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); assertEquals(entityBB.getUri(), res.getUri()); assertEquals(newIntValue, res.getIntAttribute()); @@ -1100,6 +1105,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() { assertEquals(newLongValue, res.getLongAttribute()); assertEquals(newFloatValue, res.getFloatAttribute()); assertEquals(newDoubleValue, res.getDoubleAttribute()); + assertEquals(newCharValue, res.getCharAttribute()); } diff --git a/ontodriver-rdf4j/src/main/java/cz/cvut/kbss/ontodriver/rdf4j/util/Rdf4jUtils.java b/ontodriver-rdf4j/src/main/java/cz/cvut/kbss/ontodriver/rdf4j/util/Rdf4jUtils.java index a50518116..ce832b2ee 100644 --- a/ontodriver-rdf4j/src/main/java/cz/cvut/kbss/ontodriver/rdf4j/util/Rdf4jUtils.java +++ b/ontodriver-rdf4j/src/main/java/cz/cvut/kbss/ontodriver/rdf4j/util/Rdf4jUtils.java @@ -108,6 +108,8 @@ public static Literal createLiteral(Object value, String language, ValueFactory if (value instanceof Integer) { return vf.createLiteral((Integer) value); + } else if(value instanceof Character c) { + return vf.createLiteral(c.toString()); } else if (value instanceof String) { return language != null ? vf.createLiteral((String) value, language) : vf.createLiteral((String) value); } else if (value instanceof LangString ls) { From d5cf6fe9edd62db8724eb52bdd3968b7e31dc8c1 Mon Sep 17 00:00:00 2001 From: luxbe Date: Tue, 17 Dec 2024 15:27:45 +0100 Subject: [PATCH 06/17] [Enhancement #294] Implement PR suggestions --- .../jopa/datatype/DatatypeTransformer.java | 29 +++++++++---------- .../datatype/DatatypeTransformerTest.java | 13 +++++---- .../jopa/oom/DataPropertyFieldStrategy.java | 7 +++-- .../oom/converter/CharacterConverter.java | 9 +++--- .../PluralAnnotationPropertyStrategyTest.java | 1 + .../oom/converter/CharacterConverterTest.java | 9 ++++++ .../test/runner/RetrieveOperationsRunner.java | 25 ++++++++++++++-- 7 files changed, 63 insertions(+), 30 deletions(-) diff --git a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java index d476df538..d4edbeba4 100644 --- a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java +++ b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java @@ -41,24 +41,20 @@ public class DatatypeTransformer { private static final Map> TRANSFORMERS = initTransformers(); - private static final Map, Class> WRAPPER_TO_PRIMITIVES = initWrapperToPrimitives(); + private static final Map, Class> WRAPPER_TO_PRIMITIVES = Map.of( + Integer.class, int.class, + Boolean.class, boolean.class, + Byte.class, byte.class, + Short.class, short.class, + Long.class, long.class, + Float.class, float.class, + Double.class, double.class + ); private DatatypeTransformer() { throw new AssertionError(); } - private static Map, Class> initWrapperToPrimitives() { - final Map, Class> map = new HashMap<>(); - map.put(Integer.class, int.class); - map.put(Boolean.class, boolean.class); - map.put(Byte.class, byte.class); - map.put(Short.class, short.class); - map.put(Long.class, long.class); - map.put(Float.class, float.class); - map.put(Double.class, double.class); - return map; - } - private static Map> initTransformers() { final Map> map = new HashMap<>(); map.put(new Pair(Short.class, Integer.class), value -> ((Short) value).intValue()); @@ -102,8 +98,11 @@ private static Map, Class> initWrapperToPrimitives() { * @param cls - the class to convert * @return */ - public static Class wrapperToPrimitive( Class cls ) { - return WRAPPER_TO_PRIMITIVES.get(cls); + public static Optional> wrapperTypeToPrimitiveType(Class cls) { + if(cls != null && WRAPPER_TO_PRIMITIVES.containsKey(cls)) { + return Optional.of(WRAPPER_TO_PRIMITIVES.get(cls)); + } + return Optional.empty(); } /** diff --git a/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java b/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java index f31b8ad88..b2ce322dc 100644 --- a/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java +++ b/datatype/src/test/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformerTest.java @@ -26,6 +26,7 @@ import java.net.InetAddress; import java.net.URL; +import java.util.Optional; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -33,17 +34,17 @@ class DatatypeTransformerTest { @Test - void wrapperToPrimitiveReturnsNullForNullInput() { - assertNull(DatatypeTransformer.wrapperToPrimitive(null)); + void wrapperTypeToPrimitiveTypeReturnsNullForNullInput() { + assertEquals(Optional.empty(), DatatypeTransformer.wrapperTypeToPrimitiveType(null)); } @ParameterizedTest - @MethodSource("wrapperToPrimitiveTestValues") - void wrapperToPrimitive(Class wrapper, Class primitive) { - assertEquals(primitive, DatatypeTransformer.wrapperToPrimitive(wrapper)); + @MethodSource("wrapperTypeToPrimitiveTypeTestValues") + void wrapperTypeToPrimitiveType(Class wrapper, Class primitive) { + assertEquals(Optional.of(primitive), DatatypeTransformer.wrapperTypeToPrimitiveType(wrapper)); } - private static Stream wrapperToPrimitiveTestValues() { + private static Stream wrapperTypeToPrimitiveTypeTestValues() { return Stream.of( Arguments.arguments(Integer.class, int.class), Arguments.arguments(Boolean.class, boolean.class), diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java index 4a50047a6..b4969498f 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/DataPropertyFieldStrategy.java @@ -28,6 +28,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.Optional; abstract class DataPropertyFieldStrategy, X> extends FieldStrategy { @@ -44,10 +45,10 @@ boolean isValidRange(Object value) { } boolean canBeConverted(Object value) { - if(attribute.getJavaType() != null && attribute.getJavaType().isPrimitive()) { + if(attribute.getJavaType().isPrimitive()) { // if the value is a wrapper for the primitive attribute type, it can be converted automatically - Class primitiveClass = DatatypeTransformer.wrapperToPrimitive(value.getClass()); - if (primitiveClass != null && primitiveClass.equals(attribute.getJavaType())) { + Optional> primitiveClass = DatatypeTransformer.wrapperTypeToPrimitiveType(value.getClass()); + if (primitiveClass.isPresent() && primitiveClass.get().equals(attribute.getJavaType())) { return true; } } diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java index af5eca7a1..7a7d9631d 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverter.java @@ -17,6 +17,7 @@ */ package cz.cvut.kbss.jopa.oom.converter; +import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException; import cz.cvut.kbss.ontodriver.model.LangString; /** @@ -34,12 +35,12 @@ public Object convertToAxiomValue(Character value) { @Override public Character convertToAttribute(Object value) { - assert value != null; - if (value instanceof LangString ls) { - value = ls.getValue(); + assert value != null && value instanceof String; + + if(((String) value).length() > 1) { + throw new DatatypeMappingException("Unable to map literal " + value + " to " + Character.class.getCanonicalName() + ", because its length is greater than 1"); } - assert value instanceof String && ((String) value).length() == 1; return ((String) value).charAt(0); } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java index 54ff51ba8..fa593d4eb 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java @@ -146,6 +146,7 @@ private PluralAnnotationPropertyStrategy createStrategyWithPluralAnnot when(att.getCollectionType()).thenReturn(CollectionType.SET); when(att.getBindableJavaType()).thenReturn(elementType); when(att.getJavaField()).thenReturn(entity.getDeclaredField("sources")); + when(att.getJavaType()).thenReturn(Set.class); when(att.getIRI()).thenReturn(IRI.create(DC.Terms.SOURCE)); when(att.getConverter()).thenReturn(converter); when(att.hasLanguage()).thenReturn(true); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java index 201a40b83..e40ad6d6c 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/converter/CharacterConverterTest.java @@ -17,9 +17,11 @@ */ package cz.cvut.kbss.jopa.oom.converter; +import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class CharacterConverterTest { @@ -29,4 +31,11 @@ class CharacterConverterTest { public void toAttributeSupportsIdentityConversion() { assertEquals(Character.valueOf('j'), converter.convertToAttribute("j")); } + + @Test + public void toAttributeThrowsWhenValueIsTooLong() { + DatatypeMappingException thrown = assertThrows(DatatypeMappingException.class, () -> converter.convertToAttribute("abc")); + + assertEquals(thrown.getMessage(), "Unable to map literal abc to java.lang.Character, because its length is greater than 1"); + } } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java index ec18b0d0a..6da0373ae 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java @@ -47,9 +47,12 @@ import cz.cvut.kbss.jopa.test.environment.Generators; import cz.cvut.kbss.jopa.test.environment.PersistenceFactory; import cz.cvut.kbss.jopa.test.environment.Quad; +import cz.cvut.kbss.jopa.test.environment.TestEnvironment; import cz.cvut.kbss.jopa.vocabulary.RDF; +import cz.cvut.kbss.jopa.vocabulary.XSD; import cz.cvut.kbss.ontodriver.ReloadableDataSource; import cz.cvut.kbss.ontodriver.config.OntoDriverProperties; +import cz.cvut.kbss.ontodriver.model.Literal; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -110,9 +113,27 @@ void testRetrieveSimple() { } @Test - void testRetrievePrimitive() { + void testRetrievePrimitive() throws Exception { this.em = getEntityManager("RetrieveSimplePrimitive", false); - persist(entityBB); + persistTestData(Arrays.asList( + new Quad(entityBB.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_BB)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_INT_ATTRIBUTE), + new Literal("15", XSD.INT)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_BOOLEAN_ATTRIBUTE), + new Literal("true", XSD.BOOLEAN)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_BYTE_ATTRIBUTE), + new Literal("5", XSD.BYTE)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_SHORT_ATTRIBUTE), + new Literal("10", XSD.SHORT)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_LONG_ATTRIBUTE), + new Literal("20", XSD.LONG)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_FLOAT_ATTRIBUTE), + new Literal("25.5", XSD.FLOAT)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_DOUBLE_ATTRIBUTE), + new Literal("30.7", XSD.DOUBLE)), + new Quad(entityBB.getUri(), URI.create(Vocabulary.P_BB_CHAR_ATTRIBUTE), + new Literal("j", XSD.STRING)) + ), em); em.getEntityManagerFactory().getCache().evictAll(); final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri()); From 5775d413c4f6ce5bae2d80f18ee3549caa547ca3 Mon Sep 17 00:00:00 2001 From: luxbe Date: Thu, 19 Dec 2024 16:47:40 +0100 Subject: [PATCH 07/17] Simplify SOQL grammar (#300) * Simplify Soql grammar * Simplify Soql ORDER BY and GROUP BY clauses * Replace Soql param with simplePath * Extend Soql grammar * Replace Soql identificationVariable with IDENTIFICATION_VARIABLE * Align Soql comparisonOperator operation with reference * Extend Soql literal * Replace Soql whereClauseValue * Align Soql grammar to JPQL --- .../cz/cvut/kbss/jopa/query/soql/Soql.g4 | 131 +++---- .../jopa/query/soql/SoqlQueryListener.java | 341 +++++++++--------- 2 files changed, 236 insertions(+), 236 deletions(-) diff --git a/jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4 b/jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4 index 4d105890b..383a8887e 100644 --- a/jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4 +++ b/jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4 @@ -1,48 +1,30 @@ grammar Soql; +start: querySentence EOF ; -querySentence : selectStatement whereClauseWrapper? groupByClause? orderByClause? ; +querySentence: selectStatement ; -selectStatement: typeDef params FROM tables ; +selectStatement: selectClause fromClause whereClause? groupByClause? orderByClause? ; -typeDef: SELECT ; +singleValuedObjectPathExpression: simpleSubpath DOT singleValuedObjectField ; -params: paramComma* distinctParam ; +simpleSubpath: singleValuedObjectField (DOT simpleSubpath)* ; -paramComma: distinctParam ',' ; +singleValuedObjectField: IDENTIFICATION_VARIABLE ; -distinctParam: distinct? selectedParam ; +selectClause: SELECT (DISTINCT)? selectItem (',' selectItem)* ; -selectedParam: param | count; +selectItem: selectExpression; -count: COUNT '(' param ')' ; +selectExpression: simpleSubpath | aggregateExpression ; -param: objWithAttr | objWithOutAttr ; +aggregateExpression: COUNT '(' (DISTINCT)? simpleSubpath ')'; -objWithAttr: object DOT attribute; +fromClause: FROM entityName IDENTIFICATION_VARIABLE; -objWithOutAttr: object ; +entityName: IDENTIFICATION_VARIABLE ; -distinct: DISTINCT ; - -object: IDENTIFICATION_VARIABLE ; - -attribute: IDENTIFICATION_VARIABLE ; - -joinedParams: object DOT attribute (DOT attribute)+ ; - - - -tables: tableWithName ; - -table: IDENTIFICATION_VARIABLE ; - -tableName: IDENTIFICATION_VARIABLE ; - -tableWithName: table tableName ; - - -whereClauseWrapper +whereClause : WHERE conditionalExpression ; @@ -66,38 +48,44 @@ simpleConditionalExpression ; inExpression - : whereClauseParam (NOT)? IN '('? (inItem (',' inItem)*) ')'? + : simpleSubpath (NOT)? IN '('? (inItem (',' inItem)*) ')'? ; inItem : literal - | whereClauseValue + | inputParameter ; literal - : + : STRING_LITERAL + | INT_LITERAL + | FLOAT_LITERAL + | BOOLEAN_LITERAL + | IDENTIFICATION_VARIABLE ; likeExpression - : stringExpression (NOT)? LIKE whereClauseValue + : stringExpression (NOT)? LIKE stringExpression ; memberOfExpression - : inItem (NOT)? MEMBEROF whereClauseParam + : inItem (NOT)? MEMBER OF simpleSubpath + ; + +entityExpression + : IDENTIFICATION_VARIABLE + | inputParameter ; comparisonExpression - : stringExpression COMPARISON_OPERATOR stringExpression - | simpleArithmeticExpression COMPARISON_OPERATOR simpleArithmeticExpression - | whereClauseParam COMPARISON_OPERATOR ( whereClauseParam | whereClauseValue ) + : stringExpression comparisonOperator stringExpression + | simpleArithmeticExpression comparisonOperator simpleArithmeticExpression + | entityExpression op=(EQUAL | NOT_EQUAL) ( entityExpression ) ; -whereClauseValue: (QMARK TEXT QMARK) | inputParameter ; - -whereClauseParam: param | joinedParams ; - stringExpression - : whereClauseParam + : simpleSubpath + | STRING_LITERAL | inputParameter | functionsReturningStrings ; @@ -107,7 +95,7 @@ functionsReturningStrings | 'SUBSTRING' '(' stringExpression ',' simpleArithmeticExpression ',' simpleArithmeticExpression ')' | 'LOWER' '(' stringExpression ')' | 'UPPER' '(' stringExpression ')' - | 'LANG' '(' whereClauseParam ')' + | 'LANG' '(' simpleSubpath ')' ; simpleArithmeticExpression @@ -123,7 +111,7 @@ arithmeticFactor ; arithmeticPrimary - : param + : simpleSubpath | literal | '(' simpleArithmeticExpression ')' | inputParameter @@ -138,22 +126,24 @@ functionsReturningNumerics | 'FLOOR' '(' simpleArithmeticExpression ')' ; -orderByClause: ORDERBY orderByFullFormComma orderByFullFormComma* ; - -orderByFullFormComma: orderByFullForm ','? ; - -orderByFullForm: orderByParam ORDERING? ; - -orderByParam: object DOT attribute (DOT attribute)* ; +orderByClause: ORDER BY orderByItem (',' orderByItem)* ; -groupByClause: GROUPBY groupByParamComma groupByParamComma* ; +orderByItem: singleValuedObjectPathExpression (ASC | DESC) ; -groupByParamComma: groupByParam ','? ; +groupByClause: GROUP BY groupByItem (',' groupByItem)* ; -groupByParam: object DOT attribute (DOT attribute)* ; +groupByItem: singleValuedObjectPathExpression ; inputParameter: COLON IDENTIFICATION_VARIABLE ; +comparisonOperator + : op=EQUAL + | op='>' + | op='>=' + | op='<' + | op='<=' + | op=NOT_EQUAL + ; SELECT: 'SELECT' ; @@ -169,11 +159,13 @@ AND: 'AND' ; OR: 'OR' ; -ORDERBY: 'ORDER BY' ; +BY: 'BY' ; + +OF: 'OF' ; -ORDERING: ASC | DESC ; +ORDER: 'ORDER' ; -GROUPBY: 'GROUP BY' ; +GROUP: 'GROUP' ; ASC: 'ASC' ; @@ -187,9 +179,10 @@ LIKE: 'LIKE' ; IN: 'IN' ; -MEMBEROF: 'MEMBER OF' ; +MEMBER: 'MEMBER' ; -COMPARISON_OPERATOR: '>' | '<' | '>=' | '<=' | '=' | '<>' | '!=' ; +EQUAL: '=' ; +NOT_EQUAL: '<>' | '!=' ; DOT: '.' ; @@ -197,8 +190,20 @@ QMARK: '"' ; COLON: ':' ; +TRUE: 'TRUE'; + +FALSE: 'FALSE'; + IDENTIFICATION_VARIABLE: (LOWERCASE | UPPERCASE | '_') (LOWERCASE | UPPERCASE | DIGIT | '_')* ; +STRING_LITERAL: QMARK TEXT QMARK ; + +INT_LITERAL: DIGIT+; + +FLOAT_LITERAL: DIGIT* '.' DIGIT; + +BOOLEAN_LITERAL: TRUE | FALSE; + TEXT: (LOWERCASE | UPPERCASE | DIGIT)+ ; UPPERCASE: ('A'..'Z'); @@ -207,8 +212,4 @@ LOWERCASE: ('a'..'z'); DIGIT: ('0'..'9'); -NUMBER: DIGIT+ ; - -VALUE: NUMBER ; - WHITESPACE: (' ')+ -> skip; diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/query/soql/SoqlQueryListener.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/query/soql/SoqlQueryListener.java index eeceee25e..2f3881a22 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/query/soql/SoqlQueryListener.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/query/soql/SoqlQueryListener.java @@ -90,117 +90,71 @@ public SoqlQueryListener(MetamodelImpl metamodel) { } @Override - public void enterQuerySentence(SoqlParser.QuerySentenceContext ctx) { - } - - @Override - public void exitQuerySentence(SoqlParser.QuerySentenceContext ctx) { - this.soql = ctx.getText(); - buildSparqlQueryString(); - } - - @Override - public void enterSelectStatement(SoqlParser.SelectStatementContext ctx) { - } + public void enterStart(SoqlParser.StartContext ctx) { - @Override - public void exitSelectStatement(SoqlParser.SelectStatementContext ctx) { } @Override - public void enterParams(SoqlParser.ParamsContext ctx) { - } + public void exitStart(SoqlParser.StartContext ctx) { - @Override - public void exitParams(SoqlParser.ParamsContext ctx) { } @Override - public void enterParam(SoqlParser.ParamContext ctx) { + public void enterQuerySentence(SoqlParser.QuerySentenceContext ctx) { } @Override - public void exitParam(SoqlParser.ParamContext ctx) { + public void exitQuerySentence(SoqlParser.QuerySentenceContext ctx) { + this.soql = ctx.getText(); + buildSparqlQueryString(); } @Override - public void enterJoinedParams(SoqlParser.JoinedParamsContext ctx) { - SoqlNode firstNode = linkContextNodes(ctx); - SoqlAttribute myAttr = new SoqlAttribute(firstNode); - pushNewAttribute(myAttr); - } - - private void pushNewAttribute(SoqlAttribute myAttr) { - attributes.add(myAttr); - this.attrPointer = myAttr; - } + public void enterSelectStatement(SoqlParser.SelectStatementContext ctx) { - private void popAttribute() { - this.attrPointer = attributes.remove(attributes.size() - 1); } @Override - public void exitJoinedParams(SoqlParser.JoinedParamsContext ctx) { - } + public void exitSelectStatement(SoqlParser.SelectStatementContext ctx) { - @Override - public void enterParamComma(SoqlParser.ParamCommaContext ctx) { } @Override - public void exitParamComma(SoqlParser.ParamCommaContext ctx) { - } + public void enterSingleValuedObjectPathExpression(SoqlParser.SingleValuedObjectPathExpressionContext ctx) { - @Override - public void enterDistinctParam(SoqlParser.DistinctParamContext ctx) { } @Override - public void exitDistinctParam(SoqlParser.DistinctParamContext ctx) { - } + public void exitSingleValuedObjectPathExpression(SoqlParser.SingleValuedObjectPathExpressionContext ctx) { - @Override - public void enterSelectedParam(SoqlParser.SelectedParamContext ctx) { } @Override - public void exitSelectedParam(SoqlParser.SelectedParamContext ctx) { - if (!isSelectedParamCount) { - this.projectedVariable = ctx.getText(); + public void enterSimpleSubpath(SoqlParser.SimpleSubpathContext ctx) { + if(ctx.simpleSubpath() == null) { + return; } - } - @Override - public void enterCount(SoqlParser.CountContext ctx) { - isSelectedParamCount = true; - } + if(ctx.getChildCount() == 1) { + return; + } - @Override - public void exitCount(SoqlParser.CountContext ctx) { - this.projectedVariable = ctx.getChild(2).getText(); - } + // node was already processed by parent + if(ctx.getParent() instanceof SoqlParser.SimpleSubpathContext) { + return; + } - @Override - public void enterObject(SoqlParser.ObjectContext ctx) { - } + SoqlNode owner = linkSimpleSubpath(ctx); - @Override - public void exitObject(SoqlParser.ObjectContext ctx) { - } + // don't add top level references multiple times + if(!owner.hasChild() && objectTypes.containsKey(owner.getValue())) { + return; + } - @Override - public void enterObjWithAttr(SoqlParser.ObjWithAttrContext ctx) { - String owner = getOwnerFromParam(ctx); - String attribute = getAttributeFromParam(ctx); - // objectNode.attributeNode - SoqlNode objectNode = new AttributeNode(owner); - SoqlNode attributeNode = new AttributeNode(objectNode, attribute); - objectNode.setChild(attributeNode); - setIris(objectNode); - SoqlAttribute newAttr = new SoqlAttribute(objectNode); - if (isIdentifier(objectNode, attributeNode)) { + SoqlAttribute newAttr = new SoqlAttribute(owner); + if (owner.hasChild() && isIdentifier(owner, owner.getChild())) { this.isInObjectIdentifierExpression = true; - if (projectedVariable.equals(objectNode.getValue()) && currentPointerIsNotAttributeReference()) { + if (projectedVariable.equals(owner.getValue()) && currentPointerIsNotAttributeReference()) { attrPointer.setProjected(true); } else { newAttr.setProjected(true); @@ -211,68 +165,119 @@ public void enterObjWithAttr(SoqlParser.ObjWithAttrContext ctx) { } } - private boolean isIdentifier(SoqlNode objectNode, SoqlNode attributeNode) { - if (!objectTypes.containsKey(objectNode.getValue())) { - return false; + private SoqlNode linkSimpleSubpath(ParserRuleContext ctx) { + AttributeNode firstNode = new AttributeNode(getOwnerFromParam(ctx)); + AttributeNode currentNode = firstNode; + + while (ctx.getChildCount() == 3) { + ctx = (ParserRuleContext) ctx.getChild(2); + SoqlNode prevNode = currentNode; + currentNode = new AttributeNode(prevNode, ctx.getChild(0).getText()); + prevNode.setChild(currentNode); } - final String objectName = objectTypes.get(objectNode.getValue()); - IdentifiableEntityType entityType = getEntityType(objectName); - if (entityType == null) { - return false; + setIris(firstNode); + if (currentNode.getIri().isEmpty()) { + this.isInObjectIdentifierExpression = true; + if (projectedVariable != null && projectedVariable.equals(firstNode.getValue()) && currentPointerIsNotAttributeReference()) { + attrPointer.setProjected(true); + } } - return entityType.getIdentifier().getName().equals(attributeNode.getValue()); + return firstNode; } - private boolean currentPointerIsNotAttributeReference() { - return !attrPointer.getFirstNode().hasChild(); + @Override + public void exitSimpleSubpath(SoqlParser.SimpleSubpathContext ctx) { + } @Override - public void exitObjWithAttr(SoqlParser.ObjWithAttrContext ctx) { + public void enterSingleValuedObjectField(SoqlParser.SingleValuedObjectFieldContext ctx) { + } @Override - public void enterObjWithOutAttr(SoqlParser.ObjWithOutAttrContext ctx) { + public void exitSingleValuedObjectField(SoqlParser.SingleValuedObjectFieldContext ctx) { + } @Override - public void exitObjWithOutAttr(SoqlParser.ObjWithOutAttrContext ctx) { + public void enterSelectClause(SoqlParser.SelectClauseContext ctx) { + this.typeDef = SparqlConstants.SELECT; + + if(ctx.DISTINCT() != null) { + this.isSelectedParamDistinct = true; + } } @Override - public void enterAttribute(SoqlParser.AttributeContext ctx) { + public void exitSelectClause(SoqlParser.SelectClauseContext ctx) { + } @Override - public void exitAttribute(SoqlParser.AttributeContext ctx) { + public void enterSelectItem(SoqlParser.SelectItemContext ctx) { + } @Override - public void enterTypeDef(SoqlParser.TypeDefContext ctx) { - typeDef = ctx.getChild(0).getText(); + public void exitSelectItem(SoqlParser.SelectItemContext ctx) { + } @Override - public void exitTypeDef(SoqlParser.TypeDefContext ctx) { + public void enterSelectExpression(SoqlParser.SelectExpressionContext ctx) { + + } + + private void pushNewAttribute(SoqlAttribute myAttr) { + attributes.add(myAttr); + this.attrPointer = myAttr; + } + + private void popAttribute() { + this.attrPointer = attributes.remove(attributes.size() - 1); } @Override - public void enterDistinct(SoqlParser.DistinctContext ctx) { - if (SoqlConstants.DISTINCT.equals(ctx.getChild(0).getText())) { - isSelectedParamDistinct = true; + public void exitSelectExpression(SoqlParser.SelectExpressionContext ctx) { + if (!isSelectedParamCount) { + this.projectedVariable = ctx.getText(); } } @Override - public void exitDistinct(SoqlParser.DistinctContext ctx) { + public void enterAggregateExpression(SoqlParser.AggregateExpressionContext ctx) { + if(ctx.COUNT() != null) { + isSelectedParamCount = true; + if(ctx.DISTINCT() != null) { + isSelectedParamDistinct = true; + } + + if(ctx.simpleSubpath() != null) { + this.projectedVariable = ctx.simpleSubpath().getText(); + } + } } @Override - public void enterWhereClauseWrapper(SoqlParser.WhereClauseWrapperContext ctx) { + public void exitAggregateExpression(SoqlParser.AggregateExpressionContext ctx) { + } - @Override - public void exitWhereClauseWrapper(SoqlParser.WhereClauseWrapperContext ctx) { + private boolean isIdentifier(SoqlNode objectNode, SoqlNode attributeNode) { + if (!objectTypes.containsKey(objectNode.getValue())) { + return false; + } + final String objectName = objectTypes.get(objectNode.getValue()); + IdentifiableEntityType entityType = getEntityType(objectName); + if (entityType == null) { + return false; + } + return entityType.getIdentifier().getName().equals(attributeNode.getValue()); + } + + private boolean currentPointerIsNotAttributeReference() { + return !attrPointer.getFirstNode().hasChild(); } @Override @@ -325,7 +330,7 @@ public void exitInExpression(SoqlParser.InExpressionContext ctx) { pushNewAttribute(createSyntheticAttributeForEntityId()); } final ParseTree value = resolveInExpressionValue(ctx); - if (ctx.getChild(1).getText().equals(SoqlConstants.NOT)) { + if (ctx.NOT() != null) { attrPointer.setOperator(InOperator.notIn()); } else { attrPointer.setOperator(InOperator.in()); @@ -335,17 +340,16 @@ public void exitInExpression(SoqlParser.InExpressionContext ctx) { } private SoqlAttribute createSyntheticAttributeForEntityId() { - return new SoqlAttribute( - attrPointer.getFirstNode().hasChild() ? attrPointer.getFirstNode().getChild() : - new AttributeNode(rootVariable.substring(1))); + if(attrPointer.getFirstNode().hasChild()) { + attrPointer.getFirstNode().getChild().setChild(null); + return new SoqlAttribute(attrPointer.getFirstNode().getChild()); + } + + return new SoqlAttribute(new AttributeNode(rootVariable.substring(1))); } private ParseTree resolveInExpressionValue(SoqlParser.InExpressionContext ctx) { - final ParseTree lastToken = ctx.getChild(ctx.getChildCount() - 1); - if (")".equals(lastToken.getText())) { - return ctx.getChild(ctx.getChildCount() - 2); - } - return lastToken; + return ctx.inItem().get(ctx.inItem().size() - 1); } @Override @@ -398,6 +402,16 @@ public void exitMemberOfExpression(SoqlParser.MemberOfExpressionContext ctx) { this.isInObjectIdentifierExpression = false; } + @Override + public void enterEntityExpression(SoqlParser.EntityExpressionContext ctx) { + + } + + @Override + public void exitEntityExpression(SoqlParser.EntityExpressionContext ctx) { + + } + @Override public void enterComparisonExpression(SoqlParser.ComparisonExpressionContext ctx) { } @@ -437,58 +451,39 @@ private boolean isRootIdentifier(SoqlAttribute attribute) { } @Override - public void enterTables(SoqlParser.TablesContext ctx) { - } - - @Override - public void exitTables(SoqlParser.TablesContext ctx) { + public void enterFromClause(SoqlParser.FromClauseContext ctx) { + String entityName = ctx.entityName().getText(); + String identificationVariable = ctx.IDENTIFICATION_VARIABLE().getText(); + objectTypes.put(identificationVariable, entityName); + SoqlNode node = new AttributeNode(entityName); + setObjectIri(node); + SoqlAttribute myAttr = new SoqlAttribute(node); + pushNewAttribute(myAttr); } @Override - public void enterTable(SoqlParser.TableContext ctx) { - } + public void exitFromClause(SoqlParser.FromClauseContext ctx) { - @Override - public void exitTable(SoqlParser.TableContext ctx) { } @Override - public void enterTableName(SoqlParser.TableNameContext ctx) { - } + public void enterEntityName(SoqlParser.EntityNameContext ctx) { - @Override - public void exitTableName(SoqlParser.TableNameContext ctx) { } @Override - public void enterTableWithName(SoqlParser.TableWithNameContext ctx) { - String table = ctx.getChild(0).getChild(0).getText(); - String objectName = ctx.getChild(1).getChild(0).getText(); - objectTypes.put(objectName, table); - SoqlNode node = new AttributeNode(table); - setObjectIri(node); - SoqlAttribute myAttr = new SoqlAttribute(node); - pushNewAttribute(myAttr); - } + public void exitEntityName(SoqlParser.EntityNameContext ctx) { - @Override - public void exitTableWithName(SoqlParser.TableWithNameContext ctx) { } @Override - public void enterWhereClauseValue(SoqlParser.WhereClauseValueContext ctx) { - } + public void enterWhereClause(SoqlParser.WhereClauseContext ctx) { - @Override - public void exitWhereClauseValue(SoqlParser.WhereClauseValueContext ctx) { } @Override - public void enterWhereClauseParam(SoqlParser.WhereClauseParamContext ctx) { - } + public void exitWhereClause(SoqlParser.WhereClauseContext ctx) { - @Override - public void exitWhereClauseParam(SoqlParser.WhereClauseParamContext ctx) { } @Override @@ -573,24 +568,8 @@ public void exitOrderByClause(SoqlParser.OrderByClauseContext ctx) { } @Override - public void enterOrderByFullFormComma(SoqlParser.OrderByFullFormCommaContext ctx) { - } - - @Override - public void exitOrderByFullFormComma(SoqlParser.OrderByFullFormCommaContext ctx) { - } - - @Override - public void enterOrderByFullForm(SoqlParser.OrderByFullFormContext ctx) { - } - - @Override - public void exitOrderByFullForm(SoqlParser.OrderByFullFormContext ctx) { - } - - @Override - public void enterOrderByParam(SoqlParser.OrderByParamContext ctx) { - SoqlNode firstNode = linkContextNodes(ctx); + public void enterOrderByItem(SoqlParser.OrderByItemContext ctx) { + SoqlNode firstNode = linkObjectPathExpression(ctx); String orderingBy = getOrderingBy(ctx.getParent()); SoqlOrderParameter orderParam = new SoqlOrderParameter(firstNode, orderingBy); boolean attrSet = false; @@ -611,28 +590,21 @@ public void enterOrderByParam(SoqlParser.OrderByParamContext ctx) { } @Override - public void exitOrderByParam(SoqlParser.OrderByParamContext ctx) { + public void exitOrderByItem(SoqlParser.OrderByItemContext ctx) { } @Override public void enterGroupByClause(SoqlParser.GroupByClauseContext ctx) { - } - - @Override - public void exitGroupByClause(SoqlParser.GroupByClauseContext ctx) { - } - @Override - public void enterGroupByParamComma(SoqlParser.GroupByParamCommaContext ctx) { } @Override - public void exitGroupByParamComma(SoqlParser.GroupByParamCommaContext ctx) { + public void exitGroupByClause(SoqlParser.GroupByClauseContext ctx) { } @Override - public void enterGroupByParam(SoqlParser.GroupByParamContext ctx) { - SoqlNode firstNode = linkContextNodes(ctx); + public void enterGroupByItem(SoqlParser.GroupByItemContext ctx) { + SoqlNode firstNode = linkObjectPathExpression(ctx); SoqlGroupParameter groupParam = new SoqlGroupParameter(firstNode); boolean attrSet = false; for (SoqlAttribute attr : attributes) { @@ -651,6 +623,11 @@ public void enterGroupByParam(SoqlParser.GroupByParamContext ctx) { groupAttributes.add(groupParam); } + @Override + public void exitGroupByItem(SoqlParser.GroupByItemContext ctx) { + + } + private SoqlNode linkContextNodes(ParserRuleContext ctx) { SoqlNode firstNode = new AttributeNode(getOwnerFromParam(ctx)); SoqlNode currentNode = firstNode; @@ -667,8 +644,20 @@ private SoqlNode linkContextNodes(ParserRuleContext ctx) { return firstNode; } - @Override - public void exitGroupByParam(SoqlParser.GroupByParamContext ctx) { + private SoqlNode linkObjectPathExpression(ParserRuleContext ctx) { + SoqlNode firstNode = new AttributeNode(getOwnerFromParam(ctx)); + SoqlNode currentNode = firstNode; + for (int i = 2; i < ctx.getChild(0).getChildCount(); i += 2) { + SoqlNode prevNode = currentNode; + currentNode = new AttributeNode(prevNode, ctx.getChild(0).getChild(i).getText()); + prevNode.setChild(currentNode); + } + setIris(firstNode); + if (currentNode.getIri().isEmpty()) { + currentNode.getParent().setChild(null); + this.isInObjectIdentifierExpression = true; + } + return firstNode; } @Override @@ -681,6 +670,16 @@ public void exitInputParameter(SoqlParser.InputParameterContext ctx) { } + @Override + public void enterComparisonOperator(SoqlParser.ComparisonOperatorContext ctx) { + + } + + @Override + public void exitComparisonOperator(SoqlParser.ComparisonOperatorContext ctx) { + + } + @Override public void visitTerminal(TerminalNode terminalNode) { } From bbde901cd2ade6808e209ed21799c365a06574a3 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Fri, 20 Dec 2024 10:05:55 +0100 Subject: [PATCH 08/17] [GH-301] Extract identifier metamodel setup into a reusable function. --- .../environment/utils/MetamodelFactory.java | 337 ++++++------------ 1 file changed, 106 insertions(+), 231 deletions(-) diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index 9868a39f9..3df809db9 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -78,7 +78,6 @@ import cz.cvut.kbss.jopa.model.metamodel.MappedSuperclassTypeImpl; import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute; import cz.cvut.kbss.jopa.model.metamodel.PropertiesSpecification; -import cz.cvut.kbss.jopa.model.metamodel.QueryAttribute; import cz.cvut.kbss.jopa.model.metamodel.RdfContainerAttributeImpl; import cz.cvut.kbss.jopa.model.metamodel.SingularAttribute; import cz.cvut.kbss.jopa.model.metamodel.SingularAttributeImpl; @@ -86,6 +85,7 @@ import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification; import cz.cvut.kbss.jopa.model.metamodel.gen.ManageableClassGenerator; import cz.cvut.kbss.jopa.model.metamodel.gen.PersistenceContextAwareClassGenerator; +import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; import cz.cvut.kbss.jopa.oom.converter.CustomConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.DefaultConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.ObjectOneOfEnumConverter; @@ -95,7 +95,6 @@ import cz.cvut.kbss.jopa.oom.converter.ToIntegerConverter; import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter; import cz.cvut.kbss.jopa.oom.converter.ToLongConverter; -import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.LocalDateTimeConverter; import cz.cvut.kbss.jopa.utils.Configuration; import cz.cvut.kbss.jopa.vocabulary.DC; @@ -106,7 +105,6 @@ import java.net.URL; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -160,10 +158,8 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassA.getStrAttField().getName())).thenReturn(strAttMock); when(etMock.getName()).thenReturn(OWLClassA.class.getSimpleName()); when(etMock.getTypes()).thenReturn(typesMock); - when(etMock.getAttributes()).thenReturn( - Collections.singleton(strAttMock)); - when(etMock.getFieldSpecifications()).thenReturn( - new HashSet<>(Arrays.>asList(strAttMock, typesMock, idMock))); + when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, typesMock, idMock)); when(strAttMock.getJavaField()).thenReturn(OWLClassA.getStrAttField()); when(strAttMock.getJavaType()).thenReturn(OWLClassA.getStrAttField().getType()); @@ -187,11 +183,7 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, when(typesMock.getFetchType()).thenReturn(FetchType.EAGER); when(etMock.getFieldSpecification(typesMock.getName())).thenReturn(typesMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassA.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(idMock.getName()).thenReturn(OWLClassA.class.getDeclaredField("uri").getName()); - when(etMock.getFieldSpecification("uri")).thenReturn(idMock); + initIdentifier(etMock, idMock, OWLClassA.class.getDeclaredField("uri"), false); final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); addLifecycleCallback(listenerManager, POST_LOAD, OWLClassA.getPostLoadCallback()); when(etMock.getLifecycleListenerManager()).thenReturn(listenerManager); @@ -204,6 +196,16 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, }); } + private static void initIdentifier(IdentifiableEntityType et, Identifier id, Field idField, + boolean generated) { + when(et.getIdentifier()).thenReturn(id); + when(id.getJavaField()).thenReturn(idField); + when(id.getDeclaringType()).thenReturn(et); + when(id.getName()).thenReturn(idField.getName()); + when(id.isGenerated()).thenReturn(generated); + when(et.getFieldSpecification(idField.getName())).thenReturn(id); + } + private static void addLifecycleCallback(EntityLifecycleListenerManager manager, LifecycleEvent evt, Method callback) throws Exception { final Method addCallback = EntityLifecycleListenerManager.class @@ -218,19 +220,16 @@ private static void addLifecycleCallback(EntityLifecycleListenerManager manager, * Initializes the specified mock objects to return reasonable values. */ public static void initOWLClassBMocks(IdentifiableEntityType etMock, AbstractAttribute strAttMock, - PropertiesSpecification propsMock, Identifier idMock) throws - NoSuchFieldException, - SecurityException { + PropertiesSpecification propsMock, Identifier idMock) + throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassB.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassB.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassB.getClassIri())); when(etMock.getName()).thenReturn(OWLClassB.class.getSimpleName()); when(etMock.getAttribute(OWLClassB.getStrAttField().getName())).thenReturn(strAttMock); when(etMock.getProperties()).thenReturn(propsMock); - when(etMock.getAttributes()).thenReturn( - Collections.singleton(strAttMock)); - when(etMock.getFieldSpecifications()).thenReturn( - new HashSet<>(Arrays.>asList(strAttMock, propsMock, idMock))); + when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, propsMock, idMock)); when(strAttMock.getJavaField()).thenReturn(OWLClassB.getStrAttField()); when(strAttMock.getJavaType()).thenReturn(OWLClassB.getStrAttField().getType()); @@ -251,11 +250,7 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, when(propsMock.getPropertyValueType()).thenReturn(String.class); when(etMock.getFieldSpecification(propsMock.getName())).thenReturn(propsMock); - when(idMock.getName()).thenReturn("uri"); - when(idMock.getJavaField()).thenReturn(OWLClassB.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock); + initIdentifier(etMock, idMock, OWLClassB.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(etMock.getDeclaredAttribute(anyString())).thenAnswer(args -> { final String name = args.getArgument(0); @@ -268,21 +263,17 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, public static void initOWLClassCMocks(IdentifiableEntityType etMock, ListAttributeImpl simpleListMock, ListAttributeImpl refListMock, - RdfContainerAttributeImpl rdfSeqMock, - Identifier idMock) + RdfContainerAttributeImpl rdfSeqMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassC.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassC.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassC.getClassIri())); when(etMock.getName()).thenReturn(OWLClassC.class.getSimpleName()); - when(etMock.getAttribute(OWLClassC.getSimpleListField().getName())).thenReturn( - simpleListMock); + when(etMock.getAttribute(OWLClassC.getSimpleListField().getName())).thenReturn(simpleListMock); when(etMock.getAttribute(OWLClassC.getRefListField().getName())).thenReturn(refListMock); when(etMock.getAttribute(OWLClassC.getRdfSeqField().getName())).thenReturn(rdfSeqMock); - when(etMock.getAttributes()) - .thenReturn(new HashSet<>(Arrays.>asList(simpleListMock, refListMock, rdfSeqMock))); - when(etMock.getFieldSpecifications()).thenReturn( - new HashSet<>(Arrays.>asList(simpleListMock, refListMock, rdfSeqMock, idMock))); + when(etMock.getAttributes()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock, idMock)); when(simpleListMock.getJavaField()).thenReturn(OWLClassC.getSimpleListField()); when(refListMock.getJavaField()).thenReturn(OWLClassC.getRefListField()); when(rdfSeqMock.getJavaField()).thenReturn(OWLClassC.getRdfSeqField()); @@ -290,13 +281,11 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, when(simpleListMock.getIRI()).thenReturn(IRI.create(attIri)); when(simpleListMock.getName()).thenReturn(OWLClassC.getSimpleListField().getName()); when(etMock.getFieldSpecification(simpleListMock.getName())).thenReturn(simpleListMock); - String hasListAttIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class) - .listClassIRI(); + String hasListAttIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class).listClassIRI(); when(simpleListMock.getSequenceType()).thenReturn(SequenceType.simple); when(simpleListMock.getCollectionType()).thenReturn(CollectionType.LIST); when(simpleListMock.getListClassIRI()).thenReturn(IRI.create(hasListAttIri)); - String hasNextIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class) - .hasNextPropertyIRI(); + String hasNextIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class).hasNextPropertyIRI(); when(simpleListMock.getHasNextPropertyIRI()).thenReturn(IRI.create(hasNextIri)); when(simpleListMock.getBindableJavaType()).thenReturn(OWLClassA.class); when(simpleListMock.getPersistentAttributeType()) @@ -317,11 +306,9 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, when(refListMock.getListClassIRI()).thenReturn(IRI.create(hasListAttIri)); when(refListMock.getName()).thenReturn(OWLClassC.getRefListField().getName()); when(etMock.getFieldSpecification(refListMock.getName())).thenReturn(refListMock); - hasNextIri = OWLClassC.getRefListField().getAnnotation(Sequence.class) - .hasNextPropertyIRI(); + hasNextIri = OWLClassC.getRefListField().getAnnotation(Sequence.class).hasNextPropertyIRI(); when(refListMock.getHasNextPropertyIRI()).thenReturn(IRI.create(hasNextIri)); - final String contentIri = OWLClassC.getRefListField().getAnnotation(Sequence.class) - .hasContentsPropertyIRI(); + final String contentIri = OWLClassC.getRefListField().getAnnotation(Sequence.class).hasContentsPropertyIRI(); when(refListMock.getHasContentsPropertyIRI()).thenReturn(IRI.create(contentIri)); attIri = OWLClassC.getRefListField().getAnnotation(OWLObjectProperty.class).iri(); when(refListMock.getIRI()).thenReturn(IRI.create(attIri)); @@ -353,10 +340,7 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, .thenReturn(OWLClassC.getRdfSeqField().getAnnotation(OWLObjectProperty.class).cascade()); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassC.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(idMock.getFetchType()).thenReturn(FetchType.EAGER); + initIdentifier(etMock, idMock, OWLClassC.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(etMock.getDeclaredAttribute(anyString())).thenAnswer(args -> { final String name = args.getArgument(0); @@ -382,10 +366,8 @@ public static void initOWLClassDMocks(IdentifiableEntityType etMock, when(etMock.getIRI()).thenReturn(IRI.create(OWLClassD.getClassIri())); when(etMock.getName()).thenReturn(OWLClassD.class.getSimpleName()); when(etMock.getAttribute(OWLClassD.getOwlClassAField().getName())).thenReturn(clsAMock); - when(etMock.getAttributes()).thenReturn( - Collections.singleton(clsAMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(clsAMock, idMock))); + when(etMock.getAttributes()).thenReturn(Collections.singleton(clsAMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, idMock)); when(clsAMock.getJavaField()).thenReturn(OWLClassD.getOwlClassAField()); when(clsAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); final String clsAIri = OWLClassD.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri(); @@ -399,11 +381,7 @@ public static void initOWLClassDMocks(IdentifiableEntityType etMock, when(clsAMock.getBindableJavaType()).thenReturn(OWLClassA.class); when(clsAMock.getType()).thenReturn(etA); when(etMock.getFieldSpecification(clsAMock.getName())).thenReturn(clsAMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassD.getUriField()); - when(idMock.getName()).thenReturn(OWLClassD.getUriField().getName()); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification(OWLClassD.getUriField().getName())).thenReturn(idMock); + initIdentifier(etMock, idMock, OWLClassD.getUriField(), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -415,20 +393,15 @@ public static void initOWLClassEMocks(IdentifiableEntityType etMock, when(etMock.getName()).thenReturn(OWLClassE.class.getSimpleName()); when(etMock.getAttribute(OWLClassE.getStrAttField().getName())).thenReturn(strAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(strAttMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, idMock)); when(strAttMock.getJavaField()).thenReturn(OWLClassE.getStrAttField()); - final String stringAttIri = OWLClassE.getStrAttField().getAnnotation(OWLDataProperty.class) - .iri(); + final String stringAttIri = OWLClassE.getStrAttField().getAnnotation(OWLDataProperty.class).iri(); when(strAttMock.getIRI()).thenReturn(IRI.create(stringAttIri)); when(strAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); when(strAttMock.getDeclaringType()).thenReturn(etMock); when(strAttMock.hasLanguage()).thenReturn(true); when(strAttMock.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassE.class.getDeclaredField("uri")); - when(idMock.isGenerated()).thenReturn(true); - when(idMock.getDeclaringType()).thenReturn(etMock); + initIdentifier(etMock, idMock, OWLClassE.class.getDeclaredField("uri"), true); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -440,11 +413,8 @@ public static void initOWLClassFMocks(IdentifiableEntityType etMock, when(etMock.getIRI()).thenReturn(IRI.create(OWLClassF.getClassIri())); when(etMock.getName()).thenReturn(OWLClassF.class.getSimpleName()); when(etMock.getAttribute(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); - when(etMock.getAttributes()) - .thenReturn(new HashSet<>(Arrays.>asList(setAMock, strAttMock))); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>( - Arrays.>asList(setAMock, strAttMock, idMock))); + when(etMock.getAttributes()).thenReturn(Set.of(setAMock, strAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, strAttMock, idMock)); when(setAMock.getJavaField()).thenReturn(OWLClassF.getSimpleSetField()); when(setAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); when(setAMock.getCascadeTypes()) @@ -478,11 +448,7 @@ public static void initOWLClassFMocks(IdentifiableEntityType etMock, when(strAttMock.getFetchType()).thenReturn(FetchType.EAGER); when(etMock.getFieldSpecification(OWLClassF.getStrAttField().getName())).thenReturn(strAttMock); - when(idMock.getJavaField()).thenReturn(OWLClassF.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(idMock.getName()).thenReturn("uri"); - when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock); - when(etMock.getIdentifier()).thenReturn(idMock); + initIdentifier(etMock, idMock, OWLClassF.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -496,8 +462,7 @@ public static void initOWLClassGMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassG.getOwlClassHField().getName())).thenReturn(clsHMock); when(etMock.getName()).thenReturn(OWLClassG.class.getSimpleName()); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsHMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(clsHMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsHMock, idMock)); when(clsHMock.getJavaField()).thenReturn(OWLClassG.getOwlClassHField()); when(clsHMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); when(clsHMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_g_hasH)); @@ -508,9 +473,7 @@ public static void initOWLClassGMocks(IdentifiableEntityType etMock, when(clsHMock.getDeclaringType()).thenReturn(etMock); when(clsHMock.getType()).thenReturn(etHMock); when(etMock.getFieldSpecification(clsHMock.getName())).thenReturn(clsHMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getName()).thenReturn(OWLClassG.class.getDeclaredField("uri").getName()); - when(idMock.getJavaField()).thenReturn(OWLClassG.class.getDeclaredField("uri")); + initIdentifier(etMock, idMock, OWLClassG.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -523,11 +486,8 @@ public static void initOWLClassHMocks(IdentifiableEntityType etMock, when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassH.getClassIri())); when(etMock.getName()).thenReturn(OWLClassH.class.getSimpleName()); - when(etMock.getAttributes()).thenReturn( - new HashSet<>(Arrays.>asList(clsAMock, clsGMock))); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>( - Arrays.>asList(clsAMock, clsGMock, idMock))); + when(etMock.getAttributes()).thenReturn(Set.of(clsAMock, clsGMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, clsGMock, idMock)); when(clsAMock.getJavaField()).thenReturn(OWLClassH.getOwlClassAField()); when(clsAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); when(clsAMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_h_hasA)); @@ -552,9 +512,7 @@ public static void initOWLClassHMocks(IdentifiableEntityType etMock, when(etMock.getFieldSpecification(clsGMock.getName())).thenReturn(clsGMock); when(etMock.getAttribute(clsAMock.getName())).thenReturn(clsAMock); when(etMock.getAttribute(clsGMock.getName())).thenReturn(clsGMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getName()).thenReturn(OWLClassH.class.getDeclaredField("uri").getName()); - when(idMock.getJavaField()).thenReturn(OWLClassH.class.getDeclaredField("uri")); + initIdentifier(etMock, idMock, OWLClassH.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -566,8 +524,7 @@ public static void initOWLClassIMocks(IdentifiableEntityType etMock, when(etMock.getName()).thenReturn(OWLClassI.class.getSimpleName()); when(etMock.getAttribute(OWLClassI.getOwlClassAField().getName())).thenReturn(aAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(aAttMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(aAttMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(aAttMock, idMock)); when(aAttMock.getName()).thenReturn(OWLClassI.getOwlClassAField().getName()); when(aAttMock.getJavaField()).thenReturn(OWLClassI.getOwlClassAField()); when(aAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); @@ -582,10 +539,7 @@ public static void initOWLClassIMocks(IdentifiableEntityType etMock, when(aAttMock.getDeclaringType()).thenReturn(etMock); when(aAttMock.getFetchType()).thenReturn(FetchType.LAZY); when(etMock.getFieldSpecification(aAttMock.getName())).thenReturn(aAttMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassI.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification("uri")).thenReturn(idMock); + initIdentifier(etMock, idMock, OWLClassI.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -597,14 +551,12 @@ public static void initOWLClassJMocks(IdentifiableEntityType etMock, when(etMock.getName()).thenReturn(OWLClassJ.class.getSimpleName()); when(etMock.getAttribute(OWLClassJ.getOwlClassAField().getName())).thenReturn(setAMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(setAMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(setAMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, idMock)); when(setAMock.getJavaField()).thenReturn(OWLClassJ.getOwlClassAField()); when(setAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); when(setAMock.getCascadeTypes()) .thenReturn(OWLClassJ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).cascade()); - final String clsAIri = OWLClassJ.getOwlClassAField().getAnnotation(OWLObjectProperty.class) - .iri(); + final String clsAIri = OWLClassJ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri(); when(setAMock.getIRI()).thenReturn(IRI.create(clsAIri)); when(setAMock.getJavaType()).thenReturn(Set.class); when(setAMock.isCollection()).thenReturn(Boolean.TRUE); @@ -613,9 +565,7 @@ public static void initOWLClassJMocks(IdentifiableEntityType etMock, when(setAMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); when(setAMock.getDeclaringType()).thenReturn(etMock); when(etMock.getFieldSpecification(OWLClassJ.getOwlClassAField().getName())).thenReturn(setAMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassJ.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); + initIdentifier(etMock, idMock, OWLClassJ.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -627,42 +577,31 @@ public static void initOWLClassKMocks(IdentifiableEntityType etMock, when(etMock.getName()).thenReturn(OWLClassK.class.getSimpleName()); when(etMock.getAttribute(OWLClassK.getOwlClassEField().getName())).thenReturn(clsEMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsEMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(clsEMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsEMock, idMock)); when(clsEMock.getJavaField()).thenReturn(OWLClassK.getOwlClassEField()); when(clsEMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - final String clsEIri = OWLClassK.getOwlClassEField().getAnnotation(OWLObjectProperty.class) - .iri(); + final String clsEIri = OWLClassK.getOwlClassEField().getAnnotation(OWLObjectProperty.class).iri(); when(clsEMock.getIRI()).thenReturn(IRI.create(clsEIri)); when(clsEMock.getJavaType()).thenReturn(OWLClassE.class); when(clsEMock.getDeclaringType()).thenReturn(etMock); when(clsEMock.getFetchType()).thenReturn(FetchType.LAZY); when(clsEMock.getName()).thenReturn(OWLClassK.getOwlClassEField().getName()); - when(etMock.getFieldSpecification(clsEMock.getName())).thenReturn(clsEMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassK.class.getDeclaredField("uri")); + initIdentifier(etMock, idMock, OWLClassK.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassLMocks(IdentifiableEntityType etMock, ListAttributeImpl refListMock, ListAttributeImpl simpleListMock, AbstractPluralAttribute setMock, AbstractAttribute singleAMock, - Identifier idMock) - throws NoSuchFieldException { + Identifier idMock) throws NoSuchFieldException { when(etMock.getJavaType()).thenReturn(OWLClassL.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassL.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassL.getClassIri())); when(etMock.getName()).thenReturn(OWLClassL.class.getSimpleName()); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassL.class.getDeclaredField("uri")); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getDeclaredAttributes()).thenReturn(new HashSet<>( - Arrays.>asList(refListMock, simpleListMock, setMock, singleAMock))); - when(etMock.getAttributes()).thenReturn(new HashSet<>( - Arrays.>asList(refListMock, simpleListMock, setMock, singleAMock))); - when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>( - Arrays.>asList(refListMock, simpleListMock, setMock, - simpleListMock, singleAMock, idMock))); + initIdentifier(etMock, idMock, OWLClassL.class.getDeclaredField("uri"), false); + when(etMock.getDeclaredAttributes()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock)); + when(etMock.getAttributes()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock, idMock)); when(refListMock.getJavaField()).thenReturn(OWLClassL.getReferencedListField()); when(refListMock.getName()).thenReturn(OWLClassL.getReferencedListField().getName()); @@ -736,7 +675,8 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, public static void initOWLClassMMock(IdentifiableEntityType etMock, AbstractAttribute booleanAtt, AbstractAttribute intAtt, SingularAttributeImpl longAtt, - AbstractAttribute doubleAtt, AbstractAttribute dateAtt, AbstractAttribute characterAtt, + AbstractAttribute doubleAtt, AbstractAttribute dateAtt, + AbstractAttribute characterAtt, AbstractAttribute enumAtt, AbstractAttribute ordinalEnumAtt, AbstractPluralAttribute intSetAtt, SingularAttributeImpl lexicalFormAtt, SingularAttributeImpl simpleLiteralAtt, @@ -749,23 +689,13 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, A when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassM.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassM.getClassIri())); when(etMock.getName()).thenReturn(OWLClassM.class.getSimpleName()); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(OWLClassM.getUriField()); - when(idMock.getName()).thenReturn(OWLClassM.getUriField().getName()); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock); - when(etMock.getAttributes()).thenReturn( - new HashSet<>(Arrays.>asList(booleanAtt, intAtt, longAtt, doubleAtt, - dateAtt, characterAtt, enumAtt, ordinalEnumAtt, - intSetAtt, lexicalFormAtt, - simpleLiteralAtt, explicitDatatypeAtt, - mObjectOneOfEnumAttribute))); - when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>( - Arrays.>asList(booleanAtt, intAtt, longAtt, doubleAtt, dateAtt, - characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, - lexicalFormAtt, simpleLiteralAtt, - explicitDatatypeAtt, mObjectOneOfEnumAttribute, - idMock))); + initIdentifier(etMock, idMock, OWLClassM.getUriField(), false); + when(etMock.getAttributes()).thenReturn(Set.of(booleanAtt, intAtt, longAtt, doubleAtt, + dateAtt, characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, + simpleLiteralAtt, explicitDatatypeAtt, mObjectOneOfEnumAttribute)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(booleanAtt, intAtt, longAtt, doubleAtt, dateAtt, + characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, simpleLiteralAtt, + explicitDatatypeAtt, mObjectOneOfEnumAttribute, idMock)); when(booleanAtt.getJavaField()).thenReturn(OWLClassM.getBooleanAttributeField()); when(booleanAtt.getName()).thenReturn(OWLClassM.getBooleanAttributeField().getName()); @@ -979,14 +909,9 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassN.class)); when(et.getName()).thenReturn(OWLClassN.class.getSimpleName()); when(et.getIRI()).thenReturn(IRI.create(OWLClassN.getClassIri())); - when(et.getIdentifier()).thenReturn(idN); - when(idN.getJavaField()).thenReturn(OWLClassN.getUriField()); - when(et.getFieldSpecifications()).thenReturn(new HashSet<>( - Arrays.>asList(annotationAtt, annotationUriAtt, stringAtt, - pluralAnnotationAtt, props, idN))); - when(et.getAttributes()).thenReturn(new HashSet<>( - Arrays.>asList(annotationAtt, annotationUriAtt, stringAtt, - pluralAnnotationAtt))); + initIdentifier(et, idN, OWLClassN.getUriField(), false); + when(et.getFieldSpecifications()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt, props, idN)); + when(et.getAttributes()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt)); when(annotationAtt.getJavaField()).thenReturn(OWLClassN.getAnnotationPropertyField()); when(annotationAtt.getJavaType()).thenReturn(OWLClassN.getAnnotationPropertyField().getType()); @@ -1059,11 +984,9 @@ public static void initOWLClassOMock(IdentifiableEntityType et, Singu when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassO.class)); when(et.getIdentifier()).thenReturn(idO); when(et.getName()).thenReturn(OWLClassO.class.getSimpleName()); - when(et.getIRI()).thenReturn(IRI.create(OWLClassO.getClassIri())); - when(idO.getJavaField()).thenReturn(OWLClassO.getUriField()); + initIdentifier(et, idO, OWLClassO.getUriField(), false); when(et.getAttributes()).thenReturn(Collections.singleton(stringAtt)); - when(et.getFieldSpecifications()) - .thenReturn(new HashSet<>(Arrays.>asList(stringAtt, idO))); + when(et.getFieldSpecifications()).thenReturn(Set.of(stringAtt, idO)); when(et.getFieldSpecification(stringAtt.getName())).thenReturn(stringAtt); when(stringAtt.getJavaField()).thenReturn(OWLClassO.getStringAttributeField()); when(stringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); @@ -1085,19 +1008,11 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types Exception { when(et.getJavaType()).thenReturn(OWLClassP.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassP.class)); - when(et.getIdentifier()).thenReturn(idP); when(et.getName()).thenReturn(OWLClassP.class.getSimpleName()); when(et.getIRI()).thenReturn(IRI.create(OWLClassP.getClassIri())); - when(idP.getJavaField()).thenReturn(OWLClassP.getUriField()); - when(et.getFieldSpecifications()) - .thenReturn( - new HashSet<>( - Arrays.>asList(uriAtt, urlsAtt, simpleListAtt, - refListAtt, props, types, - idP))); - when(et.getAttributes()) - .thenReturn(new HashSet<>( - Arrays.>asList(uriAtt, urlsAtt, simpleListAtt, refListAtt))); + initIdentifier(et, idP, OWLClassP.getUriField(), false); + when(et.getFieldSpecifications()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt, props, types, idP)); + when(et.getAttributes()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt)); when(et.getFieldSpecification(props.getName())).thenReturn(props); when(et.getProperties()).thenReturn(props); when(props.getJavaField()).thenReturn(OWLClassP.getPropertiesField()); @@ -1177,27 +1092,16 @@ public static void initOwlClassQMock(IdentifiableEntityType et, SingularAttributeImpl qLabelAtt, SingularAttributeImpl qOwlClassAAtt, Identifier idQ) throws Exception { - when(et.getIdentifier()).thenReturn(idQ); when(et.getJavaType()).thenReturn(OWLClassQ.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassQ.class)); when(et.getName()).thenReturn(OWLClassQ.class.getSimpleName()); - when(idQ.getJavaField()).thenReturn(OWLClassQ.getUriField()); - when(idQ.isGenerated()).thenReturn(true); - when(idQ.getDeclaringType()).thenReturn(superclassType); + initIdentifier(et, idQ, OWLClassQ.getUriField(), false); when(et.getIRI()).thenReturn(IRI.create(OWLClassQ.getClassIri())); when(et.getSupertypes()).thenReturn(Collections.singleton(superclassType)); when(superclassType.getSubtypes()).thenReturn(Collections.singleton(et)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(et.getFieldSpecifications()) - .thenReturn( - new HashSet<>( - Arrays.>asList(qStringAtt, qParentStringAtt, - qLabelAtt, qOwlClassAAtt, - idQ))); - when(et.getAttributes()) - .thenReturn(new HashSet<>( - Arrays.>asList(qStringAtt, qParentStringAtt, qLabelAtt, - qOwlClassAAtt))); + when(et.getFieldSpecifications()).thenReturn(Set.of(qStringAtt, qParentStringAtt, qLabelAtt, qOwlClassAAtt, idQ)); + when(et.getAttributes()).thenReturn(Set.of(qStringAtt, qParentStringAtt, qLabelAtt, qOwlClassAAtt)); when(qStringAtt.getJavaField()).thenReturn(OWLClassQ.getStringAttributeField()); when(qStringAtt.getJavaType()).thenReturn(OWLClassQ.getStringAttributeField().getType()); @@ -1274,21 +1178,17 @@ private static void initQMappedSuperclassMock(MappedSuperclassTypeImpl(Arrays.asList(qLabelAtt, qParentStringAtt, qOwlClassAAtt))).when(superclassType) - .getDeclaredAttributes(); + doReturn(Set.of(qLabelAtt, qParentStringAtt, qOwlClassAAtt)).when(superclassType).getDeclaredAttributes(); } public static void initOwlClassSMock(IdentifiableEntityType et, SingularAttributeImpl sNameAtt, TypesSpecification sTypes, Identifier idS) throws Exception { when(et.getName()).thenReturn(OWLClassS.class.getSimpleName()); - when(et.getIdentifier()).thenReturn(idS); - when(idS.isGenerated()).thenReturn(true); + initIdentifier(et, idS, OWLClassS.getUriField(), true); when(et.getJavaType()).thenReturn(OWLClassS.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassS.class)); - when(idS.getJavaField()).thenReturn(OWLClassS.getUriField()); - when(idS.getDeclaringType()).thenReturn(et); when(et.getIRI()).thenReturn(IRI.create(OWLClassS.getClassIri())); - when(et.getFieldSpecifications()).thenReturn(new HashSet(Arrays.asList(sNameAtt, sTypes, idS))); + when(et.getFieldSpecifications()).thenReturn(Set.of(sNameAtt, sTypes, idS)); when(et.getAttributes()).thenReturn(Collections.singleton(sNameAtt)); when(et.getTypes()).thenReturn(sTypes); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); @@ -1437,16 +1337,13 @@ static void initOwlClassRListeners(IdentifiableEntityType etR, Identi static void initOwlClassTMock(IdentifiableEntityType et, SingularAttributeImpl localDateAtt, SingularAttributeImpl localDateTimeAtt, SingularAttributeImpl owlClassSAtt, Identifier id) throws Exception { - when(et.getIdentifier()).thenReturn(id); - when(id.isGenerated()).thenReturn(true); when(et.getJavaType()).thenReturn(OWLClassT.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassT.class)); - when(id.getJavaField()).thenReturn(OWLClassT.getUriField()); - when(id.getDeclaringType()).thenReturn(et); + initIdentifier(et, id, OWLClassT.getUriField(), true); when(et.getIRI()).thenReturn(IRI.create(OWLClassT.getClassIri())); when(et.getName()).thenReturn(OWLClassT.class.getSimpleName()); - when(et.getFieldSpecifications()).thenReturn(new HashSet(Arrays.asList(localDateAtt, localDateTimeAtt, id))); - when(et.getAttributes()).thenReturn(new HashSet(Arrays.asList(localDateAtt, localDateTimeAtt))); + when(et.getFieldSpecifications()).thenReturn(Set.of(localDateAtt, localDateTimeAtt, id)); + when(et.getAttributes()).thenReturn(Set.of(localDateAtt, localDateTimeAtt)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(localDateAtt.getJavaField()).thenReturn(OWLClassT.getLocalDateField()); @@ -1494,18 +1391,13 @@ static void initOwlClassTMock(IdentifiableEntityType et, SingularAttr static void initOwlClassUMocks(IdentifiableEntityType et, SingularAttributeImpl singularStringAtt, AbstractPluralAttribute pluralStringAtt, SingularAttributeImpl modified, Identifier id) throws Exception { - when(et.getIdentifier()).thenReturn(id); - when(id.isGenerated()).thenReturn(true); when(et.getJavaType()).thenReturn(OWLClassU.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassU.class)); - when(id.getJavaField()).thenReturn(OWLClassU.getIdField()); - when(id.getDeclaringType()).thenReturn(et); - when(id.getName()).thenReturn(OWLClassU.getIdField().getName()); + initIdentifier(et, id, OWLClassU.getIdField(), true); when(et.getIRI()).thenReturn(IRI.create(OWLClassU.getClassIri())); when(et.getName()).thenReturn(OWLClassU.class.getSimpleName()); - when(et.getFieldSpecifications()) - .thenReturn(new HashSet(Arrays.asList(singularStringAtt, pluralStringAtt, modified, id))); - when(et.getAttributes()).thenReturn(new HashSet(Arrays.asList(singularStringAtt, pluralStringAtt, modified))); + when(et.getFieldSpecifications()).thenReturn(Set.of(singularStringAtt, pluralStringAtt, modified, id)); + when(et.getAttributes()).thenReturn(Set.of(singularStringAtt, pluralStringAtt, modified)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(singularStringAtt.getJavaField()).thenReturn(OWLClassU.getSingularStringAttField()); @@ -1561,19 +1453,18 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt when(et.getLifecycleListenerManager()).thenReturn(listenerManager); } - static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPluralAttribute setStringAtt, AbstractPluralAttribute listStringAtt, AbstractPluralAttribute collectionStringAtt, AbstractQueryAttribute setQueryStringAtt, AbstractQueryAttribute listQueryStringAtt, Identifier id) throws Exception { - when(et.getIdentifier()).thenReturn(id); - when(id.isGenerated()).thenReturn(true); + static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPluralAttribute setStringAtt, + AbstractPluralAttribute listStringAtt, AbstractPluralAttribute collectionStringAtt, + AbstractQueryAttribute setQueryStringAtt, AbstractQueryAttribute listQueryStringAtt, + Identifier id) throws Exception { when(et.getJavaType()).thenReturn(OWLClassW.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassW.class)); - when(id.getJavaField()).thenReturn(OWLClassW.getIdField()); - when(id.getDeclaringType()).thenReturn(et); - when(id.getName()).thenReturn(OWLClassW.getIdField().getName()); + initIdentifier(et, id, OWLClassW.getIdField(), true); when(et.getIRI()).thenReturn(IRI.create(OWLClassW.getClassIri())); when(et.getName()).thenReturn(OWLClassW.class.getSimpleName()); when(et.getFieldSpecifications()) - .thenReturn(new HashSet(Arrays.asList(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt, id))); - when(et.getAttributes()).thenReturn(new HashSet(Arrays.asList(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt))); + .thenReturn(Set.of(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt, id)); + when(et.getAttributes()).thenReturn((Set) Set.of(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(setStringAtt.getJavaField()).thenReturn(OWLClassW.getSetStringAttField()); @@ -1614,7 +1505,8 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu when(collectionStringAtt.getJavaType()).thenReturn(OWLClassW.getCollectionStringAttField().getType()); when(collectionStringAtt.getName()).thenReturn(OWLClassW.getCollectionStringAttField().getName()); when(et.getAttribute(OWLClassW.getCollectionStringAttField().getName())).thenReturn(collectionStringAtt); - when(et.getFieldSpecification(OWLClassW.getCollectionStringAttField().getName())).thenReturn(collectionStringAtt); + when(et.getFieldSpecification(OWLClassW.getCollectionStringAttField() + .getName())).thenReturn(collectionStringAtt); when(collectionStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); when(collectionStringAtt.isCollection()).thenReturn(true); when(collectionStringAtt.getCollectionType()).thenReturn(CollectionType.COLLECTION); @@ -1632,7 +1524,8 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu when(setQueryStringAtt.getName()).thenReturn(OWLClassW.getSetQueryStringAttField().getName()); when(setQueryStringAtt.getDeclaringType()).thenReturn(et); when(setQueryStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(setQueryStringAtt.getQuery()).thenReturn(OWLClassW.getSetQueryStringAttField().getAnnotation(Sparql.class).query()); + when(setQueryStringAtt.getQuery()).thenReturn(OWLClassW.getSetQueryStringAttField().getAnnotation(Sparql.class) + .query()); when(setQueryStringAtt.enableReferencingAttributes()).thenReturn(true); when(et.getFieldSpecification(OWLClassW.getSetQueryStringAttField().getName())).thenReturn(setQueryStringAtt); @@ -1641,7 +1534,8 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu when(listQueryStringAtt.getName()).thenReturn(OWLClassW.getListQueryStringAttField().getName()); when(listQueryStringAtt.getDeclaringType()).thenReturn(et); when(listQueryStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(listQueryStringAtt.getQuery()).thenReturn(OWLClassW.getListQueryStringAttField().getAnnotation(Sparql.class).query()); + when(listQueryStringAtt.getQuery()).thenReturn(OWLClassW.getListQueryStringAttField() + .getAnnotation(Sparql.class).query()); when(listQueryStringAtt.enableReferencingAttributes()).thenReturn(true); when(et.getFieldSpecification(OWLClassW.getListQueryStringAttField().getName())).thenReturn(listQueryStringAtt); } @@ -1659,14 +1553,9 @@ static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType(Arrays.>asList(strAttMock, entityAttMock))); - when(etMock.getQueryAttributes()).thenReturn( - new HashSet<>(Arrays.>asList(strQueryAttMock, - entityQueryAttMock))); - when(etMock.getFieldSpecifications()).thenReturn( - new HashSet<>(Arrays.>asList( - strAttMock, strQueryAttMock, entityAttMock, entityQueryAttMock, idMock))); + when(etMock.getAttributes()).thenReturn(Set.of(strAttMock, entityAttMock)); + when(etMock.getQueryAttributes()).thenReturn(Set.of(strQueryAttMock, entityQueryAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, strQueryAttMock, entityAttMock, entityQueryAttMock, idMock)); when(strAttMock.getJavaField()).thenReturn(OWLClassWithQueryAttr.getStrAttField()); when(strAttMock.getJavaType()).thenReturn(OWLClassWithQueryAttr.getStrAttField().getType()); @@ -1713,9 +1602,7 @@ static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType etMock, Abstract when(etMock.getName()).thenReturn(Phone.class.getSimpleName()); when(etMock.getAttribute(Phone.class.getDeclaredField("number").getName())).thenReturn(phoneNumberAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(phoneNumberAttMock)); - when(etMock.getFieldSpecifications()) - .thenReturn(new HashSet<>( - Arrays.>asList(phoneNumberAttMock, idMock))); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(phoneNumberAttMock, idMock)); when(phoneNumberAttMock.getJavaField()).thenReturn(Phone.class.getDeclaredField("number")); when(phoneNumberAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_phoneNumber)); when(phoneNumberAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); @@ -1738,9 +1623,7 @@ public static void initPhoneMocks(IdentifiableEntityType etMock, Abstract when(phoneNumberAttMock.hasLanguage()).thenReturn(false); when(phoneNumberAttMock.getName()).thenReturn(Phone.class.getDeclaredField("number").getName()); when(etMock.getAttribute(Phone.class.getDeclaredField("number").getName())).thenReturn(phoneNumberAttMock); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getName()).thenReturn(Phone.class.getDeclaredField("uri").getName()); - when(idMock.getJavaField()).thenReturn(Phone.class.getDeclaredField("uri")); + initIdentifier(etMock, idMock, Phone.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -1755,17 +1638,9 @@ public static void initPersonMocks(IdentifiableEntityType etMock, Abstra when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(etMock.getIRI()).thenReturn(IRI.create(Vocabulary.c_Person)); when(etMock.getName()).thenReturn(Person.class.getSimpleName()); - when(etMock.getIdentifier()).thenReturn(idMock); - when(idMock.getJavaField()).thenReturn(Person.class.getDeclaredField("uri")); - when(idMock.getName()).thenReturn(Person.class.getDeclaredField("uri").getName()); - when(idMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock); - when(etMock.getAttributes()).thenReturn( - new HashSet<>(Arrays.>asList(usernameAttMock, genderAttMock, ageAttMock, - phoneAttMock))); - when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>( - Arrays.>asList(usernameAttMock, genderAttMock, ageAttMock, - phoneAttMock, idMock))); + initIdentifier(etMock, idMock, Person.class.getDeclaredField("uri"), false); + when(etMock.getAttributes()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock, idMock)); when(usernameAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("username")); when(usernameAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_username)); when(usernameAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); From 1b17cd851b67221d13bf2eb5e71e54f38c8c7951 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Fri, 20 Dec 2024 14:40:11 +0100 Subject: [PATCH 09/17] [GH-301] Extract singular attribute setup into a reusable function. --- .../jopa/datatype/DatatypeTransformer.java | 13 +- .../environment/utils/MetamodelFactory.java | 822 +++++------------- .../environment/utils/MetamodelMocks.java | 10 +- .../jopa/oom/EntityDeconstructorTest.java | 122 ++- 4 files changed, 287 insertions(+), 680 deletions(-) diff --git a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java index d4edbeba4..f4e9e607f 100644 --- a/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java +++ b/datatype/src/main/java/cz/cvut/kbss/jopa/datatype/DatatypeTransformer.java @@ -91,15 +91,15 @@ private DatatypeTransformer() { /** * Converts the specified wrapper class to its corresponding primitive class. - * - * If the class parameter is a wrapper type, the equivalent primitive type will be returned (e.g. int.class for Integer.class) - * In all other cases, the return value is null. + *

+ * If the class parameter is a wrapper type, the equivalent primitive type will be returned (e.g. int.class for + * Integer.class) In all other cases, the return value is null. * * @param cls - the class to convert - * @return + * @return Optional containing the primitive type, or empty if the specified class is not a wrapper type */ public static Optional> wrapperTypeToPrimitiveType(Class cls) { - if(cls != null && WRAPPER_TO_PRIMITIVES.containsKey(cls)) { + if (cls != null && WRAPPER_TO_PRIMITIVES.containsKey(cls)) { return Optional.of(WRAPPER_TO_PRIMITIVES.get(cls)); } return Optional.empty(); @@ -112,7 +112,8 @@ public static Optional> wrapperTypeToPrimitiveType(Class cls) { * @param targetType The type to which the specified value should be converted * @param Target type * @return Value as the target type - * @throws UnsupportedTypeTransformationException If the specified value cannot be transformed to the specified target type + * @throws UnsupportedTypeTransformationException If the specified value cannot be transformed to the specified + * target type */ public static T transform(Object value, Class targetType) { Objects.requireNonNull(targetType); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index 3df809db9..3d5981be5 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -53,6 +53,7 @@ import cz.cvut.kbss.jopa.model.MultilingualString; import cz.cvut.kbss.jopa.model.annotations.CascadeType; import cz.cvut.kbss.jopa.model.annotations.FetchType; +import cz.cvut.kbss.jopa.model.annotations.Inferred; import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; @@ -68,6 +69,7 @@ import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute; import cz.cvut.kbss.jopa.model.metamodel.AbstractQueryAttribute; import cz.cvut.kbss.jopa.model.metamodel.Attribute; +import cz.cvut.kbss.jopa.model.metamodel.BasicTypeImpl; import cz.cvut.kbss.jopa.model.metamodel.CollectionType; import cz.cvut.kbss.jopa.model.metamodel.EntityLifecycleListenerManager; import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification; @@ -86,25 +88,21 @@ import cz.cvut.kbss.jopa.model.metamodel.gen.ManageableClassGenerator; import cz.cvut.kbss.jopa.model.metamodel.gen.PersistenceContextAwareClassGenerator; import cz.cvut.kbss.jopa.oom.converter.CharacterConverter; +import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.CustomConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.DefaultConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.ObjectOneOfEnumConverter; import cz.cvut.kbss.jopa.oom.converter.OrdinalEnumConverter; import cz.cvut.kbss.jopa.oom.converter.StringEnumConverter; -import cz.cvut.kbss.jopa.oom.converter.ToDoubleConverter; import cz.cvut.kbss.jopa.oom.converter.ToIntegerConverter; import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter; -import cz.cvut.kbss.jopa.oom.converter.ToLongConverter; import cz.cvut.kbss.jopa.oom.converter.datetime.LocalDateTimeConverter; import cz.cvut.kbss.jopa.utils.Configuration; -import cz.cvut.kbss.jopa.vocabulary.DC; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URI; import java.net.URL; -import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -149,7 +147,7 @@ public static void setInstantiableTypeGenerator(PersistenceContextAwareClassGene /** * Initializes the specified mock objects to return reasonable values. */ - public static void initOWLClassAMocks(IdentifiableEntityType etMock, AbstractAttribute strAttMock, + public static void initOWLClassAMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, TypesSpecification typesMock, Identifier idMock) throws Exception { when(etMock.getJavaType()).thenReturn(OWLClassA.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassA.class)); @@ -161,19 +159,8 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, typesMock, idMock)); - when(strAttMock.getJavaField()).thenReturn(OWLClassA.getStrAttField()); - when(strAttMock.getJavaType()).thenReturn(OWLClassA.getStrAttField().getType()); - when(strAttMock.getDeclaringType()).thenReturn(etMock); - final String stringAttIri = OWLClassA.getStrAttField().getAnnotation(OWLDataProperty.class).iri(); - when(strAttMock.getIRI()).thenReturn(IRI.create(stringAttIri)); - when(strAttMock.getName()).thenReturn(OWLClassA.getStrAttField().getName()); - when(strAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(strAttMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(strAttMock.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(strAttMock.getFetchType()).thenReturn(FetchType.EAGER); - when(strAttMock.hasLanguage()).thenReturn(true); - when(strAttMock.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(strAttMock.getName())).thenReturn(strAttMock); + initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassA.getStrAttField(), Attribute.PersistentAttributeType.DATA) + .language(Generators.LANG)); when(typesMock.getJavaField()).thenReturn(OWLClassA.getTypesField()); when(typesMock.getName()).thenReturn(OWLClassA.getTypesField().getName()); when(typesMock.getDeclaringType()).thenReturn(etMock); @@ -206,6 +193,65 @@ private static void initIdentifier(IdentifiableEntityType et, Identifier when(et.getFieldSpecification(idField.getName())).thenReturn(id); } + private static void initAttribute(IdentifiableEntityType etMock, SingularAttributeImpl attMock, + AttributeInfo attInfo) { + when(etMock.getFieldSpecification(attInfo.field.getName())).thenReturn(attMock); + when(etMock.getAttribute(attInfo.field.getName())).thenReturn(attMock); + when(attMock.getName()).thenReturn(attInfo.field.getName()); + when(attMock.getDeclaringType()).thenReturn(etMock); + when(attMock.getJavaField()).thenReturn(attInfo.field); + when(attMock.getJavaType()).thenReturn(attInfo.field.getType()); + when(attMock.getBindableJavaType()).thenReturn(attInfo.field.getType()); + when(attMock.getConstraints()).thenReturn(attInfo.constraints); + when(attMock.isNonEmpty()).thenReturn(attInfo.nonEmpty); + when(attMock.getPersistentAttributeType()).thenReturn(attInfo.type); + when(attMock.getConverter()).thenReturn(attInfo.converter); + switch (attInfo.type) { + case DATA: + final OWLDataProperty dp = attInfo.field.getAnnotation(OWLDataProperty.class); + when(attMock.getIRI()).thenReturn(IRI.create(dp.iri())); + when(attMock.getFetchType()).thenReturn(dp.fetch()); + when(attMock.isSimpleLiteral()).thenReturn(dp.simpleLiteral()); + when(attMock.isLexicalForm()).thenReturn(dp.lexicalForm()); + when(attMock.isAssociation()).thenReturn(false); + when(attMock.getDatatype()).thenReturn(dp.datatype()); + when(attMock.isInferred()).thenReturn(attInfo.field.getAnnotation(Inferred.class) != null); + when(attMock.getCascadeTypes()).thenReturn(new CascadeType[0]); + when(attMock.getLanguage()).thenReturn(attInfo.language); + when(attMock.hasLanguage()).thenReturn(attInfo.language != null); + when(attMock.getType()).thenReturn(BasicTypeImpl.get(attInfo.field.getType())); + break; + case OBJECT: + final OWLObjectProperty op = attInfo.field.getAnnotation(OWLObjectProperty.class); + when(attMock.getIRI()).thenReturn(IRI.create(op.iri())); + when(attMock.getFetchType()).thenReturn(op.fetch()); + when(attMock.isAssociation()).thenReturn(true); + when(attMock.isInferred()).thenReturn(attInfo.field.getAnnotation(Inferred.class) != null); + when(attMock.getCascadeTypes()).thenReturn(op.cascade()); + if (URI.class.isAssignableFrom(attInfo.field.getType())) { + when(attMock.getType()).thenReturn(BasicTypeImpl.get(URI.class)); + } else { + when(attMock.getType()).thenReturn(attInfo.valueType); + } + break; + case ANNOTATION: + final OWLAnnotationProperty ap = attInfo.field.getAnnotation(OWLAnnotationProperty.class); + when(attMock.getIRI()).thenReturn(IRI.create(ap.iri())); + when(attMock.getFetchType()).thenReturn(ap.fetch()); + when(attMock.isSimpleLiteral()).thenReturn(ap.simpleLiteral()); + when(attMock.isLexicalForm()).thenReturn(ap.lexicalForm()); + when(attMock.isAssociation()).thenReturn(false); + when(attMock.getDatatype()).thenReturn(ap.datatype()); + when(attMock.isInferred()).thenReturn(attInfo.field.getAnnotation(Inferred.class) != null); + when(attMock.getCascadeTypes()).thenReturn(new CascadeType[0]); + when(attMock.getLanguage()).thenReturn(attInfo.language); + when(attMock.hasLanguage()).thenReturn(attInfo.language != null); + when(attMock.getType()).thenReturn(Objects.requireNonNullElseGet(attInfo.valueType, + () -> BasicTypeImpl.get(attInfo.field.getType()))); + break; + } + } + private static void addLifecycleCallback(EntityLifecycleListenerManager manager, LifecycleEvent evt, Method callback) throws Exception { final Method addCallback = EntityLifecycleListenerManager.class @@ -219,7 +265,7 @@ private static void addLifecycleCallback(EntityLifecycleListenerManager manager, /** * Initializes the specified mock objects to return reasonable values. */ - public static void initOWLClassBMocks(IdentifiableEntityType etMock, AbstractAttribute strAttMock, + public static void initOWLClassBMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, PropertiesSpecification propsMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassB.class); @@ -231,16 +277,8 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, propsMock, idMock)); - when(strAttMock.getJavaField()).thenReturn(OWLClassB.getStrAttField()); - when(strAttMock.getJavaType()).thenReturn(OWLClassB.getStrAttField().getType()); - final String stringAttIri = OWLClassB.getStrAttField().getAnnotation(OWLDataProperty.class).iri(); - when(strAttMock.getIRI()).thenReturn(IRI.create(stringAttIri)); - when(strAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(strAttMock.getName()).thenReturn(OWLClassB.getStrAttField().getName()); - when(strAttMock.getDeclaringType()).thenReturn(etMock); - when(strAttMock.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(strAttMock.hasLanguage()).thenReturn(true); - when(strAttMock.getLanguage()).thenReturn(Generators.LANG); + initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassB.getStrAttField(), Attribute.PersistentAttributeType.DATA) + .language(Generators.LANG)); when(etMock.getFieldSpecification(strAttMock.getName())).thenReturn(strAttMock); when(propsMock.getJavaField()).thenReturn(OWLClassB.getPropertiesField()); when(propsMock.getJavaType()).thenReturn(OWLClassB.getPropertiesField().getType()); @@ -368,45 +406,28 @@ public static void initOWLClassDMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassD.getOwlClassAField().getName())).thenReturn(clsAMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsAMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, idMock)); - when(clsAMock.getJavaField()).thenReturn(OWLClassD.getOwlClassAField()); - when(clsAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - final String clsAIri = OWLClassD.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri(); - when(clsAMock.getIRI()).thenReturn(IRI.create(clsAIri)); - when(clsAMock.getJavaType()).thenReturn(OWLClassA.class); - when(clsAMock.getName()).thenReturn(OWLClassD.getOwlClassAField().getName()); - when(clsAMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(clsAMock.getFetchType()).thenReturn(FetchType.EAGER); - when(clsAMock.getDeclaringType()).thenReturn(etMock); - when(clsAMock.isAssociation()).thenReturn(true); - when(clsAMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(clsAMock.getType()).thenReturn(etA); + initAttribute(etMock, clsAMock, new AttributeInfo(OWLClassD.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT).valueType(etA)); when(etMock.getFieldSpecification(clsAMock.getName())).thenReturn(clsAMock); initIdentifier(etMock, idMock, OWLClassD.getUriField(), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } - public static void initOWLClassEMocks(IdentifiableEntityType etMock, AbstractAttribute strAttMock, + public static void initOWLClassEMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassE.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassE.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassE.getClassIri())); when(etMock.getName()).thenReturn(OWLClassE.class.getSimpleName()); - when(etMock.getAttribute(OWLClassE.getStrAttField().getName())).thenReturn(strAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, idMock)); - when(strAttMock.getJavaField()).thenReturn(OWLClassE.getStrAttField()); - final String stringAttIri = OWLClassE.getStrAttField().getAnnotation(OWLDataProperty.class).iri(); - when(strAttMock.getIRI()).thenReturn(IRI.create(stringAttIri)); - when(strAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(strAttMock.getDeclaringType()).thenReturn(etMock); - when(strAttMock.hasLanguage()).thenReturn(true); - when(strAttMock.getLanguage()).thenReturn(Generators.LANG); + initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassE.getStrAttField(), Attribute.PersistentAttributeType.DATA) + .language(Generators.LANG)); initIdentifier(etMock, idMock, OWLClassE.class.getDeclaredField("uri"), true); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassFMocks(IdentifiableEntityType etMock, AbstractPluralAttribute setAMock, - AbstractAttribute strAttMock, + SingularAttributeImpl strAttMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassF.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassF.class)); @@ -433,19 +454,8 @@ public static void initOWLClassFMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); when(etMock.getFieldSpecification(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); - when(strAttMock.getJavaField()).thenReturn(OWLClassF.getStrAttField()); - when(strAttMock.getJavaType()).thenReturn(OWLClassF.getStrAttField().getType()); - when(strAttMock.getDeclaringType()).thenReturn(etMock); - final String stringAttIri = OWLClassF.getStrAttField().getAnnotation(OWLDataProperty.class) - .iri(); - when(strAttMock.getIRI()).thenReturn(IRI.create(stringAttIri)); - when(strAttMock.getName()).thenReturn(OWLClassF.getStrAttField().getName()); - when(strAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(strAttMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(strAttMock.isInferred()).thenReturn(true); - when(strAttMock.hasLanguage()).thenReturn(true); - when(strAttMock.getLanguage()).thenReturn(Generators.LANG); - when(strAttMock.getFetchType()).thenReturn(FetchType.EAGER); + initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassF.getStrAttField(), Attribute.PersistentAttributeType.DATA) + .language(Generators.LANG)); when(etMock.getFieldSpecification(OWLClassF.getStrAttField().getName())).thenReturn(strAttMock); initIdentifier(etMock, idMock, OWLClassF.class.getDeclaredField("uri"), false); @@ -459,20 +469,11 @@ public static void initOWLClassGMocks(IdentifiableEntityType etMock, when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassG.class)); when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassG.getClassIri())); - when(etMock.getAttribute(OWLClassG.getOwlClassHField().getName())).thenReturn(clsHMock); when(etMock.getName()).thenReturn(OWLClassG.class.getSimpleName()); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsHMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsHMock, idMock)); - when(clsHMock.getJavaField()).thenReturn(OWLClassG.getOwlClassHField()); - when(clsHMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(clsHMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_g_hasH)); - when(clsHMock.getJavaType()).thenReturn(OWLClassH.class); - when(clsHMock.getName()).thenReturn(OWLClassG.getOwlClassHField().getName()); - when(clsHMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(clsHMock.getFetchType()).thenReturn(FetchType.EAGER); - when(clsHMock.getDeclaringType()).thenReturn(etMock); - when(clsHMock.getType()).thenReturn(etHMock); - when(etMock.getFieldSpecification(clsHMock.getName())).thenReturn(clsHMock); + initAttribute(etMock, clsHMock, new AttributeInfo(OWLClassG.getOwlClassHField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etHMock)); initIdentifier(etMock, idMock, OWLClassG.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -488,58 +489,30 @@ public static void initOWLClassHMocks(IdentifiableEntityType etMock, when(etMock.getName()).thenReturn(OWLClassH.class.getSimpleName()); when(etMock.getAttributes()).thenReturn(Set.of(clsAMock, clsGMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, clsGMock, idMock)); - when(clsAMock.getJavaField()).thenReturn(OWLClassH.getOwlClassAField()); - when(clsAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(clsAMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_h_hasA)); - when(clsAMock.getJavaType()).thenReturn(OWLClassA.class); - when(clsAMock.getName()).thenReturn(OWLClassH.getOwlClassAField().getName()); - when(clsAMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(clsAMock.getFetchType()).thenReturn(FetchType.EAGER); - when(clsAMock.getDeclaringType()).thenReturn(etMock); - when(clsAMock.getType()).thenReturn(etAMock); - - when(clsGMock.getJavaField()).thenReturn(OWLClassH.getOwlClassGField()); - when(clsGMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(clsGMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_h_hasG)); - when(clsGMock.getJavaType()).thenReturn(OWLClassG.class); - when(clsGMock.getName()).thenReturn(OWLClassH.getOwlClassGField().getName()); - when(clsGMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(clsGMock.getFetchType()).thenReturn(FetchType.LAZY); - when(clsGMock.getDeclaringType()).thenReturn(etMock); - when(clsGMock.getType()).thenReturn(etGMock); - when(etMock.getFieldSpecification(clsAMock.getName())).thenReturn(clsAMock); - when(etMock.getFieldSpecification(clsGMock.getName())).thenReturn(clsGMock); - when(etMock.getAttribute(clsAMock.getName())).thenReturn(clsAMock); - when(etMock.getAttribute(clsGMock.getName())).thenReturn(clsGMock); + initAttribute(etMock, clsAMock, new AttributeInfo(OWLClassH.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etAMock)); + initAttribute(etMock, clsGMock, new AttributeInfo(OWLClassH.getOwlClassGField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etGMock)); initIdentifier(etMock, idMock, OWLClassH.class.getDeclaredField("uri"), false); + when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassIMocks(IdentifiableEntityType etMock, SingularAttributeImpl aAttMock, + IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassI.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassI.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassI.getClassIri())); when(etMock.getName()).thenReturn(OWLClassI.class.getSimpleName()); - when(etMock.getAttribute(OWLClassI.getOwlClassAField().getName())).thenReturn(aAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(aAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(aAttMock, idMock)); - when(aAttMock.getName()).thenReturn(OWLClassI.getOwlClassAField().getName()); - when(aAttMock.getJavaField()).thenReturn(OWLClassI.getOwlClassAField()); - when(aAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(aAttMock.getCascadeTypes()) - .thenReturn(OWLClassI.getOwlClassAField().getAnnotation(OWLObjectProperty.class).cascade()); - final String clsAIri = OWLClassI.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri(); - when(aAttMock.getIRI()).thenReturn(IRI.create(clsAIri)); - when(aAttMock.getJavaType()).thenReturn(OWLClassA.class); - when(aAttMock.isCollection()).thenReturn(false); - when(aAttMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(aAttMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(aAttMock.getDeclaringType()).thenReturn(etMock); - when(aAttMock.getFetchType()).thenReturn(FetchType.LAZY); - when(etMock.getFieldSpecification(aAttMock.getName())).thenReturn(aAttMock); + + initAttribute(etMock, aAttMock, new AttributeInfo(OWLClassI.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etAMock)); initIdentifier(etMock, idMock, OWLClassI.class.getDeclaredField("uri"), false); + when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -569,30 +542,26 @@ public static void initOWLClassJMocks(IdentifiableEntityType etMock, when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } - public static void initOWLClassKMocks(IdentifiableEntityType etMock, AbstractAttribute clsEMock, + public static void initOWLClassKMocks(IdentifiableEntityType etMock, SingularAttributeImpl clsEMock, + IdentifiableEntityType etEMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassK.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassK.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassK.getClassIri())); when(etMock.getName()).thenReturn(OWLClassK.class.getSimpleName()); - when(etMock.getAttribute(OWLClassK.getOwlClassEField().getName())).thenReturn(clsEMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsEMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsEMock, idMock)); - when(clsEMock.getJavaField()).thenReturn(OWLClassK.getOwlClassEField()); - when(clsEMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - final String clsEIri = OWLClassK.getOwlClassEField().getAnnotation(OWLObjectProperty.class).iri(); - when(clsEMock.getIRI()).thenReturn(IRI.create(clsEIri)); - when(clsEMock.getJavaType()).thenReturn(OWLClassE.class); - when(clsEMock.getDeclaringType()).thenReturn(etMock); - when(clsEMock.getFetchType()).thenReturn(FetchType.LAZY); - when(clsEMock.getName()).thenReturn(OWLClassK.getOwlClassEField().getName()); + + initAttribute(etMock, clsEMock, new AttributeInfo(OWLClassK.getOwlClassEField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etEMock)); initIdentifier(etMock, idMock, OWLClassK.class.getDeclaredField("uri"), false); + when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassLMocks(IdentifiableEntityType etMock, ListAttributeImpl refListMock, ListAttributeImpl simpleListMock, AbstractPluralAttribute setMock, - AbstractAttribute singleAMock, + SingularAttributeImpl singleAMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException { when(etMock.getJavaType()).thenReturn(OWLClassL.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassL.class)); @@ -656,28 +625,16 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, when(setMock.getDeclaringType()).thenReturn(etMock); when(setMock.getJavaType()).thenReturn(Set.class); - when(singleAMock.getJavaField()).thenReturn(OWLClassL.getSingleAField()); - when(singleAMock.getName()).thenReturn(OWLClassL.getSingleAField().getName()); - when(singleAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(singleAMock.getIRI()).thenReturn( - IRI.create(OWLClassL.getSingleAField().getAnnotation(OWLObjectProperty.class).iri())); - when(singleAMock.isCollection()).thenReturn(false); - when(singleAMock.getConstraints()).thenReturn( - OWLClassL.getSingleAField().getAnnotation(ParticipationConstraints.class).value()); - when(singleAMock.isNonEmpty()) - .thenReturn(OWLClassL.getSingleAField().getAnnotation(ParticipationConstraints.class).nonEmpty()); - when(singleAMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification(OWLClassL.getSingleAField().getName())).thenReturn(singleAMock); - when(etMock.getAttribute(OWLClassL.getSingleAField().getName())).thenReturn(singleAMock); - when(singleAMock.getJavaType()).thenReturn(OWLClassA.class); + initAttribute(etMock, singleAMock, new AttributeInfo(OWLClassL.getSingleAField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etAMock).nonEmpty()); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } - public static void initOWLClassMMock(IdentifiableEntityType etMock, AbstractAttribute booleanAtt, - AbstractAttribute intAtt, SingularAttributeImpl longAtt, - AbstractAttribute doubleAtt, AbstractAttribute dateAtt, - AbstractAttribute characterAtt, - AbstractAttribute enumAtt, AbstractAttribute ordinalEnumAtt, + public static void initOWLClassMMock(IdentifiableEntityType etMock, SingularAttributeImpl booleanAtt, + SingularAttributeImpl intAtt, SingularAttributeImpl longAtt, + SingularAttributeImpl doubleAtt, SingularAttributeImpl dateAtt, + SingularAttributeImpl characterAtt, + SingularAttributeImpl enumAtt, SingularAttributeImpl ordinalEnumAtt, AbstractPluralAttribute intSetAtt, SingularAttributeImpl lexicalFormAtt, SingularAttributeImpl simpleLiteralAtt, SingularAttributeImpl explicitDatatypeAtt, @@ -697,117 +654,23 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, A characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, simpleLiteralAtt, explicitDatatypeAtt, mObjectOneOfEnumAttribute, idMock)); - when(booleanAtt.getJavaField()).thenReturn(OWLClassM.getBooleanAttributeField()); - when(booleanAtt.getName()).thenReturn(OWLClassM.getBooleanAttributeField().getName()); - when(booleanAtt.getJavaType()).thenReturn(OWLClassM.getBooleanAttributeField().getType()); - when(booleanAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_booleanAttribute)); - when(booleanAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(booleanAtt.isCollection()).thenReturn(false); - when(booleanAtt.getDeclaringType()).thenReturn(etMock); - when(booleanAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(booleanAtt.hasLanguage()).thenReturn(true); - when(booleanAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getBooleanAttributeField().getName())).thenReturn(booleanAtt); - when(etMock.getAttribute(OWLClassM.getBooleanAttributeField().getName())).thenReturn(booleanAtt); - - when(intAtt.getJavaField()).thenReturn(OWLClassM.getIntAttributeField()); - when(intAtt.getName()).thenReturn(OWLClassM.getIntAttributeField().getName()); - when(intAtt.getJavaType()).thenReturn(OWLClassM.getIntAttributeField().getType()); - when(intAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_intAttribute)); - when(intAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(intAtt.isCollection()).thenReturn(false); - when(intAtt.getDeclaringType()).thenReturn(etMock); - when(intAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(intAtt.getConverter()).thenReturn(new ToIntegerConverter()); - when(intAtt.hasLanguage()).thenReturn(true); - when(intAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getIntAttributeField().getName())).thenReturn(intAtt); - when(etMock.getAttribute(OWLClassM.getIntAttributeField().getName())).thenReturn(intAtt); - - - when(longAtt.getJavaField()).thenReturn(OWLClassM.getLongAttributeField()); - when(longAtt.getName()).thenReturn(OWLClassM.getLongAttributeField().getName()); - when(longAtt.getJavaType()).thenReturn(OWLClassM.getLongAttributeField().getType()); - when(longAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_longAttribute)); - when(longAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(longAtt.isCollection()).thenReturn(false); - when(longAtt.getBindableJavaType()).thenReturn(Long.class); - when(longAtt.getDeclaringType()).thenReturn(etMock); - when(longAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(longAtt.getConverter()).thenReturn(new ToLongConverter()); - when(longAtt.hasLanguage()).thenReturn(true); - when(longAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getLongAttributeField().getName())).thenReturn(longAtt); - when(etMock.getAttribute(OWLClassM.getLongAttributeField().getName())).thenReturn(longAtt); - - when(doubleAtt.getJavaField()).thenReturn(OWLClassM.getDoubleAttributeField()); - when(doubleAtt.getName()).thenReturn(OWLClassM.getDoubleAttributeField().getName()); - when(doubleAtt.getJavaType()).thenReturn(OWLClassM.getDoubleAttributeField().getType()); - when(doubleAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_doubleAttribute)); - when(doubleAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(doubleAtt.isCollection()).thenReturn(false); - when(doubleAtt.getDeclaringType()).thenReturn(etMock); - when(doubleAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(doubleAtt.getConverter()).thenReturn(new ToDoubleConverter()); - when(doubleAtt.hasLanguage()).thenReturn(true); - when(doubleAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getDoubleAttributeField().getName())).thenReturn(doubleAtt); - when(etMock.getAttribute(OWLClassM.getDoubleAttributeField().getName())).thenReturn(doubleAtt); - - when(dateAtt.getJavaField()).thenReturn(OWLClassM.getDateAttributeField()); - when(dateAtt.getName()).thenReturn(OWLClassM.getDateAttributeField().getName()); - when(dateAtt.getJavaType()).thenReturn(OWLClassM.getDateAttributeField().getType()); - when(dateAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_dateAttribute)); - when(dateAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(dateAtt.isCollection()).thenReturn(false); - when(dateAtt.getDeclaringType()).thenReturn(etMock); - when(dateAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(dateAtt.hasLanguage()).thenReturn(true); - when(dateAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getDateAttributeField().getName())).thenReturn(dateAtt); - when(etMock.getAttribute(OWLClassM.getDateAttributeField().getName())).thenReturn(dateAtt); - - when(characterAtt.getJavaField()).thenReturn(OWLClassM.getCharacterAttributeField()); - when(characterAtt.getName()).thenReturn(OWLClassM.getCharacterAttributeField().getName()); - when(characterAtt.getJavaType()).thenReturn(OWLClassM.getCharacterAttributeField().getType()); - when(characterAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_characterAttribute)); - when(characterAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(characterAtt.isCollection()).thenReturn(false); - when(characterAtt.getDeclaringType()).thenReturn(etMock); - when(characterAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(characterAtt.getConverter()).thenReturn(new CharacterConverter()); - when(characterAtt.hasLanguage()).thenReturn(true); - when(characterAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getCharacterAttributeField().getName())).thenReturn(characterAtt); - when(etMock.getAttribute(OWLClassM.getCharacterAttributeField().getName())).thenReturn(characterAtt); - - when(enumAtt.getJavaField()).thenReturn(OWLClassM.getEnumAttributeField()); - when(enumAtt.getName()).thenReturn(OWLClassM.getEnumAttributeField().getName()); - when(enumAtt.getJavaType()).thenReturn(OWLClassM.getEnumAttributeField().getType()); - when(enumAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_enumAttribute)); - when(enumAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(enumAtt.isCollection()).thenReturn(false); - when(enumAtt.getDeclaringType()).thenReturn(etMock); - when(enumAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(enumAtt.getConverter()).thenReturn(new StringEnumConverter<>(OWLClassM.Severity.class)); - when(enumAtt.hasLanguage()).thenReturn(true); - when(enumAtt.getLanguage()).thenReturn(Generators.LANG); - when(etMock.getFieldSpecification(OWLClassM.getEnumAttributeField().getName())).thenReturn(enumAtt); - when(etMock.getAttribute(OWLClassM.getEnumAttributeField().getName())).thenReturn(enumAtt); - - when(ordinalEnumAtt.getJavaField()).thenReturn(OWLClassM.getOrdinalEnumAttributeField()); - when(ordinalEnumAtt.getName()).thenReturn(OWLClassM.getOrdinalEnumAttributeField().getName()); - when(ordinalEnumAtt.getJavaType()).thenReturn(OWLClassM.getOrdinalEnumAttributeField().getType()); - when(ordinalEnumAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_ordinalEnumAttribute)); - when(ordinalEnumAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(ordinalEnumAtt.isCollection()).thenReturn(false); - when(ordinalEnumAtt.getDeclaringType()).thenReturn(etMock); - when(ordinalEnumAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(ordinalEnumAtt.getConverter()).thenReturn(new OrdinalEnumConverter(OWLClassM.Severity.class)); - when(ordinalEnumAtt.hasLanguage()).thenReturn(false); - when(etMock.getFieldSpecification(OWLClassM.getOrdinalEnumAttributeField().getName())).thenReturn( - ordinalEnumAtt); - when(etMock.getAttribute(OWLClassM.getOrdinalEnumAttributeField().getName())).thenReturn(ordinalEnumAtt); + initAttribute(etMock, booleanAtt, new AttributeInfo(OWLClassM.getBooleanAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(etMock, intAtt, new AttributeInfo(OWLClassM.getIntAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(etMock, longAtt, new AttributeInfo(OWLClassM.getLongAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(etMock, doubleAtt, new AttributeInfo(OWLClassM.getDoubleAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(etMock, dateAtt, new AttributeInfo(OWLClassM.getDateAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(etMock, characterAtt, new AttributeInfo(OWLClassM.getCharacterAttributeField(), Attribute.PersistentAttributeType.DATA).converter(new CharacterConverter()) + .language(null)); + initAttribute(etMock, enumAtt, new AttributeInfo(OWLClassM.getEnumAttributeField(), Attribute.PersistentAttributeType.DATA).converter(new StringEnumConverter<>(OWLClassM.Severity.class)) + .language(Generators.LANG)); + initAttribute(etMock, ordinalEnumAtt, new AttributeInfo(OWLClassM.getOrdinalEnumAttributeField(), Attribute.PersistentAttributeType.DATA).converter(new OrdinalEnumConverter(OWLClassM.Severity.class))); + initAttribute(etMock, lexicalFormAtt, new AttributeInfo(OWLClassM.getLexicalFormField(), Attribute.PersistentAttributeType.DATA).converter(new ToLexicalFormConverter())); + initAttribute(etMock, simpleLiteralAtt, new AttributeInfo(OWLClassM.getSimpleLiteralField(), Attribute.PersistentAttributeType.DATA).converter(new ToLexicalFormConverter()) + .language(null)); + initAttribute(etMock, explicitDatatypeAtt, new AttributeInfo(OWLClassM.getExplicitDatatypeField(), Attribute.PersistentAttributeType.DATA).converter(new ToLexicalFormConverter())); + initAttribute(etMock, mWithConverterAtt, new AttributeInfo(OWLClassM.getWithConverterField(), Attribute.PersistentAttributeType.DATA).converter( + new CustomConverterWrapper(new ZoneOffsetConverter(), String.class))); + initAttribute(etMock, mObjectOneOfEnumAttribute, new AttributeInfo(OWLClassM.getObjectOneOfEnumAttributeField(), Attribute.PersistentAttributeType.OBJECT).converter(new ObjectOneOfEnumConverter(OneOfEnum.class))); when(intSetAtt.getJavaField()).thenReturn(OWLClassM.getIntegerSetField()); when(intSetAtt.getName()).thenReturn(OWLClassM.getIntegerSetField().getName()); @@ -826,77 +689,6 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, A when(typeMock.getJavaType()).thenReturn(Integer.class); when(etMock.getFieldSpecification(OWLClassM.getIntegerSetField().getName())).thenReturn(intSetAtt); - when(lexicalFormAtt.getJavaField()).thenReturn(OWLClassM.getLexicalFormField()); - when(lexicalFormAtt.getName()).thenReturn(OWLClassM.getLexicalFormField().getName()); - when(lexicalFormAtt.getJavaType()).thenReturn(OWLClassM.getLexicalFormField().getType()); - when(lexicalFormAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_lexicalForm)); - when(lexicalFormAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(lexicalFormAtt.isCollection()).thenReturn(false); - when(lexicalFormAtt.getDeclaringType()).thenReturn(etMock); - when(lexicalFormAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(lexicalFormAtt.getConverter()).thenReturn(new ToLexicalFormConverter()); - when(lexicalFormAtt.isLexicalForm()).thenReturn(true); - when(etMock.getFieldSpecification(OWLClassM.getLexicalFormField().getName())).thenReturn(lexicalFormAtt); - - when(simpleLiteralAtt.getJavaField()).thenReturn(OWLClassM.getSimpleLiteralField()); - when(simpleLiteralAtt.getName()).thenReturn(OWLClassM.getSimpleLiteralField().getName()); - when(simpleLiteralAtt.getJavaType()).thenReturn(OWLClassM.getSimpleLiteralField().getType()); - when(simpleLiteralAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_simpleLiteral)); - when(simpleLiteralAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(simpleLiteralAtt.isCollection()).thenReturn(false); - when(simpleLiteralAtt.getDeclaringType()).thenReturn(etMock); - when(simpleLiteralAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(simpleLiteralAtt.getConverter()).thenReturn(new ToLexicalFormConverter()); - when(simpleLiteralAtt.isSimpleLiteral()).thenReturn(true); - when(etMock.getFieldSpecification(OWLClassM.getSimpleLiteralField().getName())).thenReturn(simpleLiteralAtt); - when(etMock.getAttribute(OWLClassM.getSimpleLiteralField().getName())).thenReturn(simpleLiteralAtt); - - when(explicitDatatypeAtt.getJavaField()).thenReturn(OWLClassM.getExplicitDatatypeField()); - when(explicitDatatypeAtt.getName()).thenReturn(OWLClassM.getExplicitDatatypeField().getName()); - when(explicitDatatypeAtt.getJavaType()).thenReturn(OWLClassM.getExplicitDatatypeField().getType()); - when(explicitDatatypeAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_explicitDatatype)); - when(explicitDatatypeAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(explicitDatatypeAtt.isCollection()).thenReturn(false); - when(explicitDatatypeAtt.getDeclaringType()).thenReturn(etMock); - when(explicitDatatypeAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(explicitDatatypeAtt.getConverter()).thenReturn(new ToLexicalFormConverter()); - when(explicitDatatypeAtt.isSimpleLiteral()).thenReturn(false); - when(explicitDatatypeAtt.getDatatype()).thenReturn(OWLClassM.getExplicitDatatypeField() - .getAnnotation(OWLDataProperty.class).datatype()); - when(etMock.getFieldSpecification(OWLClassM.getExplicitDatatypeField().getName())).thenReturn( - explicitDatatypeAtt); - when(etMock.getAttribute(OWLClassM.getExplicitDatatypeField().getName())).thenReturn(explicitDatatypeAtt); - - when(mWithConverterAtt.getJavaField()).thenReturn(OWLClassM.getWithConverterField()); - when(mWithConverterAtt.getName()).thenReturn(OWLClassM.getWithConverterField().getName()); - when(mWithConverterAtt.getJavaType()).thenReturn(OWLClassM.getWithConverterField().getType()); - when(mWithConverterAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_withConverter)); - when(mWithConverterAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(mWithConverterAtt.isCollection()).thenReturn(false); - when(mWithConverterAtt.getDeclaringType()).thenReturn(etMock); - when(mWithConverterAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(mWithConverterAtt.getConverter()).thenReturn( - new CustomConverterWrapper(new ZoneOffsetConverter(), String.class)); - when(mWithConverterAtt.isSimpleLiteral()).thenReturn(false); - when(etMock.getFieldSpecification(OWLClassM.getWithConverterField().getName())).thenReturn(mWithConverterAtt); - when(etMock.getAttribute(OWLClassM.getWithConverterField().getName())).thenReturn(mWithConverterAtt); - - when(mObjectOneOfEnumAttribute.getJavaField()).thenReturn(OWLClassM.getObjectOneOfEnumAttributeField()); - when(mObjectOneOfEnumAttribute.getName()).thenReturn(OWLClassM.getObjectOneOfEnumAttributeField().getName()); - when(mObjectOneOfEnumAttribute.getJavaType()).thenReturn(OneOfEnum.class); - when(mObjectOneOfEnumAttribute.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_objectOneOfEnumAttribute)); - when(mObjectOneOfEnumAttribute.getPersistentAttributeType()).thenReturn( - Attribute.PersistentAttributeType.OBJECT); - when(mObjectOneOfEnumAttribute.isCollection()).thenReturn(false); - when(mObjectOneOfEnumAttribute.getDeclaringType()).thenReturn(etMock); - when(mObjectOneOfEnumAttribute.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(mObjectOneOfEnumAttribute.getConverter()).thenReturn(new ObjectOneOfEnumConverter(OneOfEnum.class)); - when(mObjectOneOfEnumAttribute.hasLanguage()).thenReturn(false); - when(etMock.getFieldSpecification(OWLClassM.getObjectOneOfEnumAttributeField().getName())).thenReturn( - mObjectOneOfEnumAttribute); - when(etMock.getAttribute(OWLClassM.getObjectOneOfEnumAttributeField().getName())).thenReturn( - mObjectOneOfEnumAttribute); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -913,44 +705,9 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu when(et.getFieldSpecifications()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt, props, idN)); when(et.getAttributes()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt)); - when(annotationAtt.getJavaField()).thenReturn(OWLClassN.getAnnotationPropertyField()); - when(annotationAtt.getJavaType()).thenReturn(OWLClassN.getAnnotationPropertyField().getType()); - when(et.getAttribute(OWLClassN.getAnnotationPropertyField().getName())).thenReturn(annotationAtt); - when(annotationAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); - when(annotationAtt.isCollection()).thenReturn(false); - when(annotationAtt.getBindableJavaType()).thenReturn(String.class); - when(annotationAtt.getIRI()).thenReturn( - IRI.create(OWLClassN.getAnnotationPropertyField().getAnnotation(OWLAnnotationProperty.class).iri())); - when(annotationAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(annotationAtt.getDeclaringType()).thenReturn(et); - when(annotationAtt.hasLanguage()).thenReturn(true); - when(annotationAtt.getLanguage()).thenReturn(Generators.LANG); - - when(annotationUriAtt.getJavaField()).thenReturn(OWLClassN.getAnnotationUriField()); - when(annotationUriAtt.getJavaType()).thenReturn(OWLClassN.getAnnotationUriField().getType()); - when(et.getAttribute(OWLClassN.getAnnotationUriField().getName())).thenReturn(annotationUriAtt); - when(annotationUriAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); - when(annotationUriAtt.isCollection()).thenReturn(false); - when(annotationUriAtt.getBindableJavaType()).thenReturn(String.class); - when(annotationUriAtt.getIRI()).thenReturn( - IRI.create(OWLClassN.getAnnotationUriField().getAnnotation(OWLAnnotationProperty.class).iri())); - when(annotationUriAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(annotationUriAtt.getDeclaringType()).thenReturn(et); - when(annotationUriAtt.hasLanguage()).thenReturn(true); - when(annotationUriAtt.getLanguage()).thenReturn(Generators.LANG); - - when(stringAtt.getJavaField()).thenReturn(OWLClassN.getStringAttributeField()); - when(stringAtt.getJavaType()).thenReturn(OWLClassN.getStringAttributeField().getType()); - when(et.getAttribute(OWLClassN.getStringAttributeField().getName())).thenReturn(stringAtt); - when(stringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(stringAtt.isCollection()).thenReturn(false); - when(stringAtt.getBindableJavaType()).thenReturn(String.class); - when(stringAtt.getIRI()).thenReturn( - IRI.create(OWLClassN.getStringAttributeField().getAnnotation(OWLDataProperty.class).iri())); - when(stringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(stringAtt.getDeclaringType()).thenReturn(et); - when(stringAtt.hasLanguage()).thenReturn(true); - when(stringAtt.getLanguage()).thenReturn(Generators.LANG); + initAttribute(et, annotationAtt, new AttributeInfo(OWLClassN.getAnnotationPropertyField(), Attribute.PersistentAttributeType.ANNOTATION)); + initAttribute(et, annotationUriAtt, new AttributeInfo(OWLClassN.getAnnotationUriField(), Attribute.PersistentAttributeType.ANNOTATION)); + initAttribute(et, stringAtt, new AttributeInfo(OWLClassN.getStringAttributeField(), Attribute.PersistentAttributeType.DATA)); when(pluralAnnotationAtt.getJavaField()).thenReturn(OWLClassN.getPluralAnnotationField()); when(pluralAnnotationAtt.getJavaType()).thenReturn(OWLClassN.getPluralAnnotationField().getType()); @@ -987,23 +744,14 @@ public static void initOWLClassOMock(IdentifiableEntityType et, Singu initIdentifier(et, idO, OWLClassO.getUriField(), false); when(et.getAttributes()).thenReturn(Collections.singleton(stringAtt)); when(et.getFieldSpecifications()).thenReturn(Set.of(stringAtt, idO)); - when(et.getFieldSpecification(stringAtt.getName())).thenReturn(stringAtt); - when(stringAtt.getJavaField()).thenReturn(OWLClassO.getStringAttributeField()); - when(stringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(stringAtt.isCollection()).thenReturn(false); - when(stringAtt.getBindableJavaType()).thenReturn(String.class); - when(stringAtt.getIRI()) - .thenReturn(IRI.create(OWLClassO.getStringAttributeField().getAnnotation(OWLDataProperty.class).iri())); - when(stringAtt.getDeclaringType()).thenReturn(et); - when(stringAtt.hasLanguage()).thenReturn(true); - when(stringAtt.getLanguage()).thenReturn(Generators.LANG); + initAttribute(et, stringAtt, new AttributeInfo(OWLClassO.getStringAttributeField(), Attribute.PersistentAttributeType.DATA)); when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(et.getFieldSpecification(anyString())).thenThrow(IllegalArgumentException.class); } public static void initOWLClassPMock(IdentifiableEntityType et, TypesSpecification types, PropertiesSpecification props, - SingularAttribute uriAtt, PluralAttribute urlsAtt, + SingularAttributeImpl uriAtt, PluralAttribute urlsAtt, ListAttribute simpleListAtt, ListAttribute refListAtt, Identifier idP) throws Exception { when(et.getJavaType()).thenReturn(OWLClassP.class); @@ -1027,16 +775,7 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types when(types.getDeclaringType()).thenReturn(et); when(types.getJavaType()).thenReturn(Set.class); when(types.getElementType()).thenReturn(URI.class); - when(et.getFieldSpecification(uriAtt.getName())).thenReturn(uriAtt); - when(uriAtt.getName()).thenReturn(OWLClassP.getIndividualUriField().getName()); - when(uriAtt.getJavaField()).thenReturn(OWLClassP.getIndividualUriField()); - when(uriAtt.getDeclaringType()).thenReturn(et); - when(uriAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(uriAtt.isCollection()).thenReturn(false); - when(uriAtt.getIRI()) - .thenReturn(IRI.create(OWLClassP.getIndividualUriField().getAnnotation(OWLObjectProperty.class).iri())); - when(uriAtt.getBindableJavaType()).thenReturn(URI.class); - when(uriAtt.getJavaType()).thenReturn(OWLClassP.getIndividualUriField().getType()); + initAttribute(et, uriAtt, new AttributeInfo(OWLClassP.getIndividualUriField(), Attribute.PersistentAttributeType.OBJECT)); when(et.getFieldSpecification(urlsAtt.getName())).thenReturn(urlsAtt); when(urlsAtt.getName()).thenReturn(OWLClassP.getIndividualUrlsField().getName()); when(urlsAtt.getJavaField()).thenReturn(OWLClassP.getIndividualUrlsField()); @@ -1103,62 +842,10 @@ public static void initOwlClassQMock(IdentifiableEntityType et, when(et.getFieldSpecifications()).thenReturn(Set.of(qStringAtt, qParentStringAtt, qLabelAtt, qOwlClassAAtt, idQ)); when(et.getAttributes()).thenReturn(Set.of(qStringAtt, qParentStringAtt, qLabelAtt, qOwlClassAAtt)); - when(qStringAtt.getJavaField()).thenReturn(OWLClassQ.getStringAttributeField()); - when(qStringAtt.getJavaType()).thenReturn(OWLClassQ.getStringAttributeField().getType()); - when(qStringAtt.getName()).thenReturn(OWLClassQ.getStringAttributeField().getName()); - when(et.getAttribute(OWLClassQ.getStringAttributeField().getName())).thenReturn(qStringAtt); - when(qStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(qStringAtt.isCollection()).thenReturn(false); - when(qStringAtt.getBindableJavaType()).thenReturn(String.class); - when(qStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassQ.getStringAttributeField().getAnnotation(OWLDataProperty.class).iri())); - when(qStringAtt.getDeclaringType()).thenReturn(et); - when(qStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(qStringAtt.hasLanguage()).thenReturn(true); - when(qStringAtt.getLanguage()).thenReturn(Generators.LANG); - when(et.getFieldSpecification(qStringAtt.getName())).thenReturn(qStringAtt); - - when(qParentStringAtt.getJavaField()).thenReturn(OWLClassQ.getParentStringField()); - when(qParentStringAtt.getJavaType()).thenReturn(OWLClassQ.getParentStringField().getType()); - when(qParentStringAtt.getName()).thenReturn(OWLClassQ.getParentStringField().getName()); - when(et.getAttribute(OWLClassQ.getParentStringField().getName())).thenReturn(qParentStringAtt); - when(qParentStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(qParentStringAtt.isCollection()).thenReturn(false); - when(qParentStringAtt.getBindableJavaType()).thenReturn(String.class); - when(qParentStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassQ.getParentStringField().getAnnotation(OWLDataProperty.class).iri())); - when(qParentStringAtt.getDeclaringType()).thenReturn(superclassType); - when(qParentStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(qParentStringAtt.hasLanguage()).thenReturn(true); - when(qParentStringAtt.getLanguage()).thenReturn(Generators.LANG); - when(et.getFieldSpecification(qParentStringAtt.getName())).thenReturn(qParentStringAtt); - - when(qLabelAtt.getJavaField()).thenReturn(OWLClassQ.getLabelField()); - when(qLabelAtt.getJavaType()).thenReturn(OWLClassQ.getLabelField().getType()); - when(qLabelAtt.getName()).thenReturn(OWLClassQ.getLabelField().getName()); - when(et.getAttribute(OWLClassQ.getLabelField().getName())).thenReturn(qLabelAtt); - when(qLabelAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); - when(qLabelAtt.isCollection()).thenReturn(false); - when(qLabelAtt.getBindableJavaType()).thenReturn(String.class); - when(qLabelAtt.getIRI()).thenReturn( - IRI.create(OWLClassQ.getLabelField().getAnnotation(OWLAnnotationProperty.class).iri())); - when(qLabelAtt.getDeclaringType()).thenReturn(superclassType); - when(qLabelAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(qLabelAtt.hasLanguage()).thenReturn(true); - when(qLabelAtt.getLanguage()).thenReturn(Generators.LANG); - when(et.getFieldSpecification(qLabelAtt.getName())).thenReturn(qLabelAtt); - - when(qOwlClassAAtt.getIRI()) - .thenReturn(IRI.create(OWLClassQ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri())); - when(qOwlClassAAtt.getJavaType()).thenReturn(OWLClassA.class); - when(qOwlClassAAtt.getJavaField()).thenReturn(OWLClassQ.getOwlClassAField()); - when(qOwlClassAAtt.getName()).thenReturn(OWLClassQ.getOwlClassAField().getName()); - when(qOwlClassAAtt.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(qOwlClassAAtt.getFetchType()).thenReturn(FetchType.EAGER); - when(qOwlClassAAtt.getDeclaringType()).thenReturn(superclassType); - when(qOwlClassAAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(qOwlClassAAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(et.getFieldSpecification(qOwlClassAAtt.getName())).thenReturn(qOwlClassAAtt); + initAttribute(et, qStringAtt, new AttributeInfo(OWLClassQ.getStringAttributeField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(et, qParentStringAtt, new AttributeInfo(OWLClassQ.getParentStringField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(et, qLabelAtt, new AttributeInfo(OWLClassQ.getLabelField(), Attribute.PersistentAttributeType.ANNOTATION)); + initAttribute(et, qOwlClassAAtt, new AttributeInfo(OWLClassQ.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT)); when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(et.getDeclaredAttribute(anyString())).thenAnswer(arg -> { if (Objects.equals(arg.getArgument(0), qStringAtt.getName())) { @@ -1193,20 +880,7 @@ public static void initOwlClassSMock(IdentifiableEntityType et, Singu when(et.getTypes()).thenReturn(sTypes); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(sNameAtt.getJavaField()).thenReturn(OWLClassS.getNameField()); - when(sNameAtt.getJavaType()).thenReturn(OWLClassS.getNameField().getType()); - when(sNameAtt.getName()).thenReturn(OWLClassS.getNameField().getName()); - when(et.getAttribute(OWLClassS.getNameField().getName())).thenReturn(sNameAtt); - when(sNameAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(sNameAtt.isCollection()).thenReturn(false); - when(sNameAtt.getBindableJavaType()).thenReturn(String.class); - when(sNameAtt.getIRI()).thenReturn( - IRI.create(OWLClassS.getNameField().getAnnotation(OWLAnnotationProperty.class).iri())); - when(sNameAtt.getDeclaringType()).thenReturn(et); - when(sNameAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(sNameAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(sNameAtt.hasLanguage()).thenReturn(true); - when(sNameAtt.getLanguage()).thenReturn(Generators.LANG); + initAttribute(et, sNameAtt, new AttributeInfo(OWLClassS.getNameField(), Attribute.PersistentAttributeType.ANNOTATION)); when(sTypes.getJavaField()).thenReturn(OWLClassS.getTypesField()); when(sTypes.getName()).thenReturn(OWLClassS.getTypesField().getName()); when(sTypes.getDeclaringType()).thenReturn(et); @@ -1241,33 +915,9 @@ static void initOwlClassRMock(IdentifiableEntityType et, SingularAttr when(parentEt.getSubtypes()).thenReturn(Collections.singleton(et)); when(parentEt.hasSubtypes()).thenReturn(true); - when(rStringAtt.getJavaField()).thenReturn(OWLClassR.getStringAttField()); - when(rStringAtt.getJavaType()).thenReturn(OWLClassR.getStringAttField().getType()); - when(rStringAtt.getName()).thenReturn(OWLClassR.getStringAttField().getName()); - when(et.getAttribute(OWLClassR.getStringAttField().getName())).thenReturn(rStringAtt); - when(rStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(rStringAtt.isCollection()).thenReturn(false); - when(rStringAtt.getBindableJavaType()).thenReturn(String.class); - when(rStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassR.getStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(rStringAtt.getDeclaringType()).thenReturn(et); - when(rStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(rStringAtt.hasLanguage()).thenReturn(true); - when(rStringAtt.getLanguage()).thenReturn(Generators.LANG); - when(et.getFieldSpecification(rStringAtt.getName())).thenReturn(rStringAtt); - - when(owlClassAAtt.getIRI()) - .thenReturn(IRI.create(OWLClassR.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri())); - when(owlClassAAtt.getJavaType()).thenReturn(OWLClassA.class); - when(owlClassAAtt.getJavaField()).thenReturn(OWLClassR.getOwlClassAField()); - when(owlClassAAtt.getName()).thenReturn(OWLClassR.getOwlClassAField().getName()); - when(owlClassAAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(owlClassAAtt.getFetchType()).thenReturn(FetchType.EAGER); - when(owlClassAAtt.getDeclaringType()).thenReturn(et); - when(owlClassAAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(owlClassAAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(owlClassAAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(et.getFieldSpecification(owlClassAAtt.getName())).thenReturn(owlClassAAtt); + initAttribute(et, rStringAtt, new AttributeInfo(OWLClassR.getStringAttField(), Attribute.PersistentAttributeType.DATA)); + initAttribute(et, owlClassAAtt, new AttributeInfo(OWLClassR.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT)); + for (Attribute att : parentEt.getAttributes()) { when(et.getAttribute(att.getName())).thenReturn((AbstractAttribute) att); } @@ -1336,6 +986,7 @@ static void initOwlClassRListeners(IdentifiableEntityType etR, Identi static void initOwlClassTMock(IdentifiableEntityType et, SingularAttributeImpl localDateAtt, SingularAttributeImpl localDateTimeAtt, SingularAttributeImpl owlClassSAtt, + IdentifiableEntityType etS, Identifier id) throws Exception { when(et.getJavaType()).thenReturn(OWLClassT.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassT.class)); @@ -1346,46 +997,12 @@ static void initOwlClassTMock(IdentifiableEntityType et, SingularAttr when(et.getAttributes()).thenReturn(Set.of(localDateAtt, localDateTimeAtt)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(localDateAtt.getJavaField()).thenReturn(OWLClassT.getLocalDateField()); - when(localDateAtt.getJavaType()).thenReturn(OWLClassT.getLocalDateField().getType()); - when(localDateAtt.getName()).thenReturn(OWLClassT.getLocalDateField().getName()); - when(et.getAttribute(OWLClassT.getLocalDateField().getName())).thenReturn(localDateAtt); - when(localDateAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(localDateAtt.isCollection()).thenReturn(false); - when(localDateAtt.getBindableJavaType()).thenReturn(LocalDate.class); - when(localDateAtt.getIRI()).thenReturn( - IRI.create(OWLClassT.getLocalDateField().getAnnotation(OWLDataProperty.class).iri())); - when(localDateAtt.getDeclaringType()).thenReturn(et); - when(localDateAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(localDateAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(localDateAtt.getConverter()).thenReturn(DefaultConverterWrapper.INSTANCE); - - when(localDateTimeAtt.getJavaField()).thenReturn(OWLClassT.getLocalDateTimeField()); - when(localDateTimeAtt.getJavaType()).thenReturn(OWLClassT.getLocalDateTimeField().getType()); - when(localDateTimeAtt.getName()).thenReturn(OWLClassT.getLocalDateTimeField().getName()); - when(et.getAttribute(OWLClassT.getLocalDateTimeField().getName())).thenReturn(localDateTimeAtt); - when(localDateTimeAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(localDateTimeAtt.isCollection()).thenReturn(false); - when(localDateTimeAtt.getBindableJavaType()).thenReturn(LocalDateTime.class); - when(localDateTimeAtt.getIRI()).thenReturn( - IRI.create(OWLClassT.getLocalDateTimeField().getAnnotation(OWLDataProperty.class).iri())); - when(localDateTimeAtt.getDeclaringType()).thenReturn(et); - when(localDateTimeAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(localDateTimeAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(localDateTimeAtt.getConverter()).thenReturn(new LocalDateTimeConverter()); - - when(owlClassSAtt.getJavaField()).thenReturn(OWLClassT.getOwlClassSField()); - when(owlClassSAtt.getJavaType()).thenReturn(OWLClassT.getOwlClassSField().getType()); - when(owlClassSAtt.getName()).thenReturn(OWLClassT.getOwlClassSField().getName()); - when(et.getAttribute(OWLClassT.getOwlClassSField().getName())).thenReturn(owlClassSAtt); - when(owlClassSAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(owlClassSAtt.isCollection()).thenReturn(false); - when(owlClassSAtt.getBindableJavaType()).thenReturn(OWLClassS.class); - when(owlClassSAtt.getIRI()).thenReturn( - IRI.create(OWLClassT.getOwlClassSField().getAnnotation(OWLObjectProperty.class).iri())); - when(owlClassSAtt.getDeclaringType()).thenReturn(et); - when(owlClassSAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(owlClassSAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); + initAttribute(et, localDateAtt, new AttributeInfo(OWLClassT.getLocalDateField(), Attribute.PersistentAttributeType.DATA) + .converter(DefaultConverterWrapper.INSTANCE)); + initAttribute(et, localDateTimeAtt, new AttributeInfo(OWLClassT.getLocalDateTimeField(), Attribute.PersistentAttributeType.DATA) + .converter(new LocalDateTimeConverter())); + initAttribute(et, owlClassSAtt, new AttributeInfo(OWLClassT.getOwlClassSField(), Attribute.PersistentAttributeType.OBJECT) + .valueType(etS)); } static void initOwlClassUMocks(IdentifiableEntityType et, SingularAttributeImpl singularStringAtt, @@ -1400,21 +1017,9 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt when(et.getAttributes()).thenReturn(Set.of(singularStringAtt, pluralStringAtt, modified)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(singularStringAtt.getJavaField()).thenReturn(OWLClassU.getSingularStringAttField()); - when(singularStringAtt.getJavaType()).thenReturn(OWLClassU.getSingularStringAttField().getType()); - when(singularStringAtt.getName()).thenReturn(OWLClassU.getSingularStringAttField().getName()); - when(et.getAttribute(OWLClassU.getSingularStringAttField().getName())).thenReturn(singularStringAtt); - when(et.getFieldSpecification(OWLClassU.getSingularStringAttField().getName())).thenReturn(singularStringAtt); - when(singularStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(singularStringAtt.isCollection()).thenReturn(false); - when(singularStringAtt.getBindableJavaType()).thenReturn(MultilingualString.class); - when(singularStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassU.getSingularStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(singularStringAtt.getDeclaringType()).thenReturn(et); - when(singularStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(singularStringAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(singularStringAtt.hasLanguage()).thenReturn(false); - when(singularStringAtt.getLanguage()).thenReturn(null); + initAttribute(et, singularStringAtt, new AttributeInfo(OWLClassU.getSingularStringAttField(), Attribute.PersistentAttributeType.DATA).language(null)); + initAttribute(et, modified, new AttributeInfo(OWLClassU.getModifiedField(), Attribute.PersistentAttributeType.DATA).converter(new LocalDateTimeConverter()) + .language(null)); when(pluralStringAtt.getJavaField()).thenReturn(OWLClassU.getPluralStringAttField()); when(pluralStringAtt.getJavaType()).thenReturn(OWLClassU.getPluralStringAttField().getType()); @@ -1433,21 +1038,6 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt when(pluralStringAtt.hasLanguage()).thenReturn(false); when(pluralStringAtt.getLanguage()).thenReturn(null); - when(modified.getJavaField()).thenReturn(OWLClassU.getModifiedField()); - when(modified.getJavaType()).thenReturn(OWLClassU.getModifiedField().getType()); - when(modified.getName()).thenReturn(OWLClassU.getModifiedField().getName()); - when(et.getAttribute(OWLClassU.getModifiedField().getName())).thenReturn(modified); - when(et.getFieldSpecification(OWLClassU.getModifiedField().getName())).thenReturn(modified); - when(modified.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(modified.isCollection()).thenReturn(false); - when(modified.getBindableJavaType()).thenReturn(LocalDateTime.class); - when(modified.getIRI()).thenReturn(IRI.create(DC.Terms.MODIFIED)); - when(modified.getDeclaringType()).thenReturn(et); - when(modified.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(modified.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(modified.hasLanguage()).thenReturn(false); - when(modified.getLanguage()).thenReturn(null); - final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassU.class.getDeclaredMethod("preUpdate")); when(et.getLifecycleListenerManager()).thenReturn(listenerManager); @@ -1541,9 +1131,10 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu } static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType etMock, - AbstractQueryAttribute strQueryAttMock, AbstractAttribute strAttMock, + AbstractQueryAttribute strQueryAttMock, SingularAttributeImpl strAttMock, AbstractQueryAttribute entityQueryAttMock, - AbstractAttribute entityAttMock, + SingularAttributeImpl entityAttMock, + IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException { when(etMock.getJavaType()).thenReturn(OWLClassWithQueryAttr.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassWithQueryAttr.class)); @@ -1557,31 +1148,10 @@ static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType etMock, AbstractAttribute phoneNumberAttMock, + public static void initPhoneMocks(IdentifiableEntityType etMock, SingularAttributeImpl phoneNumberAttMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(Phone.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(Phone.class)); when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(etMock.getIRI()).thenReturn(IRI.create(Vocabulary.c_Phone)); when(etMock.getName()).thenReturn(Phone.class.getSimpleName()); - when(etMock.getAttribute(Phone.class.getDeclaredField("number").getName())).thenReturn(phoneNumberAttMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(phoneNumberAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(phoneNumberAttMock, idMock)); - when(phoneNumberAttMock.getJavaField()).thenReturn(Phone.class.getDeclaredField("number")); - when(phoneNumberAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_phoneNumber)); - when(phoneNumberAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(phoneNumberAttMock.getDeclaringType()).thenReturn(etMock); - when(phoneNumberAttMock.hasLanguage()).thenReturn(false); - when(phoneNumberAttMock.getName()).thenReturn(Phone.class.getDeclaredField("number").getName()); - when(etMock.getAttribute(Phone.class.getDeclaredField("number").getName())).thenReturn(phoneNumberAttMock); + initAttribute(etMock, phoneNumberAttMock, new AttributeInfo(Phone.class.getDeclaredField("number"), Attribute.PersistentAttributeType.DATA)); initIdentifier(etMock, idMock, Phone.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } - public static void initPersonMocks(IdentifiableEntityType etMock, AbstractAttribute usernameAttMock, - AbstractAttribute genderAttMock, - AbstractAttribute ageAttMock, SingularAttributeImpl phoneAttMock, + public static void initPersonMocks(IdentifiableEntityType etMock, SingularAttributeImpl usernameAttMock, + SingularAttributeImpl genderAttMock, + SingularAttributeImpl ageAttMock, SingularAttributeImpl phoneAttMock, AbstractIdentifiableType etPhone, TypesSpecification typesMock, Identifier idMock) throws NoSuchFieldException, SecurityException { @@ -1641,38 +1204,15 @@ public static void initPersonMocks(IdentifiableEntityType etMock, Abstra initIdentifier(etMock, idMock, Person.class.getDeclaredField("uri"), false); when(etMock.getAttributes()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock, idMock)); - when(usernameAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("username")); - when(usernameAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_username)); - when(usernameAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(usernameAttMock.getDeclaringType()).thenReturn(etMock); - when(usernameAttMock.hasLanguage()).thenReturn(false); - when(usernameAttMock.getName()).thenReturn(Person.class.getDeclaredField("username").getName()); - when(etMock.getAttribute(Person.class.getDeclaredField("username").getName())).thenReturn(usernameAttMock); - - when(genderAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("gender")); - when(genderAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_gender)); - when(genderAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(genderAttMock.getDeclaringType()).thenReturn(etMock); - when(genderAttMock.hasLanguage()).thenReturn(false); - when(genderAttMock.getName()).thenReturn(Person.class.getDeclaredField("gender").getName()); - when(etMock.getAttribute(Person.class.getDeclaredField("gender").getName())).thenReturn(genderAttMock); - - when(ageAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("age")); - when(ageAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_age)); - when(ageAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(ageAttMock.getDeclaringType()).thenReturn(etMock); - when(ageAttMock.hasLanguage()).thenReturn(false); - when(ageAttMock.getName()).thenReturn(Person.class.getDeclaredField("age").getName()); - when(etMock.getAttribute(Person.class.getDeclaredField("age").getName())).thenReturn(ageAttMock); - - when(phoneAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("phone")); - when(phoneAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_hasPhone)); - when(phoneAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(phoneAttMock.getDeclaringType()).thenReturn(etMock); - when(phoneAttMock.hasLanguage()).thenReturn(false); - when(phoneAttMock.getName()).thenReturn(Person.class.getDeclaredField("phone").getName()); - when(phoneAttMock.getType()).thenReturn(etPhone); - when(etMock.getAttribute(Person.class.getDeclaredField("phone").getName())).thenReturn(phoneAttMock); + + initAttribute(etMock, usernameAttMock, new AttributeInfo(Person.class.getDeclaredField("username"), + Attribute.PersistentAttributeType.DATA).language(null)); + initAttribute(etMock, genderAttMock, new AttributeInfo(Person.class.getDeclaredField("gender"), + Attribute.PersistentAttributeType.DATA).language(null)); + initAttribute(etMock, ageAttMock, new AttributeInfo(Person.class.getDeclaredField("age"), + Attribute.PersistentAttributeType.DATA).language(null)); + initAttribute(etMock, phoneAttMock, new AttributeInfo(Person.class.getDeclaredField("phone"), + Attribute.PersistentAttributeType.OBJECT).valueType(etPhone)); when(typesMock.getJavaField()).thenReturn(Person.class.getDeclaredField("types")); when(typesMock.getName()).thenReturn(Person.class.getDeclaredField("types").getName()); @@ -1684,4 +1224,44 @@ public static void initPersonMocks(IdentifiableEntityType etMock, Abstra when(etMock.getTypes()).thenReturn(typesMock); when(etMock.getFieldSpecification("types")).thenReturn(typesMock); } + + private static class AttributeInfo { + private final Field field; + private final Attribute.PersistentAttributeType type; + private ParticipationConstraint[] constraints = new ParticipationConstraint[0]; + private boolean nonEmpty; + private String language = Generators.LANG; + private AbstractIdentifiableType valueType; + private ConverterWrapper converter; + + private AttributeInfo(Field field, Attribute.PersistentAttributeType type) { + this.field = field; + this.type = type; + } + + private AttributeInfo constraints(ParticipationConstraint... constraints) { + this.constraints = constraints; + return this; + } + + private AttributeInfo nonEmpty() { + this.nonEmpty = true; + return this; + } + + private AttributeInfo language(String language) { + this.language = language; + return this; + } + + private AttributeInfo valueType(AbstractIdentifiableType valueType) { + this.valueType = valueType; + return this; + } + + private AttributeInfo converter(ConverterWrapper converter) { + this.converter = converter; + return this; + } + } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java index b5a037f1b..c33965589 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java @@ -386,10 +386,10 @@ public MetamodelMocks() throws Exception { MetamodelFactory.initOWLClassFMocks(etF, fSetAtt, fStringAtt, idF); MetamodelFactory.initOWLClassGMocks(etG, gOwlClassHAtt, etH, idG); MetamodelFactory.initOWLClassHMocks(etH, hOwlClassAAtt, hOwlClassGAtt, etA, etG, idH); - MetamodelFactory.initOWLClassIMocks(etI, iOwlClassAAtt, idI); + MetamodelFactory.initOWLClassIMocks(etI, iOwlClassAAtt, etA, idI); MetamodelFactory.initOWLClassJMocks(etJ, jSetAtt, idJ); - MetamodelFactory.initOWLClassKMocks(etK, kOwlClassEAtt, idK); - MetamodelFactory.initOWLClassLMocks(etL, lReferencedList, lSimpleList, lSetAtt, lOwlClassAAtt, idL); + MetamodelFactory.initOWLClassKMocks(etK, kOwlClassEAtt, etE, idK); + MetamodelFactory.initOWLClassLMocks(etL, lReferencedList, lSimpleList, lSetAtt, lOwlClassAAtt, etA, idL); MetamodelFactory.initOWLClassMMock(etM, mBooleanAtt, mIntegerAtt, mLongAtt, mDoubleAtt, mDateAtt, mCharacterAtt, mEnumAtt, mOrdinalEnumAtt, mIntegerSetAtt, mLexicalFormAtt, mSimpleLiteralAtt, mExplicitDatatypeAtt, mWithConverterAtt, mObjectOneOfEnumAttribute, idM); @@ -406,11 +406,11 @@ public MetamodelMocks() throws Exception { MetamodelFactory.initOwlClassSListeners(etS, parentListenerMock); MetamodelFactory.initOwlClassRMock(etR, rStringAtt, rOwlClassAAtt, etS); MetamodelFactory.initOwlClassRListeners(etR, etS, concreteListenerMock, anotherListenerMock); - MetamodelFactory.initOwlClassTMock(etT, tLocalDateAtt, tLocalDateTimeAtt, tOwlClassSAtt, idT); + MetamodelFactory.initOwlClassTMock(etT, tLocalDateAtt, tLocalDateTimeAtt, tOwlClassSAtt, etS, idT); MetamodelFactory.initOwlClassUMocks(etU, uSingularStringAtt, uPluralStringAtt, uModified, idU); MetamodelFactory.initOwlClassWMocks(etW, wSetStringAtt, wListStringAtt, wCollectionStringAtt, wSetQueryStringAtt, wListQueryStringAtt, idW); MetamodelFactory.initOWLClassWithQueryAttrMocks(etQA, qaStringQueryAtt, qaStringAtt, qaEntityQueryAtt, - qaEntityAtt, idQA); + qaEntityAtt, etA, idQA); MetamodelFactory.initPhoneMocks(etPhone, phoneNumberAtt, idPhone); MetamodelFactory.initPersonMocks(etPerson, personUsernameAtt, personGenderAtt, personAgeAtt, personPhoneAtt, etPhone, personTypes, idPerson); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java index d24aeac0e..fce30046e 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java @@ -17,7 +17,14 @@ */ package cz.cvut.kbss.jopa.oom; -import cz.cvut.kbss.jopa.environment.*; +import cz.cvut.kbss.jopa.environment.OWLClassA; +import cz.cvut.kbss.jopa.environment.OWLClassB; +import cz.cvut.kbss.jopa.environment.OWLClassD; +import cz.cvut.kbss.jopa.environment.OWLClassE; +import cz.cvut.kbss.jopa.environment.OWLClassM; +import cz.cvut.kbss.jopa.environment.OWLClassN; +import cz.cvut.kbss.jopa.environment.OWLClassQ; +import cz.cvut.kbss.jopa.environment.Vocabulary; import cz.cvut.kbss.jopa.environment.utils.Generators; import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks; import cz.cvut.kbss.jopa.model.annotations.Inferred; @@ -36,18 +43,33 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import java.lang.reflect.Field; import java.net.URI; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) class EntityDeconstructorTest { private static final URI CONTEXT = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/contextOne"); @@ -89,7 +111,6 @@ static void setUpBeforeClass() throws Exception { @BeforeEach void setUp() throws Exception { - MockitoAnnotations.openMocks(this); this.mocks = new MetamodelMocks(); when(oomMock.getEntityType(OWLClassA.class)).thenReturn(mocks.forOwlClassA().entityType()); when(oomMock.getConfiguration()).thenReturn(new Configuration(Collections.emptyMap())); @@ -121,7 +142,7 @@ void testMapEntityWithDataPropertyNullValue() throws Exception { entity.setUri(entityA.getUri()); final Descriptor aDescriptor = new EntityDescriptor(); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entity.getUri(), entity, - mocks.forOwlClassA().entityType(), aDescriptor); + mocks.forOwlClassA().entityType(), aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertNotNull(res); assertEquals(entity.getUri(), res.getSubject().getIdentifier()); @@ -139,8 +160,8 @@ void testMapEntityWithDataPropertyAndTypes() throws Exception { final Set types = createTypes(); entityA.setTypes(types); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityA.getUri(), - entityA, mocks.forOwlClassA().entityType(), - aDescriptor); + entityA, mocks.forOwlClassA().entityType(), + aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertNotNull(res); assertEquals(entityA.getUri(), res.getSubject().getIdentifier()); @@ -177,8 +198,8 @@ void testMapEntityWithDataPropertyAndTypesPropertyInDifferentContext() throws Ex final Set types = createTypes(); entityA.setTypes(types); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityA.getUri(), - entityA, mocks.forOwlClassA().entityType(), - aDescriptor); + entityA, mocks.forOwlClassA().entityType(), + aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertNotNull(res); assertEquals(entityA.getUri(), res.getSubject().getIdentifier()); @@ -217,7 +238,7 @@ void testMapEntityWithObjectPropertyNullValue() throws Exception { entity.setUri(entityD.getUri()); final Descriptor dDescriptor = new EntityDescriptor(); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entity.getUri(), entity, - mocks.forOwlClassD().entityType(), dDescriptor); + mocks.forOwlClassD().entityType(), dDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertNotNull(res); assertEquals(entity.getUri(), res.getSubject().getIdentifier()); @@ -234,8 +255,8 @@ void testMapEntityWithObjectPropertyNullValue() throws Exception { void testMapEntityWithObjectPropertyAndContext() throws Exception { final Descriptor dDescriptor = new EntityDescriptor(CONTEXT); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityD.getUri(), - entityD, mocks.forOwlClassD().entityType(), - dDescriptor); + entityD, mocks.forOwlClassD().entityType(), + dDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertNotNull(res); assertEquals(entityD.getUri(), res.getSubject().getIdentifier()); @@ -251,8 +272,8 @@ void testMapEntityWithProperties() throws Exception { final Map> props = Generators.generateStringProperties(); entityB.setProperties(props); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityB.getUri(), - entityB, mocks.forOwlClassB().entityType(), - bDescriptor); + entityB, mocks.forOwlClassB().entityType(), + bDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(entityB.getUri(), res.getSubject().getIdentifier()); // Class assertion, data property assertion and the properties @@ -276,8 +297,8 @@ private void verifyPropertiesForSave(Map> props, AxiomValueG void testMapEntityWithNullProperties() throws Exception { final Descriptor bDescriptor = new EntityDescriptor(); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityB.getUri(), - entityB, mocks.forOwlClassB().entityType(), - bDescriptor); + entityB, mocks.forOwlClassB().entityType(), + bDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(entityB.getUri(), res.getSubject().getIdentifier()); // Class assertion, data property assertion @@ -290,8 +311,8 @@ void testMapEntityWithPropertiesMultipleValuesPerProperty() throws Exception { entityB.setProperties(props); final Descriptor bDescriptor = new EntityDescriptor(); final AxiomValueGatherer builder = sut.mapEntityToAxioms(entityB.getUri(), - entityB, mocks.forOwlClassB().entityType(), - bDescriptor); + entityB, mocks.forOwlClassB().entityType(), + bDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(entityB.getUri(), res.getSubject().getIdentifier()); assertEquals(2, res.getAssertions().size()); @@ -315,8 +336,8 @@ private Map> createProperties() { void mapsEntityDataPropertyFieldToAxiomDescriptor() throws Exception { final Descriptor aDescriptor = new EntityDescriptor(); final AxiomValueGatherer builder = sut.mapFieldToAxioms(entityA.getUri(), entityA, - mocks.forOwlClassA().stringAttribute(), - mocks.forOwlClassA().entityType(), aDescriptor); + mocks.forOwlClassA().stringAttribute(), + mocks.forOwlClassA().entityType(), aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); assertTrue(containsDPAssertion(res, OWLClassA.getStrAttField(), entityA.getStringAttribute())); @@ -327,14 +348,14 @@ void mapsEntityDataPropertyWithNullValueToAxiomDescriptor() throws Exception { final Descriptor aDescriptor = new EntityDescriptor(); entityA.setStringAttribute(null); final AxiomValueGatherer builder = sut.mapFieldToAxioms(entityA.getUri(), entityA, - mocks.forOwlClassA().stringAttribute(), - mocks.forOwlClassA().entityType(), aDescriptor); + mocks.forOwlClassA().stringAttribute(), + mocks.forOwlClassA().entityType(), aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); assertTrue(res.getAssertions().contains( Assertion.createDataPropertyAssertion(strAttAIdentifier, - mocks.forOwlClassA().stringAttribute().getLanguage(), - mocks.forOwlClassA().stringAttribute().isInferred()))); + mocks.forOwlClassA().stringAttribute().getLanguage(), + mocks.forOwlClassA().stringAttribute().isInferred()))); final List> val = res.getAssertionValues(res.getAssertions().iterator().next()); assertEquals(1, val.size()); assertEquals(Value.nullValue(), val.get(0)); @@ -347,12 +368,12 @@ void mapsEntityObjectPropertyValueInContextToAxiomDescriptor() throws Exception dDescriptor.addAttributeContext(mocks.forOwlClassD().owlClassAAtt(), CONTEXT); when(oomMock.isManaged(entityD.getOwlClassA())).thenReturn(true); final AxiomValueGatherer builder = sut.mapFieldToAxioms(entityD.getUri(), entityD, - mocks.forOwlClassD().owlClassAAtt(), - mocks.forOwlClassD().entityType(), dDescriptor); + mocks.forOwlClassD().owlClassAAtt(), + mocks.forOwlClassD().entityType(), dDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); final Assertion ass = Assertion.createObjectPropertyAssertion(owlClassAAttIdentifier, - mocks.forOwlClassD().owlClassAAtt().isInferred()); + mocks.forOwlClassD().owlClassAAtt().isInferred()); assertEquals(CONTEXT, res.getAssertionContext(ass)); assertTrue( containsOPAssertion(res, OWLClassD.getOwlClassAField(), NamedResource.create(entityA.getUri()))); @@ -361,10 +382,8 @@ void mapsEntityObjectPropertyValueInContextToAxiomDescriptor() throws Exception @Test void mapsEntityWithStringKeyAndBasicDataAttributes() throws Exception { final Descriptor desc = new EntityDescriptor(); - final AxiomValueGatherer builder = - sut - .mapEntityToAxioms(URI.create(entityM.getKey()), entityM, mocks.forOwlClassM().entityType(), - desc); + final AxiomValueGatherer builder = sut.mapEntityToAxioms(URI.create(entityM.getKey()), entityM, mocks.forOwlClassM() + .entityType(), desc); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertTrue(containsInstanceClassAssertion(res, OWLClassM.getClassIri())); assertTrue(containsDPAssertion(res, OWLClassM.getBooleanAttributeField(), entityM.getBooleanAttribute())); @@ -372,7 +391,8 @@ void mapsEntityWithStringKeyAndBasicDataAttributes() throws Exception { assertTrue(containsDPAssertion(res, OWLClassM.getDoubleAttributeField(), entityM.getDoubleAttribute())); assertTrue(containsDPAssertion(res, OWLClassM.getLongAttributeField(), entityM.getLongAttribute())); assertTrue(containsDPAssertion(res, OWLClassM.getDateAttributeField(), entityM.getDateAttribute())); - assertTrue(containsDPAssertion(res, OWLClassM.getCharacterAttributeField(), entityM.getCharacterAttribute().toString())); + assertTrue(containsDPAssertion(res, OWLClassM.getCharacterAttributeField(), entityM.getCharacterAttribute() + .toString())); } private boolean containsInstanceClassAssertion(AxiomValueDescriptor descriptor, String classIri) { @@ -384,9 +404,15 @@ private boolean containsInstanceClassAssertion(AxiomValueDescriptor descriptor, private boolean containsDPAssertion(AxiomValueDescriptor descriptor, Field attributeField, Object value) { final OWLDataProperty annotation = attributeField.getAnnotation(OWLDataProperty.class); final URI propertyUri = URI.create(annotation.iri()); - final Assertion assertion = Assertion - .createDataPropertyAssertion(propertyUri, Generators.LANG, attributeField.getAnnotation( - Inferred.class) != null); + final Assertion assertion; + if (Character.class.isAssignableFrom(attributeField.getType())) { + assertion = Assertion + .createDataPropertyAssertion(propertyUri, attributeField.getAnnotation(Inferred.class) != null); + } else { + assertion = Assertion + .createDataPropertyAssertion(propertyUri, Generators.LANG, attributeField.getAnnotation( + Inferred.class) != null); + } return containsAssertionValue(descriptor, assertion, value); } @@ -416,7 +442,7 @@ void mapEntityToAxiomsIncludesAttributesInheritedFromMappedSuperclass() throws E assertTrue(containsDPAssertion(valueDescriptor, OWLClassQ.getParentStringField(), q.getParentString())); assertTrue(containsAPAssertion(valueDescriptor, OWLClassQ.getLabelField(), q.getLabel())); assertTrue(containsOPAssertion(valueDescriptor, OWLClassQ.getOwlClassAField(), - NamedResource.create(entityA.getUri()))); + NamedResource.create(entityA.getUri()))); } private OWLClassQ initQ() { @@ -434,7 +460,7 @@ private boolean containsAPAssertion(AxiomValueDescriptor descriptor, Field attri final URI propertyUri = URI.create(attributeField.getAnnotation(OWLAnnotationProperty.class).iri()); final Assertion assertion = Assertion .createAnnotationPropertyAssertion(propertyUri, Generators.LANG, - attributeField.getAnnotation(Inferred.class) != null); + attributeField.getAnnotation(Inferred.class) != null); return containsAssertionValue(descriptor, assertion, value); } @@ -459,12 +485,12 @@ void mapEntityToAxiomsSupportsPluralAnnotationProperty() throws Exception { Assertion.createAnnotationPropertyAssertion(URI.create(DC.Terms.SOURCE), Generators.LANG, false); assertTrue(valueDescriptor.getAssertions().contains(assertion)); assertAll(() -> assertEquals(n.getPluralAnnotation().size(), - valueDescriptor.getAssertionValues(assertion).size()), - () -> { - final List> values = - n.getPluralAnnotation().stream().map(Value::new).toList(); - assertTrue(valueDescriptor.getAssertionValues(assertion).containsAll(values)); - }); + valueDescriptor.getAssertionValues(assertion).size()), + () -> { + final List> values = + n.getPluralAnnotation().stream().map(Value::new).toList(); + assertTrue(valueDescriptor.getAssertionValues(assertion).containsAll(values)); + }); } @Test @@ -476,12 +502,12 @@ void mapsEntityObjectPropertyAssertionToSubjectContextWhenAssertionsInSubjectCon dDescriptor.addAttributeDescriptor(mocks.forOwlClassD().owlClassAAtt(), aDescriptor); when(oomMock.isManaged(entityD.getOwlClassA())).thenReturn(true); final AxiomValueGatherer builder = sut.mapFieldToAxioms(entityD.getUri(), entityD, - mocks.forOwlClassD().owlClassAAtt(), - mocks.forOwlClassD().entityType(), dDescriptor); + mocks.forOwlClassD().owlClassAAtt(), + mocks.forOwlClassD().entityType(), dDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); final Assertion ass = Assertion.createObjectPropertyAssertion(owlClassAAttIdentifier, - mocks.forOwlClassD().owlClassAAtt().isInferred()); + mocks.forOwlClassD().owlClassAAtt().isInferred()); assertEquals(subjectContext, res.getAssertionContext(ass)); } @@ -491,7 +517,7 @@ void mapFieldToAxiomsMapsSimpleLiteralFieldToAxiomWithAssertionWithoutLanguageTa entityM.setSimpleLiteral(value); final AxiomValueGatherer builder = sut .mapFieldToAxioms(URI.create(entityM.getKey()), entityM, mocks.forOwlClassM().simpleLiteralAttribute(), - mocks.forOwlClassM().entityType(), new EntityDescriptor()); + mocks.forOwlClassM().entityType(), new EntityDescriptor()); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); final Assertion a = res.getAssertions().iterator().next(); From 86c0d915e9d57cac0f2e0f689c788f7178044dba Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Fri, 20 Dec 2024 16:52:32 +0100 Subject: [PATCH 10/17] [GH-301] Extract plural attribute (except lists) setup into a reusable function. --- .../environment/utils/MetamodelFactory.java | 224 ++++++------------ .../environment/utils/MetamodelMocks.java | 4 +- .../PluralAnnotationPropertyStrategyTest.java | 34 ++- 3 files changed, 97 insertions(+), 165 deletions(-) diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index 3d5981be5..8b01eaf37 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -70,6 +70,7 @@ import cz.cvut.kbss.jopa.model.metamodel.AbstractQueryAttribute; import cz.cvut.kbss.jopa.model.metamodel.Attribute; import cz.cvut.kbss.jopa.model.metamodel.BasicTypeImpl; +import cz.cvut.kbss.jopa.model.metamodel.Bindable; import cz.cvut.kbss.jopa.model.metamodel.CollectionType; import cz.cvut.kbss.jopa.model.metamodel.EntityLifecycleListenerManager; import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification; @@ -78,7 +79,6 @@ import cz.cvut.kbss.jopa.model.metamodel.ListAttribute; import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl; import cz.cvut.kbss.jopa.model.metamodel.MappedSuperclassTypeImpl; -import cz.cvut.kbss.jopa.model.metamodel.PluralAttribute; import cz.cvut.kbss.jopa.model.metamodel.PropertiesSpecification; import cz.cvut.kbss.jopa.model.metamodel.RdfContainerAttributeImpl; import cz.cvut.kbss.jopa.model.metamodel.SingularAttribute; @@ -103,6 +103,7 @@ import java.lang.reflect.Method; import java.net.URI; import java.net.URL; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -118,7 +119,6 @@ import static cz.cvut.kbss.jopa.model.lifecycle.LifecycleEvent.PRE_UPDATE; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** @@ -193,7 +193,7 @@ private static void initIdentifier(IdentifiableEntityType et, Identifier when(et.getFieldSpecification(idField.getName())).thenReturn(id); } - private static void initAttribute(IdentifiableEntityType etMock, SingularAttributeImpl attMock, + private static void initAttribute(IdentifiableEntityType etMock, AbstractAttribute attMock, AttributeInfo attInfo) { when(etMock.getFieldSpecification(attInfo.field.getName())).thenReturn(attMock); when(etMock.getAttribute(attInfo.field.getName())).thenReturn(attMock); @@ -201,11 +201,16 @@ private static void initAttribute(IdentifiableEntityType etMock, Singular when(attMock.getDeclaringType()).thenReturn(etMock); when(attMock.getJavaField()).thenReturn(attInfo.field); when(attMock.getJavaType()).thenReturn(attInfo.field.getType()); - when(attMock.getBindableJavaType()).thenReturn(attInfo.field.getType()); when(attMock.getConstraints()).thenReturn(attInfo.constraints); when(attMock.isNonEmpty()).thenReturn(attInfo.nonEmpty); when(attMock.getPersistentAttributeType()).thenReturn(attInfo.type); when(attMock.getConverter()).thenReturn(attInfo.converter); + when(attMock.isCollection()).thenReturn(Collection.class.isAssignableFrom(attInfo.field.getType())); + if (attMock instanceof SingularAttributeImpl) { + initSingularAttribute((SingularAttributeImpl) attMock, attInfo); + } else { + initPluralAttribute((AbstractPluralAttribute) attMock, attInfo); + } switch (attInfo.type) { case DATA: final OWLDataProperty dp = attInfo.field.getAnnotation(OWLDataProperty.class); @@ -219,7 +224,6 @@ private static void initAttribute(IdentifiableEntityType etMock, Singular when(attMock.getCascadeTypes()).thenReturn(new CascadeType[0]); when(attMock.getLanguage()).thenReturn(attInfo.language); when(attMock.hasLanguage()).thenReturn(attInfo.language != null); - when(attMock.getType()).thenReturn(BasicTypeImpl.get(attInfo.field.getType())); break; case OBJECT: final OWLObjectProperty op = attInfo.field.getAnnotation(OWLObjectProperty.class); @@ -228,11 +232,6 @@ private static void initAttribute(IdentifiableEntityType etMock, Singular when(attMock.isAssociation()).thenReturn(true); when(attMock.isInferred()).thenReturn(attInfo.field.getAnnotation(Inferred.class) != null); when(attMock.getCascadeTypes()).thenReturn(op.cascade()); - if (URI.class.isAssignableFrom(attInfo.field.getType())) { - when(attMock.getType()).thenReturn(BasicTypeImpl.get(URI.class)); - } else { - when(attMock.getType()).thenReturn(attInfo.valueType); - } break; case ANNOTATION: final OWLAnnotationProperty ap = attInfo.field.getAnnotation(OWLAnnotationProperty.class); @@ -246,12 +245,22 @@ private static void initAttribute(IdentifiableEntityType etMock, Singular when(attMock.getCascadeTypes()).thenReturn(new CascadeType[0]); when(attMock.getLanguage()).thenReturn(attInfo.language); when(attMock.hasLanguage()).thenReturn(attInfo.language != null); - when(attMock.getType()).thenReturn(Objects.requireNonNullElseGet(attInfo.valueType, - () -> BasicTypeImpl.get(attInfo.field.getType()))); break; } } + private static void initSingularAttribute(SingularAttributeImpl attMock, AttributeInfo attInfo) { + when(attMock.getBindableJavaType()).thenReturn(attInfo.field.getType()); + when(attMock.getType()).thenReturn(Objects.requireNonNullElseGet(attInfo.valueType, () -> BasicTypeImpl.get(attInfo.field.getType()))); + } + + private static void initPluralAttribute(AbstractPluralAttribute attMock, AttributeInfo attInfo) { + when(attMock.getBindableJavaType()).thenReturn(attInfo.elementType); + when(attMock.getBindableType()).thenReturn(Bindable.BindableType.PLURAL_ATTRIBUTE); + when(attMock.getCollectionType()).thenReturn(attInfo.collectionType); + when(attMock.getElementType()).thenReturn(attInfo.valueType); + } + private static void addLifecycleCallback(EntityLifecycleListenerManager manager, LifecycleEvent evt, Method callback) throws Exception { final Method addCallback = EntityLifecycleListenerManager.class @@ -427,7 +436,7 @@ public static void initOWLClassEMocks(IdentifiableEntityType etMock, } public static void initOWLClassFMocks(IdentifiableEntityType etMock, AbstractPluralAttribute setAMock, - SingularAttributeImpl strAttMock, + SingularAttributeImpl strAttMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassF.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassF.class)); @@ -436,27 +445,11 @@ public static void initOWLClassFMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); when(etMock.getAttributes()).thenReturn(Set.of(setAMock, strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, strAttMock, idMock)); - when(setAMock.getJavaField()).thenReturn(OWLClassF.getSimpleSetField()); - when(setAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(setAMock.getCascadeTypes()) - .thenReturn(OWLClassF.getSimpleSetField().getAnnotation(OWLObjectProperty.class).cascade()); - final String setAIri = OWLClassF.getSimpleSetField().getAnnotation(OWLObjectProperty.class).iri(); - when(setAMock.getIRI()).thenReturn(IRI.create(setAIri)); - when(setAMock.getJavaType()).thenReturn(Set.class); - when(setAMock.getJavaField()).thenReturn(OWLClassF.getSimpleSetField()); - when(setAMock.isCollection()).thenReturn(Boolean.TRUE); - when(setAMock.getCollectionType()).thenReturn(CollectionType.SET); - when(setAMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(setAMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(setAMock.getDeclaringType()).thenReturn(etMock); - when(setAMock.getName()).thenReturn(OWLClassF.getSimpleSetField().getName()); - when(setAMock.getFetchType()).thenReturn(FetchType.LAZY); - when(etMock.getAttribute(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); - when(etMock.getFieldSpecification(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); - + initAttribute(etMock, setAMock, new AttributeInfo(OWLClassF.getSimpleSetField(), + Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.SET) + .elementType(OWLClassA.class).valueType(etAMock)); initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassF.getStrAttField(), Attribute.PersistentAttributeType.DATA) .language(Generators.LANG)); - when(etMock.getFieldSpecification(OWLClassF.getStrAttField().getName())).thenReturn(strAttMock); initIdentifier(etMock, idMock, OWLClassF.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); @@ -517,6 +510,7 @@ public static void initOWLClassIMocks(IdentifiableEntityType etMock, } public static void initOWLClassJMocks(IdentifiableEntityType etMock, AbstractPluralAttribute setAMock, + IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassJ.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassJ.class)); @@ -525,19 +519,9 @@ public static void initOWLClassJMocks(IdentifiableEntityType etMock, when(etMock.getAttribute(OWLClassJ.getOwlClassAField().getName())).thenReturn(setAMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(setAMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, idMock)); - when(setAMock.getJavaField()).thenReturn(OWLClassJ.getOwlClassAField()); - when(setAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(setAMock.getCascadeTypes()) - .thenReturn(OWLClassJ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).cascade()); - final String clsAIri = OWLClassJ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri(); - when(setAMock.getIRI()).thenReturn(IRI.create(clsAIri)); - when(setAMock.getJavaType()).thenReturn(Set.class); - when(setAMock.isCollection()).thenReturn(Boolean.TRUE); - when(setAMock.getCollectionType()).thenReturn(CollectionType.SET); - when(setAMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(setAMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(setAMock.getDeclaringType()).thenReturn(etMock); - when(etMock.getFieldSpecification(OWLClassJ.getOwlClassAField().getName())).thenReturn(setAMock); + initAttribute(etMock, setAMock, new AttributeInfo(OWLClassJ.getOwlClassAField(), + Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.SET) + .elementType(OWLClassA.class).valueType(etAMock)); initIdentifier(etMock, idMock, OWLClassJ.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -610,20 +594,9 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, when(simpleListMock.getDeclaringType()).thenReturn(etMock); when(simpleListMock.getJavaType()).thenReturn(List.class); - when(setMock.getJavaField()).thenReturn(OWLClassL.getSetField()); - when(setMock.getName()).thenReturn(OWLClassL.getSetField().getName()); - when(setMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(setMock.getIRI()).thenReturn( - IRI.create(OWLClassL.getSetField().getAnnotation(OWLObjectProperty.class).iri())); - when(setMock.getConstraints()).thenReturn( - OWLClassL.getSetField().getAnnotation(ParticipationConstraints.class).value()); - when(setMock.getCollectionType()).thenReturn(CollectionType.SET); - when(setMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(setMock.isCollection()).thenReturn(true); - when(etMock.getFieldSpecification(OWLClassL.getSetField().getName())).thenReturn(setMock); - when(etMock.getAttribute(OWLClassL.getSetField().getName())).thenReturn(setMock); - when(setMock.getDeclaringType()).thenReturn(etMock); - when(setMock.getJavaType()).thenReturn(Set.class); + initAttribute(etMock, setMock, new AttributeInfo(OWLClassL.getSetField(), + Attribute.PersistentAttributeType.OBJECT).valueType(etAMock).elementType(OWLClassA.class) + .collectionType(CollectionType.SET)); initAttribute(etMock, singleAMock, new AttributeInfo(OWLClassL.getSingleAField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etAMock).nonEmpty()); @@ -671,23 +644,10 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, S initAttribute(etMock, mWithConverterAtt, new AttributeInfo(OWLClassM.getWithConverterField(), Attribute.PersistentAttributeType.DATA).converter( new CustomConverterWrapper(new ZoneOffsetConverter(), String.class))); initAttribute(etMock, mObjectOneOfEnumAttribute, new AttributeInfo(OWLClassM.getObjectOneOfEnumAttributeField(), Attribute.PersistentAttributeType.OBJECT).converter(new ObjectOneOfEnumConverter(OneOfEnum.class))); - - when(intSetAtt.getJavaField()).thenReturn(OWLClassM.getIntegerSetField()); - when(intSetAtt.getName()).thenReturn(OWLClassM.getIntegerSetField().getName()); - when(intSetAtt.getJavaType()).thenReturn(OWLClassM.getIntegerSetField().getType()); - when(intSetAtt.getIRI()).thenReturn(IRI.create(Vocabulary.p_m_IntegerSet)); - when(intSetAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(intSetAtt.isCollection()).thenReturn(true); - when(intSetAtt.getCollectionType()).thenReturn(CollectionType.SET); - when(intSetAtt.getDeclaringType()).thenReturn(etMock); - when(intSetAtt.getConverter()).thenReturn(new ToIntegerConverter()); - final Type typeMock = mock(Type.class); - when(intSetAtt.getElementType()).thenReturn(typeMock); - when(intSetAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(intSetAtt.hasLanguage()).thenReturn(true); - when(intSetAtt.getLanguage()).thenReturn(Generators.LANG); - when(typeMock.getJavaType()).thenReturn(Integer.class); - when(etMock.getFieldSpecification(OWLClassM.getIntegerSetField().getName())).thenReturn(intSetAtt); + initAttribute(etMock, intSetAtt, new AttributeInfo(OWLClassM.getIntegerSetField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.SET) + .elementType(Integer.class) + .valueType(BasicTypeImpl.get(Integer.class)) + .converter(new ToIntegerConverter())); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -708,23 +668,10 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu initAttribute(et, annotationAtt, new AttributeInfo(OWLClassN.getAnnotationPropertyField(), Attribute.PersistentAttributeType.ANNOTATION)); initAttribute(et, annotationUriAtt, new AttributeInfo(OWLClassN.getAnnotationUriField(), Attribute.PersistentAttributeType.ANNOTATION)); initAttribute(et, stringAtt, new AttributeInfo(OWLClassN.getStringAttributeField(), Attribute.PersistentAttributeType.DATA)); - - when(pluralAnnotationAtt.getJavaField()).thenReturn(OWLClassN.getPluralAnnotationField()); - when(pluralAnnotationAtt.getJavaType()).thenReturn(OWLClassN.getPluralAnnotationField().getType()); - when(et.getAttribute(OWLClassN.getPluralAnnotationField().getName())).thenReturn(pluralAnnotationAtt); - when(pluralAnnotationAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); - when(pluralAnnotationAtt.isCollection()).thenReturn(true); - when(pluralAnnotationAtt.getBindableJavaType()).thenReturn(String.class); - when(pluralAnnotationAtt.getCollectionType()).thenReturn(CollectionType.SET); - final Type elemType = mock(Type.class); - when(elemType.getJavaType()).thenReturn(String.class); - when(pluralAnnotationAtt.getElementType()).thenReturn(elemType); - when(pluralAnnotationAtt.getIRI()).thenReturn( - IRI.create(OWLClassN.getPluralAnnotationField().getAnnotation(OWLAnnotationProperty.class).iri())); - when(pluralAnnotationAtt.getDeclaringType()).thenReturn(et); - when(pluralAnnotationAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(pluralAnnotationAtt.hasLanguage()).thenReturn(true); - when(pluralAnnotationAtt.getLanguage()).thenReturn(Generators.LANG); + initAttribute(et, pluralAnnotationAtt, + new AttributeInfo(OWLClassN.getPluralAnnotationField(), Attribute.PersistentAttributeType.ANNOTATION).elementType(String.class) + .collectionType(CollectionType.SET) + .valueType(BasicTypeImpl.get(String.class))); when(props.getJavaField()).thenReturn(OWLClassN.getPropertiesField()); when(props.getName()).thenReturn(OWLClassN.getPropertiesField().getName()); @@ -751,7 +698,7 @@ public static void initOWLClassOMock(IdentifiableEntityType et, Singu public static void initOWLClassPMock(IdentifiableEntityType et, TypesSpecification types, PropertiesSpecification props, - SingularAttributeImpl uriAtt, PluralAttribute urlsAtt, + SingularAttributeImpl uriAtt, AbstractPluralAttribute urlsAtt, ListAttribute simpleListAtt, ListAttribute refListAtt, Identifier idP) throws Exception { when(et.getJavaType()).thenReturn(OWLClassP.class); @@ -776,16 +723,8 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types when(types.getJavaType()).thenReturn(Set.class); when(types.getElementType()).thenReturn(URI.class); initAttribute(et, uriAtt, new AttributeInfo(OWLClassP.getIndividualUriField(), Attribute.PersistentAttributeType.OBJECT)); - when(et.getFieldSpecification(urlsAtt.getName())).thenReturn(urlsAtt); - when(urlsAtt.getName()).thenReturn(OWLClassP.getIndividualUrlsField().getName()); - when(urlsAtt.getJavaField()).thenReturn(OWLClassP.getIndividualUrlsField()); - when(urlsAtt.isCollection()).thenReturn(true); - when(urlsAtt.getDeclaringType()).thenReturn(et); - when(urlsAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(urlsAtt.getCollectionType()).thenReturn(CollectionType.SET); - when(urlsAtt.getBindableJavaType()).thenReturn(URL.class); - when(urlsAtt.getIRI()).thenReturn( - IRI.create(OWLClassP.getIndividualUrlsField().getAnnotation(OWLObjectProperty.class).iri())); + initAttribute(et, urlsAtt, new AttributeInfo(OWLClassP.getIndividualUrlsField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.SET) + .elementType(URL.class)); when(simpleListAtt.getName()).thenReturn(OWLClassP.getSimpleListField().getName()); when(simpleListAtt.getJavaField()).thenReturn(OWLClassP.getSimpleListField()); when(et.getFieldSpecification(OWLClassP.getSimpleListField().getName())).thenReturn(simpleListAtt); @@ -1020,23 +959,9 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt initAttribute(et, singularStringAtt, new AttributeInfo(OWLClassU.getSingularStringAttField(), Attribute.PersistentAttributeType.DATA).language(null)); initAttribute(et, modified, new AttributeInfo(OWLClassU.getModifiedField(), Attribute.PersistentAttributeType.DATA).converter(new LocalDateTimeConverter()) .language(null)); - - when(pluralStringAtt.getJavaField()).thenReturn(OWLClassU.getPluralStringAttField()); - when(pluralStringAtt.getJavaType()).thenReturn(OWLClassU.getPluralStringAttField().getType()); - when(pluralStringAtt.getName()).thenReturn(OWLClassU.getPluralStringAttField().getName()); - when(et.getAttribute(OWLClassU.getPluralStringAttField().getName())).thenReturn(pluralStringAtt); - when(et.getFieldSpecification(OWLClassU.getPluralStringAttField().getName())).thenReturn(pluralStringAtt); - when(pluralStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(pluralStringAtt.isCollection()).thenReturn(true); - when(pluralStringAtt.getCollectionType()).thenReturn(CollectionType.SET); - when(pluralStringAtt.getBindableJavaType()).thenReturn(MultilingualString.class); - when(pluralStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassU.getPluralStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(pluralStringAtt.getDeclaringType()).thenReturn(et); - when(pluralStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(pluralStringAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(pluralStringAtt.hasLanguage()).thenReturn(false); - when(pluralStringAtt.getLanguage()).thenReturn(null); + initAttribute(et, pluralStringAtt, new AttributeInfo(OWLClassU.getPluralStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.SET) + .elementType(MultilingualString.class) + .language(null)); final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassU.class.getDeclaredMethod("preUpdate")); @@ -1057,22 +982,13 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu when(et.getAttributes()).thenReturn((Set) Set.of(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(setStringAtt.getJavaField()).thenReturn(OWLClassW.getSetStringAttField()); - when(setStringAtt.getJavaType()).thenReturn(OWLClassW.getSetStringAttField().getType()); - when(setStringAtt.getName()).thenReturn(OWLClassW.getSetStringAttField().getName()); - when(et.getAttribute(OWLClassW.getSetStringAttField().getName())).thenReturn(setStringAtt); - when(et.getFieldSpecification(OWLClassW.getSetStringAttField().getName())).thenReturn(setStringAtt); - when(setStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(setStringAtt.isCollection()).thenReturn(true); - when(setStringAtt.getCollectionType()).thenReturn(CollectionType.SET); - when(setStringAtt.getBindableJavaType()).thenReturn(MultilingualString.class); - when(setStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassW.getSetStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(setStringAtt.getDeclaringType()).thenReturn(et); - when(setStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(setStringAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(setStringAtt.hasLanguage()).thenReturn(false); - when(setStringAtt.getLanguage()).thenReturn(null); + initAttribute(et, setStringAtt, + new AttributeInfo(OWLClassW.getSetStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.SET) + .elementType(MultilingualString.class) + .language(null)); + initAttribute(et, collectionStringAtt, new AttributeInfo(OWLClassW.getCollectionStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.COLLECTION) + .elementType(MultilingualString.class) + .language(null)); when(listStringAtt.getJavaField()).thenReturn(OWLClassW.getListStringAttField()); when(listStringAtt.getJavaType()).thenReturn(OWLClassW.getListStringAttField().getType()); @@ -1091,24 +1007,6 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu when(listStringAtt.hasLanguage()).thenReturn(false); when(listStringAtt.getLanguage()).thenReturn(null); - when(collectionStringAtt.getJavaField()).thenReturn(OWLClassW.getCollectionStringAttField()); - when(collectionStringAtt.getJavaType()).thenReturn(OWLClassW.getCollectionStringAttField().getType()); - when(collectionStringAtt.getName()).thenReturn(OWLClassW.getCollectionStringAttField().getName()); - when(et.getAttribute(OWLClassW.getCollectionStringAttField().getName())).thenReturn(collectionStringAtt); - when(et.getFieldSpecification(OWLClassW.getCollectionStringAttField() - .getName())).thenReturn(collectionStringAtt); - when(collectionStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(collectionStringAtt.isCollection()).thenReturn(true); - when(collectionStringAtt.getCollectionType()).thenReturn(CollectionType.COLLECTION); - when(collectionStringAtt.getBindableJavaType()).thenReturn(MultilingualString.class); - when(collectionStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassW.getCollectionStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(collectionStringAtt.getDeclaringType()).thenReturn(et); - when(collectionStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(collectionStringAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(collectionStringAtt.hasLanguage()).thenReturn(false); - when(collectionStringAtt.getLanguage()).thenReturn(null); - when(setQueryStringAtt.getJavaField()).thenReturn(OWLClassW.getSetQueryStringAttField()); when(setQueryStringAtt.getJavaType()).thenReturn(OWLClassW.getSetQueryStringAttField().getType()); when(setQueryStringAtt.getName()).thenReturn(OWLClassW.getSetQueryStringAttField().getName()); @@ -1231,8 +1129,10 @@ private static class AttributeInfo { private ParticipationConstraint[] constraints = new ParticipationConstraint[0]; private boolean nonEmpty; private String language = Generators.LANG; - private AbstractIdentifiableType valueType; + private Type valueType; + private Class elementType; private ConverterWrapper converter; + private CollectionType collectionType; private AttributeInfo(Field field, Attribute.PersistentAttributeType type) { this.field = field; @@ -1254,14 +1154,24 @@ private AttributeInfo language(String language) { return this; } - private AttributeInfo valueType(AbstractIdentifiableType valueType) { + private AttributeInfo valueType(Type valueType) { this.valueType = valueType; return this; } + private AttributeInfo elementType(Class elementType) { + this.elementType = elementType; + return this; + } + private AttributeInfo converter(ConverterWrapper converter) { this.converter = converter; return this; } + + private AttributeInfo collectionType(CollectionType collectionType) { + this.collectionType = collectionType; + return this; + } } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java index c33965589..c1e73b307 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java @@ -383,11 +383,11 @@ public MetamodelMocks() throws Exception { MetamodelFactory.initOWLClassDMocks(etD, dOwlClassAAtt, etA, idD); MetamodelClassInitializer.initMetamodelClassOWLClassD(dOwlClassAAtt, idD); MetamodelFactory.initOWLClassEMocks(etE, eStringAtt, idE); - MetamodelFactory.initOWLClassFMocks(etF, fSetAtt, fStringAtt, idF); + MetamodelFactory.initOWLClassFMocks(etF, fSetAtt, fStringAtt, etA, idF); MetamodelFactory.initOWLClassGMocks(etG, gOwlClassHAtt, etH, idG); MetamodelFactory.initOWLClassHMocks(etH, hOwlClassAAtt, hOwlClassGAtt, etA, etG, idH); MetamodelFactory.initOWLClassIMocks(etI, iOwlClassAAtt, etA, idI); - MetamodelFactory.initOWLClassJMocks(etJ, jSetAtt, idJ); + MetamodelFactory.initOWLClassJMocks(etJ, jSetAtt, etA, idJ); MetamodelFactory.initOWLClassKMocks(etK, kOwlClassEAtt, etE, idK); MetamodelFactory.initOWLClassLMocks(etL, lReferencedList, lSimpleList, lSetAtt, lOwlClassAAtt, etA, idL); MetamodelFactory.initOWLClassMMock(etM, mBooleanAtt, mIntegerAtt, mLongAtt, mDoubleAtt, mDateAtt, mCharacterAtt, mEnumAtt, diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java index fa593d4eb..0e1f97534 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/PluralAnnotationPropertyStrategyTest.java @@ -28,30 +28,53 @@ import cz.cvut.kbss.jopa.model.annotations.OWLClass; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; -import cz.cvut.kbss.jopa.model.metamodel.*; +import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute; +import cz.cvut.kbss.jopa.model.metamodel.Attribute; +import cz.cvut.kbss.jopa.model.metamodel.BasicTypeImpl; +import cz.cvut.kbss.jopa.model.metamodel.CollectionType; +import cz.cvut.kbss.jopa.model.metamodel.EntityType; +import cz.cvut.kbss.jopa.model.metamodel.IdentifiableEntityType; +import cz.cvut.kbss.jopa.model.metamodel.SetAttributeImpl; import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper; import cz.cvut.kbss.jopa.oom.converter.ObjectConverter; import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter; import cz.cvut.kbss.jopa.utils.Configuration; import cz.cvut.kbss.jopa.vocabulary.DC; import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor; -import cz.cvut.kbss.ontodriver.model.*; +import cz.cvut.kbss.ontodriver.model.Assertion; +import cz.cvut.kbss.ontodriver.model.Axiom; +import cz.cvut.kbss.ontodriver.model.AxiomImpl; +import cz.cvut.kbss.ontodriver.model.LangString; +import cz.cvut.kbss.ontodriver.model.NamedResource; +import cz.cvut.kbss.ontodriver.model.Value; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import java.net.URI; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) class PluralAnnotationPropertyStrategyTest { private static final String LANG = "en"; @@ -68,7 +91,6 @@ class PluralAnnotationPropertyStrategyTest { @BeforeEach void setUp() throws Exception { - MockitoAnnotations.openMocks(this); final Configuration configuration = new Configuration( Collections.singletonMap(JOPAPersistenceProperties.LANG, LANG)); when(mapperMock.getConfiguration()).thenReturn(configuration); From 71d6e2c183b142fddb92a0049df05e6e6e803c14 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 6 Jan 2025 15:07:04 +0100 Subject: [PATCH 11/17] [GH-301] Extract list attribute, properties and types mock initialization into reusable function. --- .../environment/utils/MetamodelFactory.java | 283 +++++------------- .../environment/utils/MetamodelMocks.java | 6 +- 2 files changed, 85 insertions(+), 204 deletions(-) diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index 8b01eaf37..6beb94871 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -61,7 +61,6 @@ import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints; import cz.cvut.kbss.jopa.model.annotations.RDFContainerType; import cz.cvut.kbss.jopa.model.annotations.Sequence; -import cz.cvut.kbss.jopa.model.annotations.SequenceType; import cz.cvut.kbss.jopa.model.annotations.Sparql; import cz.cvut.kbss.jopa.model.lifecycle.LifecycleEvent; import cz.cvut.kbss.jopa.model.metamodel.AbstractAttribute; @@ -76,7 +75,6 @@ import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification; import cz.cvut.kbss.jopa.model.metamodel.IdentifiableEntityType; import cz.cvut.kbss.jopa.model.metamodel.Identifier; -import cz.cvut.kbss.jopa.model.metamodel.ListAttribute; import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl; import cz.cvut.kbss.jopa.model.metamodel.MappedSuperclassTypeImpl; import cz.cvut.kbss.jopa.model.metamodel.PropertiesSpecification; @@ -153,7 +151,6 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassA.class)); when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassA.getClassIri())); - when(etMock.getAttribute(OWLClassA.getStrAttField().getName())).thenReturn(strAttMock); when(etMock.getName()).thenReturn(OWLClassA.class.getSimpleName()); when(etMock.getTypes()).thenReturn(typesMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); @@ -161,14 +158,7 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassA.getStrAttField(), Attribute.PersistentAttributeType.DATA) .language(Generators.LANG)); - when(typesMock.getJavaField()).thenReturn(OWLClassA.getTypesField()); - when(typesMock.getName()).thenReturn(OWLClassA.getTypesField().getName()); - when(typesMock.getDeclaringType()).thenReturn(etMock); - when(typesMock.getJavaType()).thenReturn(Set.class); - when(typesMock.getElementType()).thenReturn(String.class); - when(typesMock.isCollection()).thenReturn(true); - when(typesMock.getFetchType()).thenReturn(FetchType.EAGER); - when(etMock.getFieldSpecification(typesMock.getName())).thenReturn(typesMock); + initTypesAttribute(etMock, typesMock, new AttributeInfo(OWLClassA.getTypesField(), null).elementType(String.class)); initIdentifier(etMock, idMock, OWLClassA.class.getDeclaredField("uri"), false); final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); @@ -271,6 +261,19 @@ private static void addLifecycleCallback(EntityLifecycleListenerManager manager, addCallback.invoke(manager, evt, callback); } + private static void initTypesAttribute(IdentifiableEntityType etMock, TypesSpecification typesMock, + AttributeInfo attInfo) { + when(typesMock.getJavaField()).thenReturn(attInfo.field); + when(typesMock.getName()).thenReturn(attInfo.field.getName()); + when(typesMock.getDeclaringType()).thenReturn(etMock); + when(typesMock.getJavaType()).thenReturn(Set.class); + when(typesMock.getElementType()).thenReturn(attInfo.elementType); + when(typesMock.isCollection()).thenReturn(true); + when(typesMock.getFetchType()).thenReturn(FetchType.EAGER); + when(etMock.getFieldSpecification(attInfo.field.getName())).thenReturn(typesMock); + when(etMock.getTypes()).thenReturn(typesMock); + } + /** * Initializes the specified mock objects to return reasonable values. */ @@ -282,20 +285,12 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, when(etMock.getIRI()).thenReturn(IRI.create(OWLClassB.getClassIri())); when(etMock.getName()).thenReturn(OWLClassB.class.getSimpleName()); when(etMock.getAttribute(OWLClassB.getStrAttField().getName())).thenReturn(strAttMock); - when(etMock.getProperties()).thenReturn(propsMock); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, propsMock, idMock)); initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassB.getStrAttField(), Attribute.PersistentAttributeType.DATA) .language(Generators.LANG)); - when(etMock.getFieldSpecification(strAttMock.getName())).thenReturn(strAttMock); - when(propsMock.getJavaField()).thenReturn(OWLClassB.getPropertiesField()); - when(propsMock.getJavaType()).thenReturn(OWLClassB.getPropertiesField().getType()); - when(propsMock.getName()).thenReturn(OWLClassB.getPropertiesField().getName()); - when(propsMock.getDeclaringType()).thenReturn(etMock); - when(propsMock.getPropertyIdentifierType()).thenReturn(String.class); - when(propsMock.getPropertyValueType()).thenReturn(String.class); - when(etMock.getFieldSpecification(propsMock.getName())).thenReturn(propsMock); + initProperties(etMock, propsMock, new AttributeInfo(OWLClassB.getPropertiesField(), null), String.class, String.class); initIdentifier(etMock, idMock, OWLClassB.class.getDeclaredField("uri"), false); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); @@ -308,67 +303,38 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, }); } + private static void initProperties(IdentifiableEntityType etMock, PropertiesSpecification propsMock, + AttributeInfo attInfo, Class propertyType, Class valueType) { + when(propsMock.getJavaField()).thenReturn(attInfo.field); + when(propsMock.getJavaType()).thenReturn(attInfo.field.getType()); + when(propsMock.getName()).thenReturn(attInfo.field.getName()); + when(propsMock.getDeclaringType()).thenReturn(etMock); + when(propsMock.getPropertyIdentifierType()).thenReturn(propertyType); + when(propsMock.getPropertyValueType()).thenReturn(valueType); + when(etMock.getFieldSpecification(propsMock.getName())).thenReturn(propsMock); + when(etMock.getProperties()).thenReturn(propsMock); + } + public static void initOWLClassCMocks(IdentifiableEntityType etMock, ListAttributeImpl simpleListMock, ListAttributeImpl refListMock, - RdfContainerAttributeImpl rdfSeqMock, Identifier idMock) + RdfContainerAttributeImpl rdfSeqMock, + IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassC.class); when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassC.class)); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassC.getClassIri())); when(etMock.getName()).thenReturn(OWLClassC.class.getSimpleName()); - when(etMock.getAttribute(OWLClassC.getSimpleListField().getName())).thenReturn(simpleListMock); - when(etMock.getAttribute(OWLClassC.getRefListField().getName())).thenReturn(refListMock); when(etMock.getAttribute(OWLClassC.getRdfSeqField().getName())).thenReturn(rdfSeqMock); when(etMock.getAttributes()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock, idMock)); - when(simpleListMock.getJavaField()).thenReturn(OWLClassC.getSimpleListField()); - when(refListMock.getJavaField()).thenReturn(OWLClassC.getRefListField()); - when(rdfSeqMock.getJavaField()).thenReturn(OWLClassC.getRdfSeqField()); - String attIri = OWLClassC.getSimpleListField().getAnnotation(OWLObjectProperty.class).iri(); - when(simpleListMock.getIRI()).thenReturn(IRI.create(attIri)); - when(simpleListMock.getName()).thenReturn(OWLClassC.getSimpleListField().getName()); - when(etMock.getFieldSpecification(simpleListMock.getName())).thenReturn(simpleListMock); - String hasListAttIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class).listClassIRI(); - when(simpleListMock.getSequenceType()).thenReturn(SequenceType.simple); - when(simpleListMock.getCollectionType()).thenReturn(CollectionType.LIST); - when(simpleListMock.getListClassIRI()).thenReturn(IRI.create(hasListAttIri)); - String hasNextIri = OWLClassC.getSimpleListField().getAnnotation(Sequence.class).hasNextPropertyIRI(); - when(simpleListMock.getHasNextPropertyIRI()).thenReturn(IRI.create(hasNextIri)); - when(simpleListMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(simpleListMock.getPersistentAttributeType()) - .thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(simpleListMock.isCollection()).thenReturn(Boolean.TRUE); - when(simpleListMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(simpleListMock.isAssociation()).thenReturn(true); - when(simpleListMock.getDeclaringType()).thenReturn(etMock); - when(simpleListMock.getJavaType()).thenReturn(List.class); - when(simpleListMock.getFetchType()).thenReturn(FetchType.LAZY); - when(simpleListMock.getCascadeTypes()) - .thenReturn(OWLClassC.getSimpleListField().getAnnotation(OWLObjectProperty.class).cascade()); - - hasListAttIri = OWLClassC.getRefListField().getAnnotation(Sequence.class).listClassIRI(); - when(refListMock.getFetchType()).thenReturn(FetchType.EAGER); - when(refListMock.getSequenceType()).thenReturn(SequenceType.referenced); - when(refListMock.getCollectionType()).thenReturn(CollectionType.LIST); - when(refListMock.getListClassIRI()).thenReturn(IRI.create(hasListAttIri)); - when(refListMock.getName()).thenReturn(OWLClassC.getRefListField().getName()); - when(etMock.getFieldSpecification(refListMock.getName())).thenReturn(refListMock); - hasNextIri = OWLClassC.getRefListField().getAnnotation(Sequence.class).hasNextPropertyIRI(); - when(refListMock.getHasNextPropertyIRI()).thenReturn(IRI.create(hasNextIri)); - final String contentIri = OWLClassC.getRefListField().getAnnotation(Sequence.class).hasContentsPropertyIRI(); - when(refListMock.getHasContentsPropertyIRI()).thenReturn(IRI.create(contentIri)); - attIri = OWLClassC.getRefListField().getAnnotation(OWLObjectProperty.class).iri(); - when(refListMock.getIRI()).thenReturn(IRI.create(attIri)); - when(refListMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(refListMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(refListMock.isCollection()).thenReturn(Boolean.TRUE); - when(refListMock.isAssociation()).thenReturn(true); - when(refListMock.getConstraints()).thenReturn(new ParticipationConstraint[]{}); - when(refListMock.getDeclaringType()).thenReturn(etMock); - when(refListMock.getJavaType()).thenReturn(List.class); - when(refListMock.getCascadeTypes()) - .thenReturn(OWLClassC.getRefListField().getAnnotation(OWLObjectProperty.class).cascade()); + initListAttribute(etMock, simpleListMock, new AttributeInfo(OWLClassC.getSimpleListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(OWLClassA.class) + .valueType(etAMock)); + initListAttribute(etMock, refListMock, new AttributeInfo(OWLClassC.getRefListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(OWLClassA.class) + .valueType(etAMock)); + when(rdfSeqMock.getJavaField()).thenReturn(OWLClassC.getRdfSeqField()); when(rdfSeqMock.getFetchType()).thenReturn(FetchType.EAGER); when(rdfSeqMock.getCollectionType()).thenReturn(CollectionType.LIST); when(rdfSeqMock.getContainerType()).thenReturn(RDFContainerType.SEQ); @@ -402,6 +368,16 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, }); } + private static void initListAttribute(IdentifiableEntityType etMock, ListAttributeImpl attMock, + AttributeInfo attInfo) { + initAttribute(etMock, attMock, attInfo); + final Sequence seq = attInfo.field.getAnnotation(Sequence.class); + when(attMock.getHasNextPropertyIRI()).thenReturn(IRI.create(seq.hasNextPropertyIRI())); + when(attMock.getHasContentsPropertyIRI()).thenReturn(IRI.create(seq.hasContentsPropertyIRI())); + when(attMock.getListClassIRI()).thenReturn(IRI.create(seq.listClassIRI())); + when(attMock.getSequenceType()).thenReturn(seq.type()); + } + /** * Initializes the specified mock objects to return reasonable values. */ @@ -556,48 +532,24 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, when(etMock.getAttributes()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock, idMock)); - when(refListMock.getJavaField()).thenReturn(OWLClassL.getReferencedListField()); - when(refListMock.getName()).thenReturn(OWLClassL.getReferencedListField().getName()); - when(refListMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(refListMock.getIRI()).thenReturn( - IRI.create(OWLClassL.getReferencedListField().getAnnotation(OWLObjectProperty.class).iri())); - when(refListMock.getConstraints()).thenReturn( - OWLClassL.getReferencedListField().getAnnotation(ParticipationConstraints.class).value()); - when(refListMock.getCollectionType()).thenReturn(CollectionType.LIST); - when(refListMock.getSequenceType()).thenReturn(SequenceType.referenced); - when(refListMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(refListMock.isCollection()).thenReturn(true); - when(refListMock.getHasNextPropertyIRI()).thenReturn(IRI.create( - OWLClassL.getReferencedListField().getAnnotation(Sequence.class).hasNextPropertyIRI())); - when(refListMock.getHasContentsPropertyIRI()).thenReturn(IRI.create( - OWLClassL.getReferencedListField().getAnnotation(Sequence.class).hasContentsPropertyIRI())); - when(etMock.getFieldSpecification(OWLClassL.getReferencedListField().getName())).thenReturn(refListMock); - when(etMock.getAttribute(OWLClassL.getReferencedListField().getName())).thenReturn(refListMock); - when(refListMock.getDeclaringType()).thenReturn(etMock); - when(refListMock.getJavaType()).thenReturn(List.class); - - when(simpleListMock.getJavaField()).thenReturn(OWLClassL.getSimpleListField()); - when(simpleListMock.getName()).thenReturn(OWLClassL.getSimpleListField().getName()); - when(simpleListMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(simpleListMock.getIRI()).thenReturn( - IRI.create(OWLClassL.getSimpleListField().getAnnotation(OWLObjectProperty.class).iri())); - when(simpleListMock.getConstraints()).thenReturn( - OWLClassL.getSimpleListField().getAnnotation(ParticipationConstraints.class).value()); - when(simpleListMock.getCollectionType()).thenReturn(CollectionType.LIST); - when(simpleListMock.getSequenceType()).thenReturn(SequenceType.simple); - when(simpleListMock.getBindableJavaType()).thenReturn(OWLClassA.class); - when(simpleListMock.isCollection()).thenReturn(true); - when(simpleListMock.getHasNextPropertyIRI()).thenReturn( - IRI.create(OWLClassL.getSimpleListField().getAnnotation(OWLObjectProperty.class).iri())); - when(etMock.getFieldSpecification(OWLClassL.getSimpleListField().getName())).thenReturn(simpleListMock); - when(etMock.getAttribute(OWLClassL.getSimpleListField().getName())).thenReturn(simpleListMock); - when(simpleListMock.getDeclaringType()).thenReturn(etMock); - when(simpleListMock.getJavaType()).thenReturn(List.class); - + initListAttribute(etMock, refListMock, new AttributeInfo(OWLClassL.getReferencedListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(OWLClassA.class) + .valueType(etAMock) + .constraints(OWLClassL.getReferencedListField() + .getAnnotation(ParticipationConstraints.class) + .value())); + initListAttribute(etMock, simpleListMock, new AttributeInfo(OWLClassL.getSimpleListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(OWLClassA.class) + .valueType(etAMock) + .constraints(OWLClassL.getSimpleListField() + .getAnnotation(ParticipationConstraints.class) + .value())); initAttribute(etMock, setMock, new AttributeInfo(OWLClassL.getSetField(), Attribute.PersistentAttributeType.OBJECT).valueType(etAMock).elementType(OWLClassA.class) - .collectionType(CollectionType.SET)); - + .collectionType(CollectionType.SET) + .constraints(OWLClassL.getSetField() + .getAnnotation(ParticipationConstraints.class) + .value())); initAttribute(etMock, singleAMock, new AttributeInfo(OWLClassL.getSingleAField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etAMock).nonEmpty()); when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); @@ -673,11 +625,7 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu .collectionType(CollectionType.SET) .valueType(BasicTypeImpl.get(String.class))); - when(props.getJavaField()).thenReturn(OWLClassN.getPropertiesField()); - when(props.getName()).thenReturn(OWLClassN.getPropertiesField().getName()); - when(props.getDeclaringType()).thenReturn(et); - when(props.getPropertyIdentifierType()).thenReturn(SingularAttribute.class); - when(props.getPropertyValueType()).thenReturn(String.class); + initProperties(et, props, new AttributeInfo(OWLClassN.getPropertiesField(), null), SingularAttribute.class, String.class); when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -699,7 +647,8 @@ public static void initOWLClassOMock(IdentifiableEntityType et, Singu public static void initOWLClassPMock(IdentifiableEntityType et, TypesSpecification types, PropertiesSpecification props, SingularAttributeImpl uriAtt, AbstractPluralAttribute urlsAtt, - ListAttribute simpleListAtt, ListAttribute refListAtt, Identifier idP) throws + ListAttributeImpl simpleListAtt, ListAttributeImpl refListAtt, + Identifier idP) throws Exception { when(et.getJavaType()).thenReturn(OWLClassP.class); when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassP.class)); @@ -708,59 +657,18 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types initIdentifier(et, idP, OWLClassP.getUriField(), false); when(et.getFieldSpecifications()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt, props, types, idP)); when(et.getAttributes()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt)); - when(et.getFieldSpecification(props.getName())).thenReturn(props); - when(et.getProperties()).thenReturn(props); - when(props.getJavaField()).thenReturn(OWLClassP.getPropertiesField()); - when(props.getName()).thenReturn(OWLClassP.getPropertiesField().getName()); - when(props.getDeclaringType()).thenReturn(et); - when(props.getPropertyIdentifierType()).thenReturn(URI.class); - when(props.getPropertyValueType()).thenReturn(Object.class); - when(et.getFieldSpecification(types.getName())).thenReturn(types); - when(et.getTypes()).thenReturn(types); - when(types.getJavaField()).thenReturn(OWLClassP.getTypesField()); - when(types.getName()).thenReturn(OWLClassP.getTypesField().getName()); - when(types.getDeclaringType()).thenReturn(et); - when(types.getJavaType()).thenReturn(Set.class); - when(types.getElementType()).thenReturn(URI.class); + initProperties(et, props, new AttributeInfo(OWLClassP.getPropertiesField(), null), URI.class, Object.class); + initTypesAttribute(et, types, new AttributeInfo(OWLClassP.getTypesField(), null).elementType(URI.class)); initAttribute(et, uriAtt, new AttributeInfo(OWLClassP.getIndividualUriField(), Attribute.PersistentAttributeType.OBJECT)); initAttribute(et, urlsAtt, new AttributeInfo(OWLClassP.getIndividualUrlsField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.SET) .elementType(URL.class)); - when(simpleListAtt.getName()).thenReturn(OWLClassP.getSimpleListField().getName()); - when(simpleListAtt.getJavaField()).thenReturn(OWLClassP.getSimpleListField()); - when(et.getFieldSpecification(OWLClassP.getSimpleListField().getName())).thenReturn(simpleListAtt); - when(simpleListAtt.isCollection()).thenReturn(true); - when(simpleListAtt.isAssociation()).thenReturn(true); - when(simpleListAtt.getDeclaringType()).thenReturn(et); - when(simpleListAtt.getCollectionType()).thenReturn(CollectionType.LIST); - when(simpleListAtt.getBindableJavaType()).thenReturn(URI.class); - when(simpleListAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(simpleListAtt.getSequenceType()).thenReturn(SequenceType.simple); - final Field simpleListField = OWLClassP.getSimpleListField(); - when(simpleListAtt.getIRI()) - .thenReturn(IRI.create(simpleListField.getAnnotation(OWLObjectProperty.class).iri())); - when(simpleListAtt.getListClassIRI()) - .thenReturn(IRI.create(simpleListField.getAnnotation(Sequence.class).listClassIRI())); - when(simpleListAtt.getHasNextPropertyIRI()) - .thenReturn(IRI.create(simpleListField.getAnnotation(Sequence.class).hasNextPropertyIRI())); - - when(refListAtt.getName()).thenReturn(OWLClassP.getReferencedListField().getName()); - when(refListAtt.getJavaField()).thenReturn(OWLClassP.getReferencedListField()); - when(et.getFieldSpecification(OWLClassP.getReferencedListField().getName())).thenReturn(refListAtt); - when(refListAtt.isCollection()).thenReturn(true); - when(refListAtt.isAssociation()).thenReturn(true); - when(refListAtt.getDeclaringType()).thenReturn(et); - when(refListAtt.getCollectionType()).thenReturn(CollectionType.LIST); - when(refListAtt.getBindableJavaType()).thenReturn(URI.class); - when(refListAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT); - when(refListAtt.getSequenceType()).thenReturn(SequenceType.referenced); - final Field refListField = OWLClassP.getReferencedListField(); - when(refListAtt.getIRI()).thenReturn(IRI.create(refListField.getAnnotation(OWLObjectProperty.class).iri())); - when(refListAtt.getListClassIRI()) - .thenReturn(IRI.create(refListField.getAnnotation(Sequence.class).listClassIRI())); - when(refListAtt.getHasNextPropertyIRI()) - .thenReturn(IRI.create(refListField.getAnnotation(Sequence.class).hasNextPropertyIRI())); - when(refListAtt.getHasContentsPropertyIRI()) - .thenReturn(IRI.create(refListField.getAnnotation(Sequence.class).hasContentsPropertyIRI())); + initListAttribute(et, simpleListAtt, new AttributeInfo(OWLClassP.getSimpleListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(URI.class) + .valueType(BasicTypeImpl.get(URI.class))); + initListAttribute(et, refListAtt, new AttributeInfo(OWLClassP.getReferencedListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) + .elementType(URI.class) + .valueType(BasicTypeImpl.get(URI.class))); + when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } @@ -816,17 +724,10 @@ public static void initOwlClassSMock(IdentifiableEntityType et, Singu when(et.getIRI()).thenReturn(IRI.create(OWLClassS.getClassIri())); when(et.getFieldSpecifications()).thenReturn(Set.of(sNameAtt, sTypes, idS)); when(et.getAttributes()).thenReturn(Collections.singleton(sNameAtt)); - when(et.getTypes()).thenReturn(sTypes); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); initAttribute(et, sNameAtt, new AttributeInfo(OWLClassS.getNameField(), Attribute.PersistentAttributeType.ANNOTATION)); - when(sTypes.getJavaField()).thenReturn(OWLClassS.getTypesField()); - when(sTypes.getName()).thenReturn(OWLClassS.getTypesField().getName()); - when(sTypes.getDeclaringType()).thenReturn(et); - when(sTypes.getJavaType()).thenReturn(Set.class); - when(sTypes.getElementType()).thenReturn(String.class); - when(et.getFieldSpecification(sNameAtt.getName())).thenReturn(sNameAtt); - when(et.getFieldSpecification(sTypes.getName())).thenReturn(sTypes); + initTypesAttribute(et, sTypes, new AttributeInfo(OWLClassS.getTypesField(), null).elementType(String.class)); final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); addLifecycleCallback(listenerManager, PRE_PERSIST, OWLClassS.getPrePersistHook()); when(et.getLifecycleListenerManager()).thenReturn(listenerManager); @@ -969,7 +870,7 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt } static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPluralAttribute setStringAtt, - AbstractPluralAttribute listStringAtt, AbstractPluralAttribute collectionStringAtt, + ListAttributeImpl listStringAtt, AbstractPluralAttribute collectionStringAtt, AbstractQueryAttribute setQueryStringAtt, AbstractQueryAttribute listQueryStringAtt, Identifier id) throws Exception { when(et.getJavaType()).thenReturn(OWLClassW.class); @@ -989,23 +890,10 @@ static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPlu initAttribute(et, collectionStringAtt, new AttributeInfo(OWLClassW.getCollectionStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.COLLECTION) .elementType(MultilingualString.class) .language(null)); - - when(listStringAtt.getJavaField()).thenReturn(OWLClassW.getListStringAttField()); - when(listStringAtt.getJavaType()).thenReturn(OWLClassW.getListStringAttField().getType()); - when(listStringAtt.getName()).thenReturn(OWLClassW.getListStringAttField().getName()); - when(et.getAttribute(OWLClassW.getListStringAttField().getName())).thenReturn(listStringAtt); - when(et.getFieldSpecification(OWLClassW.getListStringAttField().getName())).thenReturn(listStringAtt); - when(listStringAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); - when(listStringAtt.isCollection()).thenReturn(true); - when(listStringAtt.getCollectionType()).thenReturn(CollectionType.LIST); - when(listStringAtt.getBindableJavaType()).thenReturn(MultilingualString.class); - when(listStringAtt.getIRI()).thenReturn( - IRI.create(OWLClassW.getListStringAttField().getAnnotation(OWLDataProperty.class).iri())); - when(listStringAtt.getDeclaringType()).thenReturn(et); - when(listStringAtt.getConstraints()).thenReturn(new ParticipationConstraint[0]); - when(listStringAtt.getCascadeTypes()).thenReturn(new CascadeType[0]); - when(listStringAtt.hasLanguage()).thenReturn(false); - when(listStringAtt.getLanguage()).thenReturn(null); + initListAttribute(et, listStringAtt, new AttributeInfo(OWLClassW.getListStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.LIST) + .elementType(String.class) + .valueType(BasicTypeImpl.get(String.class)) + .language(null)); when(setQueryStringAtt.getJavaField()).thenReturn(OWLClassW.getSetQueryStringAttField()); when(setQueryStringAtt.getJavaType()).thenReturn(OWLClassW.getSetQueryStringAttField().getType()); @@ -1112,15 +1000,8 @@ public static void initPersonMocks(IdentifiableEntityType etMock, Singul initAttribute(etMock, phoneAttMock, new AttributeInfo(Person.class.getDeclaredField("phone"), Attribute.PersistentAttributeType.OBJECT).valueType(etPhone)); - when(typesMock.getJavaField()).thenReturn(Person.class.getDeclaredField("types")); - when(typesMock.getName()).thenReturn(Person.class.getDeclaredField("types").getName()); - when(typesMock.getDeclaringType()).thenReturn(etMock); - when(typesMock.getJavaType()).thenReturn(Set.class); - when(typesMock.getElementType()).thenReturn(String.class); - when(typesMock.isCollection()).thenReturn(true); - when(typesMock.getFetchType()).thenReturn(FetchType.EAGER); - when(etMock.getTypes()).thenReturn(typesMock); - when(etMock.getFieldSpecification("types")).thenReturn(typesMock); + initTypesAttribute(etMock, typesMock, new AttributeInfo(Person.class.getDeclaredField("types"), null) + .elementType(String.class)); } private static class AttributeInfo { diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java index c1e73b307..a1e8fd3d7 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java @@ -329,7 +329,7 @@ public class MetamodelMocks { @Mock private AbstractPluralAttribute, String> wSetStringAtt; @Mock - private AbstractPluralAttribute, String> wListStringAtt; + private ListAttributeImpl wListStringAtt; @Mock private AbstractPluralAttribute, String> wCollectionStringAtt; @Mock @@ -378,7 +378,7 @@ public MetamodelMocks() throws Exception { MetamodelClassInitializer.initMetamodelClassOWLClassA(aStringAtt, aTypes, idA); MetamodelFactory.initOWLClassBMocks(etB, bStringAtt, bProperties, idB); MetamodelClassInitializer.initMetamodelClassOWLClassB(bStringAtt, bProperties, idB); - MetamodelFactory.initOWLClassCMocks(etC, cSimpleList, cReferencedList, cRdfSeq, idC); + MetamodelFactory.initOWLClassCMocks(etC, cSimpleList, cReferencedList, cRdfSeq, etA, idC); MetamodelClassInitializer.initMetamodelClassOWLClassC(cSimpleList, cReferencedList, cRdfSeq, idC); MetamodelFactory.initOWLClassDMocks(etD, dOwlClassAAtt, etA, idD); MetamodelClassInitializer.initMetamodelClassOWLClassD(dOwlClassAAtt, idD); @@ -1031,7 +1031,7 @@ public AbstractPluralAttribute, String> vSetStringAtt() { return MetamodelMocks.this.wSetStringAtt; } - public AbstractPluralAttribute, String> vListStringAtt() { + public ListAttributeImpl vListStringAtt() { return MetamodelMocks.this.wListStringAtt; } From 339757f76e18020060de5645358b3d8aaac6681a Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 6 Jan 2025 16:00:06 +0100 Subject: [PATCH 12/17] [GH-301] Extract entity type mock initialization into reusable function. --- .../environment/utils/MetamodelFactory.java | 238 +++++------------- 1 file changed, 66 insertions(+), 172 deletions(-) diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index 6beb94871..5f6184b73 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -44,7 +44,6 @@ import cz.cvut.kbss.jopa.environment.Person; import cz.cvut.kbss.jopa.environment.Phone; import cz.cvut.kbss.jopa.environment.QMappedSuperclass; -import cz.cvut.kbss.jopa.environment.Vocabulary; import cz.cvut.kbss.jopa.environment.ZoneOffsetConverter; import cz.cvut.kbss.jopa.environment.listener.AnotherListener; import cz.cvut.kbss.jopa.environment.listener.ConcreteListener; @@ -55,6 +54,7 @@ import cz.cvut.kbss.jopa.model.annotations.FetchType; import cz.cvut.kbss.jopa.model.annotations.Inferred; import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; +import cz.cvut.kbss.jopa.model.annotations.OWLClass; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraint; @@ -147,20 +147,6 @@ public static void setInstantiableTypeGenerator(PersistenceContextAwareClassGene */ public static void initOWLClassAMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, TypesSpecification typesMock, Identifier idMock) throws Exception { - when(etMock.getJavaType()).thenReturn(OWLClassA.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassA.class)); - when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassA.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassA.class.getSimpleName()); - when(etMock.getTypes()).thenReturn(typesMock); - when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); - when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, typesMock, idMock)); - - initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassA.getStrAttField(), Attribute.PersistentAttributeType.DATA) - .language(Generators.LANG)); - initTypesAttribute(etMock, typesMock, new AttributeInfo(OWLClassA.getTypesField(), null).elementType(String.class)); - - initIdentifier(etMock, idMock, OWLClassA.class.getDeclaredField("uri"), false); final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); addLifecycleCallback(listenerManager, POST_LOAD, OWLClassA.getPostLoadCallback()); when(etMock.getLifecycleListenerManager()).thenReturn(listenerManager); @@ -171,6 +157,24 @@ public static void initOWLClassAMocks(IdentifiableEntityType etMock, } throw new IllegalArgumentException("Unknown attribute " + name); }); + initEntityType(etMock, OWLClassA.class, listenerManager); + when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); + when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, typesMock, idMock)); + + initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassA.getStrAttField(), Attribute.PersistentAttributeType.DATA) + .language(Generators.LANG)); + initTypesAttribute(etMock, typesMock, new AttributeInfo(OWLClassA.getTypesField(), null).elementType(String.class)); + + initIdentifier(etMock, idMock, OWLClassA.class.getDeclaredField("uri"), false); + } + + private static void initEntityType(IdentifiableEntityType et, Class cls, EntityLifecycleListenerManager listenerManager) { + when(et.getJavaType()).thenReturn(cls); + when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(cls)); + when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); + when(et.getIRI()).thenReturn(IRI.create(cls.getAnnotation(OWLClass.class).iri())); + when(et.getName()).thenReturn(cls.getSimpleName()); + when(et.getLifecycleListenerManager()).thenReturn(listenerManager); } private static void initIdentifier(IdentifiableEntityType et, Identifier id, Field idField, @@ -280,11 +284,7 @@ private static void initTypesAttribute(IdentifiableEntityType etMock, Typ public static void initOWLClassBMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, PropertiesSpecification propsMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassB.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassB.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassB.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassB.class.getSimpleName()); - when(etMock.getAttribute(OWLClassB.getStrAttField().getName())).thenReturn(strAttMock); + initEntityType(etMock, OWLClassB.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, propsMock, idMock)); @@ -293,7 +293,6 @@ public static void initOWLClassBMocks(IdentifiableEntityType etMock, initProperties(etMock, propsMock, new AttributeInfo(OWLClassB.getPropertiesField(), null), String.class, String.class); initIdentifier(etMock, idMock, OWLClassB.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(etMock.getDeclaredAttribute(anyString())).thenAnswer(args -> { final String name = args.getArgument(0); if (Objects.equals(name, strAttMock.getName())) { @@ -320,11 +319,8 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, RdfContainerAttributeImpl rdfSeqMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassC.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassC.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassC.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassC.class.getSimpleName()); - when(etMock.getAttribute(OWLClassC.getRdfSeqField().getName())).thenReturn(rdfSeqMock); + initEntityType(etMock, OWLClassC.class, EntityLifecycleListenerManager.empty()); + when(etMock.getAttributes()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(simpleListMock, refListMock, rdfSeqMock, idMock)); initListAttribute(etMock, simpleListMock, new AttributeInfo(OWLClassC.getSimpleListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) @@ -333,7 +329,7 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, initListAttribute(etMock, refListMock, new AttributeInfo(OWLClassC.getRefListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) .elementType(OWLClassA.class) .valueType(etAMock)); - + when(etMock.getAttribute(OWLClassC.getRdfSeqField().getName())).thenReturn(rdfSeqMock); when(rdfSeqMock.getJavaField()).thenReturn(OWLClassC.getRdfSeqField()); when(rdfSeqMock.getFetchType()).thenReturn(FetchType.EAGER); when(rdfSeqMock.getCollectionType()).thenReturn(CollectionType.LIST); @@ -354,7 +350,6 @@ public static void initOWLClassCMocks(IdentifiableEntityType etMock, initIdentifier(etMock, idMock, OWLClassC.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(etMock.getDeclaredAttribute(anyString())).thenAnswer(args -> { final String name = args.getArgument(0); if (Objects.equals(name, refListMock.getName())) { @@ -384,41 +379,27 @@ private static void initListAttribute(IdentifiableEntityType etMock, List public static void initOWLClassDMocks(IdentifiableEntityType etMock, SingularAttributeImpl clsAMock, IdentifiableEntityType etA, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassD.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassD.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassD.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassD.class.getSimpleName()); - when(etMock.getAttribute(OWLClassD.getOwlClassAField().getName())).thenReturn(clsAMock); + initEntityType(etMock, OWLClassD.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsAMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, idMock)); initAttribute(etMock, clsAMock, new AttributeInfo(OWLClassD.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT).valueType(etA)); - when(etMock.getFieldSpecification(clsAMock.getName())).thenReturn(clsAMock); initIdentifier(etMock, idMock, OWLClassD.getUriField(), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassEMocks(IdentifiableEntityType etMock, SingularAttributeImpl strAttMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassE.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassE.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassE.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassE.class.getSimpleName()); + initEntityType(etMock, OWLClassE.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(strAttMock, idMock)); initAttribute(etMock, strAttMock, new AttributeInfo(OWLClassE.getStrAttField(), Attribute.PersistentAttributeType.DATA) .language(Generators.LANG)); initIdentifier(etMock, idMock, OWLClassE.class.getDeclaredField("uri"), true); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassFMocks(IdentifiableEntityType etMock, AbstractPluralAttribute setAMock, SingularAttributeImpl strAttMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassF.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassF.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassF.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassF.class.getSimpleName()); - when(etMock.getAttribute(OWLClassF.getSimpleSetField().getName())).thenReturn(setAMock); + initEntityType(etMock, OWLClassF.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Set.of(setAMock, strAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, strAttMock, idMock)); initAttribute(etMock, setAMock, new AttributeInfo(OWLClassF.getSimpleSetField(), @@ -428,34 +409,24 @@ public static void initOWLClassFMocks(IdentifiableEntityType etMock, .language(Generators.LANG)); initIdentifier(etMock, idMock, OWLClassF.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassGMocks(IdentifiableEntityType etMock, SingularAttributeImpl clsHMock, IdentifiableEntityType etHMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassG.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassG.class)); - when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassG.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassG.class.getSimpleName()); + initEntityType(etMock, OWLClassG.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsHMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsHMock, idMock)); initAttribute(etMock, clsHMock, new AttributeInfo(OWLClassG.getOwlClassHField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etHMock)); initIdentifier(etMock, idMock, OWLClassG.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassHMocks(IdentifiableEntityType etMock, SingularAttributeImpl clsAMock, SingularAttributeImpl clsGMock, IdentifiableEntityType etAMock, IdentifiableEntityType etGMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassH.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassH.class)); - when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassH.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassH.class.getSimpleName()); + initEntityType(etMock, OWLClassH.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Set.of(clsAMock, clsGMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsAMock, clsGMock, idMock)); @@ -464,69 +435,49 @@ public static void initOWLClassHMocks(IdentifiableEntityType etMock, initAttribute(etMock, clsGMock, new AttributeInfo(OWLClassH.getOwlClassGField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etGMock)); initIdentifier(etMock, idMock, OWLClassH.class.getDeclaredField("uri"), false); - - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassIMocks(IdentifiableEntityType etMock, SingularAttributeImpl aAttMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassI.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassI.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassI.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassI.class.getSimpleName()); + initEntityType(etMock, OWLClassI.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(aAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(aAttMock, idMock)); initAttribute(etMock, aAttMock, new AttributeInfo(OWLClassI.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etAMock)); initIdentifier(etMock, idMock, OWLClassI.class.getDeclaredField("uri"), false); - - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassJMocks(IdentifiableEntityType etMock, AbstractPluralAttribute setAMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassJ.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassJ.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassJ.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassJ.class.getSimpleName()); - when(etMock.getAttribute(OWLClassJ.getOwlClassAField().getName())).thenReturn(setAMock); + initEntityType(etMock, OWLClassJ.class,EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(setAMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(setAMock, idMock)); initAttribute(etMock, setAMock, new AttributeInfo(OWLClassJ.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.SET) .elementType(OWLClassA.class).valueType(etAMock)); initIdentifier(etMock, idMock, OWLClassJ.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassKMocks(IdentifiableEntityType etMock, SingularAttributeImpl clsEMock, IdentifiableEntityType etEMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(OWLClassK.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassK.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassK.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassK.class.getSimpleName()); + initEntityType(etMock, OWLClassK.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(clsEMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(clsEMock, idMock)); initAttribute(etMock, clsEMock, new AttributeInfo(OWLClassK.getOwlClassEField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etEMock)); initIdentifier(etMock, idMock, OWLClassK.class.getDeclaredField("uri"), false); - - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassLMocks(IdentifiableEntityType etMock, ListAttributeImpl refListMock, ListAttributeImpl simpleListMock, AbstractPluralAttribute setMock, SingularAttributeImpl singleAMock, IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException { - when(etMock.getJavaType()).thenReturn(OWLClassL.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassL.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassL.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassL.class.getSimpleName()); + initEntityType(etMock, OWLClassL.class, EntityLifecycleListenerManager.empty()); initIdentifier(etMock, idMock, OWLClassL.class.getDeclaredField("uri"), false); when(etMock.getDeclaredAttributes()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock)); when(etMock.getAttributes()).thenReturn(Set.of(refListMock, simpleListMock, setMock, singleAMock)); @@ -552,7 +503,6 @@ public static void initOWLClassLMocks(IdentifiableEntityType etMock, .value())); initAttribute(etMock, singleAMock, new AttributeInfo(OWLClassL.getSingleAField(), Attribute.PersistentAttributeType.OBJECT) .valueType(etAMock).nonEmpty()); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassMMock(IdentifiableEntityType etMock, SingularAttributeImpl booleanAtt, @@ -567,10 +517,7 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, S SingularAttributeImpl mObjectOneOfEnumAttribute, Identifier idMock) throws Exception { - when(etMock.getJavaType()).thenReturn(OWLClassM.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassM.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassM.getClassIri())); - when(etMock.getName()).thenReturn(OWLClassM.class.getSimpleName()); + initEntityType(etMock, OWLClassM.class, EntityLifecycleListenerManager.empty()); initIdentifier(etMock, idMock, OWLClassM.getUriField(), false); when(etMock.getAttributes()).thenReturn(Set.of(booleanAtt, intAtt, longAtt, doubleAtt, dateAtt, characterAtt, enumAtt, ordinalEnumAtt, intSetAtt, lexicalFormAtt, @@ -600,8 +547,6 @@ public static void initOWLClassMMock(IdentifiableEntityType etMock, S .elementType(Integer.class) .valueType(BasicTypeImpl.get(Integer.class)) .converter(new ToIntegerConverter())); - - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassNMock(IdentifiableEntityType et, SingularAttributeImpl annotationAtt, @@ -609,10 +554,7 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu AbstractPluralAttribute pluralAnnotationAtt, PropertiesSpecification props, Identifier idN) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassN.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassN.class)); - when(et.getName()).thenReturn(OWLClassN.class.getSimpleName()); - when(et.getIRI()).thenReturn(IRI.create(OWLClassN.getClassIri())); + initEntityType(et, OWLClassN.class, EntityLifecycleListenerManager.empty()); initIdentifier(et, idN, OWLClassN.getUriField(), false); when(et.getFieldSpecifications()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt, props, idN)); when(et.getAttributes()).thenReturn(Set.of(annotationAtt, annotationUriAtt, stringAtt, pluralAnnotationAtt)); @@ -626,21 +568,16 @@ public static void initOWLClassNMock(IdentifiableEntityType et, Singu .valueType(BasicTypeImpl.get(String.class))); initProperties(et, props, new AttributeInfo(OWLClassN.getPropertiesField(), null), SingularAttribute.class, String.class); - when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOWLClassOMock(IdentifiableEntityType et, SingularAttributeImpl stringAtt, Identifier idO) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassO.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassO.class)); - when(et.getIdentifier()).thenReturn(idO); - when(et.getName()).thenReturn(OWLClassO.class.getSimpleName()); + initEntityType(et, OWLClassO.class, EntityLifecycleListenerManager.empty()); initIdentifier(et, idO, OWLClassO.getUriField(), false); when(et.getAttributes()).thenReturn(Collections.singleton(stringAtt)); when(et.getFieldSpecifications()).thenReturn(Set.of(stringAtt, idO)); initAttribute(et, stringAtt, new AttributeInfo(OWLClassO.getStringAttributeField(), Attribute.PersistentAttributeType.DATA)); - when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(et.getFieldSpecification(anyString())).thenThrow(IllegalArgumentException.class); } @@ -650,10 +587,7 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types ListAttributeImpl simpleListAtt, ListAttributeImpl refListAtt, Identifier idP) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassP.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassP.class)); - when(et.getName()).thenReturn(OWLClassP.class.getSimpleName()); - when(et.getIRI()).thenReturn(IRI.create(OWLClassP.getClassIri())); + initEntityType(et, OWLClassP.class, EntityLifecycleListenerManager.empty()); initIdentifier(et, idP, OWLClassP.getUriField(), false); when(et.getFieldSpecifications()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt, props, types, idP)); when(et.getAttributes()).thenReturn(Set.of(uriAtt, urlsAtt, simpleListAtt, refListAtt)); @@ -668,8 +602,6 @@ public static void initOWLClassPMock(IdentifiableEntityType et, Types initListAttribute(et, refListAtt, new AttributeInfo(OWLClassP.getReferencedListField(), Attribute.PersistentAttributeType.OBJECT).collectionType(CollectionType.LIST) .elementType(URI.class) .valueType(BasicTypeImpl.get(URI.class))); - - when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initOwlClassQMock(IdentifiableEntityType et, @@ -678,11 +610,8 @@ public static void initOwlClassQMock(IdentifiableEntityType et, SingularAttributeImpl qLabelAtt, SingularAttributeImpl qOwlClassAAtt, Identifier idQ) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassQ.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassQ.class)); - when(et.getName()).thenReturn(OWLClassQ.class.getSimpleName()); + initEntityType(et, OWLClassQ.class, EntityLifecycleListenerManager.empty()); initIdentifier(et, idQ, OWLClassQ.getUriField(), false); - when(et.getIRI()).thenReturn(IRI.create(OWLClassQ.getClassIri())); when(et.getSupertypes()).thenReturn(Collections.singleton(superclassType)); when(superclassType.getSubtypes()).thenReturn(Collections.singleton(et)); when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); @@ -693,7 +622,6 @@ public static void initOwlClassQMock(IdentifiableEntityType et, initAttribute(et, qParentStringAtt, new AttributeInfo(OWLClassQ.getParentStringField(), Attribute.PersistentAttributeType.DATA)); initAttribute(et, qLabelAtt, new AttributeInfo(OWLClassQ.getLabelField(), Attribute.PersistentAttributeType.ANNOTATION)); initAttribute(et, qOwlClassAAtt, new AttributeInfo(OWLClassQ.getOwlClassAField(), Attribute.PersistentAttributeType.OBJECT)); - when(et.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); when(et.getDeclaredAttribute(anyString())).thenAnswer(arg -> { if (Objects.equals(arg.getArgument(0), qStringAtt.getName())) { return qStringAtt; @@ -717,33 +645,39 @@ private static void initQMappedSuperclassMock(MappedSuperclassTypeImpl et, SingularAttributeImpl sNameAtt, TypesSpecification sTypes, Identifier idS) throws Exception { - when(et.getName()).thenReturn(OWLClassS.class.getSimpleName()); - initIdentifier(et, idS, OWLClassS.getUriField(), true); - when(et.getJavaType()).thenReturn(OWLClassS.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassS.class)); - when(et.getIRI()).thenReturn(IRI.create(OWLClassS.getClassIri())); + final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); + addLifecycleCallback(listenerManager, PRE_PERSIST, OWLClassS.getPrePersistHook()); + when(et.getLifecycleListenerManager()).thenReturn(listenerManager); + initEntityType(et, OWLClassS.class, listenerManager); when(et.getFieldSpecifications()).thenReturn(Set.of(sNameAtt, sTypes, idS)); when(et.getAttributes()).thenReturn(Collections.singleton(sNameAtt)); - when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); + initIdentifier(et, idS, OWLClassS.getUriField(), true); initAttribute(et, sNameAtt, new AttributeInfo(OWLClassS.getNameField(), Attribute.PersistentAttributeType.ANNOTATION)); initTypesAttribute(et, sTypes, new AttributeInfo(OWLClassS.getTypesField(), null).elementType(String.class)); - final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); - addLifecycleCallback(listenerManager, PRE_PERSIST, OWLClassS.getPrePersistHook()); - when(et.getLifecycleListenerManager()).thenReturn(listenerManager); } static void initOwlClassRMock(IdentifiableEntityType et, SingularAttributeImpl rStringAtt, SingularAttributeImpl owlClassAAtt, IdentifiableEntityType parentEt) throws Exception { + final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); + final Method addParent = EntityLifecycleListenerManager.class + .getDeclaredMethod("addParent", EntityLifecycleListenerManager.class); + addParent.setAccessible(true); + addParent.invoke(listenerManager, parentEt.getLifecycleListenerManager()); + addLifecycleCallback(listenerManager, PRE_PERSIST, OWLClassR.getPrePersistHook()); + addLifecycleCallback(listenerManager, POST_PERSIST, OWLClassR.getPostPersistHook()); + addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassR.getPreUpdateHook()); + addLifecycleCallback(listenerManager, POST_UPDATE, OWLClassR.getPostUpdateHook()); + addLifecycleCallback(listenerManager, PRE_REMOVE, OWLClassR.getPreRemoveHook()); + addLifecycleCallback(listenerManager, POST_REMOVE, OWLClassR.getPostRemoveHook()); + addLifecycleCallback(listenerManager, POST_LOAD, OWLClassR.getPostLoadHook()); + when(et.getLifecycleListenerManager()).thenReturn(listenerManager); + + initEntityType(et, OWLClassR.class, listenerManager); final Identifier id = parentEt.getIdentifier(); when(et.getIdentifier()).thenReturn(id); - when(et.getJavaType()).thenReturn(OWLClassR.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassR.class)); - when(et.getIRI()).thenReturn(IRI.create(OWLClassR.getClassIri())); - when(et.getName()).thenReturn(OWLClassR.class.getSimpleName()); final Set attributes = new HashSet<>(parentEt.getAttributes()); - when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); attributes.add(rStringAtt); attributes.add(owlClassAAtt); final Set fieldSpecs = new HashSet(parentEt.getFieldSpecifications()); @@ -764,19 +698,6 @@ static void initOwlClassRMock(IdentifiableEntityType et, SingularAttr for (FieldSpecification fs : parentEt.getFieldSpecifications()) { when(et.getFieldSpecification(fs.getName())).thenReturn(fs); } - final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); - final Method addParent = EntityLifecycleListenerManager.class - .getDeclaredMethod("addParent", EntityLifecycleListenerManager.class); - addParent.setAccessible(true); - addParent.invoke(listenerManager, parentEt.getLifecycleListenerManager()); - addLifecycleCallback(listenerManager, PRE_PERSIST, OWLClassR.getPrePersistHook()); - addLifecycleCallback(listenerManager, POST_PERSIST, OWLClassR.getPostPersistHook()); - addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassR.getPreUpdateHook()); - addLifecycleCallback(listenerManager, POST_UPDATE, OWLClassR.getPostUpdateHook()); - addLifecycleCallback(listenerManager, PRE_REMOVE, OWLClassR.getPreRemoveHook()); - addLifecycleCallback(listenerManager, POST_REMOVE, OWLClassR.getPostRemoveHook()); - addLifecycleCallback(listenerManager, POST_LOAD, OWLClassR.getPostLoadHook()); - when(et.getLifecycleListenerManager()).thenReturn(listenerManager); } static void initOwlClassSListeners(IdentifiableEntityType etS, @@ -828,14 +749,10 @@ static void initOwlClassTMock(IdentifiableEntityType et, SingularAttr SingularAttributeImpl localDateTimeAtt, SingularAttributeImpl owlClassSAtt, IdentifiableEntityType etS, Identifier id) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassT.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassT.class)); + initEntityType(et, OWLClassT.class, etS.getLifecycleListenerManager()); initIdentifier(et, id, OWLClassT.getUriField(), true); - when(et.getIRI()).thenReturn(IRI.create(OWLClassT.getClassIri())); - when(et.getName()).thenReturn(OWLClassT.class.getSimpleName()); when(et.getFieldSpecifications()).thenReturn(Set.of(localDateAtt, localDateTimeAtt, id)); when(et.getAttributes()).thenReturn(Set.of(localDateAtt, localDateTimeAtt)); - when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); initAttribute(et, localDateAtt, new AttributeInfo(OWLClassT.getLocalDateField(), Attribute.PersistentAttributeType.DATA) .converter(DefaultConverterWrapper.INSTANCE)); @@ -848,14 +765,13 @@ static void initOwlClassTMock(IdentifiableEntityType et, SingularAttr static void initOwlClassUMocks(IdentifiableEntityType et, SingularAttributeImpl singularStringAtt, AbstractPluralAttribute pluralStringAtt, SingularAttributeImpl modified, Identifier id) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassU.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassU.class)); + final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); + addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassU.class.getDeclaredMethod("preUpdate")); + when(et.getLifecycleListenerManager()).thenReturn(listenerManager); + initEntityType(et, OWLClassU.class, listenerManager); initIdentifier(et, id, OWLClassU.getIdField(), true); - when(et.getIRI()).thenReturn(IRI.create(OWLClassU.getClassIri())); - when(et.getName()).thenReturn(OWLClassU.class.getSimpleName()); when(et.getFieldSpecifications()).thenReturn(Set.of(singularStringAtt, pluralStringAtt, modified, id)); when(et.getAttributes()).thenReturn(Set.of(singularStringAtt, pluralStringAtt, modified)); - when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); initAttribute(et, singularStringAtt, new AttributeInfo(OWLClassU.getSingularStringAttField(), Attribute.PersistentAttributeType.DATA).language(null)); initAttribute(et, modified, new AttributeInfo(OWLClassU.getModifiedField(), Attribute.PersistentAttributeType.DATA).converter(new LocalDateTimeConverter()) @@ -863,25 +779,17 @@ static void initOwlClassUMocks(IdentifiableEntityType et, SingularAtt initAttribute(et, pluralStringAtt, new AttributeInfo(OWLClassU.getPluralStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.SET) .elementType(MultilingualString.class) .language(null)); - - final EntityLifecycleListenerManager listenerManager = new EntityLifecycleListenerManager(); - addLifecycleCallback(listenerManager, PRE_UPDATE, OWLClassU.class.getDeclaredMethod("preUpdate")); - when(et.getLifecycleListenerManager()).thenReturn(listenerManager); } static void initOwlClassWMocks(IdentifiableEntityType et, AbstractPluralAttribute setStringAtt, ListAttributeImpl listStringAtt, AbstractPluralAttribute collectionStringAtt, AbstractQueryAttribute setQueryStringAtt, AbstractQueryAttribute listQueryStringAtt, Identifier id) throws Exception { - when(et.getJavaType()).thenReturn(OWLClassW.class); - when(et.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassW.class)); + initEntityType(et, OWLClassW.class, EntityLifecycleListenerManager.empty()); initIdentifier(et, id, OWLClassW.getIdField(), true); - when(et.getIRI()).thenReturn(IRI.create(OWLClassW.getClassIri())); - when(et.getName()).thenReturn(OWLClassW.class.getSimpleName()); when(et.getFieldSpecifications()) .thenReturn(Set.of(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt, id)); when(et.getAttributes()).thenReturn((Set) Set.of(setStringAtt, listStringAtt, collectionStringAtt, setQueryStringAtt, listQueryStringAtt)); - when(et.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); initAttribute(et, setStringAtt, new AttributeInfo(OWLClassW.getSetStringAttField(), Attribute.PersistentAttributeType.DATA).collectionType(CollectionType.SET) @@ -922,13 +830,9 @@ static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType etAMock, Identifier idMock) throws NoSuchFieldException { - when(etMock.getJavaType()).thenReturn(OWLClassWithQueryAttr.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(OWLClassWithQueryAttr.class)); - when(etMock.getIRI()).thenReturn(IRI.create(OWLClassWithQueryAttr.getClassIri())); - when(etMock.getAttribute(OWLClassWithQueryAttr.getStrAttField().getName())).thenReturn(strAttMock); + initEntityType(etMock, OWLClassWithQueryAttr.class, EntityLifecycleListenerManager.empty()); when(etMock.getQueryAttribute(OWLClassWithQueryAttr.getStrQueryAttField() .getName())).thenReturn(strQueryAttMock); - when(etMock.getName()).thenReturn(OWLClassWithQueryAttr.class.getSimpleName()); when(etMock.getAttributes()).thenReturn(Set.of(strAttMock, entityAttMock)); when(etMock.getQueryAttributes()).thenReturn(Set.of(strQueryAttMock, entityQueryAttMock)); @@ -959,21 +863,15 @@ static void initOWLClassWithQueryAttrMocks(IdentifiableEntityType etMock, SingularAttributeImpl phoneNumberAttMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(Phone.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(Phone.class)); - when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(etMock.getIRI()).thenReturn(IRI.create(Vocabulary.c_Phone)); - when(etMock.getName()).thenReturn(Phone.class.getSimpleName()); + initEntityType(etMock, Phone.class, EntityLifecycleListenerManager.empty()); when(etMock.getAttributes()).thenReturn(Collections.singleton(phoneNumberAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(phoneNumberAttMock, idMock)); initAttribute(etMock, phoneNumberAttMock, new AttributeInfo(Phone.class.getDeclaredField("number"), Attribute.PersistentAttributeType.DATA)); initIdentifier(etMock, idMock, Phone.class.getDeclaredField("uri"), false); - when(etMock.getLifecycleListenerManager()).thenReturn(EntityLifecycleListenerManager.empty()); } public static void initPersonMocks(IdentifiableEntityType etMock, SingularAttributeImpl usernameAttMock, @@ -982,11 +880,7 @@ public static void initPersonMocks(IdentifiableEntityType etMock, Singul AbstractIdentifiableType etPhone, TypesSpecification typesMock, Identifier idMock) throws NoSuchFieldException, SecurityException { - when(etMock.getJavaType()).thenReturn(Person.class); - when(etMock.getInstantiableJavaType()).thenReturn((Class) instantiableTypeGenerator.generate(Person.class)); - when(etMock.getPersistenceType()).thenReturn(Type.PersistenceType.ENTITY); - when(etMock.getIRI()).thenReturn(IRI.create(Vocabulary.c_Person)); - when(etMock.getName()).thenReturn(Person.class.getSimpleName()); + initEntityType(etMock, Person.class, EntityLifecycleListenerManager.empty()); initIdentifier(etMock, idMock, Person.class.getDeclaredField("uri"), false); when(etMock.getAttributes()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock)); when(etMock.getFieldSpecifications()).thenReturn(Set.of(usernameAttMock, genderAttMock, ageAttMock, phoneAttMock, idMock)); From 972f1eb593eee7809893cf53b97fb020751a46c1 Mon Sep 17 00:00:00 2001 From: luxbe Date: Thu, 9 Jan 2025 15:06:02 +0100 Subject: [PATCH 13/17] [Bug #306] trigger LazyLoading in contains method --- .../kbss/jopa/model/EntityManagerImpl.java | 24 ++++++++++++++++--- .../test/runner/RetrieveOperationsRunner.java | 14 ++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java index f8bffe87a..c18014386 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java @@ -28,6 +28,8 @@ import cz.cvut.kbss.jopa.model.query.TypedQuery; import cz.cvut.kbss.jopa.model.query.criteria.CriteriaBuilder; import cz.cvut.kbss.jopa.model.query.criteria.CriteriaQuery; +import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy; +import cz.cvut.kbss.jopa.proxy.reference.EntityReferenceProxy; import cz.cvut.kbss.jopa.query.criteria.CriteriaParameterFiller; import cz.cvut.kbss.jopa.sessions.ServerSession; import cz.cvut.kbss.jopa.sessions.UnitOfWork; @@ -396,15 +398,31 @@ public void detach(Object entity) { public boolean contains(Object entity) { try { ensureOpen(); - Objects.requireNonNull(entity); - checkClassIsValidEntity(entity.getClass()); - return getCurrentPersistenceContext().contains(entity); + + Object loadedEntity = getLoadedEntity(entity); + + Objects.requireNonNull(loadedEntity); + checkClassIsValidEntity(loadedEntity.getClass()); + return getCurrentPersistenceContext().contains(loadedEntity); } catch (RuntimeException e) { markTransactionForRollback(); throw e; } } + /** + * This method loads the entity if it is lazy loaded. + * If the entity is not a {@link LazyLoadingProxy}, nothing happens + * + * @param entity - the entity to check and load + * @return the lazy loaded entity or the original + */ + private Object getLoadedEntity(Object entity) { + return entity instanceof LazyLoadingProxy proxy + ? proxy.triggerLazyLoading() + : entity; + } + @Override public void close() { ensureOpen(); diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java index 6da0373ae..857334c80 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/RetrieveOperationsRunner.java @@ -18,7 +18,6 @@ package cz.cvut.kbss.jopa.test.runner; import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties; -import cz.cvut.kbss.jopa.model.SequencesVocabulary; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import cz.cvut.kbss.jopa.model.query.TypedQuery; @@ -47,7 +46,6 @@ import cz.cvut.kbss.jopa.test.environment.Generators; import cz.cvut.kbss.jopa.test.environment.PersistenceFactory; import cz.cvut.kbss.jopa.test.environment.Quad; -import cz.cvut.kbss.jopa.test.environment.TestEnvironment; import cz.cvut.kbss.jopa.vocabulary.RDF; import cz.cvut.kbss.jopa.vocabulary.XSD; import cz.cvut.kbss.ontodriver.ReloadableDataSource; @@ -74,7 +72,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -186,6 +183,17 @@ void testRetrieveWithLazyAttribute() { assertTrue(em.contains(resI.getOwlClassA())); } + @Test + void testContainsTriggersLoadingLazyLoadedAttribute() { + this.em = getEntityManager("ContainsTriggersLazyLoading", false); + persist(entityI); + + final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri()); + assertInstanceOf(LazyLoadingProxy.class, resI.getOwlClassA()); + assertTrue(em.contains(resI.getOwlClassA())); + assertInstanceOf(OWLClassA.class, resI.getOwlClassA()); + } + @Test void testRetrieveWithGeneratedId() { this.em = getEntityManager("RetrieveGenerated", false); From 3f8f4803641cea081f94068a36ffcd3e95e59e96 Mon Sep 17 00:00:00 2001 From: luxbe Date: Thu, 9 Jan 2025 16:50:41 +0100 Subject: [PATCH 14/17] [Bug #306] trigger LazyLoading in remove method --- .../kbss/jopa/model/EntityManagerImpl.java | 25 +++++++++++-------- .../test/runner/DeleteOperationsRunner.java | 20 +++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java index c18014386..b2f380b6e 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/model/EntityManagerImpl.java @@ -247,27 +247,30 @@ private void mergeX(Attribute at, Object merged, Object toMerge, Descripto } @Override - public void remove(Object object) { + public void remove(Object entity) { try { ensureOpen(); - Objects.requireNonNull(object); - checkClassIsValidEntity(object.getClass()); - if (isCascadingCycle(object)) { - LOG.warn("Remove cascading cycle detected in instance {}.", object); + + final Object loadedEntity = getLoadedEntity(entity); + Objects.requireNonNull(loadedEntity); + + checkClassIsValidEntity(loadedEntity.getClass()); + if (isCascadingCycle(loadedEntity)) { + LOG.warn("Remove cascading cycle detected in instance {}.", loadedEntity); return; } - switch (getState(object)) { + switch (getState(loadedEntity)) { case MANAGED_NEW: case MANAGED: - getCurrentPersistenceContext().removeObject(object); - registerProcessedInstance(object); + getCurrentPersistenceContext().removeObject(loadedEntity); + registerProcessedInstance(loadedEntity); // Intentional fall-through case REMOVED: - new OneLevelRemoveCascadeExplorer(this::remove).start(this, object, CascadeType.REMOVE); + new OneLevelRemoveCascadeExplorer(this::remove).start(this, loadedEntity, CascadeType.REMOVE); break; default: - throw new IllegalArgumentException("Entity " + object + " is not managed and cannot be removed."); + throw new IllegalArgumentException("Entity " + loadedEntity + " is not managed and cannot be removed."); } } catch (RuntimeException e) { markTransactionForRollback(); @@ -399,7 +402,7 @@ public boolean contains(Object entity) { try { ensureOpen(); - Object loadedEntity = getLoadedEntity(entity); + final Object loadedEntity = getLoadedEntity(entity); Objects.requireNonNull(loadedEntity); checkClassIsValidEntity(loadedEntity.getClass()); diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java index c9002128b..12c9e6803 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java @@ -21,6 +21,7 @@ import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; +import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy; import cz.cvut.kbss.jopa.test.*; import cz.cvut.kbss.jopa.test.environment.DataAccessor; import cz.cvut.kbss.jopa.test.environment.Generators; @@ -500,4 +501,23 @@ void removeDeletesSimpleLiteralStatement() { em.createNativeQuery("ASK { ?x ?y ?z .}", Boolean.class).setParameter("x", URI.create(entityM.getKey())) .getSingleResult()); } + + @Test + void removeTriggersLazyLoading() { + this.em = getEntityManager("removeTriggersLazyLoading", true); + persist(entityI); + + em.getEntityManagerFactory().getCache().evictAll(); + final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri()); + assertInstanceOf(LazyLoadingProxy.class, resI.getOwlClassA()); + + em.getTransaction().begin(); + em.remove(resI.getOwlClassA()); + em.getTransaction().commit(); + + assertNull(em.find(OWLClassA.class, entityA.getUri())); + assertFalse( + em.createNativeQuery("ASK { ?x ?y ?z .}", Boolean.class).setParameter("x", entityA.getUri()) + .getSingleResult()); + } } From 80e41a147ed4e942387ad31a39442e5b268c600e Mon Sep 17 00:00:00 2001 From: luxbe Date: Wed, 15 Jan 2025 10:33:42 +0100 Subject: [PATCH 15/17] [Bug #306] override removeTriggersLazyLoading test in OWL integration tests to account for implementation differences --- .../owlapi/DeleteOperationsTest.java | 31 +++++++++++++++++++ .../test/runner/DeleteOperationsRunner.java | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/jopa-integration-tests-owlapi/src/test/java/cz/cvut/kbss/jopa/test/integration/owlapi/DeleteOperationsTest.java b/jopa-integration-tests-owlapi/src/test/java/cz/cvut/kbss/jopa/test/integration/owlapi/DeleteOperationsTest.java index b66b9cb79..d81e2637d 100644 --- a/jopa-integration-tests-owlapi/src/test/java/cz/cvut/kbss/jopa/test/integration/owlapi/DeleteOperationsTest.java +++ b/jopa-integration-tests-owlapi/src/test/java/cz/cvut/kbss/jopa/test/integration/owlapi/DeleteOperationsTest.java @@ -17,6 +17,10 @@ */ package cz.cvut.kbss.jopa.test.integration.owlapi; +import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy; +import cz.cvut.kbss.jopa.test.OWLClassA; +import cz.cvut.kbss.jopa.test.OWLClassI; +import cz.cvut.kbss.jopa.test.Vocabulary; import cz.cvut.kbss.jopa.test.environment.OwlapiDataAccessor; import cz.cvut.kbss.jopa.test.environment.OwlapiPersistenceFactory; import cz.cvut.kbss.jopa.test.runner.DeleteOperationsRunner; @@ -25,6 +29,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; + public class DeleteOperationsTest extends DeleteOperationsRunner { private static final Logger LOG = LoggerFactory.getLogger(DeleteOperationsTest.class); @@ -46,4 +54,27 @@ public void settingDatatypeCollectionToNullRemovesAllValues() { public void clearingDatatypeCollectionRemovesAllValues() { // Another issue with OWL2Query, causes reasoner exception } + + @Test + @Override + public void removeTriggersLazyLoading() { + // OWL doesn't remove all statements, unlike RDF4J and Jena, but keeps certain top-level attributes. That's why this test has to be overriden + this.em = getEntityManager("removeTriggersLazyLoading", true); + persist(entityI); + + em.getEntityManagerFactory().getCache().evictAll(); + final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri()); + assertInstanceOf(LazyLoadingProxy.class, resI.getOwlClassA()); + + em.getTransaction().begin(); + em.remove(resI.getOwlClassA()); + em.getTransaction().commit(); + + assertNull(em.find(OWLClassA.class, entityA.getUri())); + + // verify that the attributes of A were removed + assertFalse( + em.createNativeQuery("ASK { ?x <"+ Vocabulary.P_A_STRING_ATTRIBUTE + "> ?z .}", Boolean.class).setParameter("x", entityA.getUri()) + .getSingleResult()); + } } diff --git a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java index 12c9e6803..3df2535f7 100644 --- a/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java +++ b/jopa-integration-tests/src/main/java/cz/cvut/kbss/jopa/test/runner/DeleteOperationsRunner.java @@ -503,7 +503,7 @@ void removeDeletesSimpleLiteralStatement() { } @Test - void removeTriggersLazyLoading() { + public void removeTriggersLazyLoading() { this.em = getEntityManager("removeTriggersLazyLoading", true); persist(entityI); From d75a9fbee2680fa7ebfd3abf68bc8f3e028172e1 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Sat, 18 Jan 2025 18:56:19 +0100 Subject: [PATCH 16/17] [Upd] Update RDF4J to 5.1.0. --- ontodriver-rdf4j/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ontodriver-rdf4j/pom.xml b/ontodriver-rdf4j/pom.xml index 432b6956e..318f4b962 100644 --- a/ontodriver-rdf4j/pom.xml +++ b/ontodriver-rdf4j/pom.xml @@ -14,7 +14,7 @@ - 5.0.3 + 5.1.0 From 4ea77c4c1bcfc16070c496cf6e03313b42c9276c Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Sat, 18 Jan 2025 18:59:29 +0100 Subject: [PATCH 17/17] [2.2.2] Bump version, update changelog. --- CHANGELOG.md | 7 +++++++ datatype/pom.xml | 2 +- jopa-api/pom.xml | 2 +- jopa-distribution/pom.xml | 2 +- jopa-impl/pom.xml | 2 +- jopa-integration-tests-jena/pom.xml | 2 +- jopa-integration-tests-owlapi/pom.xml | 2 +- jopa-integration-tests-rdf4j/pom.xml | 2 +- jopa-integration-tests/pom.xml | 2 +- jopa-maven-plugin/pom.xml | 2 +- jopa-owl2java/pom.xml | 2 +- jopa-owlapi-utils/pom.xml | 2 +- modelgen/pom.xml | 2 +- ontodriver-api/pom.xml | 2 +- ontodriver-jena/pom.xml | 2 +- ontodriver-owlapi/pom.xml | 2 +- ontodriver-rdf4j/pom.xml | 2 +- pom.xml | 2 +- 18 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b7b57f97..4be7660a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # JOPA - Change Log +## 2.2.2 - 2025-01-18 +- Fix an issue with parsing SPARQL query parameters (Enhancement #294). +- Fix an issue with passing a lazy loading proxy to `EntityManager.remove` (Bug #306). +- Refactor test metamodel setup (GH #301). +- Allow using primitive types as mapped attribute types. +- Dependency updates: RDF4J 5.1.0. + ## 2.2.1 - 2024-12-12 - Rename `MultilingualString` in OntoDriver to `Translations` to prevent confusion with `MultilingualString` from JOPA (GH #288). - Fix a SPARQL query parsing issue (Bug #294). diff --git a/datatype/pom.xml b/datatype/pom.xml index ce20ae372..b80ad6368 100644 --- a/datatype/pom.xml +++ b/datatype/pom.xml @@ -6,7 +6,7 @@ jopa-all cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 ../pom.xml diff --git a/jopa-api/pom.xml b/jopa-api/pom.xml index 950874411..756ca5d36 100644 --- a/jopa-api/pom.xml +++ b/jopa-api/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/jopa-distribution/pom.xml b/jopa-distribution/pom.xml index 6bb579aa0..357f176df 100644 --- a/jopa-distribution/pom.xml +++ b/jopa-distribution/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/jopa-impl/pom.xml b/jopa-impl/pom.xml index 92887482b..69f9420d1 100644 --- a/jopa-impl/pom.xml +++ b/jopa-impl/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/jopa-integration-tests-jena/pom.xml b/jopa-integration-tests-jena/pom.xml index 3e7debf36..0ef2d4995 100644 --- a/jopa-integration-tests-jena/pom.xml +++ b/jopa-integration-tests-jena/pom.xml @@ -5,7 +5,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml 4.0.0 diff --git a/jopa-integration-tests-owlapi/pom.xml b/jopa-integration-tests-owlapi/pom.xml index e6f50542c..d59591727 100644 --- a/jopa-integration-tests-owlapi/pom.xml +++ b/jopa-integration-tests-owlapi/pom.xml @@ -5,7 +5,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml 4.0.0 diff --git a/jopa-integration-tests-rdf4j/pom.xml b/jopa-integration-tests-rdf4j/pom.xml index fa4f594ea..777c3d441 100644 --- a/jopa-integration-tests-rdf4j/pom.xml +++ b/jopa-integration-tests-rdf4j/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml jopa-integration-tests-rdf4j diff --git a/jopa-integration-tests/pom.xml b/jopa-integration-tests/pom.xml index 5378a2270..e9d90093c 100644 --- a/jopa-integration-tests/pom.xml +++ b/jopa-integration-tests/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml 4.0.0 diff --git a/jopa-maven-plugin/pom.xml b/jopa-maven-plugin/pom.xml index 7505095ba..97c795282 100644 --- a/jopa-maven-plugin/pom.xml +++ b/jopa-maven-plugin/pom.xml @@ -5,7 +5,7 @@ jopa-all cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 ../pom.xml 4.0.0 diff --git a/jopa-owl2java/pom.xml b/jopa-owl2java/pom.xml index 2264c1d73..6b856b1c8 100644 --- a/jopa-owl2java/pom.xml +++ b/jopa-owl2java/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/jopa-owlapi-utils/pom.xml b/jopa-owlapi-utils/pom.xml index 37fe5f21c..4e4c56aa3 100644 --- a/jopa-owlapi-utils/pom.xml +++ b/jopa-owlapi-utils/pom.xml @@ -6,7 +6,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/modelgen/pom.xml b/modelgen/pom.xml index ef9d88550..985f8d514 100644 --- a/modelgen/pom.xml +++ b/modelgen/pom.xml @@ -6,7 +6,7 @@ jopa-all cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 ../pom.xml diff --git a/ontodriver-api/pom.xml b/ontodriver-api/pom.xml index 70fbe3368..5fa0e7ea6 100644 --- a/ontodriver-api/pom.xml +++ b/ontodriver-api/pom.xml @@ -6,7 +6,7 @@ jopa-all cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 ../pom.xml diff --git a/ontodriver-jena/pom.xml b/ontodriver-jena/pom.xml index 186a41cf5..1d9012181 100644 --- a/ontodriver-jena/pom.xml +++ b/ontodriver-jena/pom.xml @@ -8,7 +8,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/ontodriver-owlapi/pom.xml b/ontodriver-owlapi/pom.xml index 6bd45ad33..20fa7d143 100644 --- a/ontodriver-owlapi/pom.xml +++ b/ontodriver-owlapi/pom.xml @@ -11,7 +11,7 @@ cz.cvut.kbss.jopa jopa-all - 2.2.1 + 2.2.2 ../pom.xml diff --git a/ontodriver-rdf4j/pom.xml b/ontodriver-rdf4j/pom.xml index 318f4b962..25d9c9eed 100644 --- a/ontodriver-rdf4j/pom.xml +++ b/ontodriver-rdf4j/pom.xml @@ -10,7 +10,7 @@ jopa-all cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 diff --git a/pom.xml b/pom.xml index 7b6827e1d..ff4bdd3cc 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 cz.cvut.kbss.jopa - 2.2.1 + 2.2.2 jopa-all pom JOPA