Skip to content

Method: testUpdateObjectPropertyInMappedSuperclass()

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.runner;
16:
17: import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
18: import cz.cvut.kbss.jopa.exceptions.RollbackException;
19: import cz.cvut.kbss.jopa.test.*;
20: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
23: import org.junit.jupiter.api.Test;
24: import org.slf4j.Logger;
25:
26: import static org.hamcrest.MatcherAssert.assertThat;
27: import static org.hamcrest.Matchers.instanceOf;
28: import static org.junit.jupiter.api.Assertions.*;
29:
30: public abstract class UpdateOperationsWithInheritanceRunner extends BaseInheritanceRunner {
31:
32: public UpdateOperationsWithInheritanceRunner(Logger logger, PersistenceFactory persistenceFactory,
33: DataAccessor dataAccessor) {
34: super(logger, persistenceFactory, dataAccessor);
35: }
36:
37: @Test
38: void testUpdateFieldsOfEntityWithMappedSuperclass() {
39: this.em = getEntityManager("UpdateEntityWithMappedSuperclass", true);
40: persist(entityQ, entityA);
41:
42: entityQ.setStringAttribute("newStringAttribute");
43: entityQ.setParentString("newParentStringAttribute");
44: entityQ.setLabel("newLabel");
45: em.getTransaction().begin();
46: em.merge(entityQ);
47: em.getTransaction().commit();
48:
49: final OWLClassQ res = findRequired(OWLClassQ.class, entityQ.getUri());
50: assertEquals(entityQ.getStringAttribute(), res.getStringAttribute());
51: assertEquals(entityQ.getParentString(), res.getParentString());
52: assertEquals(entityQ.getLabel(), res.getLabel());
53: }
54:
55: @Test
56: void testUpdateObjectPropertyInMappedSuperclass() {
57: this.em = getEntityManager("UpdateObjectPropertyInMappedSuperclass", true);
58: persist(entityQ, entityA);
59: final OWLClassA entityA2 = new OWLClassA(Generators.generateUri());
60: entityA2.setStringAttribute("entityA2StringAttribute");
61:
62: entityQ.setOwlClassA(entityA2);
63: em.getTransaction().begin();
64: em.merge(entityQ);
65: em.persist(entityA2);
66: em.getTransaction().commit();
67:
68: final OWLClassQ res = findRequired(OWLClassQ.class, entityQ.getUri());
69: assertNotNull(res.getOwlClassA());
70: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
71: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
72: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
73: }
74:
75: @Test
76: void settingNonEmptyFieldInMappedSuperclassThrowsICViolationOnMerge() {
77: this.em = getEntityManager("SettingNonEmptyFieldInMappedSuperclassThrowsICViolation", true);
78: persist(entityQ, entityA);
79:
80: final RollbackException ex = assertThrows(RollbackException.class, () -> {
81: entityQ.setOwlClassA(null);
82: em.getTransaction().begin();
83: em.merge(entityQ);
84: em.getTransaction().commit();
85: });
86: assertThat(ex.getCause(), instanceOf(IntegrityConstraintViolatedException.class));
87:
88: }
89:
90: @Test
91: void testUpdateDataPropertyInEntitySuperclass() {
92: this.em = getEntityManager("updateDataPropertyInEntitySuperclass", true);
93: persist(entityT, entityA);
94:
95: final String newName = "newName";
96: final int newInt = Generators.randomInt(Integer.MAX_VALUE);
97: entityT.setName(newName);
98: entityT.setIntAttribute(newInt);
99: final String newDescription = "new entity description";
100: em.getTransaction().begin();
101: final OWLClassT merged = em.merge(entityT);
102: merged.setDescription(newDescription);
103: em.getTransaction().commit();
104:
105: final OWLClassT result = findRequired(OWLClassT.class, entityT.getUri());
106: assertEquals(newName, result.getName());
107: assertEquals(newDescription, result.getDescription());
108: assertEquals(newInt, result.getIntAttribute().intValue());
109: }
110:
111: @Test
112: void updateAllowsSettingValueOfPolymorphicAttributeToInstanceOfDifferentSubtype() {
113: this.em = getEntityManager("updateAllowsSettingValueOfPolymorphicAttributeToInstanceOfDifferentSubtype", true);
114: persist(entityU, entityT, entityA);
115:
116: final OWLClassU newReference = new OWLClassU();
117: newReference.setName("UpdatedU");
118: newReference.setDescription("Description");
119:
120: em.getTransaction().begin();
121: em.persist(newReference);
122: final OWLClassU toUpdate = findRequired(OWLClassU.class, entityU.getUri());
123: toUpdate.setOwlClassS(newReference);
124: em.getTransaction().commit();
125:
126: final OWLClassU result = findRequired(OWLClassU.class, entityU.getUri());
127: assertTrue(result.getOwlClassS() instanceof OWLClassU);
128: assertEquals(newReference.getUri(), result.getOwlClassS().getUri());
129: assertNotNull(em.find(OWLClassS.class, entityT.getUri()));
130: }
131: }