Skip to contentMethod: testUpdatePluralPlainLiteralObjectPropertyAttribute()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.test.runner;
14:
15: import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
16: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
17: import cz.cvut.kbss.jopa.exceptions.RollbackException;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
20: import cz.cvut.kbss.jopa.oom.exceptions.UnpersistedChangeException;
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 cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
26: import org.junit.Test;
27: import org.slf4j.Logger;
28:
29: import java.lang.reflect.Field;
30: import java.net.MalformedURLException;
31: import java.net.URI;
32: import java.net.URL;
33: import java.time.Instant;
34: import java.util.*;
35: import java.util.stream.Collectors;
36: import java.util.stream.IntStream;
37:
38: import static org.hamcrest.CoreMatchers.containsString;
39: import static org.hamcrest.core.Is.isA;
40: import static org.junit.Assert.*;
41:
42: public abstract class UpdateOperationsRunner extends BaseRunner {
43:
44: private OWLClassA entityA2;
45: private OWLClassF entityF;
46: private OWLClassJ entityJ;
47: private OWLClassO entityO;
48:
49: public UpdateOperationsRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
50: super(logger, persistenceFactory, dataAccessor);
51: initialize();
52: }
53:
54: private void initialize() {
55: this.entityF = new OWLClassF();
56: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
57: this.entityA2 = new OWLClassA();
58: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
59: this.entityJ = new OWLClassJ();
60: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
61: final Set<OWLClassA> set = new HashSet<>(2);
62: set.add(entityA);
63: set.add(entityA2);
64: entityJ.setOwlClassA(set);
65: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
66: final Set<OWLClassE> es = new HashSet<>();
67: for (int i = 0; i < 5; i++) {
68: final OWLClassE e = new OWLClassE();
69: e.setStringAttribute("StringAttributeNo" + i);
70: es.add(e);
71: }
72: entityO.setOwlClassESet(es);
73: }
74:
75: @Test
76: public void testUpdateDataLeaveLazy() throws Exception {
77: logger.debug(
78: "Test: update data property. Leaves lazily loaded field empty and checks that after commit the field's value hasn't changed.");
79: this.em = getEntityManager("UpdateDataProperty", false);
80: entityB.setProperties(Generators.createProperties());
81: persist(entityB);
82:
83: em.getTransaction().begin();
84: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
85: assertNotNull(b);
86: final Field propsField = OWLClassB.class.getDeclaredField("properties");
87: propsField.setAccessible(true);
88: assertNull(propsField.get(b));
89: final String newString = "NewString";
90: b.setStringAttribute(newString);
91: em.getTransaction().commit();
92:
93: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
94: assertNotNull(res);
95: assertEquals(newString, res.getStringAttribute());
96: assertNotNull(res.getProperties());
97: assertEquals(entityB.getProperties(), res.getProperties());
98: }
99:
100: @Test
101: public void testUpdateDataPropertySetNull() {
102: logger.debug("Test: update data property. Set it to null.");
103: this.em = getEntityManager("UpdateDataPropertyToNull", true);
104: persist(entityA);
105:
106: em.getTransaction().begin();
107: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
108: assertNotNull(a);
109: assertNotNull(a.getStringAttribute());
110: a.setStringAttribute(null);
111: em.getTransaction().commit();
112:
113: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
114: assertNotNull(res);
115: assertNull(res.getStringAttribute());
116: assertEquals(entityA.getTypes(), res.getTypes());
117: }
118:
119: @Test
120: public void testUpdateReference() {
121: logger.debug("Test: update reference to entity.");
122: this.em = getEntityManager("UpdateReference", true);
123: persist(entityD, entityI);
124: // em.persist(entityA, ctx);
125:
126: final OWLClassA newA = new OWLClassA();
127: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
128: newA.setStringAttribute("newA");
129: em.getTransaction().begin();
130: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
131: assertNotNull(d.getOwlClassA());
132: d.setOwlClassA(newA);
133: final OWLClassI i = em.find(OWLClassI.class, entityI.getUri());
134: assertNotNull(i.getOwlClassA());
135: i.setOwlClassA(newA);
136: em.persist(newA);
137: em.getTransaction().commit();
138:
139: final OWLClassA resA1 = em.find(OWLClassA.class, newA.getUri());
140: assertNotNull(resA1);
141: final OWLClassD resD = em.find(OWLClassD.class, d.getUri());
142: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
143: //                assertSame(resD.getOwlClassA(), resA1);
144: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
145: final OWLClassI resI = em.find(OWLClassI.class, i.getUri());
146: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
147: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
148: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
149: //                assertEquals(resA1, resI.getOwlClassA());
150: }
151:
152: @Test
153: public void mergedInstanceWithChangesInCascadedPluralReferenceAttributeContainsUpdatedValues() {
154: this.em = getEntityManager("MergeSet", false);
155: persist(entityJ);
156: em.clear();
157:
158: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
159:
160: em.getTransaction().begin();
161: OWLClassJ merged = em.merge(entityJ);
162:
163: for (final OWLClassA a : merged.getOwlClassA()) {
164: assertEquals(a.getStringAttribute(), "NEWVALUE");
165: }
166: em.getTransaction().commit();
167: }
168:
169: @Test
170: public void testMergeDetachedWithChanges() {
171: logger.debug("Test: merge detached entity with changes.");
172: this.em = getEntityManager("UpdateDetached", true);
173: persist(entityA);
174:
175: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
176: assertTrue(em.contains(a));
177: em.detach(a);
178: assertFalse(em.contains(a));
179: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
180: a.getTypes().add(newType);
181: em.getTransaction().begin();
182: final OWLClassA merged = em.merge(a);
183: assertTrue(merged.getTypes().contains(newType));
184: em.getTransaction().commit();
185:
186: final OWLClassA res = em.find(OWLClassA.class, a.getUri());
187: assertEquals(a.getTypes().size(), res.getTypes().size());
188: assertTrue(res.getTypes().containsAll(a.getTypes()));
189: }
190:
191: @Test
192: public void testMergeDetachedCascade() {
193: logger.debug("Test: merge detached with cascade.");
194: this.em = getEntityManager("UpdateCascade", true);
195: em.getTransaction().begin();
196: em.persist(entityH);
197: assertTrue(em.contains(entityA));
198: em.getTransaction().commit();
199:
200: em.getTransaction().begin();
201: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
202: assertNotNull(h.getOwlClassA());
203: em.detach(h);
204: assertFalse(em.contains(h));
205: assertFalse(em.contains(h.getOwlClassA()));
206: final String newStr = "newStringAttribute";
207: h.getOwlClassA().setStringAttribute(newStr);
208: final OWLClassH merged = em.merge(h);
209: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
210: em.getTransaction().commit();
211:
212: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
213: assertNotNull(res);
214: assertEquals(newStr, res.getStringAttribute());
215: }
216:
217: @Test
218: public void testMergeDetachedWithObjectPropertyChange() {
219: logger.debug("Test: merge detached with object property change.");
220: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
221: persist(entityD, entityA);
222:
223: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
224: em.detach(d);
225: d.setOwlClassA(entityA2);
226: em.getTransaction().begin();
227: em.merge(d);
228: em.persist(entityA2);
229: em.getTransaction().commit();
230:
231: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
232: assertNotNull(res);
233: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
234: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
235: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
236: assertNotNull(resA);
237: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
238: assertEquals(entityA.getTypes(), resA.getTypes());
239: }
240:
241: @Test
242: public void testRemoveFromSimpleList() {
243: logger.debug("Test: remove entity from simple list. (But keep it in the ontology.)");
244: this.em = getEntityManager("UpdateRemoveFromSimpleList", true);
245: entityC.setSimpleList(Generators.createSimpleList());
246: persistEntityWithList();
247:
248: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
249: assertNotNull(c);
250: em.getTransaction().begin();
251: final OWLClassA a = c.getSimpleList().get(1);
252: c.getSimpleList().remove(a);
253: em.getTransaction().commit();
254:
255: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
256: assertNotNull(resA);
257: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
258: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
259: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
260: for (OWLClassA aa : resC.getSimpleList()) {
261: assertNotEquals(resA.getUri(), aa.getUri());
262: }
263: }
264:
265: private void persistEntityWithList() {
266: em.getTransaction().begin();
267: em.persist(entityC);
268: if (entityC.getSimpleList() != null) {
269: entityC.getSimpleList().forEach(em::persist);
270: }
271: if (entityC.getReferencedList() != null) {
272: entityC.getReferencedList().forEach(em::persist);
273: }
274: em.getTransaction().commit();
275: }
276:
277: @Test
278: public void testAddToSimpleList() {
279: logger.debug("Test: add entity to simple list.");
280: this.em = getEntityManager("UpdateAddToSimpleList", true);
281: entityC.setSimpleList(Generators.createSimpleList());
282: em.getTransaction().begin();
283: em.persist(entityC);
284: entityC.getSimpleList().forEach(em::persist);
285: em.persist(entityA);
286: em.getTransaction().commit();
287:
288: em.getTransaction().begin();
289: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
290: assertNotNull(c);
291: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
292: assertNotNull(a);
293: assertFalse(c.getSimpleList().contains(a));
294: c.getSimpleList().add(a);
295: em.getTransaction().commit();
296:
297: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
298: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
299: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
300: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
301: assertNotNull(resA);
302: assertTrue(resC.getSimpleList().contains(resA));
303: }
304:
305: @Test
306: public void testClearSimpleList() {
307: logger.debug("Test: clear a simple list (but keep the entities in ontology).");
308: this.em = getEntityManager("UpdateClearSimpleList", true);
309: entityC.setSimpleList(Generators.createSimpleList());
310: persistEntityWithList();
311:
312: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
313: assertNotNull(c);
314: assertFalse(c.getSimpleList().isEmpty());
315: em.getTransaction().begin();
316: c.getSimpleList().clear();
317: em.getTransaction().commit();
318:
319: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
320: assertNotNull(resC);
321: assertTrue(resC.getSimpleList() == null || resC.getSimpleList().isEmpty());
322: for (OWLClassA a : entityC.getSimpleList()) {
323: assertNotNull(em.find(OWLClassA.class, a.getUri()));
324: }
325: }
326:
327: @Test
328: public void testReplaceSimpleList() {
329: logger.debug("Test: replace simple list with a new one.");
330: this.em = getEntityManager("UpdateReplaceSimpleList", true);
331: entityC.setSimpleList(Generators.createSimpleList());
332: persistEntityWithList();
333:
334: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
335: final List<OWLClassA> newList = new ArrayList<>(1);
336: newList.add(entityA);
337: em.getTransaction().begin();
338: em.persist(entityA);
339: c.setSimpleList(newList);
340: em.getTransaction().commit();
341:
342: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
343: assertNotNull(resC);
344: assertEquals(newList.size(), resC.getSimpleList().size());
345: boolean found;
346: for (OWLClassA a : newList) {
347: found = false;
348: for (OWLClassA aa : resC.getSimpleList()) {
349: if (a.getUri().equals(aa.getUri())) {
350: found = true;
351: break;
352: }
353: }
354: assertTrue(found);
355: }
356: for (OWLClassA a : entityC.getSimpleList()) {
357: assertNotNull(em.find(OWLClassA.class, a.getUri()));
358: }
359: }
360:
361: @Test
362: public void testRemoveFromReferencedList() {
363: logger.debug("Test: remove entity from referenced list. (But keep it in the ontology.");
364: this.em = getEntityManager("UpdateRemoveFromReferencedList", true);
365: entityC.setReferencedList(Generators.createReferencedList());
366: persistEntityWithList();
367:
368: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
369: assertNotNull(c);
370: em.getTransaction().begin();
371: final OWLClassA a = c.getReferencedList().get(Generators.randomInt(c.getReferencedList().size()));
372: c.getReferencedList().remove(a);
373: em.getTransaction().commit();
374:
375: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
376: assertNotNull(resA);
377: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
378: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
379: assertEquals(entityC.getReferencedList().size() - 1, resC.getReferencedList().size());
380: for (OWLClassA aa : resC.getReferencedList()) {
381: assertNotEquals(resA.getUri(), aa.getUri());
382: }
383: }
384:
385: @Test
386: public void testAddToReferencedList() {
387: logger.debug("Test: add entity to Referenced list.");
388: this.em = getEntityManager("UpdateAddToReferencedList", true);
389: entityC.setReferencedList(Generators.createReferencedList());
390: persistEntityWithList();
391:
392: em.getTransaction().begin();
393: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
394: assertNotNull(c);
395: em.persist(entityA);
396: c.getReferencedList().add(entityA);
397: em.getTransaction().commit();
398:
399: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
400: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
401: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
402: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
403: assertNotNull(resA);
404: assertTrue(resC.getReferencedList().contains(resA));
405: }
406:
407: @Test
408: public void testClearReferencedList() {
409: logger.debug("Test: clear referenced list (but keep the entities in ontology).");
410: this.em = getEntityManager("UpdateClearReferencedList", true);
411: entityC.setReferencedList(Generators.createReferencedList());
412: persistEntityWithList();
413:
414: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
415: assertNotNull(c);
416: assertFalse(c.getReferencedList().isEmpty());
417: em.getTransaction().begin();
418: c.setReferencedList(null);
419: em.getTransaction().commit();
420:
421: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
422: assertNotNull(resC);
423: assertNull(resC.getReferencedList());
424: for (OWLClassA a : entityC.getReferencedList()) {
425: assertNotNull(em.find(OWLClassA.class, a.getUri()));
426: }
427: }
428:
429: @Test
430: public void testReplaceReferencedList() {
431: logger.debug("Test: replace referenced list with a new one.");
432: this.em = getEntityManager("UpdateReplaceReferencedList", true);
433: entityC.setReferencedList(Generators.createReferencedList());
434: persistEntityWithList();
435:
436: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
437: final List<OWLClassA> newList = new ArrayList<>(1);
438: newList.add(entityA);
439: em.getTransaction().begin();
440: em.persist(entityA);
441: c.setReferencedList(newList);
442: em.getTransaction().commit();
443:
444: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
445: assertNotNull(resC);
446: assertEquals(newList.size(), resC.getReferencedList().size());
447: boolean found;
448: for (OWLClassA a : newList) {
449: found = false;
450: for (OWLClassA aa : resC.getReferencedList()) {
451: if (a.getUri().equals(aa.getUri())) {
452: found = true;
453: break;
454: }
455: }
456: assertTrue(found);
457: }
458: for (OWLClassA a : entityC.getReferencedList()) {
459: assertNotNull(em.find(OWLClassA.class, a.getUri()));
460: }
461: }
462:
463: @Test
464: public void testAddNewToProperties() {
465: logger.debug("Test: add a new property value to entity's properties.");
466: this.em = getEntityManager("UpdateAddNewToProperties", false);
467: entityB.setProperties(Generators.createProperties());
468: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
469: expected.putAll(entityB.getProperties());
470: persist(entityB);
471: em.clear();
472:
473: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
474: assertNotNull(b);
475: assertEquals(expected.size(), b.getProperties().size());
476: em.getTransaction().begin();
477: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
478: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
479: expected.putAll(b.getProperties());
480: em.getTransaction().commit();
481:
482: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
483: assertNotNull(res);
484: assertEquals(expected.size(), res.getProperties().size());
485: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
486: assertTrue(res.getProperties().containsKey(e.getKey()));
487: final Set<String> s = e.getValue();
488: final Set<String> resS = res.getProperties().get(e.getKey());
489: assertEquals(s.size(), resS.size());
490: assertTrue(s.containsAll(resS));
491: }
492: }
493:
494: @Test
495: public void testAddPropertyValue() {
496: logger.debug("Test: add another value to an existing property.");
497: this.em = getEntityManager("UpdateAddPropertyValue", false);
498: entityB.setProperties(Generators.createProperties());
499: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
500: final String prop = entityB.getProperties().keySet().iterator().next();
501: expected.putAll(entityB.getProperties());
502: persist(entityB);
503: em.clear();
504:
505: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
506: assertNotNull(b);
507: assertEquals(expected.size(), b.getProperties().size());
508: em.getTransaction().begin();
509: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
510: expected.putAll(b.getProperties());
511: em.getTransaction().commit();
512:
513: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
514: assertNotNull(res);
515: assertEquals(expected.size(), res.getProperties().size());
516: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
517: assertTrue(res.getProperties().containsKey(e.getKey()));
518: final Set<String> s = e.getValue();
519: final Set<String> resS = res.getProperties().get(e.getKey());
520: assertEquals(s.size(), resS.size());
521: if (e.getKey().equals(prop)) {
522: assertTrue(s.size() > 1);
523: for (String val : s) {
524: assertTrue(resS.contains(val));
525: }
526: } else {
527: assertTrue(resS.contains(s.iterator().next()));
528: }
529: }
530: }
531:
532: @Test
533: public void testAddPropertyValueDetached() {
534: logger.debug("Test: add another value to an existing property.");
535: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
536: entityB.setProperties(Generators.createProperties());
537: final String prop = entityB.getProperties().keySet().iterator().next();
538: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
539: persist(entityB);
540: em.clear();
541:
542: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
543: b.getProperties().get(prop).add(newPropertyValue);
544: em.detach(b);
545: em.getTransaction().begin();
546: em.merge(b);
547: em.getTransaction().commit();
548:
549: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
550: assertNotNull(res);
551: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
552: }
553:
554: @Test
555: public void testAddNewTypedPropertyWithValues() {
556: logger.debug("Test: add a new property with values. Typed.");
557: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
558: entityP.setProperties(Generators.createTypedProperties());
559: persist(entityP);
560: em.clear();
561:
562: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
563: em.getTransaction().begin();
564: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
565: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", new Date())));
566: em.getTransaction().commit();
567:
568: em.clear();
569:
570: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
571: assertNotNull(res);
572: assertTrue(res.getProperties().containsKey(property));
573: assertEquals(p.getProperties(), res.getProperties());
574: }
575:
576: @Test
577: public void testAddTypedPropertyValue() {
578: logger.debug("Test: add an existing property value. Typed.");
579: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
580: entityP.setProperties(Generators.createTypedProperties());
581: persist(entityP);
582: em.clear();
583:
584: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
585: em.getTransaction().begin();
586: final URI property = p.getProperties().keySet().iterator().next();
587: final Object value = generateValueForProperty(p, property);
588: p.getProperties().get(property).add(value);
589: em.getTransaction().commit();
590:
591: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
592: assertTrue(res.getProperties().get(property).contains(value));
593: assertEquals(p.getProperties(), res.getProperties());
594: }
595:
596: private Object generateValueForProperty(OWLClassP instance, URI property) {
597: final Set<Object> values = instance.getProperties().get(property);
598: assert values != null && !values.isEmpty();
599: if (values.iterator().next() instanceof URI) {
600: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
601: } else {
602: return "StringValue";
603: }
604: }
605:
606: @Test(expected = OWLInferredAttributeModifiedException.class)
607: public void testModifyInferredAttribute() {
608: logger.debug("Test: modify an inferred attribute.");
609: this.em = getEntityManager("ModifyInferredAttribute", false);
610: persist(entityF);
611:
612: em.getTransaction().begin();
613: final OWLClassF f = em.find(OWLClassF.class, entityF.getUri());
614: assertNotNull(f);
615: f.setSecondStringAttribute("otherValue");
616: em.getTransaction().commit();
617: }
618:
619: @Test
620: public void testModifyAttributesOfBasicTypes() {
621: logger.debug("Test: modify attributes of basic Java types (Integer, Boolean etc.).");
622: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
623: persist(entityM);
624:
625: em.getTransaction().begin();
626: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
627: m.setBooleanAttribute(!entityM.getBooleanAttribute());
628: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
629: m.setLongAttribute(m.getLongAttribute() + 100L);
630: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
631: em.getTransaction().commit();
632:
633: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
634: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
635: assertEquals(m.getLongAttribute(), res.getLongAttribute());
636: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
637: assertEquals(m.getDateAttribute(), res.getDateAttribute());
638: }
639:
640: @Test
641: public void testModifyEnumAttribute() {
642: logger.debug("Test: modify enum attribute.");
643: this.em = getEntityManager("ModifyEnumAttribute", false);
644: assertNotNull(entityM.getEnumAttribute());
645: persist(entityM);
646:
647: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
648: em.getTransaction().begin();
649: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
650: m.setEnumAttribute(updated);
651: em.getTransaction().commit();
652:
653: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
654: assertNotNull(res);
655: assertEquals(updated, res.getEnumAttribute());
656: }
657:
658: @Test
659: public void testCascadeOfNewlyPersistedOnMerge() {
660: logger.debug("Test: cascade merging of an object that has been just persisted.");
661: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
662: em.getTransaction().begin();
663: em.persist(entityO);
664: entityO.getOwlClassESet().forEach(em::persist);
665: em.getTransaction().commit();
666:
667: final OWLClassE newE = new OWLClassE();
668: newE.setStringAttribute("NewlyAddedE");
669: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
670: em.detach(toUpdate);
671:
672: em.getTransaction().begin();
673: toUpdate.getOwlClassESet().add(newE);
674: em.persist(newE);
675: final OWLClassO merged = em.merge(toUpdate);
676: assertTrue(merged.getOwlClassESet().contains(newE));
677: em.getTransaction().commit();
678:
679: final OWLClassE resE = em.find(OWLClassE.class, newE.getUri());
680: assertNotNull(resE);
681: final OWLClassO resO = em.find(OWLClassO.class, entityO.getUri());
682: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
683: }
684:
685: @Test
686: public void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
687: logger.debug(
688: "Test: modify collection after cascade merge and check that the changes have been propagated on commit.");
689: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
690: em.getTransaction().begin();
691: em.persist(entityJ);
692: em.getTransaction().commit();
693:
694: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
695: em.getTransaction().begin();
696: final OWLClassJ merged = em.merge(entityJ);
697: merged.getOwlClassA().add(newA);
698: em.persist(newA);
699: em.getTransaction().commit();
700:
701: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
702: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
703: boolean found = false;
704: for (OWLClassA a : result.getOwlClassA()) {
705: if (a.getUri().equals(newA.getUri())) {
706: found = true;
707: break;
708: }
709: }
710: assertTrue(found);
711: }
712:
713: @Test
714: public void testAddPropertiesWhenTheyWereNullOriginally() {
715: logger.debug("Test: add properties on update when the field was originally null.");
716: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
717: em.getTransaction().begin();
718: assertNull(entityB.getProperties());
719: em.persist(entityB);
720: em.getTransaction().commit();
721:
722: final OWLClassB update = em.find(OWLClassB.class, entityB.getUri());
723: em.detach(update);
724: final Map<String, Set<String>> properties = Generators.createProperties(2);
725: update.setProperties(properties);
726: em.getTransaction().begin();
727: em.merge(update);
728: em.getTransaction().commit();
729:
730: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
731: assertNotNull(result.getProperties());
732: assertEquals(properties, result.getProperties());
733: }
734:
735: @Test
736: public void testUpdatePlainLiteralObjectPropertyValueToAnotherIndividual() {
737: logger.debug("Test: update plain literal object property value to a different individual.");
738: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValue", true);
739: em.getTransaction().begin();
740: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#oldIndividual"));
741: em.persist(entityP);
742: em.getTransaction().commit();
743:
744: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
745: final URI newValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newIndividual");
746: em.getTransaction().begin();
747: update.setIndividualUri(newValue);
748: em.getTransaction().commit();
749:
750: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
751: assertEquals(newValue, result.getIndividualUri());
752: }
753:
754: @Test
755: public void testUpdatePlainLiteralObjectPropertyValueToNull() {
756: logger.debug("Test: update plain literal object property value to null.");
757: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValueToNull", true);
758: em.getTransaction().begin();
759: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#SomeIndividual"));
760: em.persist(entityP);
761: em.getTransaction().commit();
762:
763: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
764: assertNotNull(update.getIndividualUri());
765: em.getTransaction().begin();
766: update.setIndividualUri(null);
767: em.getTransaction().commit();
768:
769: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
770: assertNull(result.getIndividualUri());
771: }
772:
773: @Test
774: public void testUpdatePluralPlainLiteralObjectPropertyAttribute() throws MalformedURLException {
775: logger.debug("Test: update plural plain literal object property attribute.");
776: this.em = getEntityManager("UpdatePluralPlainLiteralObjectPropertyValue", true);
777: entityP.setIndividuals(Generators.createUrls());
778: persist(entityP);
779:
780: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
781: em.getTransaction().begin();
782: final URL added = new URL("http://krizik.felk.cvut.cz/ontologies/jopa#added");
783: final URL removed = entityP.getIndividuals().iterator().next();
784: update.getIndividuals().add(added);
785: update.getIndividuals().remove(removed);
786: em.getTransaction().commit();
787:
788: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
789: assertNotNull(res);
790: assertEquals(update.getIndividuals(), res.getIndividuals());
791: }
792:
793: @Test
794: public void testUpdateSimpleListOfIdentifiersByAddingNewItems() {
795: this.em = getEntityManager("UpdateSimpleListOfIdentifiersByAddingItems", true);
796: entityP.setSimpleList(Generators.createListOfIdentifiers());
797: persist(entityP);
798:
799: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
800: em.getTransaction().begin();
801: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
802: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
803: // Insert at random position
804: update.getSimpleList().add(Generators.randomInt(update.getSimpleList().size()), u);
805: }
806: em.getTransaction().commit();
807:
808: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
809: assertNotNull(res);
810: assertEquals(update.getSimpleList(), res.getSimpleList());
811: }
812:
813: @Test
814: public void testUpdateReferencedListByRemovingAndAddingItems() {
815: this.em = getEntityManager("UpdateReferencedListByRemovingAndAddingItems", true);
816: entityP.setReferencedList(Generators.createListOfIdentifiers());
817: persist(entityP);
818:
819: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
820: em.getTransaction().begin();
821: for (int i = 0; i < Generators.randomPositiveInt(5, 10); i++) {
822: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
823: // We might even overwrite items set in previous iterations, but it does not matter. JOPA should handle it
824: update.getReferencedList().set(Generators.randomInt(update.getReferencedList().size()), u);
825: }
826: em.getTransaction().commit();
827:
828: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
829: assertNotNull(res);
830: assertEquals(update.getReferencedList(), res.getReferencedList());
831: }
832:
833: @Test
834: public void testUpdateStringAnnotationPropertyValue() {
835: this.em = getEntityManager("UpdateStringAnnotationPropertyValue", true);
836: entityN.setAnnotationProperty("PropertyValue");
837: persist(entityN);
838:
839: final String newValue = "newValue";
840: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
841: assertNotNull(update);
842: em.getTransaction().begin();
843: update.setAnnotationProperty(newValue);
844: em.getTransaction().commit();
845:
846: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
847: assertEquals(newValue, res.getAnnotationProperty());
848: }
849:
850: @Test
851: public void testUpdateAnnotationPropertyWithUriValueToDifferentValue() {
852: this.em = getEntityManager("UpdateAnnotationPropertyWithUriValueToDifferentValue", true);
853: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value"));
854: persist(entityN);
855:
856: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
857: assertNotNull(update);
858: em.getTransaction().begin();
859: final URI newUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newValue");
860: update.setAnnotationUri(newUri);
861: em.getTransaction().commit();
862:
863: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
864: assertEquals(newUri, res.getAnnotationUri());
865: }
866:
867: @Test
868: public void testUpdateUriTypes() {
869: this.em = getEntityManager("UpdateUriTypes", true);
870: entityP.setTypes(Generators.createUriTypes());
871: persist(entityP);
872:
873: em.getTransaction().begin();
874: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
875: final Iterator<URI> it = update.getTypes().iterator();
876: while (it.hasNext()) {
877: it.next();
878: if (Generators.randomBoolean()) {
879: it.remove();
880: }
881: }
882: update.getTypes().addAll(Generators.createUriTypes());
883: em.getTransaction().commit();
884:
885: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
886: assertEquals(update.getTypes().size(), result.getTypes().size());
887: assertTrue(update.getTypes().containsAll(result.getTypes()));
888: }
889:
890: @Test
891: public void updateWithInvalidInstanceThrowsIntegrityConstraintsViolation() {
892: thrown.expect(RollbackException.class);
893: thrown.expectCause(isA(IntegrityConstraintViolatedException.class));
894:
895: this.em = getEntityManager("UpdateWithInvalidInstanceThrowsICViolation", true);
896: persist(entityN);
897:
898: entityN.setStringAttribute(null);
899: em.getTransaction().begin();
900: em.merge(entityN);
901: em.getTransaction().commit();
902: }
903:
904: @Test
905: public void addingValuesToDatatypePropertyCollectionAddsThemIntoRepository() {
906: this.em = getEntityManager("addingValuesToDatatypePropertyCollectionAddsThemIntoRepository", false);
907: persist(entityM);
908:
909: IntStream.generate(Generators::randomInt).limit(7).forEach(entityM.getIntegerSet()::add);
910: em.getTransaction().begin();
911: em.merge(entityM);
912: em.getTransaction().commit();
913:
914: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
915: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
916: }
917:
918: @Test
919: public void removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository() {
920: this.em = getEntityManager("removingValuesFromDatatypePropertyCollectionRemovesThemFromRepository", false);
921: persist(entityM);
922:
923: final Iterator<Integer> it = entityM.getIntegerSet().iterator();
924: while (it.hasNext()) {
925: it.next();
926: if (Generators.randomBoolean()) {
927: it.remove();
928: }
929: }
930: // Make sure there is at least one element
931: entityM.getIntegerSet().add(Generators.randomInt());
932:
933: em.getTransaction().begin();
934: em.merge(entityM);
935: em.getTransaction().commit();
936:
937: final OWLClassM result = em.find(OWLClassM.class, entityM.getKey());
938: assertEquals(entityM.getIntegerSet(), result.getIntegerSet());
939: }
940:
941: @Test
942: public void mergeReplacesReferenceWithInstanceFromRepository() {
943: this.em = getEntityManager("mergeReplacesReferenceWithInstanceFromRepository", true);
944: persist(entityD, entityA);
945:
946: final String originalString = entityA.getStringAttribute();
947: entityA.setStringAttribute("updatedStringAttribute");
948: em.getTransaction().begin();
949: final OWLClassD merged = em.merge(entityD);
950: assertEquals(originalString, merged.getOwlClassA().getStringAttribute());
951: em.getTransaction().commit();
952:
953: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri());
954: assertEquals(originalString, result.getStringAttribute());
955: }
956:
957: @Test
958: public void mergeMergesUpdatedDataPropertyFields() {
959: this.em = getEntityManager("mergeMergesUpdatedDataPropertyFields", true);
960: persist(entityA);
961: final Set<String> newTypes =
962: Generators.createUriTypes().stream().map(URI::toString).collect(Collectors.toSet());
963: entityA.setTypes(newTypes);
964: final String newStringAtt = "newStringAttribute";
965: entityA.setStringAttribute(newStringAtt);
966:
967: em.getTransaction().begin();
968: final OWLClassA merged = em.merge(entityA);
969: assertEquals(newStringAtt, merged.getStringAttribute());
970: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, merged.getTypes()));
971: em.getTransaction().commit();
972:
973: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri());
974: assertEquals(newStringAtt, result.getStringAttribute());
975: assertTrue(TestEnvironmentUtils.typesEqual(newTypes, result.getTypes()));
976: }
977:
978: @Test
979: public void mergeMergesUpdateToObjectPropertyField() {
980: this.em = getEntityManager("mergeMergesUpdateToObjectPropertyField", true);
981: persist(entityD, entityA, entityA2);
982:
983: em.getTransaction().begin();
984: entityD.setOwlClassA(entityA2);
985: final OWLClassD merged = em.merge(entityD);
986: assertEquals(entityA2.getUri(), merged.getOwlClassA().getUri());
987: em.getTransaction().commit();
988:
989: final OWLClassD dResult = em.find(OWLClassD.class, entityD.getUri());
990: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
991: final OWLClassA aReference = em.find(OWLClassA.class, entityA2.getUri());
992: assertSame(dResult.getOwlClassA(), aReference);
993: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
994: }
995:
996: @Test
997: public void updateSupportsSavingStringLiteralWithDifferentLanguageTag() throws Exception {
998: this.em = getEntityManager("updateSupportsSavingStringLiteralWithDifferentLanguageTag", false);
999: persist(entityA);
1000:
1001: entityA.setStringAttribute("hodnota v cestine");
1002: final Descriptor descriptor = new EntityDescriptor();
1003: descriptor.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), "cs");
1004: em.getTransaction().begin();
1005: em.merge(entityA, descriptor);
1006: em.getTransaction().commit();
1007:
1008: final OWLClassA resultOne = em.find(OWLClassA.class, entityA.getUri());
1009: assertNull(resultOne.getStringAttribute());
1010: em.clear();
1011: final OWLClassA resultTwo = em.find(OWLClassA.class, entityA.getUri(), descriptor);
1012: assertEquals(entityA.getStringAttribute(), resultTwo.getStringAttribute());
1013: }
1014:
1015: @Test
1016: public void mergeDetachedRemovesObsoleteDescriptorInSecondLevelCache() {
1017: this.em = getEntityManager("mergeDetachedReplacesObsoleteDescriptorInSecondLevelCache", true);
1018: final Descriptor descriptorOne = new EntityDescriptor();
1019: descriptorOne.setLanguage("en");
1020: em.getTransaction().begin();
1021: em.persist(entityA, descriptorOne);
1022: em.getTransaction().commit();
1023: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
1024:
1025: final Descriptor descriptorTwo = new EntityDescriptor();
1026: descriptorTwo.setLanguage("cs");
1027: assertNotEquals(descriptorOne, descriptorTwo);
1028: entityA.setStringAttribute("cesky");
1029: em.getTransaction().begin();
1030: em.merge(entityA, descriptorTwo);
1031: em.getTransaction().commit();
1032:
1033: assertFalse(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorOne));
1034: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
1035: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
1036: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
1037: }
1038:
1039: @Test
1040: public void mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting() throws Exception {
1041: this.em = getEntityManager("mergeUpdatesCacheEventWhenDescriptorContainsOnlyAttributeLanguageSetting", true);
1042: persist(entityA);
1043:
1044: final Descriptor descriptorTwo = new EntityDescriptor();
1045: descriptorTwo.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), "cs");
1046: entityA.setStringAttribute("cesky");
1047: em.getTransaction().begin();
1048: em.merge(entityA, descriptorTwo);
1049: em.getTransaction().commit();
1050:
1051: final OWLClassA result = em.find(OWLClassA.class, entityA.getUri(), descriptorTwo);
1052: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
1053: assertTrue(em.getEntityManagerFactory().getCache().contains(OWLClassA.class, entityA.getUri(), descriptorTwo));
1054: }
1055:
1056: @Test
1057: public void updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue() {
1058: this.em = getEntityManager("updateRemovesPendingAssertionWhenItIsReplacedByAnotherValue", true);
1059: entityD.setOwlClassA(null);
1060: persist(entityD, entityA2);
1061:
1062: em.getTransaction().begin();
1063: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
1064: d.setOwlClassA(entityA);
1065: d.setOwlClassA(em.find(OWLClassA.class, entityA2.getUri()));
1066: em.getTransaction().commit();
1067:
1068: final OWLClassD dResult = em.find(OWLClassD.class, entityD.getUri());
1069: assertNotNull(dResult.getOwlClassA());
1070: assertEquals(entityA2.getUri(), dResult.getOwlClassA().getUri());
1071: assertNull(em.find(OWLClassA.class, entityA.getUri()));
1072: }
1073:
1074: @Test
1075: public void updatesKeepsPendingAssertionPluralAttribute() {
1076: this.em = getEntityManager("updatesKeepsPendingAssertionPluralAttribute", true);
1077: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
1078: entityL.setSet(new HashSet<>());
1079: entityL.getSet().add(entityA2);
1080: entityL.setSimpleList(Collections.singletonList(entityA2));
1081: entityL.setSingleA(entityA2);
1082: entityL.setUri(Generators.generateUri());
1083: persist(entityL, entityA2);
1084:
1085: em.getTransaction().begin();
1086: final OWLClassL inst = em.find(OWLClassL.class, entityL.getUri());
1087: inst.getSet().add(entityA);
1088: thrown.expect(RollbackException.class);
1089: thrown.expectCause(isA(UnpersistedChangeException.class));
1090: thrown.expectMessage(containsString(entityA.toString()));
1091: em.getTransaction().commit();
1092: }
1093:
1094: @Test
1095: public void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged() {
1096: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributePreviouslyMerged",
1097: true);
1098: persist(entityD, entityA);
1099:
1100: final String newString = "updatedString";
1101: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1102: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1103: aUpdate.setStringAttribute(newString);
1104: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1105: dUpdate.setOwlClassA(aUpdate);
1106: em.clear();
1107: em.getTransaction().begin();
1108: final OWLClassD orig = em.find(OWLClassD.class, entityD.getUri());
1109: em.detach(orig);
1110: em.find(OWLClassA.class, entityA.getUri());
1111: em.merge(aUpdate);
1112: em.merge(dUpdate);
1113: em.getTransaction().commit();
1114: final OWLClassD result = em.find(OWLClassD.class, entityD.getUri());
1115: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1116: }
1117:
1118: @Test
1119: public void mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater() {
1120: this.em = getEntityManager("mergeCorrectlyUpdatesCacheInCaseOfChangeInReferencedAttributeMergedLater",
1121: true);
1122: persist(entityD, entityA);
1123:
1124: final String newString = "updatedString";
1125: final OWLClassD dUpdate = new OWLClassD(entityD.getUri());
1126: final OWLClassA aUpdate = new OWLClassA(entityA.getUri());
1127: aUpdate.setStringAttribute(newString);
1128: aUpdate.setTypes(new HashSet<>(entityA.getTypes()));
1129: dUpdate.setOwlClassA(aUpdate);
1130: em.clear();
1131: em.getTransaction().begin();
1132: final OWLClassD orig = em.find(OWLClassD.class, entityD.getUri());
1133: em.detach(orig);
1134: em.merge(dUpdate);
1135: em.merge(aUpdate);
1136: em.getTransaction().commit();
1137: final OWLClassD result = em.find(OWLClassD.class, entityD.getUri());
1138: assertEquals(newString, result.getOwlClassA().getStringAttribute());
1139: }
1140:
1141: @Test
1142: public void mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache() {
1143: this.em = getEntityManager("mergeCorrectlyMergesChangesOnPluralObjectPropertyIntoCache", true);
1144: final OWLClassL entityL = new OWLClassL(Generators.generateUri());
1145: entityL.setSet(new HashSet<>());
1146: entityL.getSet().add(entityA);
1147: entityL.getSet().add(entityA2);
1148: entityL.setSimpleList(Collections.singletonList(entityA2));
1149: entityL.setSingleA(entityA2);
1150: persist(entityL, entityA, entityA2);
1151:
1152: final OWLClassL toUpdate = new OWLClassL(entityL.getUri());
1153: toUpdate.setSet(new HashSet<>(entityL.getSet()));
1154: toUpdate.getSet().remove(entityA2);
1155: toUpdate.setSimpleList(Collections.singletonList(entityA2));
1156: toUpdate.setSingleA(entityA2);
1157: em.getTransaction().begin();
1158: final OWLClassL original = em.find(OWLClassL.class, entityL.getUri());
1159: assertEquals(2, original.getSet().size());
1160: for (OWLClassA a : original.getSet()) {
1161: if (a.getUri().equals(entityA2.getUri())) {
1162: original.getSet().remove(a);
1163: break;
1164: }
1165: }
1166: toUpdate.getSet().forEach(a -> em.merge(a));
1167: em.merge(toUpdate);
1168: em.getTransaction().commit();
1169:
1170: final OWLClassL result = em.find(OWLClassL.class, entityL.getUri());
1171: assertEquals(1, result.getSet().size());
1172: }
1173:
1174: @Test
1175: public void updateKeepsPendingListReferenceWhenItemIsAddedToIt() {
1176: this.em = getEntityManager("updateKeepsPendingListReferenceWhenItemIsAddedToIt", true);
1177: final OWLClassK entityK = new OWLClassK();
1178: entityK.setSimpleList(generateEInstances(5));
1179: entityK.setReferencedList(generateEInstances(6));
1180: em.getTransaction().begin();
1181: em.persist(entityK);
1182: entityK.getSimpleList().forEach(e -> {
1183: assertNull(e.getUri());
1184: em.persist(e);
1185: });
1186: entityK.getReferencedList().forEach(e -> {
1187: assertNull(e.getUri());
1188: em.persist(e);
1189: });
1190: em.getTransaction().commit();
1191:
1192: final OWLClassE addedSimple = new OWLClassE();
1193: addedSimple.setStringAttribute("addedSimple");
1194: final OWLClassE addedReferenced = new OWLClassE();
1195: addedReferenced.setStringAttribute("addedReferenced");
1196: em.getTransaction().begin();
1197: final OWLClassK update = em.find(OWLClassK.class, entityK.getUri());
1198: update.getSimpleList().add(addedSimple);
1199: update.getReferencedList().add(addedReferenced);
1200: assertNull(addedSimple.getUri());
1201: assertNull(addedReferenced.getUri());
1202: em.persist(addedSimple);
1203: em.persist(addedReferenced);
1204: em.getTransaction().commit();
1205:
1206: final OWLClassK result = em.find(OWLClassK.class, entityK.getUri());
1207: assertEquals(addedSimple.getUri(), result.getSimpleList().get(result.getSimpleList().size() - 1).getUri());
1208: assertEquals(addedReferenced.getUri(),
1209: result.getReferencedList().get(result.getReferencedList().size() - 1).getUri());
1210: }
1211:
1212: private List<OWLClassE> generateEInstances(int count) {
1213: return IntStream.range(0, count).mapToObj(i -> {
1214: final OWLClassE e = new OWLClassE();
1215: e.setStringAttribute("instance" + i);
1216: return e;
1217: }).collect(Collectors.toList());
1218: }
1219:
1220: @Test
1221: public void mergeRemovedThrowsIllegalArgumentException() {
1222: this.em = getEntityManager("mergeRemovedThrowsIllegalArgument", true);
1223: thrown.expect(IllegalArgumentException.class);
1224: thrown.expectMessage(containsString("removed"));
1225: persist(entityA);
1226:
1227: em.getTransaction().begin();
1228: final OWLClassA toRemove = em.find(OWLClassA.class, entityA.getUri());
1229: em.remove(toRemove);
1230: em.merge(toRemove);
1231: }
1232:
1233: @Test
1234: public void changesDoneAfterInstanceRefreshAreCommittedToRepository() {
1235: this.em = getEntityManager("changesDoneAfterInstanceRefreshAreCommittedToRepository", true);
1236: persist(entityD, entityA, entityA2);
1237:
1238: em.getTransaction().begin();
1239: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
1240: final OWLClassA origA = d.getOwlClassA();
1241: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1242: newA.setStringAttribute("newAWithStringAttribute");
1243: d.setOwlClassA(em.find(OWLClassA.class, entityA2.getUri()));
1244: em.refresh(d);
1245: assertEquals(origA, d.getOwlClassA());
1246: d.setOwlClassA(newA);
1247: em.persist(newA);
1248: em.getTransaction().commit();
1249:
1250: final OWLClassD result = em.find(OWLClassD.class, entityD.getUri());
1251: assertEquals(newA.getUri(), result.getOwlClassA().getUri());
1252: assertEquals(newA.getStringAttribute(), result.getOwlClassA().getStringAttribute());
1253: }
1254:
1255: @Test
1256: public void refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges() {
1257: this.em = getEntityManager("refreshOfPluralObjectPropertyReplacesCollectionWithOriginalAndAllowsFurtherChanges",
1258: true);
1259: persist(entityJ);
1260:
1261: em.getTransaction().begin();
1262: final OWLClassJ j = em.find(OWLClassJ.class, entityJ.getUri());
1263: j.getOwlClassA().clear();
1264: em.refresh(j);
1265: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1266: newA.setStringAttribute("newAWithStringAttribute");
1267: j.getOwlClassA().add(newA);
1268: em.persist(newA);
1269: em.getTransaction().commit();
1270:
1271: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
1272: assertEquals(entityJ.getOwlClassA().size() + 1, result.getOwlClassA().size());
1273: final Set<URI> aUris = new HashSet<>(Arrays.asList(entityA.getUri(), entityA2.getUri(), newA.getUri()));
1274: result.getOwlClassA().forEach(a -> assertTrue(aUris.contains(a.getUri())));
1275: }
1276:
1277: /**
1278: * Bug #33
1279: */
1280: @Test
1281: public void mergeWithUpdatedPropertyValueRemovesOriginalAssertion() {
1282: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertion", false);
1283: persist(entityH, entityA);
1284:
1285: em.getTransaction().begin();
1286: entityH.setOwlClassA(entityA2);
1287: em.merge(entityH);
1288: em.getTransaction().commit();
1289:
1290: final OWLClassH result = em.find(OWLClassH.class, entityH.getUri());
1291: assertEquals(entityA2.getUri(), result.getOwlClassA().getUri());
1292: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
1293: }
1294:
1295: /**
1296: * Bug #33, but for plural attributes
1297: */
1298: @Test
1299: public void mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute() {
1300: this.em = getEntityManager("mergeWithUpdatedPropertyValueRemovesOriginalAssertionsFromPluralAttribute", false);
1301: persist(entityJ);
1302:
1303: assertEquals(2, entityJ.getOwlClassA().size());
1304: final OWLClassA newA = new OWLClassA(Generators.generateUri());
1305: entityJ.setOwlClassA(Collections.singleton(newA));
1306: em.getTransaction().begin();
1307: em.merge(entityJ);
1308: em.getTransaction().commit();
1309:
1310: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
1311: assertEquals(1, result.getOwlClassA().size());
1312: assertEquals(newA.getUri(), result.getOwlClassA().iterator().next().getUri());
1313: }
1314:
1315: @Test
1316: public void updateSupportsJava8Instant() {
1317: this.em = getEntityManager("updateSupportsJava8Instant", true);
1318: final OWLClassX entityX = new OWLClassX();
1319: entityX.setInstant(Instant.now());
1320: persist(entityX);
1321:
1322: final Instant newInstant = Instant.ofEpochMilli(System.currentTimeMillis() + 10000);
1323: em.getTransaction().begin();
1324: final OWLClassX toUpdate = em.find(OWLClassX.class, entityX.getUri());
1325: toUpdate.setInstant(newInstant);
1326: em.getTransaction().commit();
1327:
1328: final OWLClassX cachedResult = em.find(OWLClassX.class, entityX.getUri());
1329: assertEquals(newInstant, cachedResult.getInstant());
1330: em.getEntityManagerFactory().getCache().evict(OWLClassX.class);
1331: final OWLClassX result = em.find(OWLClassX.class, entityX.getUri());
1332: assertEquals(newInstant, result.getInstant());
1333: }
1334:
1335: @Test
1336: public void updateSupportsPluralAnnotationProperty() {
1337: this.em = getEntityManager("updateSupportsPluralAnnotationProperty", true);
1338: final Set<String> annotations = IntStream.range(0, 5).mapToObj(i -> "Source" + i).collect(Collectors.toSet());
1339: entityN.setPluralAnnotationProperty(annotations);
1340: persist(entityN);
1341:
1342: final String added = "Added Source";
1343: final String removed = annotations.iterator().next();
1344: annotations.remove(removed);
1345: annotations.add(added);
1346: entityN.getPluralAnnotationProperty().remove(removed);
1347: entityN.getPluralAnnotationProperty().add(added);
1348: em.getTransaction().begin();
1349: final OWLClassN merged = em.merge(entityN);
1350: assertEquals(annotations, merged.getPluralAnnotationProperty());
1351: em.getTransaction().commit();
1352:
1353: final OWLClassN result = em.find(OWLClassN.class, entityN.getId());
1354: assertEquals(annotations, result.getPluralAnnotationProperty());
1355: }
1356:
1357: @Test
1358: public void updateSupportsSettingSubclassOnPolymorphicAttribute() {
1359: this.em = getEntityManager("updateSupportsSettingSubclassOnPolymorphicAttribute", true);
1360: final OWLClassU u = new OWLClassU();
1361: final OWLClassU reference = new OWLClassU();
1362: em.getTransaction().begin();
1363: em.persist(reference);
1364: em.getTransaction().commit();
1365:
1366: em.getTransaction().begin();
1367: final OWLClassU managedRef = em.merge(reference);
1368: u.setOwlClassS(managedRef);
1369: em.persist(u);
1370: em.getTransaction().commit();
1371:
1372: final OWLClassU result = em.find(OWLClassU.class, u.getUri());
1373: assertNotNull(result);
1374: assertEquals(reference.getUri(), result.getOwlClassS().getUri());
1375: }
1376: }