Skip to content

Method: testUpdateFieldsOfEntityWithMappedSuperclass()

1: /**
2: * Copyright (C) 2016 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.Generators;
21: import org.junit.Test;
22: import org.slf4j.Logger;
23:
24: import static org.hamcrest.core.Is.isA;
25: import static org.junit.Assert.assertEquals;
26: import static org.junit.Assert.assertNotNull;
27: import static org.junit.Assert.assertTrue;
28:
29: public abstract class UpdateOperationsWithInheritanceRunner extends BaseInheritanceRunner {
30:
31: public UpdateOperationsWithInheritanceRunner(Logger logger) {
32: super(logger);
33: }
34:
35: @Test
36: public void testUpdateFieldsOfEntityWithMappedSuperclass() {
37: this.em = getEntityManager("UpdateEntityWithMappedSuperclass", true);
38: persist(entityQ, entityA);
39:
40: entityQ.setStringAttribute("newStringAttribute");
41: entityQ.setParentString("newParentStringAttribute");
42: entityQ.setLabel("newLabel");
43: em.getTransaction().begin();
44: em.merge(entityQ);
45: em.getTransaction().commit();
46:
47: final OWLClassQ res = em.find(OWLClassQ.class, entityQ.getUri());
48: assertEquals(entityQ.getStringAttribute(), res.getStringAttribute());
49: assertEquals(entityQ.getParentString(), res.getParentString());
50: assertEquals(entityQ.getLabel(), res.getLabel());
51: }
52:
53: @Test
54: public void testUpdateObjectPropertyInMappedSuperclass() {
55: this.em = getEntityManager("UpdateObjectPropertyInMappedSuperclass", true);
56: persist(entityQ, entityA);
57: final OWLClassA entityA2 = new OWLClassA(Generators.generateUri());
58: entityA2.setStringAttribute("entityA2StringAttribute");
59:
60: entityQ.setOwlClassA(entityA2);
61: em.getTransaction().begin();
62: em.merge(entityQ);
63: em.persist(entityA2);
64: em.getTransaction().commit();
65:
66: final OWLClassQ res = em.find(OWLClassQ.class, entityQ.getUri());
67: assertNotNull(res.getOwlClassA());
68: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
69: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
70: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
71: }
72:
73: @Test
74: public void settingNonEmptyFieldInMappedSuperclassThrowsICViolationOnMerge() {
75: this.em = getEntityManager("SettingNonEmptyFieldInMappedSuperclassThrowsICViolation", true);
76: persist(entityQ, entityA);
77:
78: thrown.expect(RollbackException.class);
79: thrown.expectCause(isA(IntegrityConstraintViolatedException.class));
80:
81: entityQ.setOwlClassA(null);
82: em.getTransaction().begin();
83: em.merge(entityQ);
84: em.getTransaction().commit();
85: }
86:
87: @Test
88: public void testUpdateDataPropertyInEntitySuperclass() {
89: this.em = getEntityManager("updateDataPropertyInEntitySuperclass", true);
90: persist(entityT, entityA);
91:
92: final String newName = "newName";
93: final int newInt = Generators.randomInt(Integer.MAX_VALUE);
94: entityT.setName(newName);
95: entityT.setIntAttribute(newInt);
96: final String newDescription = "new entity description";
97: em.getTransaction().begin();
98: final OWLClassT merged = em.merge(entityT);
99: merged.setDescription(newDescription);
100: em.getTransaction().commit();
101:
102: final OWLClassT result = em.find(OWLClassT.class, entityT.getUri());
103: assertEquals(newName, result.getName());
104: assertEquals(newDescription, result.getDescription());
105: assertEquals(newInt, result.getIntAttribute().intValue());
106: }
107:
108: @Test
109: public void updateAllowsSettingValueOfPolymorphicAttributeToInstanceOfDifferentSubtype() {
110: this.em = getEntityManager("updateAllowsSettingValueOfPolymorphicAttributeToInstanceOfDifferentSubtype", true);
111: persist(entityU, entityT, entityA);
112:
113: final OWLClassU newReference = new OWLClassU();
114: newReference.setName("UpdatedU");
115: newReference.setDescription("Description");
116:
117: em.getTransaction().begin();
118: em.persist(newReference);
119: final OWLClassU toUpdate = em.find(OWLClassU.class, entityU.getUri());
120: toUpdate.setOwlClassS(newReference);
121: em.getTransaction().commit();
122:
123: final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
124: assertNotNull(result);
125: assertTrue(result.getOwlClassS() instanceof OWLClassU);
126: assertEquals(newReference.getUri(), result.getOwlClassS().getUri());
127: assertNotNull(em.find(OWLClassS.class, entityT.getUri()));
128: }
129: }