Skip to content

Commit

Permalink
[Fix] Ensure IndirectMultilingualStrings are unwrapped when entity is…
Browse files Browse the repository at this point in the history
… detached from persistence context.
  • Loading branch information
ledsoft committed Jan 22, 2024
1 parent 6697a58 commit db4d6a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package cz.cvut.kbss.jopa.sessions;

import cz.cvut.kbss.jopa.adapters.IndirectCollection;
import cz.cvut.kbss.jopa.adapters.IndirectWrapper;
import cz.cvut.kbss.jopa.exceptions.EntityNotFoundException;
import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
Expand Down Expand Up @@ -278,11 +277,11 @@ public void clear() {

private void detachAllManagedInstances() {
cloneMapping.forEach(instance -> {
removeIndirectCollections(instance);
removeIndirectWrappers(instance);
deregisterEntityFromPersistenceContext(instance);
});
newObjectsCloneToOriginal.keySet().forEach(instance -> {
removeIndirectCollections(instance);
removeIndirectWrappers(instance);
deregisterEntityFromPersistenceContext(instance);
});
}
Expand Down Expand Up @@ -893,7 +892,7 @@ public void unregisterObject(Object object) {
if (original != null) {
cloneBuilder.removeVisited(original, repoMap.getEntityDescriptor(object));
}
removeIndirectCollections(object);
removeIndirectWrappers(object);
deregisterEntityFromPersistenceContext(object);
unregisterEntityFromOntologyContext(object);
}
Expand Down Expand Up @@ -1102,18 +1101,18 @@ public Object createIndirectCollection(Object collection, Object owner, Field fi
}

/**
* Remove indirect collection implementations from the specified entity (if present).
* Removes {@link IndirectWrapper} instances from the specified entity (if present).
*
* @param entity The entity to remove indirect collections from
* @param entity The entity to remove indirect wrappers from
*/
private void removeIndirectCollections(Object entity) {
private void removeIndirectWrappers(Object entity) {
assert entity != null;
final EntityType<?> et = entityType(entity.getClass());
for (FieldSpecification<?, ?> fs : et.getFieldSpecifications()) {
final Object value = EntityPropertiesUtils.getFieldValue(fs.getJavaField(), entity);
if (value instanceof IndirectCollection) {
IndirectCollection<?> indCol = (IndirectCollection<?>) value;
EntityPropertiesUtils.setFieldValue(fs.getJavaField(), entity, indCol.unwrap());
if (value instanceof IndirectWrapper) {
IndirectWrapper indirectWrapper = (IndirectWrapper) value;
EntityPropertiesUtils.setFieldValue(fs.getJavaField(), entity, indirectWrapper.unwrap());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,7 @@ static void initOwlClassUMocks(IdentifiableEntityType<OWLClassU> et, SingularAtt
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);
Expand All @@ -1322,6 +1323,7 @@ static void initOwlClassUMocks(IdentifiableEntityType<OWLClassU> et, SingularAtt
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package cz.cvut.kbss.jopa.sessions;

import cz.cvut.kbss.jopa.adapters.IndirectMap;
import cz.cvut.kbss.jopa.adapters.IndirectMultilingualString;
import cz.cvut.kbss.jopa.adapters.IndirectSet;
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.OWLClassF;
import cz.cvut.kbss.jopa.environment.OWLClassL;
import cz.cvut.kbss.jopa.environment.OWLClassR;
import cz.cvut.kbss.jopa.environment.OWLClassU;
import cz.cvut.kbss.jopa.environment.Vocabulary;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.exception.IdentifierNotSetException;
Expand All @@ -35,6 +37,7 @@
import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
import cz.cvut.kbss.jopa.model.EntityManagerImpl.State;
import cz.cvut.kbss.jopa.model.LoadState;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraint;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
Expand Down Expand Up @@ -64,10 +67,13 @@
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
Expand Down Expand Up @@ -1126,4 +1132,15 @@ void isInferredThrowsIllegalArgumentExceptionWhenInstanceIsNotManaged() {
assertThrows(IllegalArgumentException.class, () -> uow.isInferred(entityD, metamodelMocks.forOwlClassD().owlClassAAtt(), entityD.getOwlClassA()));
verify(storageMock, never()).isInferred(any(), any(), any(), any());
}

@Test
void unregisterObjectRemovesIndirectMultilingualStringOfManagedObjectBeingDetached() {
when(transactionMock.isActive()).thenReturn(Boolean.TRUE);
final OWLClassU entityU = new OWLClassU(Generators.createIndividualIdentifier());
entityU.setSingularStringAtt(MultilingualString.create("test", "en"));
final OWLClassU managed = (OWLClassU) uow.registerExistingObject(entityU, descriptor);
assertInstanceOf(IndirectMultilingualString.class, managed.getSingularStringAtt());
uow.unregisterObject(managed);
assertThat(managed.getSingularStringAtt(), not(instanceOf(IndirectMultilingualString.class)));
}
}

0 comments on commit db4d6a7

Please sign in to comment.