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