Skip to content

Package: DeleteOperationsMultiContextRunner

DeleteOperationsMultiContextRunner

nameinstructionbranchcomplexitylinemethod
DeleteOperationsMultiContextRunner(Logger)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testRemoveCascadeOverContexts()
M: 0 C: 134
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 25
100%
M: 0 C: 1
100%
testRemoveFromContext()
M: 0 C: 71
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testRemoveFromOneKeepInTheOther()
M: 0 C: 101
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
testRemoveObjectPropertyFromContext()
M: 0 C: 106
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 23
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.runner;
14:
15: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
16: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
17: import cz.cvut.kbss.jopa.test.OWLClassA;
18: import cz.cvut.kbss.jopa.test.OWLClassD;
19: import cz.cvut.kbss.jopa.test.OWLClassG;
20: import cz.cvut.kbss.jopa.test.OWLClassH;
21: import org.junit.Test;
22: import org.slf4j.Logger;
23:
24: import static org.junit.Assert.*;
25:
26: public abstract class DeleteOperationsMultiContextRunner extends BaseRunner {
27:
28: public DeleteOperationsMultiContextRunner(Logger logger) {
29: super(logger);
30: }
31:
32: @Test
33: public void testRemoveFromContext() throws Exception {
34: logger.debug("Test: remove entity from a context.");
35: this.em = getEntityManager("MultiRemoveFromContext", false);
36: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
37: em.getTransaction().begin();
38: em.persist(entityA, aDescriptor);
39: em.getTransaction().commit();
40:
41: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
42: assertNotNull(a);
43: em.getTransaction().begin();
44: em.remove(a);
45: assertFalse(em.contains(a));
46: em.getTransaction().commit();
47:
48: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
49: assertNull(res);
50: }
51:
52: @Test
53: public void testRemoveFromOneKeepInTheOther() throws Exception {
54: logger.debug("Test: persist an entity into two contexts and then remove it from one of them.");
55: this.em = getEntityManager("MultiRemoveFromOneContextAndKeepInTheOther", false);
56: final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
57: final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
58: em.getTransaction().begin();
59: em.persist(entityA, aDescriptorOne);
60: em.persist(entityA, aDescriptorTwo);
61: em.getTransaction().commit();
62:
63: final OWLClassA aOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
64: assertNotNull(aOne);
65: final OWLClassA aTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
66: assertNotNull(aTwo);
67: em.getTransaction().begin();
68: em.remove(aTwo);
69: em.getTransaction().commit();
70:
71: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
72: assertNotNull(resOne);
73: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
74: assertNull(resTwo);
75: }
76:
77: @Test
78: public void testRemoveObjectPropertyFromContext() throws Exception {
79: logger.debug("Test: remove object property value from a context.");
80: this.em = getEntityManager("MultiRemoveObjectPropertyFromContext", false);
81: final Descriptor dDescriptor = new EntityDescriptor(CONTEXT_ONE);
82: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
83: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
84: em.getTransaction().begin();
85: em.persist(entityD, dDescriptor);
86: em.persist(entityA, aDescriptor);
87: em.getTransaction().commit();
88:
89: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
90: assertNotNull(d);
91: final OWLClassA a = d.getOwlClassA();
92: assertNotNull(a);
93: d.setOwlClassA(null);
94: em.getTransaction().begin();
95: em.remove(a);
96: em.getTransaction().commit();
97:
98: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
99: assertNotNull(resD);
100: assertNull(resD.getOwlClassA());
101: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
102: assertNull(resA);
103: }
104:
105: @Test
106: public void testRemoveCascadeOverContexts() throws Exception {
107: logger.debug("Test: remove entities through cascade, each in a different context.");
108: this.em = getEntityManager("MultiRemoveCascadeOverContexts", false);
109: final Descriptor gDescriptor = new EntityDescriptor();
110: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
111: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
112: hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
113: gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
114: em.getTransaction().begin();
115: em.persist(entityG, gDescriptor);
116: em.getTransaction().commit();
117:
118: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
119: assertNotNull(a);
120: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
121: assertNotNull(h);
122: assertSame(a, h.getOwlClassA());
123: final OWLClassG g = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
124: assertNotNull(g);
125: assertSame(h, g.getOwlClassH());
126: em.getTransaction().begin();
127: em.remove(g);
128: em.getTransaction().commit();
129:
130: assertNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
131: assertNull(em.find(OWLClassH.class, entityH.getUri(), hDescriptor));
132: assertNull(em.find(OWLClassG.class, entityG.getUri(), gDescriptor));
133: }
134: }