Skip to contentMethod: persistTwiceIntoSameContextIsInvalid()
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.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.DataAccessor;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
25: import org.junit.Test;
26: import org.slf4j.Logger;
27:
28: import java.net.URI;
29: import java.util.Map;
30: import java.util.Set;
31:
32: import static org.junit.Assert.*;
33:
34: public abstract class CreateOperationsMultiContextRunner extends BaseRunner {
35:
36: private OWLClassF entityF;
37: private OWLClassK entityK;
38:
39: public CreateOperationsMultiContextRunner(Logger logger, PersistenceFactory persistenceFactory,
40: DataAccessor dataAccessor) {
41: super(logger, persistenceFactory, dataAccessor);
42: initialize();
43: }
44:
45: private void initialize() {
46: this.entityF = new OWLClassF();
47: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
48: this.entityK = new OWLClassK();
49: entityK.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityK"));
50: }
51:
52: @Test
53: public void testPersistDataPropertyIntoContext() throws Exception {
54: this.em = getEntityManager("MultiPersistDataPropertyIntoContext", false);
55: final Descriptor aDescriptor = new EntityDescriptor();
56: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
57: em.getTransaction().begin();
58: em.persist(entityA, aDescriptor);
59: em.getTransaction().commit();
60:
61: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
62: assertNotNull(res);
63: assertEquals(entityA.getUri(), res.getUri());
64: assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
65: assertEquals(entityA.getTypes().size(), res.getTypes().size());
66: assertTrue(entityA.getTypes().containsAll(res.getTypes()));
67: }
68:
69: @Test
70: public void testPersistObjectPropertyIntoContext() throws Exception {
71: this.em = getEntityManager("MultiPersistObjectPropertyIntoContext", false);
72: final Descriptor dDescriptor = new EntityDescriptor();
73: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
74: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
75: em.getTransaction().begin();
76: em.persist(entityD, dDescriptor);
77: em.persist(entityA, aDescriptor);
78: em.getTransaction().commit();
79:
80: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
81: assertNotNull(resD);
82: assertNotNull(resD.getOwlClassA());
83: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
84: assertNotNull(resA);
85: assertEquals(resD.getOwlClassA().getUri(), resA.getUri());
86: assertEquals(resD.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
87: assertTrue(resD.getOwlClassA().getTypes().containsAll(resA.getTypes()));
88: }
89:
90: @Test
91: public void testPersistWithGeneratedIntoContext() {
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(expected = OWLEntityExistsException.class)
124: public void persistTwiceIntoSameContextIsInvalid() {
125: this.em = getEntityManager("MultiPersistTwiceIntoOneContext", false);
126: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
127: em.getTransaction().begin();
128: em.persist(entityA, aDescriptor);
129: em.getTransaction().commit();
130:
131: em.getTransaction().begin();
132: em.persist(entityA, aDescriptor);
133: em.getTransaction().commit();
134: }
135:
136: @Test
137: public void persistTwiceIntoDifferentContextsIsLegal() {
138: this.em = getEntityManager("MultiPersistTwiceIntoDifferentContexts", false);
139: final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
140: final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
141: em.getTransaction().begin();
142: em.persist(entityA, aDescriptorOne);
143: em.getTransaction().commit();
144: em.getTransaction().begin();
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: final Descriptor bDescriptor = new EntityDescriptor();
161: entityB.setProperties(Generators.createProperties(10));
162: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_ONE);
163: em.getTransaction().begin();
164: em.persist(entityB, bDescriptor);
165: em.getTransaction().commit();
166:
167: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
168: assertNotNull(res);
169: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
170: assertEquals(entityB.getProperties().size(), res.getProperties().size());
171: for (Map.Entry<String, Set<String>> e : res.getProperties().entrySet()) {
172: assertTrue(entityB.getProperties().containsKey(e.getKey()));
173: assertEquals(e.getValue(), entityB.getProperties().get(e.getKey()));
174: }
175: }
176:
177: @Test
178: public void testPersistCascadeIntoThreeContexts() throws Exception {
179: this.em = getEntityManager("MultiPersistCascadeIntoThreeContexts", false);
180: final Descriptor gDescriptor = new EntityDescriptor();
181: final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
182: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
183: hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
184: gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
185: em.getTransaction().begin();
186: em.persist(entityG, gDescriptor);
187: assertTrue(em.contains(entityG));
188: assertTrue(em.contains(entityH));
189: assertTrue(em.contains(entityA));
190: em.getTransaction().commit();
191:
192: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
193: assertNotNull(resA);
194: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
195: assertNotNull(resH);
196: assertSame(resA, resH.getOwlClassA());
197: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
198: assertNotNull(resG);
199: assertSame(resH, resG.getOwlClassH());
200: }
201:
202: @Test
203: public void testPersistSetWithAttributeContexts() throws Exception {
204: this.em = getEntityManager("MultiPersistSetWithAttributeContexts", false);
205: entityF.setSimpleSet(Generators.createSimpleSet(20));
206: final Descriptor fDescriptor = new EntityDescriptor();
207: final Descriptor setDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
208: OWLClassF.class.getDeclaredField("simpleSet"));
209: fDescriptor.addAttributeDescriptor(OWLClassF.class.getDeclaredField("simpleSet"), setDescriptor);
210: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
211: setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("types"), CONTEXT_TWO);
212: em.getTransaction().begin();
213: em.persist(entityF, fDescriptor);
214: for (OWLClassA a : entityF.getSimpleSet()) {
215: em.persist(a, setDescriptor);
216: }
217: em.getTransaction().commit();
218:
219: final OWLClassF resF = em.find(OWLClassF.class, entityF.getUri(), fDescriptor);
220: assertNotNull(resF);
221: assertEquals(entityF.getSimpleSet().size(), resF.getSimpleSet().size());
222: for (OWLClassA a : resF.getSimpleSet()) {
223: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), setDescriptor);
224: assertNotNull(resA);
225: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
226: assertEquals(a.getTypes(), resA.getTypes());
227: }
228: }
229:
230: @Test
231: public void testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext()
232: throws Exception {
233: this.em = getEntityManager("PersistEntityWithObjectPropertyWithGeneratedIdentifierContexts", true);
234: entityK.setOwlClassE(entityE);
235: assertNull(entityE.getUri());
236: final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_TWO);
237: final Descriptor kDescriptor = new EntityDescriptor(CONTEXT_ONE);
238: kDescriptor.addAttributeDescriptor(OWLClassK.class.getDeclaredField("owlClassE"), eDescriptor);
239: em.getTransaction().begin();
240: em.persist(entityK, kDescriptor);
241: em.persist(entityE, eDescriptor);
242: assertNotNull(entityE.getUri());
243: em.getTransaction().commit();
244:
245: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
246: assertNotNull(resE);
247: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
248: final OWLClassK resK = em.find(OWLClassK.class, entityK.getUri(), kDescriptor);
249: assertNotNull(resK);
250: assertEquals(resE, resK.getOwlClassE());
251: }
252:
253: @Test
254: public void testPersistEntityWithMappedSuperclassPuttingReferenceIntoDifferentContext() throws Exception {
255: this.em = getEntityManager("PersistEntityWithMappedSuperclassReferenceInContext", true);
256: final Descriptor qDescriptor = new EntityDescriptor(CONTEXT_ONE);
257: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
258: qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
259: qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
260: em.getTransaction().begin();
261: em.persist(entityQ, qDescriptor);
262: em.persist(entityA, aDescriptor);
263: em.getTransaction().commit();
264:
265: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
266: assertNotNull(resA);
267: final OWLClassQ resQ = em.find(OWLClassQ.class, entityQ.getUri(), qDescriptor);
268: assertNotNull(resQ);
269: assertEquals(entityQ.getStringAttribute(), resQ.getStringAttribute());
270: assertEquals(entityQ.getParentString(), resQ.getParentString());
271: assertEquals(entityQ.getLabel(), resQ.getLabel());
272: assertNotNull(resQ.getOwlClassA());
273: assertEquals(resA, resQ.getOwlClassA());
274: }
275: }