Skip to content

Method: lambda$mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache$10(OWLClassA)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.runner;
19:
20: import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
21: import cz.cvut.kbss.jopa.exceptions.RollbackException;
22: import cz.cvut.kbss.jopa.model.EntityManager;
23: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
24: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
25: import cz.cvut.kbss.jopa.oom.exception.UnpersistedChangeException;
26: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
27: import cz.cvut.kbss.jopa.test.OWLClassA;
28: import cz.cvut.kbss.jopa.test.OWLClassB;
29: import cz.cvut.kbss.jopa.test.OWLClassD;
30: import cz.cvut.kbss.jopa.test.OWLClassE;
31: import cz.cvut.kbss.jopa.test.OWLClassG;
32: import cz.cvut.kbss.jopa.test.OWLClassH;
33: import cz.cvut.kbss.jopa.test.OWLClassI;
34: import cz.cvut.kbss.jopa.test.OWLClassJ;
35: import cz.cvut.kbss.jopa.test.OWLClassL;
36: import cz.cvut.kbss.jopa.test.OWLClassM;
37: import cz.cvut.kbss.jopa.test.OWLClassN;
38: import cz.cvut.kbss.jopa.test.OWLClassO;
39: import cz.cvut.kbss.jopa.test.OWLClassP;
40: import cz.cvut.kbss.jopa.test.OWLClassU;
41: import cz.cvut.kbss.jopa.test.OWLClassX;
42: import cz.cvut.kbss.jopa.test.OWLClassZ;
43: import cz.cvut.kbss.jopa.test.OWLClassZChild;
44: import cz.cvut.kbss.jopa.test.Vocabulary;
45: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
46: import cz.cvut.kbss.jopa.test.environment.Generators;
47: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
48: import cz.cvut.kbss.jopa.test.environment.Quad;
49: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
50: import cz.cvut.kbss.jopa.vocabulary.XSD;
51: import org.junit.jupiter.api.Test;
52: import org.slf4j.Logger;
53:
54: import java.net.MalformedURLException;
55: import java.net.URI;
56: import java.net.URL;
57: import java.time.Instant;
58: import java.time.OffsetDateTime;
59: import java.time.temporal.ChronoUnit;
60: import java.util.Arrays;
61: import java.util.Collection;
62: import java.util.Collections;
63: import java.util.Date;
64: import java.util.HashMap;
65: import java.util.HashSet;
66: import java.util.Iterator;
67: import java.util.Map;
68: import java.util.Set;
69: import java.util.stream.Collectors;
70: import java.util.stream.IntStream;
71:
72: import static org.hamcrest.CoreMatchers.containsString;
73: import static org.hamcrest.CoreMatchers.instanceOf;
74: import static org.hamcrest.MatcherAssert.assertThat;
75: import static org.junit.jupiter.api.Assertions.assertEquals;
76: import static org.junit.jupiter.api.Assertions.assertFalse;
77: import static org.junit.jupiter.api.Assertions.assertInstanceOf;
78: import static org.junit.jupiter.api.Assertions.assertNotEquals;
79: import static org.junit.jupiter.api.Assertions.assertNotNull;
80: import static org.junit.jupiter.api.Assertions.assertNull;
81: import static org.junit.jupiter.api.Assertions.assertSame;
82: import static org.junit.jupiter.api.Assertions.assertThrows;
83: import static org.junit.jupiter.api.Assertions.assertTrue;
84:
85: public abstract class UpdateOperationsRunner extends BaseRunner {
86:
87: private OWLClassA entityA2;
88: private OWLClassJ entityJ;
89: private OWLClassO entityO;
90:
91: public UpdateOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
92: super(logger, persistenceFactory, dataAccessor);
93: initialize();
94: }
95:
96: private void initialize() {
97: this.entityA2 = new OWLClassA();
98: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
99: this.entityJ = new OWLClassJ();
100: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
101: final Set<OWLClassA> set = new HashSet<>(2);
102: set.add(entityA);
103: set.add(entityA2);
104: entityJ.setOwlClassA(set);
105: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
106: final Set<OWLClassE> es = new HashSet<>();
107: for (int i = 0; i < 5; i++) {
108: final OWLClassE e = new OWLClassE();
109: e.setStringAttribute("StringAttributeNo" + i);
110: es.add(e);
111: }
112: entityO.setOwlClassESet(es);
113: }
114:
115: @Test
116: void testUpdateDataLeaveLazy() {
117: this.em = getEntityManager("UpdateDataProperty", false);
118: entityB.setProperties(Generators.createProperties());
119: persist(entityB);
120:
121: em.getTransaction().begin();
122: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
123: assertInstanceOf(LazyLoadingProxy.class, b.getProperties());
124: final String newString = "NewString";
125: b.setStringAttribute(newString);
126: em.getTransaction().commit();
127:
128: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri());
129: assertEquals(newString, res.getStringAttribute());
130: assertNotNull(res.getProperties());
131: // Trigger lazy loading
132: assertFalse(res.getProperties().isEmpty());
133: assertEquals(entityB.getProperties(), res.getProperties());
134: }
135:
136: @Test
137: void testUpdateDataPropertySetNull() {
138: this.em = getEntityManager("UpdateDataPropertyToNull", true);
139: persist(entityA);
140:
141: em.getTransaction().begin();
142: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
143: assertNotNull(a.getStringAttribute());
144: a.setStringAttribute(null);
145: em.getTransaction().commit();
146:
147: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
148: assertNull(res.getStringAttribute());
149: assertEquals(entityA.getTypes(), res.getTypes());
150: }
151:
152: @Test
153: void testUpdateReference() {
154: this.em = getEntityManager("UpdateReference", true);
155: persist(entityD, entityI);
156:
157: final OWLClassA newA = new OWLClassA();
158: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
159: newA.setStringAttribute("newA");
160: em.getTransaction().begin();
161: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
162: assertNotNull(d.getOwlClassA());
163: d.setOwlClassA(newA);
164: final OWLClassI i = findRequired(OWLClassI.class, entityI.getUri());
165: assertNotNull(i.getOwlClassA());
166: i.setOwlClassA(newA);
167: em.persist(newA);
168: em.getTransaction().commit();
169:
170: final OWLClassA resA1 = findRequired(OWLClassA.class, newA.getUri());
171: final OWLClassD resD = findRequired(OWLClassD.class, d.getUri());
172: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
173: verifyExists(OWLClassA.class, entityA.getUri());
174: final OWLClassI resI = findRequired(OWLClassI.class, i.getUri());
175: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
176: verifyExists(OWLClassA.class, entityA.getUri());
177: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
178: }
179:
180: @Test
181: void mergedInstanceWithChangesInCascadedPluralReferenceAttributeContainsUpdatedValues() {
182: this.em = getEntityManager("MergeSet", false);
183: persist(entityJ);
184: em.clear();
185:
186: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
187:
188: em.getTransaction().begin();
189: OWLClassJ merged = em.merge(entityJ);
190:
191: for (final OWLClassA a : merged.getOwlClassA()) {
192: assertEquals(a.getStringAttribute(), "NEWVALUE");
193: }
194: em.getTransaction().commit();
195: }
196:
197: @Test
198: void testMergeDetachedWithChanges() {
199: this.em = getEntityManager("UpdateDetached", true);
200: persist(entityA);
201:
202: final OWLClassA a = findRequired(OWLClassA.class, entityA.getUri());
203: assertTrue(em.contains(a));
204: em.detach(a);
205: assertFalse(em.contains(a));
206: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
207: a.getTypes().add(newType);
208: em.getTransaction().begin();
209: final OWLClassA merged = em.merge(a);
210: assertTrue(merged.getTypes().contains(newType));
211: em.getTransaction().commit();
212:
213: final OWLClassA res = findRequired(OWLClassA.class, a.getUri());
214: assertEquals(a.getTypes().size(), res.getTypes().size());
215: assertTrue(res.getTypes().containsAll(a.getTypes()));
216: }
217:
218: @Test
219: void testMergeDetachedCascade() {
220: this.em = getEntityManager("UpdateCascade", true);
221: em.getTransaction().begin();
222: em.persist(entityH);
223: assertTrue(em.contains(entityA));
224: em.getTransaction().commit();
225:
226: em.getTransaction().begin();
227: final OWLClassH h = findRequired(OWLClassH.class, entityH.getUri());
228: assertNotNull(h.getOwlClassA());
229: em.detach(h);
230: assertFalse(em.contains(h));
231: assertFalse(em.contains(h.getOwlClassA()));
232: final String newStr = "newStringAttribute";
233: h.getOwlClassA().setStringAttribute(newStr);
234: final OWLClassH merged = em.merge(h);
235: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
236: em.getTransaction().commit();
237:
238: final OWLClassA res = findRequired(OWLClassA.class, entityA.getUri());
239: assertEquals(newStr, res.getStringAttribute());
240: }
241:
242: @Test
243: void testMergeDetachedWithObjectPropertyChange() {
244: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
245: persist(entityD, entityA);
246:
247: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
248: em.detach(d);
249: d.setOwlClassA(entityA2);
250: em.getTransaction().begin();
251: em.merge(d);
252: em.persist(entityA2);
253: em.getTransaction().commit();
254:
255: final OWLClassD res = findRequired(OWLClassD.class, entityD.getUri());
256: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
257: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
258: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri());
259: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
260: assertEquals(entityA.getTypes(), resA.getTypes());
261: }
262:
263: @Test
264: void testAddNewToProperties() {
265: this.em = getEntityManager("UpdateAddNewToProperties", false);
266: entityB.setProperties(Generators.createProperties());
267: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
268: expected.putAll(entityB.getProperties());
269: persist(entityB);
270: em.clear();
271:
272: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
273: assertEquals(expected.size(), b.getProperties().size());
274: em.getTransaction().begin();
275: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
276: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
277: expected.putAll(b.getProperties());
278: em.getTransaction().commit();
279:
280: final OWLClassB res = findRequired(OWLClassB.class, b.getUri());
281: assertEquals(expected.size(), res.getProperties().size());
282: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
283: assertTrue(res.getProperties().containsKey(e.getKey()));
284: final Set<String> s = e.getValue();
285: final Set<String> resS = res.getProperties().get(e.getKey());
286: assertEquals(s.size(), resS.size());
287: assertTrue(s.containsAll(resS));
288: }
289: }
290:
291: @Test
292: void testAddPropertyValue() {
293: this.em = getEntityManager("UpdateAddPropertyValue", false);
294: entityB.setProperties(Generators.createProperties());
295: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
296: final String prop = entityB.getProperties().keySet().iterator().next();
297: expected.putAll(entityB.getProperties());
298: persist(entityB);
299: em.clear();
300:
301: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
302: assertEquals(expected.size(), b.getProperties().size());
303: em.getTransaction().begin();
304: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
305: expected.putAll(b.getProperties());
306: em.getTransaction().commit();
307:
308: final OWLClassB res = findRequired(OWLClassB.class, b.getUri());
309: assertEquals(expected.size(), res.getProperties().size());
310: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
311: assertTrue(res.getProperties().containsKey(e.getKey()));
312: final Set<String> s = e.getValue();
313: final Set<String> resS = res.getProperties().get(e.getKey());
314: assertEquals(s.size(), resS.size());
315: if (e.getKey().equals(prop)) {
316: assertTrue(s.size() > 1);
317: for (String val : s) {
318: assertTrue(resS.contains(val));
319: }
320: } else {
321: assertTrue(resS.contains(s.iterator().next()));
322: }
323: }
324: }
325:
326: @Test
327: void testAddPropertyValueDetached() {
328: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
329: entityB.setProperties(Generators.createProperties());
330: final String prop = entityB.getProperties().keySet().iterator().next();
331: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
332: persist(entityB);
333: em.clear();
334:
335: final OWLClassB b = findRequired(OWLClassB.class, entityB.getUri());
336: b.getProperties().get(prop).add(newPropertyValue);
337: em.detach(b);
338: em.getTransaction().begin();
339: em.merge(b);
340: em.getTransaction().commit();
341:
342: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri());
343: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
344: }
345:
346: @Test
347: void testAddNewTypedPropertyWithValues() {
348: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
349: entityP.setProperties(Generators.createTypedProperties());
350: persist(entityP);
351: em.clear();
352:
353: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
354: em.getTransaction().begin();
355: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
356: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", OffsetDateTime.now()
357: .truncatedTo(ChronoUnit.MILLIS))));
358: em.getTransaction().commit();
359:
360: em.clear();
361:
362: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
363: assertTrue(res.getProperties().containsKey(property));
364: assertEquals(p.getProperties(), res.getProperties());
365: }
366:
367: @Test
368: void testAddTypedPropertyValue() {
369: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
370: entityP.setProperties(Generators.createTypedProperties());
371: persist(entityP);
372: em.clear();
373:
374: final OWLClassP p = findRequired(OWLClassP.class, entityP.getUri());
375: em.getTransaction().begin();
376: final URI property = p.getProperties().keySet().iterator().next();
377: final Object value = generateValueForProperty(p, property);
378: p.getProperties().get(property).add(value);
379: em.getTransaction().commit();
380:
381: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
382: assertTrue(res.getProperties().get(property).contains(value));
383: assertEquals(p.getProperties(), res.getProperties());
384: }
385:
386: private static Object generateValueForProperty(OWLClassP instance, URI property) {
387: final Set<Object> values = instance.getProperties().get(property);
388: assert values != null && !values.isEmpty();
389: if (values.iterator().next() instanceof URI) {
390: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
391: } else {
392: return "StringValue";
393: }
394: }
395:
396: @Test
397: void testModifyAttributesOfBasicTypes() {
398: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
399: persist(entityM);
400:
401: em.getTransaction().begin();
402: final OWLClassM m = findRequired(OWLClassM.class, entityM.getKey());
403: m.setBooleanAttribute(!entityM.getBooleanAttribute());
404: m.setFloatAttribute(m.getFloatAttribute() - 50.0F);
405: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
406: m.setLongAttribute(m.getLongAttribute() + 100L);
407: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
408: em.getTransaction().commit();
409:
410: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
411: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
412: assertEquals(m.getLongAttribute(), res.getLongAttribute());
413: assertEquals(m.getFloatAttribute(), res.getFloatAttribute());
414: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
415: assertEquals(m.getDateAttribute(), res.getDateAttribute());
416: }
417:
418: @Test
419: void testModifyEnumAttribute() {
420: this.em = getEntityManager("ModifyEnumAttribute", false);
421: assertNotNull(entityM.getEnumAttribute());
422: persist(entityM);
423:
424: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
425: em.getTransaction().begin();
426: final OWLClassM m = findRequired(OWLClassM.class, entityM.getKey());
427: m.setEnumAttribute(updated);
428: em.getTransaction().commit();
429:
430: final OWLClassM res = findRequired(OWLClassM.class, entityM.getKey());
431: assertEquals(updated, res.getEnumAttribute());
432: }
433:
434: @Test
435: void testCascadeOfNewlyPersistedOnMerge() {
436: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
437: em.getTransaction().begin();
438: em.persist(entityO);
439: entityO.getOwlClassESet().forEach(em::persist);
440: em.getTransaction().commit();
441:
442: final OWLClassE newE = new OWLClassE();
443: newE.setStringAttribute("NewlyAddedE");
444: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
445: em.detach(toUpdate);
446:
447: em.getTransaction().begin();
448: toUpdate.getOwlClassESet().add(newE);
449: em.persist(newE);
450: final OWLClassO merged = em.merge(toUpdate);
451: assertTrue(merged.getOwlClassESet().contains(newE));
452: em.getTransaction().commit();
453:
454: verifyExists(OWLClassE.class, newE.getUri());
455: final OWLClassO resO = findRequired(OWLClassO.class, entityO.getUri());
456: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
457: }
458:
459: @Test
460: void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
461: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
462: em.getTransaction().begin();
463: em.persist(entityJ);
464: em.getTransaction().commit();
465:
466: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
467: em.getTransaction().begin();
468: final OWLClassJ merged = em.merge(entityJ);
469: merged.getOwlClassA().add(newA);
470: em.persist(newA);
471: em.getTransaction().commit();
472:
473: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
474: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
475: boolean found = false;
476: for (OWLClassA a : result.getOwlClassA()) {
477: if (a.getUri().equals(newA.getUri())) {
478: found = true;
479: break;
480: }
481: }
482: assertTrue(found);
483: }
484:
485: @Test
486: void testAddPropertiesWhenTheyWereNullOriginally() {
487: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
488: em.getTransaction().begin();
489: assertNull(entityB.getProperties());
490: em.persist(entityB);
491: em.getTransaction().commit();
492:
493: final OWLClassB update = findRequired(OWLClassB.class, entityB.getUri());
494: em.detach(update);
495: final Map<String, Set<String>> properties = Generators.createProperties(2);
496: update.setProperties(properties);
497: em.getTransaction().begin();
498: em.merge(update);
499: em.getTransaction().commit();
500:
501: final OWLClassB result = findRequired(OWLClassB.class, entityB.getUri());
502: assertNotNull(result.getProperties());
503: assertEquals(properties, result.getProperties());
504: }
505:
506: @Test
507: void testUpdatePlainLiteralObjectPropertyValueToAnotherIndividual() {
508: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValue", true);
509: em.getTransaction().begin();
510: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#oldIndividual"));
511: em.persist(entityP);
512: em.getTransaction().commit();
513:
514: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
515: final URI newValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newIndividual");
516: em.getTransaction().begin();
517: update.setIndividualUri(newValue);
518: em.getTransaction().commit();
519:
520: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
521: assertEquals(newValue, result.getIndividualUri());
522: }
523:
524: @Test
525: void testUpdatePlainLiteralObjectPropertyValueToNull() {
526: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValueToNull", true);
527: em.getTransaction().begin();
528: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#SomeIndividual"));
529: em.persist(entityP);
530: em.getTransaction().commit();
531:
532: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
533: assertNotNull(update.getIndividualUri());
534: em.getTransaction().begin();
535: update.setIndividualUri(null);
536: em.getTransaction().commit();
537:
538: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
539: assertNull(result.getIndividualUri());
540: }
541:
542: @Test
543: void testUpdatePluralPlainLiteralObjectPropertyAttribute() throws MalformedURLException {
544: this.em = getEntityManager("UpdatePluralPlainLiteralObjectPropertyValue", true);
545: entityP.setIndividuals(Generators.createUrls());
546: persist(entityP);
547:
548: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
549: em.getTransaction().begin();
550: final URL added = new URL("http://krizik.felk.cvut.cz/ontologies/jopa#added");
551: final URL removed = entityP.getIndividuals().iterator().next();
552: update.getIndividuals().add(added);
553: update.getIndividuals().remove(removed);
554: em.getTransaction().commit();
555:
556: final OWLClassP res = findRequired(OWLClassP.class, entityP.getUri());
557: assertEquals(update.getIndividuals(), res.getIndividuals());
558: }
559:
560: @Test
561: void testUpdateStringAnnotationPropertyValue() {
562: this.em = getEntityManager("UpdateStringAnnotationPropertyValue", true);
563: entityN.setAnnotationProperty("PropertyValue");
564: persist(entityN);
565:
566: final String newValue = "newValue";
567: final OWLClassN update = findRequired(OWLClassN.class, entityN.getId());
568: assertNotNull(update);
569: em.getTransaction().begin();
570: update.setAnnotationProperty(newValue);
571: em.getTransaction().commit();
572:
573: final OWLClassN res = findRequired(OWLClassN.class, entityN.getId());
574: assertEquals(newValue, res.getAnnotationProperty());
575: }
576:
577: @Test
578: void testUpdateAnnotationPropertyWithUriValueToDifferentValue() {
579: this.em = getEntityManager("UpdateAnnotationPropertyWithUriValueToDifferentValue", true);
580: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value"));
581: persist(entityN);
582:
583: final OWLClassN update = findRequired(OWLClassN.class, entityN.getId());
584: assertNotNull(update);
585: em.getTransaction().begin();
586: final URI newUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newValue");
587: update.setAnnotationUri(newUri);
588: em.getTransaction().commit();
589:
590: final OWLClassN res = findRequired(OWLClassN.class, entityN.getId());
591: assertEquals(newUri, res.getAnnotationUri());
592: }
593:
594: @Test
595: void testUpdateUriTypes() {
596: this.em = getEntityManager("UpdateUriTypes", true);
597: entityP.setTypes(Generators.createUriTypes());
598: persist(entityP);
599:
600: em.getTransaction().begin();
601: final OWLClassP update = findRequired(OWLClassP.class, entityP.getUri());
602: final Iterator<URI> it = update.getTypes().iterator();
603: while (it.hasNext()) {
604: it.next();
605: if (Generators.randomBoolean()) {
606: it.remove();
607: }
608: }
609: update.getTypes().addAll(Generators.createUriTypes());
610: em.getTransaction().commit();
611:
612: final OWLClassP result = findRequired(OWLClassP.class, entityP.getUri());
613: assertEquals(update.getTypes().size(), result.getTypes().size());
614: assertTrue(update.getTypes().containsAll(result.getTypes()));
615: }
616:
617: @Test
618: void updateWithInvalidInstanceThrowsIntegrityConstraintsViolation() {
619: this.em = getEntityManager("UpdateWithInvalidInstanceThrowsICViolation", true);
620: persist(entityN);
621:
622: entityN.setStringAttribute(null);
623: em.getTransaction().begin();
624: final RollbackException ex = assertThrows(RollbackException.class, () -> {
625: em.merge(entityN);
626: em.getTransaction().commit();
627: });
628: assertThat(ex.getCause(), instanceOf(IntegrityConstraintViolatedException.class));
629: }
630:
631: @Test
632: void addingValuesToDatatypePropertyCollectionAddsThemIntoRepository() {
633: this.em = getEntityManager("addingValuesToDatatypePropertyCollectionAddsThemIntoRepository", false);
634: persist(entityM);
635:
636: IntStream.generate(Generators::randomInt).limit(7).forEach(entityM.getIntegerSet()::add);
637: em.getTransaction().begin();
638: em.merge(entityM);
639: em.getTransaction().commit();
640:
641: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
642: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
643: }
644:
645: @Test
646: void removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository() {
647: this.em = getEntityManager("removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository", false);
648: persist(entityM);
649:
650: final Iterator<Integer> it = entityM.getIntegerSet().iterator();
651: while (it.hasNext()) {
652: it.next();
653: if (Generators.randomBoolean()) {
654: it.remove();
655: }
656: }
657: // Make sure there is at least one element
658: entityM.getIntegerSet().add(Generators.randomInt());
659:
660: em.getTransaction().begin();
661: em.merge(entityM);
662: em.getTransaction().commit();
663:
664: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
665: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
666: }
667:
668: @Test
669: void mergeReplacesReferenceWithInstanceFromRepository() {
670: this.em = getEntityManager("mergeReplacesReferenceWithInstanceFromRepository", true);
671: persist(entityD, entityA);
672:
673: final String originalString = entityA.getStringAttribute();
674: entityA.setStringAttribute("updatedStringAttribute");
675: em.getTransaction().begin();
676: final OWLClassD merged = em.merge(entityD);
677: assertEquals(originalString, merged.getOwlClassA().getStringAttribute());
678: em.getTransaction().commit();
679:
680: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri());
681: assertEquals(originalString, result.getStringAttribute());
682: }
683:
684: @Test
685: void mergeMergesUpdatedDataPropertyFields() {
686: this.em = getEntityManager("mergeMergesUpdatedDataPropertyFields", true);
687: persist(entityA);
688: final Set<String> newTypes =
689: Generators.createUriTypes().stream().map(URI::toString).collect(Collectors.toSet());
690: entityA.setTypes(newTypes);
691: final String newStringAtt = "newStringAttribute";
692: entityA.setStringAttribute(newStringAtt);
693:
694: em.getTransaction().begin();
695: final OWLClassA merged = em.merge(entityA);
696: assertEquals(newStringAtt, merged.getStringAttribute());
697: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, merged.getTypes()));
698: em.getTransaction().commit();
699:
700: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri());
701: assertEquals(newStringAtt, result.getStringAttribute());
702: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, result.getTypes()));
703: }
704:
705: @Test
706: void mergeMergesUpdateToObjectPropertyField() {
707: this.em = getEntityManager("mergeMergesUpdateToObjectPropertyField", true);
708: persist(entityD, entityA, entityA2);
709:
710: em.getTransaction().begin();
711: entityD.setOwlClassA(entityA2);
712: final OWLClassD merged = em.merge(entityD);
713: assertEquals(entityA2.getUri(), merged.getOwlClassA().getUri());
714: em.getTransaction().commit();
715:
716: final OWLClassD dResult = findRequired(OWLClassD.class, entityD.getUri());
717: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
718: final OWLClassA aReference = findRequired(OWLClassA.class, entityA2.getUri());
719: assertSame(dResult.getOwlClassA(), aReference);
720: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
721: }
722:
723: @Test
724: void updateSupportsSavingStringLiteralWithDifferentLanguageTag() {
725: this.em = getEntityManager("updateSupportsSavingStringLiteralWithDifferentLanguageTag", false);
726: persist(entityA);
727:
728: entityA.setStringAttribute("hodnota v cestine");
729: final Descriptor descriptor = new EntityDescriptor();
730: descriptor.setAttributeLanguage(fieldSpecification(OWLClassA.class, "stringAttribute"), "cs");
731: transactional(() -> em.merge(entityA, descriptor));
732:
733: final OWLClassA resultOne = findRequired(OWLClassA.class, entityA.getUri());
734: assertNull(resultOne.getStringAttribute());
735: em.clear();
736: final OWLClassA resultTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
737: assertEquals(entityA.getStringAttribute(), resultTwo.getStringAttribute());
738: }
739:
740: @Test
741: void mergeDetachedRemovesObsoleteDescriptorInSecondLevelCache() {
742: this.em = getEntityManager("mergeDetachedReplacesObsoleteDescriptorInSecondLevelCache", true);
743: final Descriptor descriptorOne = new EntityDescriptor();
744: descriptorOne.setLanguage("en");
745: transactional(() -> em.persist(entityA, descriptorOne));
746: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
747:
748: final Descriptor descriptorTwo = new EntityDescriptor();
749: descriptorTwo.setLanguage("cs");
750: assertNotEquals(descriptorOne, descriptorTwo);
751: entityA.setStringAttribute("cesky");
752: transactional(() -> em.merge(entityA, descriptorTwo));
753:
754: assertFalse(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
755: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
756: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
757: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
758: }
759:
760: @Test
761: void mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting() {
762: this.em = getEntityManager("mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting", true);
763: persist(entityA);
764:
765: final Descriptor descriptorTwo = new EntityDescriptor();
766: descriptorTwo.setAttributeLanguage(fieldSpecification(OWLClassA.class, "stringAttribute"), "cs");
767: entityA.setStringAttribute("cesky");
768: transactional(() -> em.merge(entityA, descriptorTwo));
769:
770: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
771: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
772: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
773: }
774:
775: @Test
776: void updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue() {
777: this.em = getEntityManager("updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue", true);
778: entityD.setOwlClassA(null);
779: persist(entityD, entityA2);
780:
781: transactional(() -> {
782: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
783: d.setOwlClassA(entityA);
784: d.setOwlClassA(findRequired(OWLClassA.class, entityA2.getUri()));
785: });
786:
787: final OWLClassD dResult = findRequired(OWLClassD.class, entityD.getUri());
788: assertNotNull(dResult.getOwlClassA());
789: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
790: assertNull(em.find(OWLClassA.class, entityA.getUri()));
791: }
792:
793: @Test
794: void updatesKeepsPendingAssertionPluralAttribute() {
795: this.em = getEntityManager("updatesKeepsPendingAssertionPluralAttribute", true);
796: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
797: entityL.setSet(new HashSet<>());
798: entityL.getSet().add(entityA2);
799: entityL.setSimpleList(Collections.singletonList(entityA2));
800: entityL.setSingleA(entityA2);
801: entityL.setUri(Generators.generateUri());
802: persist(entityL, entityA2);
803:
804: em.getTransaction().begin();
805: final OWLClassL inst = findRequired(OWLClassL.class, entityL.getUri());
806: inst.getSet().add(entityA);
807: final RollbackException ex = assertThrows(RollbackException.class, () -> em.getTransaction().commit());
808: assertThat(ex.getCause(), instanceOf(UnpersistedChangeException.class));
809: assertThat(ex.getMessage(), containsString(entityA.toString()));
810: }
811:
812: @Test
813: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged() {
814: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged",
815: true);
816: persist(entityD, entityA);
817:
818: final String newString = "updatedString";
819: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
820: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
821: aUpdate.setStringAttribute(newString);
822: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
823: dUpdate.setOwlClassA(aUpdate);
824: em.clear();
825: transactional(() -> {
826: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
827: em.detach(orig);
828: em.find(OWLClassA.class, entityA.getUri());
829: em.merge(aUpdate);
830: em.merge(dUpdate);
831: });
832: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
833: assertEquals(newString, result.getOwlClassA().getStringAttribute());
834: }
835:
836: @Test
837: void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater() {
838: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater",
839: true);
840: persist(entityD, entityA);
841:
842: final String newString = "updatedString";
843: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
844: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
845: aUpdate.setStringAttribute(newString);
846: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
847: dUpdate.setOwlClassA(aUpdate);
848: em.clear();
849: transactional(() -> {
850: final OWLClassD orig = findRequired(OWLClassD.class, entityD.getUri());
851: em.detach(orig);
852: em.merge(dUpdate);
853: em.merge(aUpdate);
854: });
855: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
856: assertEquals(newString, result.getOwlClassA().getStringAttribute());
857: }
858:
859: @Test
860: void mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache() {
861: this.em = getEntityManager("mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache", true);
862: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
863: entityL.setSet(new HashSet<>());
864: entityL.getSet().add(entityA);
865: entityL.getSet().add(entityA2);
866: entityL.setSimpleList(Collections.singletonList(entityA2));
867: entityL.setSingleA(entityA2);
868: persist(entityL, entityA, entityA2);
869:
870: final OWLClassL toUpdate = new OWLClassL(entityL.getUri());
871: toUpdate.setSet(new HashSet<>(entityL.getSet()));
872: toUpdate.getSet().remove(entityA2);
873: toUpdate.setSimpleList(Collections.singletonList(entityA2));
874: toUpdate.setSingleA(entityA2);
875: transactional(() -> {
876: final OWLClassL original = findRequired(OWLClassL.class, entityL.getUri());
877: assertEquals(2, original.getSet().size());
878: for (OWLClassA a : original.getSet()) {
879: if (a.getUri().equals(entityA2.getUri())) {
880: original.getSet().remove(a);
881: break;
882: }
883: }
884: toUpdate.getSet().forEach(a -> em.merge(a));
885: em.merge(toUpdate);
886: });
887:
888: final OWLClassL result = findRequired(OWLClassL.class, entityL.getUri());
889: assertEquals(1, result.getSet().size());
890: }
891:
892: @Test
893: void mergeRemovedThrowsIllegalArgumentException() {
894: this.em = getEntityManager("mergeRemovedThrowsIllegalArgument", true);
895: persist(entityA);
896:
897: em.getTransaction().begin();
898: final OWLClassA toRemove = findRequired(OWLClassA.class, entityA.getUri());
899: em.remove(toRemove);
900: final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> em.merge(toRemove));
901: assertThat(ex.getMessage(), containsString("removed"));
902: }
903:
904: @Test
905: void changesDoneAfterInstanceRefreshAreCommittedToRepository() {
906: this.em = getEntityManager("changesDoneAfterInstanceRefreshAreCommittedToRepository", true);
907: persist(entityD, entityA, entityA2);
908:
909: em.getTransaction().begin();
910: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
911: final OWLClassA origA = d.getOwlClassA();
912: final OWLClassA newA = new OWLClassA(Generators.generateUri());
913: newA.setStringAttribute("newAWithStringAttribute");
914: d.setOwlClassA(em.find(OWLClassA.class, entityA2.getUri()));
915: em.refresh(d);
916: assertEquals(origA, d.getOwlClassA());
917: d.setOwlClassA(newA);
918: em.persist(newA);
919: em.getTransaction().commit();
920:
921: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
922: assertEquals(newA.getUri(), result.getOwlClassA().getUri());
923: assertEquals(newA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
924: }
925:
926: @Test
927: void refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges() {
928: this.em = getEntityManager("refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges",
929: true);
930: persist(entityJ);
931:
932: em.getTransaction().begin();
933: final OWLClassJ j = findRequired(OWLClassJ.class, entityJ.getUri());
934: j.getOwlClassA().clear();
935: em.refresh(j);
936: final OWLClassA newA = new OWLClassA(Generators.generateUri());
937: newA.setStringAttribute("newAWithStringAttribute");
938: j.getOwlClassA().add(newA);
939: em.persist(newA);
940: em.getTransaction().commit();
941:
942: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
943: assertEquals(entityJ.getOwlClassA().size() + 1, result.getOwlClassA().size());
944: final Set<URI> aUris = new HashSet<>(Arrays.asList(entityA.getUri(), entityA2.getUri(), newA.getUri()));
945: result.getOwlClassA().forEach(a -> assertTrue(aUris.contains(a.getUri())));
946: }
947:
948: /**
949: * Bug #33
950: */
951: @Test
952: void mergeWithUpdatedPropertyValueRemovesOriginalAssertion() {
953: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertion", false);
954: persist(entityH, entityA);
955:
956: em.getTransaction().begin();
957: entityH.setOwlClassA(entityA2);
958: em.merge(entityH);
959: em.getTransaction().commit();
960:
961: final OWLClassH result = findRequired(OWLClassH.class, entityH.getUri());
962: assertEquals(entityA2.getUri(), result.getOwlClassA().getUri());
963: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
964: }
965:
966: /**
967: * Bug #33, but for plural attributes
968: */
969: @Test
970: void mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute() {
971: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute", false);
972: persist(entityJ);
973:
974: assertEquals(2, entityJ.getOwlClassA().size());
975: final OWLClassA newA = new OWLClassA(Generators.generateUri());
976: entityJ.setOwlClassA(Collections.singleton(newA));
977: transactional(() -> em.merge(entityJ));
978:
979: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
980: assertEquals(1, result.getOwlClassA().size());
981: assertEquals(newA.getUri(), result.getOwlClassA().iterator().next().getUri());
982: }
983:
984: @Test
985: void updateSupportsJava8Instant() {
986: this.em = getEntityManager("updateSupportsJava8Instant", true);
987: final OWLClassX entityX = new OWLClassX();
988: entityX.setInstant(Instant.now());
989: persist(entityX);
990:
991: final Instant newInstant = Instant.ofEpochMilli(System.currentTimeMillis() + 10000);
992: em.getTransaction().begin();
993: final OWLClassX toUpdate = findRequired(OWLClassX.class, entityX.getUri());
994: toUpdate.setInstant(newInstant);
995: em.getTransaction().commit();
996:
997: final OWLClassX cachedResult = findRequired(OWLClassX.class, entityX.getUri());
998: assertEquals(newInstant, cachedResult.getInstant());
999: em.getEntityManagerFactory().getCache().evict(OWLClassX.class);
1000: final OWLClassX result = findRequired(OWLClassX.class, entityX.getUri());
1001: assertEquals(newInstant, result.getInstant());
1002: }
1003:
1004: @Test
1005: void updateSupportsPluralAnnotationProperty() {
1006: this.em = getEntityManager("updateSupportsPluralAnnotationProperty", true);
1007: final Set<String> annotations = IntStream.range(0, 5).mapToObj(i -> "Source" + i).collect(Collectors.toSet());
1008: entityN.setPluralAnnotationProperty(annotations);
1009: persist(entityN);
1010:
1011: final String added = "Added Source";
1012: final String removed = annotations.iterator().next();
1013: annotations.remove(removed);
1014: annotations.add(added);
1015: entityN.getPluralAnnotationProperty().remove(removed);
1016: entityN.getPluralAnnotationProperty().add(added);
1017: em.getTransaction().begin();
1018: final OWLClassN merged = em.merge(entityN);
1019: assertEquals(annotations, merged.getPluralAnnotationProperty());
1020: em.getTransaction().commit();
1021:
1022: final OWLClassN result = findRequired(OWLClassN.class, entityN.getId());
1023: assertEquals(annotations, result.getPluralAnnotationProperty());
1024: }
1025:
1026: @Test
1027: void updateSupportsSettingSubclassOnPolymorphicAttribute() {
1028: this.em = getEntityManager("updateSupportsSettingSubclassOnPolymorphicAttribute", true);
1029: final OWLClassU u = new OWLClassU();
1030: final OWLClassU reference = new OWLClassU();
1031: persist(reference);
1032:
1033: em.getTransaction().begin();
1034: final OWLClassU managedRef = em.merge(reference);
1035: u.setOwlClassS(managedRef);
1036: em.persist(u);
1037: em.getTransaction().commit();
1038:
1039: final OWLClassU result = findRequired(OWLClassU.class, u.getUri());
1040: assertEquals(reference.getUri(), result.getOwlClassS().getUri());
1041: }
1042:
1043: @Test
1044: void updateSupportsUpdatingSimpleLiteralValue() {
1045: this.em = getEntityManager("updateSupportsUpdatingSimpleLiteralValue", true);
1046: entityM.setSimpleLiteral("test original");
1047: persist(entityM);
1048:
1049: updateSimpleLiteralAndVerify();
1050: }
1051:
1052: private void updateSimpleLiteralAndVerify() {
1053: em.getTransaction().begin();
1054: final String newValue = "new test value";
1055: final OWLClassM toUpdate = findRequired(OWLClassM.class, entityM.getKey());
1056: toUpdate.setSimpleLiteral(newValue);
1057: em.getTransaction().commit();
1058:
1059: verifyValueDatatype(URI.create(entityM.getKey()), Vocabulary.p_m_simpleLiteral, XSD.STRING);
1060: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
1061: assertEquals(newValue, result.getSimpleLiteral());
1062: }
1063:
1064: @Test
1065: void updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute() throws Exception {
1066: this.em = getEntityManager("updateOverwritesLanguageStringWithSimpleLiteralForSimpleLiteralAttribute", true);
1067: persist(entityM);
1068: final Collection<Quad> originalQuad = Collections.singleton(
1069: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_simpleLiteral), "test", "en"));
1070: persistTestData(originalQuad, em);
1071:
1072: updateSimpleLiteralAndVerify();
1073: verifyStatementsNotPresent(originalQuad, em);
1074: }
1075:
1076: @Test
1077: void updateSupportCollectionAttribute() {
1078: this.em = getEntityManager("updateSupportCollectionAttribute", true);
1079: final OWLClassX x = new OWLClassX();
1080: x.setACollection(new HashSet<>(Arrays.asList(entityA, entityA2)));
1081:
1082: final OWLClassA newA = new OWLClassA();
1083: newA.setUri(Generators.generateUri());
1084: newA.setStringAttribute("stringAttributeeee");
1085: em.getTransaction().begin();
1086: em.persist(newA);
1087: x.setACollection(Collections.singletonList(newA));
1088: final OWLClassX merged = em.merge(x);
1089: assertFalse(merged.getACollection().isEmpty());
1090: em.getTransaction().commit();
1091:
1092: final OWLClassX result = findRequired(OWLClassX.class, x.getUri());
1093: assertEquals(1, result.getACollection().size());
1094: assertEquals(newA.getUri(), result.getACollection().iterator().next().getUri());
1095: }
1096:
1097: // Bug #81
1098: @Test
1099: void updateReferencedEntitySynchronizesCacheContent() {
1100: this.em = getEntityManager("updateReferencedEntitySynchronizesCacheContent", true);
1101: final OWLClassZ z = new OWLClassZ();
1102: z.setUri(Generators.generateUri());
1103: final OWLClassZChild root = new OWLClassZChild();
1104: root.setId(Generators.generateUri());
1105: root.setName("root");
1106: z.setRoot(root);
1107: final OWLClassZChild child = new OWLClassZChild();
1108: final String originalString = "child";
1109: child.setId(Generators.generateUri());
1110: child.setName(originalString);
1111: root.getChildren().add(child);
1112: transactional(() -> em.persist(z));
1113:
1114: final OWLClassZ zVerify = em.find(OWLClassZ.class, z.getUri());
1115: assertNotNull(zVerify);
1116: assertEquals(originalString, zVerify.getRoot().getChildren().iterator().next().getName());
1117: em.clear();
1118:
1119: final String updatedString = "newChild";
1120: child.setName(updatedString);
1121: transactional(() -> em.merge(child));
1122:
1123: final OWLClassZ result = em.find(OWLClassZ.class, z.getUri());
1124: assertEquals(updatedString, result.getRoot().getChildren().iterator().next().getName());
1125: }
1126:
1127: /**
1128: * Bug #97
1129: */
1130: @Test
1131: void mergeHandlesCascadingAndReferencedObjects() {
1132: this.em = getEntityManager("mergeHandlesCascadingAndReferencedObjects", true);
1133: final OWLClassG entityG = new OWLClassG(Generators.generateUri());
1134: final OWLClassH entityH = new OWLClassH(Generators.generateUri());
1135: entityG.setOwlClassH(entityH);
1136: entityH.setOwlClassA(entityA);
1137: persist(entityG, entityA, entityA2);
1138:
1139: entityG.getOwlClassH().setOwlClassA(entityA2);
1140: em.clear();
1141: transactional(() -> em.merge(entityG));
1142:
1143: final OWLClassG result = findRequired(OWLClassG.class, entityG.getUri());
1144: assertEquals(entityA2.getUri(), result.getOwlClassH().getOwlClassA().getUri());
1145: final OWLClassA aResult = findRequired(OWLClassA.class, entityA.getUri());
1146: // The incorrect merge messes up the cached value of the original entityA
1147: assertEquals(entityA.getUri(), aResult.getUri());
1148: }
1149:
1150: /**
1151: * Bug #202
1152: */
1153: @Test
1154: public void concurrentTransactionsLeaveDataInConsistentState() {
1155: final String a1String = "a1String";
1156: final String a2String = "a2String";
1157: this.em= getEntityManager("concurrentTransactionsLeaveDataInConsistentState", false);
1158: final EntityManager secondEm = em.getEntityManagerFactory().createEntityManager();
1159: persist(entityA);
1160: em.getTransaction().begin();
1161: final OWLClassA a1 = em.find(OWLClassA.class, entityA.getUri());
1162:
1163: secondEm.getTransaction().begin();
1164: final OWLClassA a2 = secondEm.find(OWLClassA.class, entityA.getUri());
1165:
1166: a1.setStringAttribute(a1String);
1167: a2.setStringAttribute(a2String);
1168: em.getTransaction().commit();
1169: secondEm.getTransaction().commit();
1170:
1171: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri());
1172: assertEquals(a2String, result.getStringAttribute());
1173: }
1174: }