Skip to content

Package: RetrieveOperationsMultiContextRunner

RetrieveOperationsMultiContextRunner

nameinstructionbranchcomplexitylinemethod
RetrieveOperationsMultiContextRunner(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%
testRetrieveLazyReferenceFromContext()
M: 0 C: 91
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
testRetrievePropertiesFromContext()
M: 0 C: 72
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testRetrieveReferencedListFromContext()
M: 0 C: 125
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
testRetrieveSimilarFromTwoContexts()
M: 0 C: 94
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
testRetrieveSimpleListFromContext()
M: 0 C: 125
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 22
100%
M: 0 C: 1
100%

Coverage

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.model.descriptors.ObjectPropertyCollectionDescriptor;
20: import cz.cvut.kbss.jopa.test.OWLClassA;
21: import cz.cvut.kbss.jopa.test.OWLClassB;
22: import cz.cvut.kbss.jopa.test.OWLClassC;
23: import cz.cvut.kbss.jopa.test.OWLClassI;
24: import cz.cvut.kbss.jopa.test.environment.Generators;
25: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
26: import org.junit.Test;
27: import org.slf4j.Logger;
28:
29: import java.lang.reflect.Field;
30:
31: import static org.junit.Assert.*;
32:
33: public abstract class RetrieveOperationsMultiContextRunner extends BaseRunner {
34:
35: public RetrieveOperationsMultiContextRunner(Logger logger) {
36: super(logger);
37: }
38:
39: @Test
40: public void testRetrieveSimilarFromTwoContexts() throws Exception {
41: logger.debug(
42: "Test: persist entities with the same URI but different attributes into two contexts and then retrieve them.");
43: this.em = getEntityManager("MultiRetrieveSimilarFromTwoContexts", false);
44: final OWLClassA entityATwo = new OWLClassA();
45: entityATwo.setUri(entityA.getUri());
46: entityATwo.setStringAttribute("SomeCompletelyDifferentStringAttribute");
47: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
48: final Descriptor aTwoDescriptor = new EntityDescriptor(CONTEXT_TWO);
49: em.getTransaction().begin();
50: em.persist(entityA, aDescriptor);
51: em.getTransaction().commit();
52: em.getTransaction().begin();
53: em.persist(entityATwo, aTwoDescriptor);
54: em.getTransaction().commit();
55:
56: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
57: assertNotNull(resOne);
58: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
59: final OWLClassA resTwo = em.find(OWLClassA.class, entityATwo.getUri(), aTwoDescriptor);
60: assertNotNull(resTwo);
61: assertEquals(entityATwo.getStringAttribute(), resTwo.getStringAttribute());
62: }
63:
64: @Test
65: public void testRetrieveSimpleListFromContext() throws Exception {
66: logger.debug("Test: retrieve simple list and its values from a different context.");
67: this.em = getEntityManager("MultiRetrieveSimpleListFromContext", false);
68: entityC.setSimpleList(Generators.createSimpleList(10));
69: final Descriptor cDescriptor = new EntityDescriptor();
70: final Descriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
71: OWLClassC.class.getDeclaredField("simpleList"));
72: cDescriptor.addAttributeDescriptor(OWLClassC.class.getDeclaredField("simpleList"), listDescriptor);
73: em.getTransaction().begin();
74: em.persist(entityC, cDescriptor);
75:• for (OWLClassA a : entityC.getSimpleList()) {
76: em.persist(a, listDescriptor);
77: }
78: em.getTransaction().commit();
79:
80: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
81: assertNotNull(resC);
82: assertNotNull(resC.getSimpleList());
83: assertEquals(entityC.getSimpleList().size(), resC.getSimpleList().size());
84:• for (OWLClassA a : entityC.getSimpleList()) {
85: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), listDescriptor);
86: assertNotNull(resA);
87: assertEquals(a.getUri(), resA.getUri());
88: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
89: }
90: }
91:
92: @Test
93: public void testRetrieveReferencedListFromContext() throws Exception {
94: logger.debug("Test: retrieve referenced list and its values from a different context.");
95: this.em = getEntityManager("MultiRetrieveReferencedListFromContext", false);
96: entityC.setReferencedList(Generators.createReferencedList(15));
97: final Descriptor cDescriptor = new EntityDescriptor();
98: final Descriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
99: OWLClassC.class.getDeclaredField("referencedList"));
100: cDescriptor.addAttributeDescriptor(OWLClassC.class.getDeclaredField("referencedList"), listDescriptor);
101: em.getTransaction().begin();
102: em.persist(entityC, cDescriptor);
103:• for (OWLClassA a : entityC.getReferencedList()) {
104: em.persist(a, listDescriptor);
105: }
106: em.getTransaction().commit();
107:
108: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
109: assertNotNull(resC);
110: assertNotNull(resC.getReferencedList());
111: assertEquals(entityC.getReferencedList().size(), resC.getReferencedList().size());
112:• for (OWLClassA a : entityC.getReferencedList()) {
113: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), listDescriptor);
114: assertNotNull(resA);
115: assertEquals(a.getUri(), resA.getUri());
116: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
117: }
118: }
119:
120: @Test
121: public void testRetrieveLazyReferenceFromContext() throws Exception {
122: logger.debug("Test: retrieve entity with lazy loaded reference in another context.");
123: this.em = getEntityManager("MultiRetrieveLazyReferenceFromContext", false);
124: final Descriptor iDescriptor = new EntityDescriptor(CONTEXT_ONE);
125: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
126: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
127: iDescriptor.addAttributeDescriptor(OWLClassI.class.getDeclaredField("owlClassA"), aDescriptor);
128: em.getTransaction().begin();
129: // The relationship is CascadeType.PERSIST
130: em.persist(entityI, iDescriptor);
131: em.getTransaction().commit();
132:
133: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri(), iDescriptor);
134: assertNotNull(resI);
135: final Field refAField = OWLClassI.class.getDeclaredField("owlClassA");
136: refAField.setAccessible(true);
137: assertNull(refAField.get(resI));
138: assertNotNull(resI.getOwlClassA());
139: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
140: assertNotNull(resA);
141: // If we were using cache, ref.getOwlClassA() and resA would be the same
142: assertEquals(resI.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
143: }
144:
145: @Test
146: public void testRetrievePropertiesFromContext() throws Exception {
147: logger.debug("Test: retrieve entity properties from a context.");
148: this.em = getEntityManager("MultiRetrievePropertiesFromContext", false);
149: entityB.setProperties(Generators.createProperties(50));
150: final Descriptor bDescriptor = new EntityDescriptor(CONTEXT_ONE);
151: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_TWO);
152: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("stringAttribute"), null);
153: em.getTransaction().begin();
154: em.persist(entityB, bDescriptor);
155: em.getTransaction().commit();
156:
157: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
158: assertNotNull(res);
159: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
160: assertTrue(TestEnvironmentUtils.arePropertiesEqual(entityB.getProperties(), res.getProperties()));
161: }
162: }