Skip to contentMethod: testRemoveCascadeOverContexts()
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.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.test.OWLClassA;
20: import cz.cvut.kbss.jopa.test.OWLClassD;
21: import cz.cvut.kbss.jopa.test.OWLClassG;
22: import cz.cvut.kbss.jopa.test.OWLClassH;
23: import org.junit.Test;
24: import org.slf4j.Logger;
25:
26: import static org.junit.Assert.*;
27:
28: public abstract class DeleteOperationsMultiContextRunner extends BaseRunner {
29:
30: public DeleteOperationsMultiContextRunner(Logger logger) {
31: super(logger);
32: }
33:
34: @Test
35: public void testRemoveFromContext() throws Exception {
36: logger.debug("Test: remove entity from a context.");
37: this.em = getEntityManager("MultiRemoveFromContext", false);
38: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
39: em.getTransaction().begin();
40: em.persist(entityA, aDescriptor);
41: em.getTransaction().commit();
42:
43: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
44: assertNotNull(a);
45: em.getTransaction().begin();
46: em.remove(a);
47: assertFalse(em.contains(a));
48: em.getTransaction().commit();
49:
50: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
51: assertNull(res);
52: }
53:
54: @Test
55: public void testRemoveFromOneKeepInTheOther() throws Exception {
56: logger.debug("Test: persist an entity into two contexts and then remove it from one of them.");
57: this.em = getEntityManager("MultiRemoveFromOneContextAndKeepInTheOther", false);
58: final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
59: final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
60: em.getTransaction().begin();
61: em.persist(entityA, aDescriptorOne);
62: em.persist(entityA, aDescriptorTwo);
63: em.getTransaction().commit();
64:
65: final OWLClassA aOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
66: assertNotNull(aOne);
67: final OWLClassA aTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
68: assertNotNull(aTwo);
69: em.getTransaction().begin();
70: em.remove(aTwo);
71: em.getTransaction().commit();
72:
73: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
74: assertNotNull(resOne);
75: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
76: assertNull(resTwo);
77: }
78:
79: @Test
80: public void testRemoveObjectPropertyFromContext() throws Exception {
81: logger.debug("Test: remove object property value from a context.");
82: this.em = getEntityManager("MultiRemoveObjectPropertyFromContext", false);
83: final Descriptor dDescriptor = new EntityDescriptor(CONTEXT_ONE);
84: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
85: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
86: em.getTransaction().begin();
87: em.persist(entityD, dDescriptor);
88: em.persist(entityA, aDescriptor);
89: em.getTransaction().commit();
90:
91: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
92: assertNotNull(d);
93: final OWLClassA a = d.getOwlClassA();
94: assertNotNull(a);
95: d.setOwlClassA(null);
96: em.getTransaction().begin();
97: em.remove(a);
98: em.getTransaction().commit();
99:
100: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
101: assertNotNull(resD);
102: assertNull(resD.getOwlClassA());
103: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
104: assertNull(resA);
105: }
106:
107: @Test
108: public void testRemoveCascadeOverContexts() throws Exception {
109: logger.debug("Test: remove entities through cascade, each in a different context.");
110: this.em = getEntityManager("MultiRemoveCascadeOverContexts", false);
111: final Descriptor gDescriptor = new EntityDescriptor();
112: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
113: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
114: hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
115: gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
116: em.getTransaction().begin();
117: em.persist(entityG, gDescriptor);
118: em.getTransaction().commit();
119:
120: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
121: assertNotNull(a);
122: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
123: assertNotNull(h);
124: assertSame(a, h.getOwlClassA());
125: final OWLClassG g = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
126: assertNotNull(g);
127: assertSame(h, g.getOwlClassH());
128: em.getTransaction().begin();
129: em.remove(g);
130: em.getTransaction().commit();
131:
132: assertNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
133: assertNull(em.find(OWLClassH.class, entityH.getUri(), hDescriptor));
134: assertNull(em.find(OWLClassG.class, entityG.getUri(), gDescriptor));
135: }
136: }