Package: RetrieveOperationsMultiContextRunner
RetrieveOperationsMultiContextRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RetrieveOperationsMultiContextRunner(Logger) |
|
|
|
|
|
||||||||||||||||||||
testRetrieveLazyReferenceFromContext() |
|
|
|
|
|
||||||||||||||||||||
testRetrievePropertiesFromContext() |
|
|
|
|
|
||||||||||||||||||||
testRetrieveReferencedListFromContext() |
|
|
|
|
|
||||||||||||||||||||
testRetrieveSimilarFromTwoContexts() |
|
|
|
|
|
||||||||||||||||||||
testRetrieveSimpleListFromContext() |
|
|
|
|
|
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.persist(entityATwo, aTwoDescriptor);
52: em.getTransaction().commit();
53:
54: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
55: assertNotNull(resOne);
56: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
57: final OWLClassA resTwo = em.find(OWLClassA.class, entityATwo.getUri(), aTwoDescriptor);
58: assertNotNull(resTwo);
59: assertEquals(entityATwo.getStringAttribute(), resTwo.getStringAttribute());
60: }
61:
62: @Test
63: public void testRetrieveSimpleListFromContext() throws Exception {
64: logger.debug("Test: retrieve simple list and its values from a different context.");
65: this.em = getEntityManager("MultiRetrieveSimpleListFromContext", false);
66: entityC.setSimpleList(Generators.createSimpleList(10));
67: final Descriptor cDescriptor = new EntityDescriptor();
68: final Descriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
69: OWLClassC.class.getDeclaredField("simpleList"));
70: cDescriptor.addAttributeDescriptor(OWLClassC.class.getDeclaredField("simpleList"), listDescriptor);
71: em.getTransaction().begin();
72: em.persist(entityC, cDescriptor);
73:• for (OWLClassA a : entityC.getSimpleList()) {
74: em.persist(a, listDescriptor);
75: }
76: em.getTransaction().commit();
77:
78: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
79: assertNotNull(resC);
80: assertNotNull(resC.getSimpleList());
81: assertEquals(entityC.getSimpleList().size(), resC.getSimpleList().size());
82:• for (OWLClassA a : entityC.getSimpleList()) {
83: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), listDescriptor);
84: assertNotNull(resA);
85: assertEquals(a.getUri(), resA.getUri());
86: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
87: }
88: }
89:
90: @Test
91: public void testRetrieveReferencedListFromContext() throws Exception {
92: logger.debug("Test: retrieve referenced list and its values from a different context.");
93: this.em = getEntityManager("MultiRetrieveReferencedListFromContext", false);
94: entityC.setReferencedList(Generators.createReferencedList(15));
95: final Descriptor cDescriptor = new EntityDescriptor();
96: final Descriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
97: OWLClassC.class.getDeclaredField("referencedList"));
98: cDescriptor.addAttributeDescriptor(OWLClassC.class.getDeclaredField("referencedList"), listDescriptor);
99: em.getTransaction().begin();
100: em.persist(entityC, cDescriptor);
101:• for (OWLClassA a : entityC.getReferencedList()) {
102: em.persist(a, listDescriptor);
103: }
104: em.getTransaction().commit();
105:
106: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
107: assertNotNull(resC);
108: assertNotNull(resC.getReferencedList());
109: assertEquals(entityC.getReferencedList().size(), resC.getReferencedList().size());
110:• for (OWLClassA a : entityC.getReferencedList()) {
111: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), listDescriptor);
112: assertNotNull(resA);
113: assertEquals(a.getUri(), resA.getUri());
114: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
115: }
116: }
117:
118: @Test
119: public void testRetrieveLazyReferenceFromContext() throws Exception {
120: logger.debug("Test: retrieve entity with lazy loaded reference in another context.");
121: this.em = getEntityManager("MultiRetrieveLazyReferenceFromContext", false);
122: final Descriptor iDescriptor = new EntityDescriptor(CONTEXT_ONE);
123: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
124: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
125: iDescriptor.addAttributeDescriptor(OWLClassI.class.getDeclaredField("owlClassA"), aDescriptor);
126: em.getTransaction().begin();
127: // The relationship is CascadeType.PERSIST
128: em.persist(entityI, iDescriptor);
129: em.getTransaction().commit();
130:
131: final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri(), iDescriptor);
132: assertNotNull(resI);
133: final Field refAField = OWLClassI.class.getDeclaredField("owlClassA");
134: refAField.setAccessible(true);
135: assertNull(refAField.get(resI));
136: assertNotNull(resI.getOwlClassA());
137: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
138: assertNotNull(resA);
139: // If we were using cache, ref.getOwlClassA() and resA would be the same
140: assertEquals(resI.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
141: }
142:
143: @Test
144: public void testRetrievePropertiesFromContext() throws Exception {
145: logger.debug("Test: retrieve entity properties from a context.");
146: this.em = getEntityManager("MultiRetrievePropertiesFromContext", false);
147: entityB.setProperties(Generators.createProperties(50));
148: final Descriptor bDescriptor = new EntityDescriptor(CONTEXT_ONE);
149: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_TWO);
150: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("stringAttribute"), null);
151: em.getTransaction().begin();
152: em.persist(entityB, bDescriptor);
153: em.getTransaction().commit();
154:
155: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
156: assertNotNull(res);
157: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
158: assertTrue(TestEnvironmentUtils.arePropertiesEqual(entityB.getProperties(), res.getProperties()));
159: }
160: }