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