Skip to contentMethod: testAddPropertyValue()
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:
20: import java.lang.reflect.Field;
21: import java.net.URI;
22: import java.util.*;
23: import java.util.logging.Logger;
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.config(
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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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.config("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(expected = OWLInferredAttributeModifiedException.class)
541: public void testModifyInferredAttribute() {
542: logger.config("Test: modify an inferred attribute.");
543: this.em = getEntityManager("ModifyInferredAttribute", false);
544: persist(entityF);
545:
546: em.getTransaction().begin();
547: final OWLClassF f = em.find(OWLClassF.class, entityF.getUri());
548: assertNotNull(f);
549: f.setSecondStringAttribute("otherValue");
550: em.getTransaction().commit();
551: }
552:
553: @Test
554: public void testModifyAttributesOfBasicTypes() {
555: logger.config("Test: modify attributes of basic Java types (Integer, Boolean etc.).");
556: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
557: persist(entityM);
558:
559: em.getTransaction().begin();
560: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
561: m.setBooleanAttribute(!entityM.getBooleanAttribute());
562: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
563: m.setLongAttribute(m.getLongAttribute() + 100L);
564: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
565: em.getTransaction().commit();
566:
567: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
568: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
569: assertEquals(m.getLongAttribute(), res.getLongAttribute());
570: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
571: assertEquals(m.getDateAttribute(), res.getDateAttribute());
572: }
573:
574: @Test
575: public void testModifyEnumAttribute() {
576: logger.config("Test: modify enum attribute.");
577: this.em = getEntityManager("ModifyEnumAttribute", false);
578: assertNotNull(entityM.getEnumAttribute());
579: persist(entityM);
580:
581: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
582: em.getTransaction().begin();
583: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
584: m.setEnumAttribute(updated);
585: em.getTransaction().commit();
586:
587: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
588: assertNotNull(res);
589: assertEquals(updated, res.getEnumAttribute());
590: }
591:
592: @Test
593: public void testCascadeOfNewlyPersistedOnMerge() {
594: logger.config("Test: cascade merging of an object that has been just persisted.");
595: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
596: em.getTransaction().begin();
597: em.persist(entityO);
598: entityO.getOwlClassESet().forEach(em::persist);
599: em.getTransaction().commit();
600:
601: final OWLClassE newE = new OWLClassE();
602: newE.setStringAttribute("NewlyAddedE");
603: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
604: em.detach(toUpdate);
605:
606: em.getTransaction().begin();
607: toUpdate.getOwlClassESet().add(newE);
608: em.persist(newE);
609: final OWLClassO merged = em.merge(toUpdate);
610: assertTrue(merged.getOwlClassESet().contains(newE));
611: em.getTransaction().commit();
612:
613: final OWLClassE resE = em.find(OWLClassE.class, newE.getUri());
614: assertNotNull(resE);
615: final OWLClassO resO = em.find(OWLClassO.class, entityO.getUri());
616: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
617: }
618:
619: @Test
620: public void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
621: logger.config(
622: "Test: modify collection after cascade merge and check that the changes have been propagated on commit.");
623: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
624: em.getTransaction().begin();
625: em.persist(entityJ);
626: em.getTransaction().commit();
627:
628: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
629: em.getTransaction().begin();
630: final OWLClassJ merged = em.merge(entityJ);
631: merged.getOwlClassA().add(newA);
632: em.persist(newA);
633: em.getTransaction().commit();
634:
635: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
636: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
637: boolean found = false;
638: for (OWLClassA a : result.getOwlClassA()) {
639: if (a.getUri().equals(newA.getUri())) {
640: found = true;
641: break;
642: }
643: }
644: assertTrue(found);
645: }
646:
647: @Test
648: public void testAddPropertiesWhenTheyWereNullOriginally() throws Exception {
649: logger.config("Test: add properties on update when the field was originally null.");
650: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
651: em.getTransaction().begin();
652: assertNull(entityB.getProperties());
653: em.persist(entityB);
654: em.getTransaction().commit();
655:
656: final OWLClassB update = em.find(OWLClassB.class, entityB.getUri());
657: em.detach(update);
658: final Map<String, Set<String>> properties = Generators.createProperties(2);
659: update.setProperties(properties);
660: em.getTransaction().begin();
661: em.merge(update);
662: em.getTransaction().commit();
663:
664: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
665: assertNotNull(result.getProperties());
666: assertEquals(properties, result.getProperties());
667: }
668: }