Package: CreateOperationsMultiContextRunner
CreateOperationsMultiContextRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CreateOperationsMultiContextRunner(Logger) |
|
|
|
|
|
||||||||||||||||||||
initialize() |
|
|
|
|
|
||||||||||||||||||||
persistTwiceIntoDifferentContextsIsLegal() |
|
|
|
|
|
||||||||||||||||||||
persistTwiceIntoSameContextIsLegal() |
|
|
|
|
|
||||||||||||||||||||
testPersistCascadeIntoThreeContexts() |
|
|
|
|
|
||||||||||||||||||||
testPersistDataPropertyIntoContext() |
|
|
|
|
|
||||||||||||||||||||
testPersistEntityWithMappedSuperclassPuttingReferenceIntoDifferentContext() |
|
|
|
|
|
||||||||||||||||||||
testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext() |
|
|
|
|
|
||||||||||||||||||||
testPersistObjectPropertyIntoContext() |
|
|
|
|
|
||||||||||||||||||||
testPersistPropertiesIntoDifferentContext() |
|
|
|
|
|
||||||||||||||||||||
testPersistReferenceIntoContextAndThenOwnerIntoDefault() |
|
|
|
|
|
||||||||||||||||||||
testPersistSetWithAttributeContexts() |
|
|
|
|
|
||||||||||||||||||||
testPersistWithGeneratedIntoContext() |
|
|
|
|
|
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.*;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import org.junit.Test;
23: import org.slf4j.Logger;
24:
25: import java.net.URI;
26: import java.util.Map;
27: import java.util.Set;
28:
29: import static org.junit.Assert.*;
30:
31: public abstract class CreateOperationsMultiContextRunner extends BaseRunner {
32:
33: private OWLClassF entityF;
34: private OWLClassK entityK;
35:
36: public CreateOperationsMultiContextRunner(Logger logger) {
37: super(logger);
38: initialize();
39: }
40:
41: private void initialize() {
42: this.entityF = new OWLClassF();
43: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
44: this.entityK = new OWLClassK();
45: entityK.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityK"));
46: }
47:
48: @Test
49: public void testPersistDataPropertyIntoContext() throws Exception {
50: logger.debug("Test: persist an entity into the default context and its data property into a different one.");
51: this.em = getEntityManager("MultiPersistDataPropertyIntoContext", false);
52: final Descriptor aDescriptor = new EntityDescriptor();
53: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
54: em.getTransaction().begin();
55: em.persist(entityA, aDescriptor);
56: em.getTransaction().commit();
57:
58: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
59: assertNotNull(res);
60: assertEquals(entityA.getUri(), res.getUri());
61: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
62: assertEquals(entityA.getTypes().size(), res.getTypes().size());
63: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
64: }
65:
66: @Test
67: public void testPersistObjectPropertyIntoContext() throws Exception {
68: logger.debug(
69: "Test: persist entity into one context and its object property into another, along with its own attributes.");
70: this.em = getEntityManager("MultiPersistObjectPropertyIntoContext", false);
71: final Descriptor dDescriptor = new EntityDescriptor();
72: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
73: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
74: em.getTransaction().begin();
75: em.persist(entityD, dDescriptor);
76: em.persist(entityA, aDescriptor);
77: em.getTransaction().commit();
78:
79: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
80: assertNotNull(resD);
81: assertNotNull(resD.getOwlClassA());
82: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
83: assertNotNull(resA);
84: assertEquals(resD.getOwlClassA().getUri(), resA.getUri());
85: assertEquals(resD.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
86: assertTrue(resD.getOwlClassA().getTypes().containsAll(resA.getTypes()));
87: }
88:
89: @Test
90: public void testPersistWithGeneratedIntoContext() {
91: logger.debug("Test: persist entity with generated ID into a context.");
92: this.em = getEntityManager("MultiPersistWithGeneratedIntoContext", false);
93: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_ONE);
94: em.getTransaction().begin();
95: em.persist(entityE, eDescriptor);
96: em.getTransaction().commit();
97:
98: final OWLClassE res = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
99: assertNotNull(res);
100: assertEquals(entityE.getUri(), res.getUri());
101: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
102: }
103:
104: @Test
105: public void testPersistReferenceIntoContextAndThenOwnerIntoDefault() {
106: this.em = getEntityManager("ReferenceIntoContextThenOwnerIntoDefault", false);
107: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_ONE);
108: em.getTransaction().begin();
109: em.persist(entityA, eDescriptor);
110: em.getTransaction().commit();
111:
112: em.clear();
113: em.getTransaction().begin();
114: entityD.setOwlClassA(entityA);
115: em.persist(entityD);
116: em.getTransaction().commit();
117:
118: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
119: assertNotNull(res);
120: assertNotNull(res.getOwlClassA());
121: }
122:
123: @Test
124: public void persistTwiceIntoSameContextIsLegal() {
125: this.em = getEntityManager("MultiPersistTwiceIntoOneContext", false);
126: logger.debug("Test: persist an entity twice into the same context.");
127: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
128: em.getTransaction().begin();
129: em.persist(entityA, aDescriptor);
130: em.getTransaction().commit();
131:
132: em.getTransaction().begin();
133: em.persist(entityA, aDescriptor);
134: em.getTransaction().commit();
135: assertNotNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
136: }
137:
138: @Test
139: public void persistTwiceIntoDifferentContextsIsLegal() {
140: this.em = getEntityManager("MultiPersistTwiceIntoDifferentContexts", false);
141: logger.debug("Test: persist an entity into two different contexts.");
142: final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
143: final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
144: em.getTransaction().begin();
145: em.persist(entityA, aDescriptorOne);
146: em.persist(entityA, aDescriptorTwo);
147: em.getTransaction().commit();
148:
149: final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
150: assertNotNull(resOne);
151: final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
152: assertNotNull(resTwo);
153: assertNotSame(resOne, resTwo);
154: assertEquals(resOne.getUri(), resTwo.getUri());
155: assertEquals(resOne.getStringAttribute(), resTwo.getStringAttribute());
156: }
157:
158: @Test
159: public void testPersistPropertiesIntoDifferentContext() throws Exception {
160: this.em = getEntityManager("MultiPersistPropertiesIntoDifferentContext", false);
161: logger.debug("Test: persist an entity and persist its properties into a different context.");
162: final Descriptor bDescriptor = new EntityDescriptor();
163: entityB.setProperties(Generators.createProperties(10));
164: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_ONE);
165: em.getTransaction().begin();
166: em.persist(entityB, bDescriptor);
167: em.getTransaction().commit();
168:
169: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
170: assertNotNull(res);
171: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
172: assertEquals(entityB.getProperties().size(), res.getProperties().size());
173:• for (Map.Entry<String, Set<String>> e : res.getProperties().entrySet()) {
174: assertTrue(entityB.getProperties().containsKey(e.getKey()));
175: assertEquals(e.getValue(), entityB.getProperties().get(e.getKey()));
176: }
177: }
178:
179: @Test
180: public void testPersistCascadeIntoThreeContexts() throws Exception {
181: this.em = getEntityManager("MultiPersistCascadeIntoThreeContexts", false);
182: logger.debug("Test: persist three entities in cascaded relationship, each into a different context.");
183: final Descriptor gDescriptor = new EntityDescriptor();
184: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
185: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
186: hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
187: gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
188: em.getTransaction().begin();
189: em.persist(entityG, gDescriptor);
190: assertTrue(em.contains(entityG));
191: assertTrue(em.contains(entityH));
192: assertTrue(em.contains(entityA));
193: em.getTransaction().commit();
194:
195: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
196: assertNotNull(resA);
197: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
198: assertNotNull(resH);
199: assertSame(resA, resH.getOwlClassA());
200: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
201: assertNotNull(resG);
202: assertSame(resH, resG.getOwlClassH());
203: }
204:
205: @Test
206: public void testPersistSetWithAttributeContexts() throws Exception {
207: logger.debug(
208: "Test: persist entity with simple set, the set will be in a different context and attributes of its element in another.");
209: this.em = getEntityManager("MultiPersistSetWithAttributeContexts", false);
210: entityF.setSimpleSet(Generators.createSimpleSet(20));
211: final Descriptor fDescriptor = new EntityDescriptor();
212: final Descriptor setDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
213: OWLClassF.class.getDeclaredField("simpleSet"));
214: fDescriptor.addAttributeDescriptor(OWLClassF.class.getDeclaredField("simpleSet"), setDescriptor);
215: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
216: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("types"), CONTEXT_TWO);
217: em.getTransaction().begin();
218: em.persist(entityF, fDescriptor);
219:• for (OWLClassA a : entityF.getSimpleSet()) {
220: em.persist(a, setDescriptor);
221: }
222: em.getTransaction().commit();
223:
224: final OWLClassF resF = em.find(OWLClassF.class, entityF.getUri(), fDescriptor);
225: assertNotNull(resF);
226: assertEquals(entityF.getSimpleSet().size(), resF.getSimpleSet().size());
227:• for (OWLClassA a : resF.getSimpleSet()) {
228: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), setDescriptor);
229: assertNotNull(resA);
230: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
231: assertEquals(a.getTypes(), resA.getTypes());
232: }
233: }
234:
235: @Test
236: public void testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext()
237: throws Exception {
238: logger.debug("Test: persist entity with reference to an entity with generated identifier. "
239: + "The identifier should be generated automatically before the referenced entity itself is persisted.");
240: this.em = getEntityManager("PersistEntityWithObjectPropertyWithGeneratedIdentifierContexts", true);
241: entityK.setOwlClassE(entityE);
242: assertNull(entityE.getUri());
243: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_TWO);
244: final Descriptor kDescriptor = new EntityDescriptor(CONTEXT_ONE);
245: kDescriptor.addAttributeDescriptor(OWLClassK.class.getDeclaredField("owlClassE"), eDescriptor);
246: em.getTransaction().begin();
247: em.persist(entityK, kDescriptor);
248: assertNotNull(entityE.getUri());
249: em.persist(entityE, eDescriptor);
250: em.getTransaction().commit();
251:
252: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
253: assertNotNull(resE);
254: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
255: final OWLClassK resK = em.find(OWLClassK.class, entityK.getUri(), kDescriptor);
256: assertNotNull(resK);
257: assertEquals(resE, resK.getOwlClassE());
258: }
259:
260: @Test
261: public void testPersistEntityWithMappedSuperclassPuttingReferenceIntoDifferentContext() throws Exception {
262: this.em = getEntityManager("PersistEntityWithMappedSuperclassReferenceInContext", true);
263: final Descriptor qDescriptor = new EntityDescriptor(CONTEXT_ONE);
264: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
265: qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
266: qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
267: em.getTransaction().begin();
268: em.persist(entityQ, qDescriptor);
269: em.persist(entityA, aDescriptor);
270: em.getTransaction().commit();
271:
272: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
273: assertNotNull(resA);
274: final OWLClassQ resQ = em.find(OWLClassQ.class, entityQ.getUri(), qDescriptor);
275: assertNotNull(resQ);
276: assertEquals(entityQ.getStringAttribute(), resQ.getStringAttribute());
277: assertEquals(entityQ.getParentString(), resQ.getParentString());
278: assertEquals(entityQ.getLabel(), resQ.getLabel());
279: assertNotNull(resQ.getOwlClassA());
280: assertEquals(resA, resQ.getOwlClassA());
281: }
282: }