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