Skip to contentMethod: testMergeDetachedWithObjectPropertyChange()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.runner;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
18: import cz.cvut.kbss.jopa.test.*;
19: import cz.cvut.kbss.jopa.test.environment.Generators;
20: import org.junit.Test;
21: import org.slf4j.Logger;
22:
23: import java.lang.reflect.Field;
24: import java.net.MalformedURLException;
25: import java.net.URI;
26: import java.net.URL;
27: import java.util.*;
28:
29: import static org.junit.Assert.*;
30:
31: public abstract class UpdateOperationsRunner extends BaseRunner {
32:
33: private OWLClassA entityA2;
34: private OWLClassF entityF;
35: private OWLClassJ entityJ;
36: private OWLClassO entityO;
37:
38: public UpdateOperationsRunner(Logger logger) {
39: super(logger);
40: initialize();
41: }
42:
43: private void initialize() {
44: this.entityF = new OWLClassF();
45: entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
46: this.entityA2 = new OWLClassA();
47: entityA2.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
48: this.entityJ = new OWLClassJ();
49: entityJ.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityJ"));
50: final Set<OWLClassA> set = new HashSet<>(2);
51: set.add(entityA);
52: set.add(entityA2);
53: entityJ.setOwlClassA(set);
54: this.entityO = new OWLClassO(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityO"));
55: final Set<OWLClassE> es = new HashSet<>();
56: for (int i = 0; i < 5; i++) {
57: final OWLClassE e = new OWLClassE();
58: e.setStringAttribute("StringAttributeNo" + i);
59: es.add(e);
60: }
61: entityO.setOwlClassESet(es);
62: }
63:
64: @Test
65: public void testUpdateDataLeaveLazy() throws Exception {
66: logger.debug(
67: "Test: update data property. Leaves lazily loaded field empty and checks that after commit the field's value hasn't changed.");
68: this.em = getEntityManager("UpdateDataProperty", false);
69: entityB.setProperties(Generators.createProperties());
70: persist(entityB);
71:
72: em.getTransaction().begin();
73: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
74: assertNotNull(b);
75: final Field propsField = OWLClassB.class.getDeclaredField("properties");
76: propsField.setAccessible(true);
77: assertNull(propsField.get(b));
78: final String newString = "NewString";
79: b.setStringAttribute(newString);
80: em.getTransaction().commit();
81:
82: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
83: assertNotNull(res);
84: assertEquals(newString, res.getStringAttribute());
85: assertNotNull(res.getProperties());
86: assertEquals(entityB.getProperties(), res.getProperties());
87: }
88:
89: @Test
90: public void testUpdateDataPropertySetNull() {
91: logger.debug("Test: update data property. Set it to null.");
92: this.em = getEntityManager("UpdateDataPropertyToNull", true);
93: persist(entityA);
94:
95: em.getTransaction().begin();
96: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
97: assertNotNull(a);
98: assertNotNull(a.getStringAttribute());
99: a.setStringAttribute(null);
100: em.getTransaction().commit();
101:
102: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
103: assertNotNull(res);
104: assertNull(res.getStringAttribute());
105: assertEquals(entityA.getTypes(), res.getTypes());
106: }
107:
108: @Test
109: public void testUpdateReference() {
110: logger.debug("Test: update reference to entity.");
111: this.em = getEntityManager("UpdateReference", true);
112: persist(entityD, entityI);
113: // em.persist(entityA, ctx);
114:
115: final OWLClassA newA = new OWLClassA();
116: newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
117: newA.setStringAttribute("newA");
118: em.getTransaction().begin();
119: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
120: assertNotNull(d.getOwlClassA());
121: d.setOwlClassA(newA);
122: final OWLClassI i = em.find(OWLClassI.class, entityI.getUri());
123: assertNotNull(i.getOwlClassA());
124: i.setOwlClassA(newA);
125: em.persist(newA);
126: em.getTransaction().commit();
127:
128: final OWLClassA resA1 = em.find(OWLClassA.class, newA.getUri());
129: assertNotNull(resA1);
130: final OWLClassD resD = em.find(OWLClassD.class, d.getUri());
131: assertEquals(resA1.getUri(), resD.getOwlClassA().getUri());
132: //                assertSame(resD.getOwlClassA(), resA1);
133: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
134: final OWLClassI resI = em.find(OWLClassI.class, i.getUri());
135: assertEquals(newA.getUri(), resI.getOwlClassA().getUri());
136: assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
137: assertEquals(resA1.getUri(), resI.getOwlClassA().getUri());
138: //                assertEquals(resA1, resI.getOwlClassA());
139: }
140:
141: @Test
142: public void testMergeSet() throws Exception {
143: logger.debug("Test: merge set property.");
144: this.em = getEntityManager("MergeSet", false);
145: persist(entityJ);
146: em.clear();
147:
148: entityJ.getOwlClassA().forEach(a -> a.setStringAttribute("NEWVALUE"));
149:
150: em.getTransaction().begin();
151: OWLClassJ merged = em.merge(entityJ);
152:
153: for (final OWLClassA a : merged.getOwlClassA()) {
154: assertEquals(a.getStringAttribute(), "NEWVALUE");
155: }
156: em.getTransaction().commit();
157: }
158:
159: @Test
160: public void testMergeDetachedWithChanges() {
161: logger.debug("Test: merge detached entity with changes.");
162: this.em = getEntityManager("UpdateDetached", true);
163: persist(entityA);
164:
165: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
166: assertTrue(em.contains(a));
167: em.detach(a);
168: assertFalse(em.contains(a));
169: final String newType = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#AddedType";
170: a.getTypes().add(newType);
171: em.getTransaction().begin();
172: final OWLClassA merged = em.merge(a);
173: assertTrue(merged.getTypes().contains(newType));
174: em.getTransaction().commit();
175:
176: final OWLClassA res = em.find(OWLClassA.class, a.getUri());
177: assertEquals(a.getTypes().size(), res.getTypes().size());
178: assertTrue(res.getTypes().containsAll(a.getTypes()));
179: }
180:
181: @Test
182: public void testMergeDetachedCascade() {
183: logger.debug("Test: merge detached with cascade.");
184: this.em = getEntityManager("UpdateCascade", true);
185: em.getTransaction().begin();
186: em.persist(entityH);
187: assertTrue(em.contains(entityA));
188: em.getTransaction().commit();
189:
190: em.getTransaction().begin();
191: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
192: assertNotNull(h.getOwlClassA());
193: em.detach(h);
194: assertFalse(em.contains(h));
195: assertFalse(em.contains(h.getOwlClassA()));
196: final String newStr = "newStringAttribute";
197: h.getOwlClassA().setStringAttribute(newStr);
198: final OWLClassH merged = em.merge(h);
199: assertEquals(newStr, merged.getOwlClassA().getStringAttribute());
200: em.getTransaction().commit();
201:
202: final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
203: assertNotNull(res);
204: assertEquals(newStr, res.getStringAttribute());
205: }
206:
207: @Test
208: public void testMergeDetachedWithObjectPropertyChange() {
209: logger.debug("Test: merge detached with object property change.");
210: this.em = getEntityManager("UpdateDetachedWithOPChange", true);
211: persist(entityD, entityA);
212:
213: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
214: em.detach(d);
215: d.setOwlClassA(entityA2);
216: em.getTransaction().begin();
217: em.merge(d);
218: em.persist(entityA2);
219: em.getTransaction().commit();
220:
221: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
222: assertNotNull(res);
223: assertEquals(entityA2.getUri(), res.getOwlClassA().getUri());
224: assertEquals(entityA2.getStringAttribute(), res.getOwlClassA().getStringAttribute());
225: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
226: assertNotNull(resA);
227: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
228: assertEquals(entityA.getTypes(), resA.getTypes());
229: }
230:
231: @Test
232: public void testRemoveFromSimpleList() {
233: logger.debug("Test: remove entity from simple list. (But keep it in the ontology.)");
234: this.em = getEntityManager("UpdateRemoveFromSimpleList", true);
235: entityC.setSimpleList(Generators.createSimpleList());
236: persistEntityWithList();
237:
238: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
239: assertNotNull(c);
240: em.getTransaction().begin();
241: final OWLClassA a = c.getSimpleList().get(1);
242: c.getSimpleList().remove(a);
243: em.getTransaction().commit();
244:
245: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
246: assertNotNull(resA);
247: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
248: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
249: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
250: for (OWLClassA aa : resC.getSimpleList()) {
251: assertFalse(resA.getUri().equals(aa.getUri()));
252: }
253: }
254:
255: private void persistEntityWithList() {
256: em.getTransaction().begin();
257: em.persist(entityC);
258: if (entityC.getSimpleList() != null) {
259: entityC.getSimpleList().forEach(em::persist);
260: }
261: if (entityC.getReferencedList() != null) {
262: entityC.getReferencedList().forEach(em::persist);
263: }
264: em.getTransaction().commit();
265: }
266:
267: @Test
268: public void testAddToSimpleList() {
269: logger.debug("Test: add entity to simple list.");
270: this.em = getEntityManager("UpdateAddToSimpleList", true);
271: entityC.setSimpleList(Generators.createSimpleList());
272: em.getTransaction().begin();
273: em.persist(entityC);
274: entityC.getSimpleList().forEach(em::persist);
275: em.persist(entityA);
276: em.getTransaction().commit();
277:
278: em.getTransaction().begin();
279: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
280: assertNotNull(c);
281: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
282: assertNotNull(a);
283: assertFalse(c.getSimpleList().contains(a));
284: c.getSimpleList().add(a);
285: em.getTransaction().commit();
286:
287: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
288: assertEquals(c.getSimpleList().size(), resC.getSimpleList().size());
289: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
290: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
291: assertNotNull(resA);
292: assertTrue(resC.getSimpleList().contains(resA));
293: }
294:
295: @Test
296: public void testClearSimpleList() {
297: logger.debug("Test: clear a simple list (but keep the entities in ontology).");
298: this.em = getEntityManager("UpdateClearSimpleList", true);
299: entityC.setSimpleList(Generators.createSimpleList());
300: persistEntityWithList();
301:
302: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
303: assertNotNull(c);
304: assertFalse(c.getSimpleList().isEmpty());
305: em.getTransaction().begin();
306: c.getSimpleList().clear();
307: em.getTransaction().commit();
308:
309: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
310: assertNotNull(resC);
311: assertTrue(resC.getSimpleList() == null || resC.getSimpleList().isEmpty());
312: for (OWLClassA a : entityC.getSimpleList()) {
313: assertNotNull(em.find(OWLClassA.class, a.getUri()));
314: }
315: }
316:
317: @Test
318: public void testReplaceSimpleList() {
319: logger.debug("Test: replace simple list with a new one.");
320: this.em = getEntityManager("UpdateReplaceSimpleList", true);
321: entityC.setSimpleList(Generators.createSimpleList());
322: persistEntityWithList();
323:
324: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
325: final List<OWLClassA> newList = new ArrayList<>(1);
326: newList.add(entityA);
327: em.getTransaction().begin();
328: em.persist(entityA);
329: c.setSimpleList(newList);
330: em.getTransaction().commit();
331:
332: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
333: assertNotNull(resC);
334: assertEquals(newList.size(), resC.getSimpleList().size());
335: boolean found;
336: for (OWLClassA a : newList) {
337: found = false;
338: for (OWLClassA aa : resC.getSimpleList()) {
339: if (a.getUri().equals(aa.getUri())) {
340: found = true;
341: break;
342: }
343: }
344: assertTrue(found);
345: }
346: for (OWLClassA a : entityC.getSimpleList()) {
347: assertNotNull(em.find(OWLClassA.class, a.getUri()));
348: }
349: }
350:
351: @Test
352: public void testRemoveFromReferencedList() {
353: logger.debug("Test: remove entity from referenced list. (But keep it in the ontology.");
354: this.em = getEntityManager("UpdateRemoveFromReferencedList", true);
355: entityC.setReferencedList(Generators.createReferencedList());
356: persistEntityWithList();
357:
358: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
359: assertNotNull(c);
360: em.getTransaction().begin();
361: final OWLClassA a = c.getReferencedList().get(Generators.randomInt(c.getReferencedList().size()));
362: c.getReferencedList().remove(a);
363: em.getTransaction().commit();
364:
365: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
366: assertNotNull(resA);
367: final OWLClassC resC = em.find(OWLClassC.class, c.getUri());
368: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
369: assertEquals(entityC.getReferencedList().size() - 1, resC.getReferencedList().size());
370: for (OWLClassA aa : resC.getReferencedList()) {
371: assertFalse(resA.getUri().equals(aa.getUri()));
372: }
373: }
374:
375: @Test
376: public void testAddToReferencedList() {
377: logger.debug("Test: add entity to Referenced list.");
378: this.em = getEntityManager("UpdateAddToReferencedList", true);
379: entityC.setReferencedList(Generators.createReferencedList());
380: persistEntityWithList();
381:
382: em.getTransaction().begin();
383: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
384: assertNotNull(c);
385: em.persist(entityA);
386: c.getReferencedList().add(entityA);
387: em.getTransaction().commit();
388:
389: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
390: assertEquals(c.getReferencedList().size(), resC.getReferencedList().size());
391: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
392: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri());
393: assertNotNull(resA);
394: assertTrue(resC.getReferencedList().contains(resA));
395: }
396:
397: @Test
398: public void testClearReferencedList() {
399: logger.debug("Test: clear referenced list (but keep the entities in ontology).");
400: this.em = getEntityManager("UpdateClearReferencedList", true);
401: entityC.setReferencedList(Generators.createReferencedList());
402: persistEntityWithList();
403:
404: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
405: assertNotNull(c);
406: assertFalse(c.getReferencedList().isEmpty());
407: em.getTransaction().begin();
408: c.setReferencedList(null);
409: em.getTransaction().commit();
410:
411: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
412: assertNotNull(resC);
413: assertNull(resC.getReferencedList());
414: for (OWLClassA a : entityC.getReferencedList()) {
415: assertNotNull(em.find(OWLClassA.class, a.getUri()));
416: }
417: }
418:
419: @Test
420: public void testReplaceReferencedList() {
421: logger.debug("Test: replace referenced list with a new one.");
422: this.em = getEntityManager("UpdateReplaceReferencedList", true);
423: entityC.setReferencedList(Generators.createReferencedList());
424: persistEntityWithList();
425:
426: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
427: final List<OWLClassA> newList = new ArrayList<>(1);
428: newList.add(entityA);
429: em.getTransaction().begin();
430: em.persist(entityA);
431: c.setReferencedList(newList);
432: em.getTransaction().commit();
433:
434: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
435: assertNotNull(resC);
436: assertEquals(newList.size(), resC.getReferencedList().size());
437: boolean found;
438: for (OWLClassA a : newList) {
439: found = false;
440: for (OWLClassA aa : resC.getReferencedList()) {
441: if (a.getUri().equals(aa.getUri())) {
442: found = true;
443: break;
444: }
445: }
446: assertTrue(found);
447: }
448: for (OWLClassA a : entityC.getReferencedList()) {
449: assertNotNull(em.find(OWLClassA.class, a.getUri()));
450: }
451: }
452:
453: @Test
454: public void testAddNewToProperties() {
455: logger.debug("Test: add a new property value to entity's properties.");
456: this.em = getEntityManager("UpdateAddNewToProperties", false);
457: entityB.setProperties(Generators.createProperties());
458: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
459: expected.putAll(entityB.getProperties());
460: persist(entityB);
461: em.clear();
462:
463: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
464: assertNotNull(b);
465: assertEquals(expected.size(), b.getProperties().size());
466: em.getTransaction().begin();
467: b.getProperties().put("http://krizik.felk.cvut.cz/ontologies/jopa/attributes#propertyFour",
468: Collections.singleton("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup"));
469: expected.putAll(b.getProperties());
470: em.getTransaction().commit();
471:
472: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
473: assertNotNull(res);
474: assertEquals(expected.size(), res.getProperties().size());
475: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
476: assertTrue(res.getProperties().containsKey(e.getKey()));
477: final Set<String> s = e.getValue();
478: final Set<String> resS = res.getProperties().get(e.getKey());
479: assertEquals(s.size(), resS.size());
480: assertTrue(s.containsAll(resS));
481: }
482: }
483:
484: @Test
485: public void testAddPropertyValue() {
486: logger.debug("Test: add another value to an existing property.");
487: this.em = getEntityManager("UpdateAddPropertyValue", false);
488: entityB.setProperties(Generators.createProperties());
489: final Map<String, Set<String>> expected = new HashMap<>(entityB.getProperties().size() + 3);
490: final String prop = entityB.getProperties().keySet().iterator().next();
491: expected.putAll(entityB.getProperties());
492: persist(entityB);
493: em.clear();
494:
495: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
496: assertNotNull(b);
497: assertEquals(expected.size(), b.getProperties().size());
498: em.getTransaction().begin();
499: b.getProperties().get(prop).add("http://krizik.felk.cvut.cz/ontologies/jopa/Stroustrup");
500: expected.putAll(b.getProperties());
501: em.getTransaction().commit();
502:
503: final OWLClassB res = em.find(OWLClassB.class, b.getUri());
504: assertNotNull(res);
505: assertEquals(expected.size(), res.getProperties().size());
506: for (Map.Entry<String, Set<String>> e : expected.entrySet()) {
507: assertTrue(res.getProperties().containsKey(e.getKey()));
508: final Set<String> s = e.getValue();
509: final Set<String> resS = res.getProperties().get(e.getKey());
510: assertEquals(s.size(), resS.size());
511: if (e.getKey().equals(prop)) {
512: assertTrue(s.size() > 1);
513: for (String val : s) {
514: assertTrue(resS.contains(val));
515: }
516: } else {
517: assertTrue(resS.contains(s.iterator().next()));
518: }
519: }
520: }
521:
522: @Test
523: public void testAddPropertyValueDetached() {
524: logger.debug("Test: add another value to an existing property.");
525: this.em = getEntityManager("UpdateAddPropertyValueDetached", false);
526: entityB.setProperties(Generators.createProperties());
527: final String prop = entityB.getProperties().keySet().iterator().next();
528: final String newPropertyValue = "http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue";
529: persist(entityB);
530: em.clear();
531:
532: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri());
533: b.getProperties().get(prop).add(newPropertyValue);
534: em.detach(b);
535: em.getTransaction().begin();
536: em.merge(b);
537: em.getTransaction().commit();
538:
539: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
540: assertNotNull(res);
541: assertTrue(res.getProperties().get(prop).contains(newPropertyValue));
542: }
543:
544: @Test
545: public void testAddNewTypedPropertyWithValues() {
546: logger.debug("Test: add a new property with values. Typed.");
547: this.em = getEntityManager("UpdateAddNewToPropertiesTyped", false);
548: entityP.setProperties(Generators.createTypedProperties());
549: persist(entityP);
550: em.clear();
551:
552: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
553: em.getTransaction().begin();
554: final URI property = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newProperty");
555: p.getProperties().put(property, new HashSet<>(Arrays.asList(1, "Two", new Date())));
556: em.getTransaction().commit();
557:
558: em.clear();
559:
560: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
561: assertNotNull(res);
562: assertTrue(res.getProperties().containsKey(property));
563: assertEquals(p.getProperties(), res.getProperties());
564: }
565:
566: @Test
567: public void testAddTypedPropertyValue() {
568: logger.debug("Test: add an existing property value. Typed.");
569: this.em = getEntityManager("UpdateAddPropertyValueTyped", false);
570: entityP.setProperties(Generators.createTypedProperties());
571: persist(entityP);
572: em.clear();
573:
574: final OWLClassP p = em.find(OWLClassP.class, entityP.getUri());
575: em.getTransaction().begin();
576: final URI property = p.getProperties().keySet().iterator().next();
577: final Object value = generateValueForProperty(p, property);
578: p.getProperties().get(property).add(value);
579: em.getTransaction().commit();
580:
581: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
582: assertTrue(res.getProperties().get(property).contains(value));
583: assertEquals(p.getProperties(), res.getProperties());
584: }
585:
586: private Object generateValueForProperty(OWLClassP instance, URI property) {
587: final Set<Object> values = instance.getProperties().get(property);
588: assert values != null && !values.isEmpty();
589: if (values.iterator().next() instanceof URI) {
590: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newPropertyValue");
591: } else {
592: return "StringValue";
593: }
594: }
595:
596: @Test(expected = OWLInferredAttributeModifiedException.class)
597: public void testModifyInferredAttribute() {
598: logger.debug("Test: modify an inferred attribute.");
599: this.em = getEntityManager("ModifyInferredAttribute", false);
600: persist(entityF);
601:
602: em.getTransaction().begin();
603: final OWLClassF f = em.find(OWLClassF.class, entityF.getUri());
604: assertNotNull(f);
605: f.setSecondStringAttribute("otherValue");
606: em.getTransaction().commit();
607: }
608:
609: @Test
610: public void testModifyAttributesOfBasicTypes() {
611: logger.debug("Test: modify attributes of basic Java types (Integer, Boolean etc.).");
612: this.em = getEntityManager("ModifyBasicTypeAttributes", false);
613: persist(entityM);
614:
615: em.getTransaction().begin();
616: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
617: m.setBooleanAttribute(!entityM.getBooleanAttribute());
618: m.setDoubleAttribute(m.getDoubleAttribute() - 100.0);
619: m.setLongAttribute(m.getLongAttribute() + 100L);
620: m.setDateAttribute(new Date(System.currentTimeMillis() + 10000));
621: em.getTransaction().commit();
622:
623: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
624: assertEquals(m.getBooleanAttribute(), res.getBooleanAttribute());
625: assertEquals(m.getLongAttribute(), res.getLongAttribute());
626: assertEquals(m.getDoubleAttribute(), res.getDoubleAttribute());
627: assertEquals(m.getDateAttribute(), res.getDateAttribute());
628: }
629:
630: @Test
631: public void testModifyEnumAttribute() {
632: logger.debug("Test: modify enum attribute.");
633: this.em = getEntityManager("ModifyEnumAttribute", false);
634: assertNotNull(entityM.getEnumAttribute());
635: persist(entityM);
636:
637: final OWLClassM.Severity updated = OWLClassM.Severity.LOW;
638: em.getTransaction().begin();
639: final OWLClassM m = em.find(OWLClassM.class, entityM.getKey());
640: m.setEnumAttribute(updated);
641: em.getTransaction().commit();
642:
643: final OWLClassM res = em.find(OWLClassM.class, entityM.getKey());
644: assertNotNull(res);
645: assertEquals(updated, res.getEnumAttribute());
646: }
647:
648: @Test
649: public void testCascadeOfNewlyPersistedOnMerge() {
650: logger.debug("Test: cascade merging of an object that has been just persisted.");
651: this.em = getEntityManager("CascadeMergeWithNewlyPersisted", true);
652: em.getTransaction().begin();
653: em.persist(entityO);
654: entityO.getOwlClassESet().forEach(em::persist);
655: em.getTransaction().commit();
656:
657: final OWLClassE newE = new OWLClassE();
658: newE.setStringAttribute("NewlyAddedE");
659: final OWLClassO toUpdate = em.find(OWLClassO.class, entityO.getUri());
660: em.detach(toUpdate);
661:
662: em.getTransaction().begin();
663: toUpdate.getOwlClassESet().add(newE);
664: em.persist(newE);
665: final OWLClassO merged = em.merge(toUpdate);
666: assertTrue(merged.getOwlClassESet().contains(newE));
667: em.getTransaction().commit();
668:
669: final OWLClassE resE = em.find(OWLClassE.class, newE.getUri());
670: assertNotNull(resE);
671: final OWLClassO resO = em.find(OWLClassO.class, entityO.getUri());
672: assertEquals(entityO.getOwlClassESet().size() + 1, resO.getOwlClassESet().size());
673: }
674:
675: @Test
676: public void modificationsOfCollectionAfterCascadeMergeAreWrittenOnCommit() {
677: logger.debug(
678: "Test: modify collection after cascade merge and check that the changes have been propagated on commit.");
679: this.em = getEntityManager("ModifyCollectionAfterCascadeMerge", true);
680: em.getTransaction().begin();
681: em.persist(entityJ);
682: em.getTransaction().commit();
683:
684: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/newEntityA"));
685: em.getTransaction().begin();
686: final OWLClassJ merged = em.merge(entityJ);
687: merged.getOwlClassA().add(newA);
688: em.persist(newA);
689: em.getTransaction().commit();
690:
691: final OWLClassJ result = em.find(OWLClassJ.class, entityJ.getUri());
692: assertEquals(merged.getOwlClassA().size(), result.getOwlClassA().size());
693: boolean found = false;
694: for (OWLClassA a : result.getOwlClassA()) {
695: if (a.getUri().equals(newA.getUri())) {
696: found = true;
697: break;
698: }
699: }
700: assertTrue(found);
701: }
702:
703: @Test
704: public void testAddPropertiesWhenTheyWereNullOriginally() {
705: logger.debug("Test: add properties on update when the field was originally null.");
706: this.em = getEntityManager("AddPropertiesToNullOriginals", true);
707: em.getTransaction().begin();
708: assertNull(entityB.getProperties());
709: em.persist(entityB);
710: em.getTransaction().commit();
711:
712: final OWLClassB update = em.find(OWLClassB.class, entityB.getUri());
713: em.detach(update);
714: final Map<String, Set<String>> properties = Generators.createProperties(2);
715: update.setProperties(properties);
716: em.getTransaction().begin();
717: em.merge(update);
718: em.getTransaction().commit();
719:
720: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
721: assertNotNull(result.getProperties());
722: assertEquals(properties, result.getProperties());
723: }
724:
725: @Test
726: public void testUpdatePlainLiteralObjectPropertyValueToAnotherIndividual() {
727: logger.debug("Test: update plain literal object property value to a different individual.");
728: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValue", true);
729: em.getTransaction().begin();
730: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#oldIndividual"));
731: em.persist(entityP);
732: em.getTransaction().commit();
733:
734: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
735: final URI newValue = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newIndividual");
736: em.getTransaction().begin();
737: update.setIndividualUri(newValue);
738: em.getTransaction().commit();
739:
740: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
741: assertEquals(newValue, result.getIndividualUri());
742: }
743:
744: @Test
745: public void testUpdatePlainLiteralObjectPropertyValueToNull() {
746: logger.debug("Test: update plain literal object property value to null.");
747: this.em = getEntityManager("UpdatePlainLiteralObjectPropertyValueToNull", true);
748: em.getTransaction().begin();
749: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#SomeIndividual"));
750: em.persist(entityP);
751: em.getTransaction().commit();
752:
753: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
754: assertNotNull(update.getIndividualUri());
755: em.getTransaction().begin();
756: update.setIndividualUri(null);
757: em.getTransaction().commit();
758:
759: final OWLClassP result = em.find(OWLClassP.class, entityP.getUri());
760: assertNull(result.getIndividualUri());
761: }
762:
763: @Test
764: public void testUpdatePluralPlainLiteralObjectPropertyAttribute() throws MalformedURLException {
765: logger.debug("Test: update plural plain literal object property attribute.");
766: this.em = getEntityManager("UpdatePluralPlainLiteralObjectPropertyValue", true);
767: entityP.setIndividuals(Generators.createUrls());
768: persist(entityP);
769:
770: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
771: em.getTransaction().begin();
772: final URL added = new URL("http://krizik.felk.cvut.cz/ontologies/jopa#added");
773: final URL removed = entityP.getIndividuals().iterator().next();
774: update.getIndividuals().add(added);
775: update.getIndividuals().remove(removed);
776: em.getTransaction().commit();
777:
778: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
779: assertNotNull(res);
780: assertEquals(update.getIndividuals(), res.getIndividuals());
781: }
782:
783: @Test
784: public void testUpdateSimpleListOfIdentifiersByAddingNewItems() {
785: this.em = getEntityManager("UpdateSimpleListOfIdentifiersByAddingItems", true);
786: entityP.setSimpleList(Generators.createListOfIdentifiers());
787: persist(entityP);
788:
789: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
790: em.getTransaction().begin();
791: for (int i = 0; i < Generators.randomPositiveInt(10); i++) {
792: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
793: // Insert at random position
794: update.getSimpleList().add(Generators.randomInt(update.getSimpleList().size()), u);
795: }
796: em.getTransaction().commit();
797:
798: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
799: assertNotNull(res);
800: assertEquals(update.getSimpleList(), res.getSimpleList());
801: }
802:
803: @Test
804: public void testUpdateReferencedListByRemovingAndAddingItems() {
805: this.em = getEntityManager("UpdateReferencedListByRemovingAndAddingItems", true);
806: entityP.setReferencedList(Generators.createListOfIdentifiers());
807: persist(entityP);
808:
809: final OWLClassP update = em.find(OWLClassP.class, entityP.getUri());
810: em.getTransaction().begin();
811: for (int i = 0; i < Generators.randomPositiveInt(10); i++) {
812: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
813: // We might even overwrite items set in previous iterations, but it does not matter. JOPA should handle it
814: update.getReferencedList().set(Generators.randomInt(update.getReferencedList().size()), u);
815: }
816: em.getTransaction().commit();
817:
818: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
819: assertNotNull(res);
820: assertEquals(update.getReferencedList(), res.getReferencedList());
821: }
822:
823: @Test
824: public void testUpdateStringAnnotationPropertyValue() {
825: this.em = getEntityManager("UpdateStringAnnotationPropertyValue", true);
826: entityN.setAnnotationProperty("PropertyValue");
827: persist(entityN);
828:
829: final String newValue = "newValue";
830: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
831: assertNotNull(update);
832: em.getTransaction().begin();
833: update.setAnnotationProperty(newValue);
834: em.getTransaction().commit();
835:
836: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
837: assertEquals(newValue, res.getAnnotationProperty());
838: }
839:
840: @Test
841: public void testUpdateAnnotationPropertyWithUriValueToDifferentValue() {
842: this.em = getEntityManager("UpdateAnnotationPropertyWithUriValueToDifferentValue", true);
843: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#value"));
844: persist(entityN);
845:
846: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
847: assertNotNull(update);
848: em.getTransaction().begin();
849: final URI newUri = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#newValue");
850: update.setAnnotationUri(newUri);
851: em.getTransaction().commit();
852:
853: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
854: assertEquals(newUri, res.getAnnotationUri());
855: }
856: }