Skip to content

Method: modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit()

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.OWLInferredAttributeModifiedException;
16: import cz.cvut.kbss.jopa.test.*;
17: import cz.cvut.kbss.jopa.test.environment.Generators;
18: import org.junit.Test;
19: import org.slf4j.Logger;
20:
21: import java.lang.reflect.Field;
22: import java.net.URI;
23: import java.util.*;
24:
25: import static org.junit.Assert.*;
26:
27: public abstract class UpdateOperationsRunner extends BaseRunner {
28:
29: private OWLClassA entityA2;
30: private OWLClassF entityF;
31: private OWLClassJ entityJ;
32: private OWLClassO entityO;
33:
34: public UpdateOperationsRunner(Logger logger) {
35: super(logger);
36: initialize();
37: }
38:
39: private void initialize() {
40: this.entityF = new OWLClassF();
41: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
42: this.entityA2 = new OWLClassA();
43: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
44: this.entityJ = new OWLClassJ();
45: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
46: final Set<OWLClassA> set = new HashSet<>(2);
47: set.add(entityA);
48: set.add(entityA2);
49: entityJ.setOwlClassA(set);
50: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
51: final Set<OWLClassE> es = new HashSet<>();
52: for (int i = 0; i < 5; i++) {
53: final OWLClassE e = new OWLClassE();
54: e.setStringAttribute("StringAttributeNo" + i);
55: es.add(e);
56: }
57: entityO.setOwlClassESet(es);
58: }
59:
60: @Test
61: public void testUpdateDataLeaveLazy() throws Exception {
62: logger.debug(
63: "Test: update data property. Leaves lazily loaded field empty and checks that after commit the field's value hasn't changed.");
64: this.em = getEntityManager("UpdateDataProperty", false);
65: entityB.setProperties(Generators.createProperties());
66: persist(entityB);
67:
68: em.getTransaction().begin();
69: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
70: assertNotNull(b);
71: final Field propsField = OWLClassB.class.getDeclaredField("properties");
72: propsField.setAccessible(true);
73: assertNull(propsField.get(b));
74: final String newString = "NewString";
75: b.setStringAttribute(newString);
76: em.getTransaction().commit();
77:
78: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
79: assertNotNull(res);
80: assertEquals(newString, res.getStringAttribute());
81: assertNotNull(res.getProperties());
82: assertEquals(entityB.getProperties(), res.getProperties());
83: }
84:
85: @Test
86: public void testUpdateDataPropertySetNull() {
87: logger.debug("Test: update data property. Set it to null.");
88: this.em = getEntityManager("UpdateDataPropertyToNull", true);
89: persist(entityA);
90:
91: em.getTransaction().begin();
92: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
93: assertNotNull(a);
94: assertNotNull(a.getStringAttribute());
95: a.setStringAttribute(null);
96: em.getTransaction().commit();
97:
98: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
99: assertNotNull(res);
100: assertNull(res.getStringAttribute());
101: assertEquals(entityA.getTypes(), res.getTypes());
102: }
103:
104: @Test
105: public void testUpdateReference() {
106: logger.debug("Test: update reference to entity.");
107: this.em = getEntityManager("UpdateReference", true);
108: persist(entityD, entityI);
109: // em.persist(entityA, ctx);
110:
111: final OWLClassA newA = new OWLClassA();
112: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
113: newA.setStringAttribute("newA");
114: em.getTransaction().begin();
115: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
116: assertNotNull(d.getOwlClassA());
117: d.setOwlClassA(newA);
118: final OWLClassI i = em.find(OWLClassI.class, entityI.getUri());
119: assertNotNull(i.getOwlClassA());
120: i.setOwlClassA(newA);
121: em.persist(newA);
122: em.getTransaction().commit();
123:
124: final OWLClassA resA1 = em.find(OWLClassA.class, newA.getUri());
125: assertNotNull(resA1);
126: final OWLClassD resD = em.find(OWLClassD.class, d.getUri());
127: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
128: //                assertSame(resD.getOwlClassA(), resA1);
129: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
130: final OWLClassI resI = em.find(OWLClassI.class, i.getUri());
131: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
132: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
133: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
134: //                assertEquals(resA1, resI.getOwlClassA());
135: }
136:
137: @Test
138: public void testMergeSet() throws Exception {
139: logger.debug("Test: merge set property.");
140: this.em = getEntityManager("MergeSet", false);
141: persist(entityJ);
142: em.clear();
143:
144: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
145:
146: em.getTransaction().begin();
147: OWLClassJ merged = em.merge(entityJ);
148:
149: for (final OWLClassA a : merged.getOwlClassA()) {
150: assertEquals(a.getStringAttribute(), "NEWVALUE");
151: }
152: em.getTransaction().commit();
153: }
154:
155: @Test
156: public void testMergeDetachedWithChanges() {
157: logger.debug("Test: merge detached entity with changes.");
158: this.em = getEntityManager("UpdateDetached", true);
159: persist(entityA);
160:
161: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
162: assertTrue(em.contains(a));
163: em.detach(a);
164: assertFalse(em.contains(a));
165: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
166: a.getTypes().add(newType);
167: em.getTransaction().begin();
168: final OWLClassA merged = em.merge(a);
169: assertTrue(merged.getTypes().contains(newType));
170: em.getTransaction().commit();
171:
172: final OWLClassA res = em.find(OWLClassA.class, a.getUri());
173: assertEquals(a.getTypes().size(), res.getTypes().size());
174: assertTrue(res.getTypes().containsAll(a.getTypes()));
175: }
176:
177: @Test
178: public void testMergeDetachedCascade() {
179: logger.debug("Test: merge detached with cascade.");
180: this.em = getEntityManager("UpdateCascade", true);
181: em.getTransaction().begin();
182: em.persist(entityH);
183: assertTrue(em.contains(entityA));
184: em.getTransaction().commit();
185:
186: em.getTransaction().begin();
187: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
188: assertNotNull(h.getOwlClassA());
189: em.detach(h);
190: assertFalse(em.contains(h));
191: assertFalse(em.contains(h.getOwlClassA()));
192: final String newStr = "newStringAttribute";
193: h.getOwlClassA().setStringAttribute(newStr);
194: final OWLClassH merged = em.merge(h);
195: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
196: em.getTransaction().commit();
197:
198: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
199: assertNotNull(res);
200: assertEquals(newStr, res.getStringAttribute());
201: }
202:
203: @Test
204: public void testMergeDetachedWithObjectPropertyChange() {
205: logger.debug("Test: merge detached with object property change.");
206: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
207: persist(entityD, entityA);
208:
209: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
210: em.detach(d);
211: d.setOwlClassA(entityA2);
212: em.getTransaction().begin();
213: em.merge(d);
214: em.persist(entityA2);
215: em.getTransaction().commit();
216:
217: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
218: assertNotNull(res);
219: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
220: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
221: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
222: assertNotNull(resA);
223: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
224: assertEquals(entityA.getTypes(), resA.getTypes());
225: }
226:
227: @Test
228: public void testRemoveFromSimpleList() {
229: logger.debug("Test: remove entity from simple list. (But keep it in the ontology.)");
230: this.em = getEntityManager("UpdateRemoveFromSimpleList", true);
231: entityC.setSimpleList(Generators.createSimpleList());
232: persistEntityWithList();
233:
234: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
235: assertNotNull(c);
236: em.getTransaction().begin();
237: final OWLClassA a = c.getSimpleList().get(1);
238: c.getSimpleList().remove(a);
239: em.getTransaction().commit();
240:
241: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
242: assertNotNull(resA);
243: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
244: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
245: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
246: for (OWLClassA aa : resC.getSimpleList()) {
247: assertFalse(resA.getUri().equals(aa.getUri()));
248: }
249: }
250:
251: private void persistEntityWithList() {
252: em.getTransaction().begin();
253: em.persist(entityC);
254: if (entityC.getSimpleList() != null) {
255: entityC.getSimpleList().forEach(em::persist);
256: }
257: if (entityC.getReferencedList() != null) {
258: entityC.getReferencedList().forEach(em::persist);
259: }
260: em.getTransaction().commit();
261: }
262:
263: @Test
264: public void testAddToSimpleList() {
265: logger.debug("Test: add entity to simple list.");
266: this.em = getEntityManager("UpdateAddToSimpleList", true);
267: entityC.setSimpleList(Generators.createSimpleList());
268: em.getTransaction().begin();
269: em.persist(entityC);
270: entityC.getSimpleList().forEach(em::persist);
271: em.persist(entityA);
272: em.getTransaction().commit();
273:
274: em.getTransaction().begin();
275: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
276: assertNotNull(c);
277: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
278: assertNotNull(a);
279: assertFalse(c.getSimpleList().contains(a));
280: c.getSimpleList().add(a);
281: em.getTransaction().commit();
282:
283: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
284: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
285: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
286: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
287: assertNotNull(resA);
288: assertTrue(resC.getSimpleList().contains(resA));
289: }
290:
291: @Test
292: public void testClearSimpleList() {
293: logger.debug("Test: clear a simple list (but keep the entities in ontology).");
294: this.em = getEntityManager("UpdateClearSimpleList", true);
295: entityC.setSimpleList(Generators.createSimpleList());
296: persistEntityWithList();
297:
298: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
299: assertNotNull(c);
300: assertFalse(c.getSimpleList().isEmpty());
301: em.getTransaction().begin();
302: c.getSimpleList().clear();
303: em.getTransaction().commit();
304:
305: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
306: assertNotNull(resC);
307: assertTrue(resC.getSimpleList() == null || resC.getSimpleList().isEmpty());
308: for (OWLClassA a : entityC.getSimpleList()) {
309: assertNotNull(em.find(OWLClassA.class, a.getUri()));
310: }
311: }
312:
313: @Test
314: public void testReplaceSimpleList() {
315: logger.debug("Test: replace simple list with a new one.");
316: this.em = getEntityManager("UpdateReplaceSimpleList", true);
317: entityC.setSimpleList(Generators.createSimpleList());
318: persistEntityWithList();
319:
320: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
321: final List<OWLClassA> newList = new ArrayList<>(1);
322: newList.add(entityA);
323: em.getTransaction().begin();
324: em.persist(entityA);
325: c.setSimpleList(newList);
326: em.getTransaction().commit();
327:
328: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
329: assertNotNull(resC);
330: assertEquals(newList.size(), resC.getSimpleList().size());
331: boolean found;
332: for (OWLClassA a : newList) {
333: found = false;
334: for (OWLClassA aa : resC.getSimpleList()) {
335: if (a.getUri().equals(aa.getUri())) {
336: found = true;
337: break;
338: }
339: }
340: assertTrue(found);
341: }
342: for (OWLClassA a : entityC.getSimpleList()) {
343: assertNotNull(em.find(OWLClassA.class, a.getUri()));
344: }
345: }
346:
347: @Test
348: public void testRemoveFromReferencedList() {
349: logger.debug("Test: remove entity from referenced list. (But keep it in the ontology.");
350: this.em = getEntityManager("UpdateRemoveFromReferencedList", true);
351: entityC.setReferencedList(Generators.createReferencedList());
352: persistEntityWithList();
353:
354: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
355: assertNotNull(c);
356: em.getTransaction().begin();
357: final OWLClassA a = c.getReferencedList().get(3);
358: c.getReferencedList().remove(a);
359: em.getTransaction().commit();
360:
361: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
362: assertNotNull(resA);
363: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
364: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
365: assertEquals(entityC.getReferencedList().size() - 1, resC.getReferencedList().size());
366: for (OWLClassA aa : resC.getReferencedList()) {
367: assertFalse(resA.getUri().equals(aa.getUri()));
368: }
369: }
370:
371: @Test
372: public void testAddToReferencedList() {
373: logger.debug("Test: add entity to Referenced list.");
374: this.em = getEntityManager("UpdateAddToReferencedList", true);
375: entityC.setReferencedList(Generators.createReferencedList());
376: persistEntityWithList();
377:
378: em.getTransaction().begin();
379: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
380: assertNotNull(c);
381: em.persist(entityA);
382: c.getReferencedList().add(entityA);
383: em.getTransaction().commit();
384:
385: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
386: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
387: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
388: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
389: assertNotNull(resA);
390: assertTrue(resC.getReferencedList().contains(resA));
391: }
392:
393: @Test
394: public void testClearReferencedList() {
395: logger.debug("Test: clear referenced list (but keep the entities in ontology).");
396: this.em = getEntityManager("UpdateClearReferencedList", true);
397: entityC.setReferencedList(Generators.createReferencedList());
398: persistEntityWithList();
399:
400: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
401: assertNotNull(c);
402: assertFalse(c.getReferencedList().isEmpty());
403: em.getTransaction().begin();
404: c.setReferencedList(null);
405: em.getTransaction().commit();
406:
407: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
408: assertNotNull(resC);
409: assertNull(resC.getReferencedList());
410: for (OWLClassA a : entityC.getReferencedList()) {
411: assertNotNull(em.find(OWLClassA.class, a.getUri()));
412: }
413: }
414:
415: @Test
416: public void testReplaceReferencedList() {
417: logger.debug("Test: replace referenced list with a new one.");
418: this.em = getEntityManager("UpdateReplaceReferencedList", true);
419: entityC.setReferencedList(Generators.createReferencedList());
420: persistEntityWithList();
421:
422: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
423: final List<OWLClassA> newList = new ArrayList<>(1);
424: newList.add(entityA);
425: em.getTransaction().begin();
426: em.persist(entityA);
427: c.setReferencedList(newList);
428: em.getTransaction().commit();
429:
430: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
431: assertNotNull(resC);
432: assertEquals(newList.size(), resC.getReferencedList().size());
433: boolean found;
434: for (OWLClassA a : newList) {
435: found = false;
436: for (OWLClassA aa : resC.getReferencedList()) {
437: if (a.getUri().equals(aa.getUri())) {
438: found = true;
439: break;
440: }
441: }
442: assertTrue(found);
443: }
444: for (OWLClassA a : entityC.getReferencedList()) {
445: assertNotNull(em.find(OWLClassA.class, a.getUri()));
446: }
447: }
448:
449: @Test
450: public void testAddNewToProperties() {
451: logger.debug("Test: add a new property value to entity's properties.");
452: this.em = getEntityManager("UpdateAddNewToProperties", false);
453: entityB.setProperties(Generators.createProperties());
454: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
455: expected.putAll(entityB.getProperties());
456: persist(entityB);
457: em.clear();
458:
459: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
460: assertNotNull(b);
461: assertEquals(expected.size(), b.getProperties().size());
462: em.getTransaction().begin();
463: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
464: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
465: expected.putAll(b.getProperties());
466: em.getTransaction().commit();
467:
468: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
469: assertNotNull(res);
470: assertEquals(expected.size(), res.getProperties().size());
471: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
472: assertTrue(res.getProperties().containsKey(e.getKey()));
473: final Set<String> s = e.getValue();
474: final Set<String> resS = res.getProperties().get(e.getKey());
475: assertEquals(s.size(), resS.size());
476: assertTrue(s.containsAll(resS));
477: }
478: }
479:
480: @Test
481: public void testAddPropertyValue() {
482: logger.debug("Test: add another value to an existing property.");
483: this.em = getEntityManager("UpdateAddPropertyValue", false);
484: entityB.setProperties(Generators.createProperties());
485: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
486: final String prop = entityB.getProperties().keySet().iterator().next();
487: expected.putAll(entityB.getProperties());
488: persist(entityB);
489: em.clear();
490:
491: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
492: assertNotNull(b);
493: assertEquals(expected.size(), b.getProperties().size());
494: em.getTransaction().begin();
495: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
496: expected.putAll(b.getProperties());
497: em.getTransaction().commit();
498:
499: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
500: assertNotNull(res);
501: assertEquals(expected.size(), res.getProperties().size());
502: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
503: assertTrue(res.getProperties().containsKey(e.getKey()));
504: final Set<String> s = e.getValue();
505: final Set<String> resS = res.getProperties().get(e.getKey());
506: assertEquals(s.size(), resS.size());
507: if (e.getKey().equals(prop)) {
508: assertTrue(s.size() > 1);
509: for (String val : s) {
510: assertTrue(resS.contains(val));
511: }
512: } else {
513: assertTrue(resS.contains(s.iterator().next()));
514: }
515: }
516: }
517:
518: @Test
519: public void testAddPropertyValueDetached() {
520: logger.debug("Test: add another value to an existing property.");
521: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
522: entityB.setProperties(Generators.createProperties());
523: final String prop = entityB.getProperties().keySet().iterator().next();
524: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
525: persist(entityB);
526: em.clear();
527:
528: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
529: b.getProperties().get(prop).add(newPropertyValue);
530: em.detach(b);
531: em.getTransaction().begin();
532: em.merge(b);
533: em.getTransaction().commit();
534:
535: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
536: assertNotNull(res);
537: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
538: }
539:
540: @Test
541: public void testAddNewTypedPropertyWithValues() {
542: logger.debug("Test: add a new property with values. Typed.");
543: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
544: entityP.setProperties(Generators.createTypedProperties());
545: persist(entityP);
546: em.clear();
547:
548: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
549: em.getTransaction().begin();
550: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
551: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", new Date())));
552: em.getTransaction().commit();
553:
554: em.clear();
555:
556: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
557: assertNotNull(res);
558: assertTrue(res.getProperties().containsKey(property));
559: assertEquals(p.getProperties(), res.getProperties());
560: }
561:
562: @Test
563: public void testAddTypedPropertyValue() {
564: logger.debug("Test: add an existing property value. Typed.");
565: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
566: entityP.setProperties(Generators.createTypedProperties());
567: persist(entityP);
568: em.clear();
569:
570: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
571: em.getTransaction().begin();
572: final URI property = p.getProperties().keySet().iterator().next();
573: final Object value = generateValueForProperty(p, property);
574: p.getProperties().get(property).add(value);
575: em.getTransaction().commit();
576:
577: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
578: assertTrue(res.getProperties().get(property).contains(value));
579: assertEquals(p.getProperties(), res.getProperties());
580: }
581:
582: private Object generateValueForProperty(OWLClassP instance, URI property) {
583: final Set<Object> values = instance.getProperties().get(property);
584: assert values != null && !values.isEmpty();
585: if (values.iterator().next() instanceof URI) {
586: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
587: } else {
588: return "StringValue";
589: }
590: }
591:
592: @Test(expected = OWLInferredAttributeModifiedException.class)
593: public void testModifyInferredAttribute() {
594: logger.debug("Test: modify an inferred attribute.");
595: this.em = getEntityManager("ModifyInferredAttribute", false);
596: persist(entityF);
597:
598: em.getTransaction().begin();
599: final OWLClassF f = em.find(OWLClassF.class, entityF.getUri());
600: assertNotNull(f);
601: f.setSecondStringAttribute("otherValue");
602: em.getTransaction().commit();
603: }
604:
605: @Test
606: public void testModifyAttributesOfBasicTypes() {
607: logger.debug("Test: modify attributes of basic Java types (Integer, Boolean etc.).");
608: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
609: persist(entityM);
610:
611: em.getTransaction().begin();
612: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
613: m.setBooleanAttribute(!entityM.getBooleanAttribute());
614: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
615: m.setLongAttribute(m.getLongAttribute() + 100L);
616: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
617: em.getTransaction().commit();
618:
619: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
620: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
621: assertEquals(m.getLongAttribute(), res.getLongAttribute());
622: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
623: assertEquals(m.getDateAttribute(), res.getDateAttribute());
624: }
625:
626: @Test
627: public void testModifyEnumAttribute() {
628: logger.debug("Test: modify enum attribute.");
629: this.em = getEntityManager("ModifyEnumAttribute", false);
630: assertNotNull(entityM.getEnumAttribute());
631: persist(entityM);
632:
633: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
634: em.getTransaction().begin();
635: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
636: m.setEnumAttribute(updated);
637: em.getTransaction().commit();
638:
639: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
640: assertNotNull(res);
641: assertEquals(updated, res.getEnumAttribute());
642: }
643:
644: @Test
645: public void testCascadeOfNewlyPersistedOnMerge() {
646: logger.debug("Test: cascade merging of an object that has been just persisted.");
647: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
648: em.getTransaction().begin();
649: em.persist(entityO);
650: entityO.getOwlClassESet().forEach(em::persist);
651: em.getTransaction().commit();
652:
653: final OWLClassE newE = new OWLClassE();
654: newE.setStringAttribute("NewlyAddedE");
655: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
656: em.detach(toUpdate);
657:
658: em.getTransaction().begin();
659: toUpdate.getOwlClassESet().add(newE);
660: em.persist(newE);
661: final OWLClassO merged = em.merge(toUpdate);
662: assertTrue(merged.getOwlClassESet().contains(newE));
663: em.getTransaction().commit();
664:
665: final OWLClassE resE = em.find(OWLClassE.class, newE.getUri());
666: assertNotNull(resE);
667: final OWLClassO resO = em.find(OWLClassO.class, entityO.getUri());
668: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
669: }
670:
671: @Test
672: public void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
673: logger.debug(
674: "Test: modify collection after cascade merge and check that the changes have been propagated on commit.");
675: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
676: em.getTransaction().begin();
677: em.persist(entityJ);
678: em.getTransaction().commit();
679:
680: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
681: em.getTransaction().begin();
682: final OWLClassJ merged = em.merge(entityJ);
683: merged.getOwlClassA().add(newA);
684: em.persist(newA);
685: em.getTransaction().commit();
686:
687: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
688: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
689: boolean found = false;
690:• for (OWLClassA a : result.getOwlClassA()) {
691:• if (a.getUri().equals(newA.getUri())) {
692: found = true;
693: break;
694: }
695: }
696: assertTrue(found);
697: }
698:
699: @Test
700: public void testAddPropertiesWhenTheyWereNullOriginally() throws Exception {
701: logger.debug("Test: add properties on update when the field was originally null.");
702: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
703: em.getTransaction().begin();
704: assertNull(entityB.getProperties());
705: em.persist(entityB);
706: em.getTransaction().commit();
707:
708: final OWLClassB update = em.find(OWLClassB.class, entityB.getUri());
709: em.detach(update);
710: final Map<String, Set<String>> properties = Generators.createProperties(2);
711: update.setProperties(properties);
712: em.getTransaction().begin();
713: em.merge(update);
714: em.getTransaction().commit();
715:
716: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
717: assertNotNull(result.getProperties());
718: assertEquals(properties, result.getProperties());
719: }
720: }