Skip to content

Method: persistingDetachedEntityIsIllegal()

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.exceptions.RollbackException;
19: import cz.cvut.kbss.jopa.model.EntityManager;
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
23: import org.junit.Test;
24: import org.slf4j.Logger;
25:
26: import java.net.URI;
27: import java.net.URL;
28: import java.util.Collections;
29: import java.util.HashMap;
30: import java.util.Map;
31: import java.util.Set;
32:
33: import static org.junit.Assert.*;
34:
35: public abstract class CreateOperationsRunner extends BaseRunner {
36:
37: protected CreateOperationsRunner(Logger logger) {
38: super(logger);
39: }
40:
41: @Test
42: public void testPersistWithGeneratedId() {
43: this.em = getEntityManager("PersistWithGenerated", false);
44: persist(entityA, entityE);
45:
46: final OWLClassA resA1 = em.find(OWLClassA.class, entityA.getUri());
47: assertNotNull(resA1);
48: assertEquals(entityA.getStringAttribute(), resA1.getStringAttribute());
49: assertEquals(entityA.getTypes().size(), resA1.getTypes().size());
50: assertTrue(entityA.getTypes().containsAll(resA1.getTypes()));
51:
52: assertNotNull(entityE.getUri());
53: final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri());
54: assertNotNull(resE);
55: assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
56: }
57:
58: @Test(expected = PrimaryKeyNotSetException.class)
59: public void persistingEntityWithoutIdAndWithoutGeneratedIdThrowsException() {
60: this.em = getEntityManager("PersistWithoutId", false);
61: final OWLClassB b = new OWLClassB();
62: b.setStringAttribute("someValue");
63: persist(b);
64: }
65:
66: @Test(expected = NullPointerException.class)
67: public void persistNullThrowsNPX() {
68: this.em = getEntityManager("PersistNull", false);
69: em.getTransaction().begin();
70: em.persist(null);
71: }
72:
73: @Test
74: public void testPersistAndRollbackChanges() {
75: this.em = getEntityManager("PersistRollback", false);
76: em.getTransaction().begin();
77: em.persist(entityE);
78: assertTrue(em.contains(entityE));
79: em.getTransaction().rollback();
80:
81: assertFalse(em.contains(entityE));
82: assertNull(em.find(entityE.getClass(), entityE.getUri()));
83: }
84:
85: @Test(expected = RollbackException.class)
86: public void persistingInRollbackOnlyThrowsExceptionOnCommit() {
87: this.em = getEntityManager("PersistRollbackOnly", false);
88: em.getTransaction().begin();
89: em.getTransaction().setRollbackOnly();
90: em.persist(entityE);
91: em.getTransaction().commit();
92: }
93:
94: @Test
95: public void testPersistWithCascade() {
96: this.em = getEntityManager("PersistWithCascade", false);
97: persist(entityG);
98:
99: final OWLClassA resA2 = em.find(OWLClassA.class, entityA.getUri());
100: assertNotNull(resA2);
101: final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri());
102: assertNotNull(resH);
103: assertEquals(resH.getOwlClassA(), resA2);
104: final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri());
105: assertNotNull(resG);
106: assertEquals(resG.getOwlClassH(), resH);
107: assertEquals(resG.getOwlClassH().getOwlClassA(), resA2);
108: }
109:
110: @Test(expected = RollbackException.class)
111: public void persistingOnlyOnePartOfRelationWithoutCascadeThrowsRollbackException() {
112: this.em = getEntityManager("PersistWithoutCascade", false);
113: persist(entityD);
114: }
115:
116: @Test(expected = OWLEntityExistsException.class)
117: public void persistingDetachedEntityIsIllegal() {
118: this.em = getEntityManager("PersistDetached", false);
119: persist(entityA);
120:
121: final OWLClassA det = em.find(OWLClassA.class, entityA.getUri());
122: assertNotNull(det);
123: em.detach(det);
124: em.persist(det);
125: }
126:
127: @Test
128: public void testPersistWithSimpleList() {
129: this.em = getEntityManager("PersistSimpleList", false);
130: entityC.setSimpleList(Generators.createSimpleList(10));
131: em.getTransaction().begin();
132: em.persist(entityC);
133: entityC.getSimpleList().forEach(em::persist);
134: em.getTransaction().commit();
135:
136: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(1).getUri());
137: assertNotNull(a);
138: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
139: assertNotNull(c);
140: assertNotNull(c.getSimpleList());
141: assertFalse(c.getSimpleList().isEmpty());
142: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
143: assertTrue(c.getSimpleList().contains(a));
144: }
145:
146: @Test(expected = RollbackException.class)
147: public void persistingEntityWithSimpleListWithoutCascadeIsIllegal() {
148: this.em = getEntityManager("PersistSimpleListNoCascade", false);
149: entityC.setSimpleList(Generators.createSimpleList(10));
150: persist(entityC);
151: }
152:
153: @Test
154: public void testPersistWithReferencedList() {
155: this.em = getEntityManager("PersistReferencedList", false);
156: entityC.setReferencedList(Generators.createReferencedList(5));
157: em.getTransaction().begin();
158: em.persist(entityC);
159: entityC.getReferencedList().forEach(em::persist);
160: assertTrue(em.contains(entityC));
161: assertTrue(em.contains(entityC.getReferencedList().get(0)));
162: em.getTransaction().commit();
163:
164: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
165: assertNotNull(c);
166: assertNotNull(c.getReferencedList());
167: assertFalse(c.getReferencedList().isEmpty());
168: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
169: for (OWLClassA a : entityC.getReferencedList()) {
170: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
171: assertNotNull(resA);
172: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
173: assertTrue(c.getReferencedList().contains(resA));
174: }
175: }
176:
177: @Test(expected = RollbackException.class)
178: public void persistingEntityWithReferencedListWithoutCascadeIsIllegal() {
179: this.em = getEntityManager("PersistReferencedListNoCascade", false);
180: entityC.setReferencedList(Generators.createReferencedList(5));
181: persist(entityC);
182: }
183:
184: @Test
185: public void testPersistSimpleAndReferencedList() {
186: this.em = getEntityManager("PersistSimpleAndReferencedList", false);
187: entityC.setReferencedList(Generators.createReferencedList(5));
188: entityC.setSimpleList(Generators.createSimpleList(5));
189: em.getTransaction().begin();
190: em.persist(entityC);
191: entityC.getSimpleList().forEach(em::persist);
192: entityC.getReferencedList().forEach(em::persist);
193: em.getTransaction().commit();
194:
195: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
196: assertNotNull(c);
197: assertNotNull(c.getSimpleList());
198: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
199: assertNotNull(c.getReferencedList());
200: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
201: for (OWLClassA a : entityC.getSimpleList()) {
202: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
203: assertNotNull(resA);
204: assertTrue(c.getSimpleList().contains(resA));
205: }
206: for (OWLClassA a : entityC.getReferencedList()) {
207: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
208: assertNotNull(resA);
209: assertTrue(c.getReferencedList().contains(resA));
210: }
211: }
212:
213: @Test
214: public void testPersistWithProperties() {
215: this.em = getEntityManager("PersistWithProperties", false);
216: final Map<String, Set<String>> props = new HashMap<>(3);
217: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyOne", Collections
218: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Individual10"));
219: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyTwo", Collections
220: .singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/SomeEntity"));
221: props.put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyThree",
222: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityG"));
223: final Map<String, Set<String>> expected = new HashMap<>(4);
224: expected.putAll(props);
225: entityB.setProperties(props);
226: persist(entityB);
227: em.clear();
228:
229: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
230: assertNotNull(res);
231: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
232: assertNotNull(res.getProperties());
233: assertFalse(res.getProperties().isEmpty());
234: assertEquals(expected.size(), res.getProperties().size());
235: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
236: assertTrue(res.getProperties().containsKey(e.getKey()));
237: final Set<String> s = e.getValue();
238: final Set<String> resS = res.getProperties().get(e.getKey());
239: assertNotNull(resS);
240: assertEquals(1, resS.size());
241: assertEquals(s.iterator().next(), resS.iterator().next());
242: }
243: }
244:
245: @Test
246: public void testPersistWithEmptyProperties() {
247: this.em = getEntityManager("PersistWithPropertiesEmpty", false);
248: entityB.setProperties(Collections.emptyMap());
249: em.getTransaction().begin();
250: em.persist(entityB);
251: assertTrue(em.contains(entityB));
252: em.getTransaction().commit();
253: em.clear();
254:
255: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
256: assertNotNull(b);
257: assertEquals(entityB.getUri(), b.getUri());
258: assertEquals(entityB.getStringAttribute(), b.getStringAttribute());
259: assertNull(b.getProperties());
260: }
261:
262: @Test(expected = OWLEntityExistsException.class)
263: public void persistTwoInstancesOfDifferentClassesWithSameUriIntoTheSamePersistenceContextIsIllegal() {
264: this.em = getEntityManager("PersistURITwiceInDifferentClassesSamePC", false);
265: final URI pk = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
266: final OWLClassA a = new OWLClassA();
267: a.setUri(pk);
268: final OWLClassB b = new OWLClassB();
269: b.setUri(pk);
270: em.getTransaction().begin();
271: em.persist(a);
272: em.persist(b);
273: em.getTransaction().commit();
274: }
275:
276: @Test
277: public void persistTwoInstancesOfDifferentClassesWithSameUriIntoDifferentPersistenceContextsIsLegal() {
278: this.em = getEntityManager("PersistURITwiceInDifferentClassesDifferentPCs", false);
279: final URI uri = URI.create("http://krizik.felk.cvut.cz/jopa/onto/sameEntity");
280: entityA.setUri(uri);
281: entityB.setUri(uri);
282: em.getTransaction().begin();
283: em.persist(entityA);
284: em.getTransaction().commit();
285: final EntityManager emTwo = em.getEntityManagerFactory().createEntityManager();
286: try {
287: emTwo.getTransaction().begin();
288: emTwo.persist(entityB);
289: emTwo.getTransaction().commit();
290:
291: assertNotNull(emTwo.find(OWLClassA.class, entityA.getUri()));
292: assertNotNull(em.find(OWLClassB.class, entityB.getUri()));
293: } finally {
294: emTwo.close();
295: }
296: }
297:
298: @Test
299: public void testPersistEntityWithBasicTypeAttributes() {
300: this.em = getEntityManager("PersistEntityWithBasicTypeAttributes", false);
301: persist(entityM);
302: em.clear();
303:
304: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
305: assertNotNull(res);
306: assertEquals(entityM.getKey(), res.getKey());
307: assertEquals(entityM.getBooleanAttribute(), res.getBooleanAttribute());
308: assertEquals(entityM.getIntAttribute(), res.getIntAttribute());
309: assertEquals(entityM.getLongAttribute(), res.getLongAttribute());
310: assertEquals(entityM.getDoubleAttribute(), res.getDoubleAttribute());
311: assertEquals(entityM.getDateAttribute(), res.getDateAttribute());
312: }
313:
314: @Test
315: public void testPersistAndUpdateAttributeBeforeCommit() {
316: this.em = getEntityManager("PersistAndUpdateBeforeCommit", false);
317: final String updatedValue = "updatedStringAttributeValue";
318: em.getTransaction().begin();
319: em.persist(entityA);
320: entityA.setStringAttribute(updatedValue);
321: em.getTransaction().commit();
322: em.clear();
323:
324: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
325: assertNotNull(res);
326: assertEquals(updatedValue, res.getStringAttribute());
327: }
328:
329: @Test
330: public void testPersistEntityWithEnumAttribute() {
331: this.em = getEntityManager("PersistEntityWithEnum", false);
332: persist(entityM);
333:
334: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
335: assertNotNull(res);
336: assertEquals(entityM.getEnumAttribute(), res.getEnumAttribute());
337: }
338:
339: @Test
340: public void testPersistTypedProperties() {
341: this.em = getEntityManager("PersistTypedProperties", false);
342: entityP.setProperties(Generators.createTypedProperties());
343: em.getTransaction().begin();
344: em.persist(entityP);
345: em.getTransaction().commit();
346: em.clear();
347:
348: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
349: assertNotNull(res);
350: assertEquals(entityP.getProperties(), res.getProperties());
351: }
352:
353: @Test
354: public void testPersistInstanceWithPlainIdentifierObjectPropertyValue() {
355: this.em = getEntityManager("PersistInstanceWithIdentifierObjectPropertyValue", false);
356: final URI value = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#individualAAA");
357: entityP.setIndividualUri(value);
358: em.getTransaction().begin();
359: em.persist(entityP);
360: em.getTransaction().commit();
361: em.clear();
362:
363: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
364: assertNotNull(res);
365: assertEquals(value, res.getIndividualUri());
366: }
367:
368: @Test
369: public void testPersistInstanceWithPluralObjectPropertyAttributeRepresentedByUrls() {
370: this.em = getEntityManager("PersistInstanceWithPluralIdentifierObjectPropertyValue", false);
371: final Set<URL> urls = Generators.createUrls();
372: entityP.setIndividuals(urls);
373: em.getTransaction().begin();
374: em.persist(entityP);
375: em.getTransaction().commit();
376: em.clear();
377:
378: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
379: assertNotNull(res);
380: assertEquals(urls, res.getIndividuals());
381: }
382:
383: @Test
384: public void testPersistInstanceWithSimpleListOfIdentifiers() {
385: this.em = getEntityManager("PersistInstanceWithSimpleListOfIdentifiers", false);
386: entityP.setSimpleList(Generators.createListOfIdentifiers());
387: em.getTransaction().begin();
388: em.persist(entityP);
389: em.getTransaction().commit();
390: em.clear();
391:
392: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
393: assertNotNull(res);
394: assertEquals(entityP.getSimpleList(), res.getSimpleList());
395: }
396:
397: @Test
398: public void testPersistInstanceWithReferencedListOfIdentifiers() {
399: this.em = getEntityManager("PersistInstanceWithReferencedListOfIdentifiers", false);
400: entityP.setReferencedList(Generators.createListOfIdentifiers());
401: em.getTransaction().begin();
402: em.persist(entityP);
403: em.getTransaction().commit();
404: em.clear();
405:
406: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
407: assertNotNull(res);
408: assertEquals(entityP.getReferencedList(), res.getReferencedList());
409: }
410:
411: @Test
412: public void testPersistInstanceWithAnnotationProperties() {
413: this.em = getEntityManager("PersistInstanceWithAnnotationPropertyValues", false);
414: final String apValue = "annotationPropertyValue";
415: final URI apUriValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationPropertyValue");
416: entityN.setAnnotationProperty(apValue);
417: entityN.setAnnotationUri(apUriValue);
418: em.getTransaction().begin();
419: em.persist(entityN);
420: em.getTransaction().commit();
421: em.clear();
422: assertNotNull(entityN.getId());
423:
424: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
425: assertEquals(apValue, res.getAnnotationProperty());
426: assertEquals(apUriValue, res.getAnnotationUri());
427: }
428:
429: @Test
430: public void persistEntityWithNonNullGeneratedIdentifierDoesNotRewriteIdentifier() {
431: this.em = getEntityManager("PersistEntityWithNonNullGeneratedIdentifiersDoesNotRewriteIdentifier", false);
432: final URI u = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
433: entityE.setUri(u);
434: em.getTransaction().begin();
435: em.persist(entityE);
436: em.getTransaction().commit();
437:
438: assertEquals(u, entityE.getUri());
439: final OWLClassE res = em.find(OWLClassE.class, u);
440: assertNotNull(res);
441: assertEquals(u, res.getUri());
442: assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
443: }
444:
445: @Test
446: public void persistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem() {
447: this.em = getEntityManager("PersistEntityAndReferenceWithNonNullGeneratedIdentifiersDoesNotRewriteThem", false);
448: final URI uK = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityKLives");
449: final URI uE = URI.create("http://krizik.felk.cvut.cz/ontolgoies/jopa#EntityELives");
450: final OWLClassK entityK = new OWLClassK();
451: entityK.setUri(uK);
452: entityK.setOwlClassE(entityE);
453: entityE.setUri(uE);
454: em.getTransaction().begin();
455: em.persist(entityK);
456: em.persist(entityE);
457: em.getTransaction().commit();
458:
459: assertEquals(uK, entityK.getUri());
460: assertEquals(uE, entityE.getUri());
461: final OWLClassK resK = em.find(OWLClassK.class, uK);
462: assertNotNull(resK);
463: assertEquals(uE, resK.getOwlClassE().getUri());
464: final OWLClassE resE = em.find(OWLClassE.class, uE);
465: assertNotNull(resE);
466: assertEquals(uE, resE.getUri());
467: }
468:
469: @Test
470: public void testPersistEntityWithUriTypes() {
471: this.em = getEntityManager("PersistEntityWithUriTypes", false);
472: entityP.setTypes(Generators.createUriTypes());
473: em.getTransaction().begin();
474: em.persist(entityP);
475: em.getTransaction().commit();
476: em.clear();
477:
478: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
479: assertEquals(entityP.getTypes().size(), result.getTypes().size());
480: assertTrue(entityP.getTypes().containsAll(result.getTypes()));
481: }
482:
483: @Test
484: public void persistEntityWithDatatypePropertyCollectionPersistsAllValues() {
485: assertFalse(entityM.getIntegerSet().isEmpty());
486: this.em = getEntityManager("PersistEntityWithDatatypePropertyCollection", false);
487: em.getTransaction().begin();
488: em.persist(entityM);
489: em.getTransaction().commit();
490:
491: assertNotNull(entityM.getKey());
492: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
493: assertNotNull(result);
494: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
495: }
496: }