Skip to content

Method: testRemoveReference()

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.model.annotations.Id;
18: import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
19: import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
20: import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
21: import cz.cvut.kbss.jopa.test.*;
22: import cz.cvut.kbss.jopa.test.environment.Generators;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import org.junit.Ignore;
25: import org.junit.Test;
26: import org.slf4j.Logger;
27:
28: import java.lang.reflect.Field;
29: import java.net.URI;
30: import java.util.*;
31:
32: import static org.junit.Assert.*;
33:
34: public abstract class DeleteOperationsRunner extends BaseRunner {
35:
36: public DeleteOperationsRunner(Logger logger) {
37: super(logger);
38: }
39:
40: @Test
41: public void testRemoveSimple() {
42: this.em = getEntityManager("SimpleRemove", false);
43: persist(entityA);
44:
45: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
46: assertNotNull(a);
47: em.getTransaction().begin();
48: em.remove(a);
49: em.getTransaction().commit();
50:
51: assertNull(em.find(OWLClassA.class, entityA.getUri()));
52: }
53:
54: // TODO First we need to resolve referential integrity
55: @Ignore
56: @Test
57: public void testRemoveReference() {
58: this.em = getEntityManager("RemoveReference", false);
59: persist(entityD, entityA);
60:
61: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
62: assertNotNull(a);
63: em.getTransaction().begin();
64: em.remove(a);
65: em.getTransaction().commit();
66:
67: final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
68: assertNotNull(res);
69: assertNull(em.find(OWLClassA.class, entityA.getUri()));
70: }
71:
72: @Test
73: public void testRemoveCascade() {
74: this.em = getEntityManager("RemoveCascade", false);
75: em.getTransaction().begin();
76: em.persist(entityG);
77: assertTrue(em.contains(entityG));
78: assertTrue(em.contains(entityH));
79: assertTrue(em.contains(entityA));
80: em.getTransaction().commit();
81:
82: em.getTransaction().begin();
83: final OWLClassG g = em.find(OWLClassG.class, entityG.getUri());
84: final OWLClassH h = em.find(OWLClassH.class, entityH.getUri());
85: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
86: assertNotNull(g);
87: assertNotNull(h);
88: assertNotNull(a);
89: assertTrue(em.contains(g));
90: assertTrue(em.contains(h));
91: assertTrue(em.contains(a));
92: assertNotNull(g);
93: em.remove(g);
94: assertFalse(em.contains(g));
95: assertFalse(em.contains(h));
96: assertFalse(em.contains(a));
97: em.getTransaction().commit();
98:
99: assertNull(em.find(OWLClassG.class, entityG.getUri()));
100: assertNull(em.find(OWLClassH.class, entityH.getUri()));
101: assertNull(em.find(OWLClassA.class, entityA.getUri()));
102: }
103:
104: @Test(expected = IllegalArgumentException.class)
105: public void testRemoveDetached() {
106: this.em = getEntityManager("RemoveDetached", false);
107: assertNull(entityE.getUri());
108: persist(entityE);
109: assertNotNull(entityE.getUri());
110:
111: em.getTransaction().begin();
112: final OWLClassE e = em.find(OWLClassE.class, entityE.getUri());
113: assertNotNull(e);
114: assertTrue(em.contains(e));
115: em.detach(e);
116: assertFalse(em.contains(e));
117: em.remove(e);
118: }
119:
120: @Test
121: public void testRemoveFromSimpleList() {
122: this.em = getEntityManager("RemoveFromSimpleList", false);
123: final int size = 5;
124: entityC.setSimpleList(Generators.createSimpleList(size));
125: em.getTransaction().begin();
126: em.persist(entityC);
127: entityC.getSimpleList().forEach(em::persist);
128: em.getTransaction().commit();
129:
130: final int randIndex = Generators.randomInt(size);
131: final OWLClassA a = em.find(OWLClassA.class, entityC.getSimpleList().get(randIndex).getUri());
132: assertNotNull(a);
133: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
134: assertNotNull(c);
135: em.getTransaction().begin();
136: // We have to remove A from the simple list as well because otherwise we would break the chain in instances
137: assertTrue(c.getSimpleList().remove(a));
138: em.remove(a);
139: em.getTransaction().commit();
140:
141: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
142: assertNull(resA);
143: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
144: boolean found = false;
145: for (OWLClassA aa : resC.getSimpleList()) {
146: if (aa.getUri().equals(a.getUri())) {
147: found = true;
148: break;
149: }
150: }
151: assertFalse(found);
152: }
153:
154: @Test
155: public void testRemoveFromReferencedList() {
156: this.em = getEntityManager("RemoveFromReferencedList", false);
157: final int size = 10;
158: entityC.setReferencedList(Generators.createReferencedList(size));
159: em.getTransaction().begin();
160: em.persist(entityC);
161: entityC.getReferencedList().forEach(em::persist);
162: em.getTransaction().commit();
163:
164: final int randIndex = Generators.randomInt(size);
165: final OWLClassA a = em.find(OWLClassA.class, entityC.getReferencedList().get(randIndex).getUri());
166: assertNotNull(a);
167: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
168: assertNotNull(c);
169: em.getTransaction().begin();
170: // We have to remove A from the referenced list as well because otherwise we would break the chain in instances
171: assertTrue(c.getReferencedList().remove(a));
172: em.remove(a);
173: em.getTransaction().commit();
174:
175: final OWLClassA resA = em.find(OWLClassA.class, a.getUri());
176: assertNull(resA);
177: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri());
178: boolean found = false;
179: for (OWLClassA aa : resC.getReferencedList()) {
180: if (aa.getUri().equals(a.getUri())) {
181: found = true;
182: break;
183: }
184: }
185: assertFalse(found);
186: }
187:
188: @Test
189: public void testRemoveListOwner() {
190: this.em = getEntityManager("RemoveListOwner", false);
191: entityC.setSimpleList(Generators.createSimpleList());
192: entityC.setReferencedList(Generators.createReferencedList());
193: em.getTransaction().begin();
194: em.persist(entityC);
195: entityC.getSimpleList().forEach(em::persist);
196: entityC.getReferencedList().forEach(em::persist);
197: em.getTransaction().commit();
198:
199: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri());
200: assertNotNull(c);
201: em.getTransaction().begin();
202: em.remove(c);
203: em.getTransaction().commit();
204:
205: em.getEntityManagerFactory().getCache().evictAll();
206: for (OWLClassA a : entityC.getSimpleList()) {
207: assertNotNull(em.find(OWLClassA.class, a.getUri()));
208: }
209: for (OWLClassA a : entityC.getReferencedList()) {
210: assertNotNull(em.find(OWLClassA.class, a.getUri()));
211: }
212: }
213:
214: @Test
215: public void testRemoveNotYetCommitted() {
216: this.em = getEntityManager("RemoveNotYetCommitted", false);
217: em.getTransaction().begin();
218: em.persist(entityE);
219: assertTrue(em.contains(entityE));
220: em.remove(entityE);
221: assertFalse(em.contains(entityE));
222: em.getTransaction().commit();
223:
224: final OWLClassE res = em.find(OWLClassE.class, entityE.getUri());
225: assertNull(res);
226: }
227:
228: @Test
229: public void testRemoveMergedWithCascading() {
230: this.em = getEntityManager("CascadeMergeAndRemove", false);
231: em.getTransaction().begin();
232: em.persist(entityG);
233: assertTrue(em.contains(entityA));
234: assertTrue(em.contains(entityG));
235: assertTrue(em.contains(entityH));
236: em.getTransaction().commit();
237: em.clear();
238:
239: final OWLClassG toDetach = em.find(OWLClassG.class, entityG.getUri());
240: assertNotNull(toDetach);
241: em.detach(toDetach);
242: assertFalse(em.contains(toDetach));
243: assertFalse(em.contains(toDetach.getOwlClassH()));
244: assertFalse(em.contains(toDetach.getOwlClassH().getOwlClassA()));
245:
246: em.getTransaction().begin();
247: final OWLClassG toRemove = em.merge(toDetach);
248: em.remove(toRemove);
249: em.getTransaction().commit();
250:
251: assertNull(em.find(OWLClassG.class, entityG.getUri()));
252: assertNull(em.find(OWLClassH.class, entityH.getUri()));
253: assertNull(em.find(OWLClassA.class, entityA.getUri()));
254: }
255:
256: @Test
257: public void removeDeletesAllPropertyAssertionsMappedByEntity() throws Exception {
258: this.em = getEntityManager("RemoveDeletesAllMappedAttributes", false);
259: em.getTransaction().begin();
260: entityC.setSimpleList(Generators.createSimpleList(5));
261: em.persist(entityC);
262: entityC.getSimpleList().forEach(em::persist);
263: em.getTransaction().commit();
264:
265: final OWLClassC toRemove = em.find(OWLClassC.class, entityC.getUri());
266: em.getTransaction().begin();
267: em.remove(toRemove);
268: em.getTransaction().commit();
269:
270: final List<String> properties = resolveCProperties();
271: for (String prop : properties) {
272: assertFalse(
273: em.createNativeQuery("ASK WHERE { ?x ?p ?o .}", Boolean.class).setParameter("x", entityC.getUri())
274: .setParameter("p", URI.create(prop)).getSingleResult());
275: }
276: }
277:
278: private List<String> resolveCProperties() throws Exception {
279: final List<String> lst = new ArrayList<>();
280: for (Field f : OWLClassC.class.getDeclaredFields()) {
281: if (f.getAnnotation(Id.class) != null || EntityPropertiesUtils.isFieldTransient(f)) {
282: continue;
283: }
284: if (f.getAnnotation(OWLDataProperty.class) != null) {
285: lst.add(f.getAnnotation(OWLDataProperty.class).iri());
286: } else if (f.getAnnotation(OWLObjectProperty.class) != null) {
287: lst.add(f.getAnnotation(OWLObjectProperty.class).iri());
288: } else if (f.getAnnotation(OWLAnnotationProperty.class) != null) {
289: lst.add(f.getAnnotation(OWLAnnotationProperty.class).iri());
290: }
291: }
292: return lst;
293: }
294:
295: @Test
296: public void testRemoveUnmappedPropertyValue() {
297: entityB.setProperties(Generators.createProperties());
298: this.em = getEntityManager("RemoveUnmappedPropertyValue", false);
299: em.getTransaction().begin();
300: em.persist(entityB);
301: em.getTransaction().commit();
302:
303: final String property = entityB.getProperties().keySet().iterator().next();
304: final Set<String> values = entityB.getProperties().get(property);
305: assertTrue(values.size() > 0);
306: final String valueToRemove = values.iterator().next();
307: em.getTransaction().begin();
308: final OWLClassB toUpdate = em.find(OWLClassB.class, entityB.getUri());
309: assertNotNull(toUpdate.getProperties());
310: assertTrue(toUpdate.getProperties().containsKey(property));
311: assertTrue(toUpdate.getProperties().get(property).contains(valueToRemove));
312: toUpdate.getProperties().get(property).remove(valueToRemove);
313: em.getTransaction().commit();
314:
315: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
316: assertNotNull(result.getProperties());
317: assertTrue(result.getProperties().containsKey(property));
318: assertEquals(values.size() - 1, result.getProperties().get(property).size());
319: assertFalse(result.getProperties().get(property).contains(valueToRemove));
320: }
321:
322: @Test
323: public void testRemoveAllValuesOfUnmappedProperty() {
324: entityB.setProperties(Generators.createProperties());
325: this.em = getEntityManager("RemoveAllValuesOfUnmappedProperty", false);
326: em.getTransaction().begin();
327: em.persist(entityB);
328: em.getTransaction().commit();
329:
330: final String property = entityB.getProperties().keySet().iterator().next();
331: em.getTransaction().begin();
332: final OWLClassB toUpdate = em.find(OWLClassB.class, entityB.getUri());
333: assertNotNull(toUpdate.getProperties());
334: assertTrue(toUpdate.getProperties().containsKey(property));
335: toUpdate.getProperties().remove(property);
336: em.getTransaction().commit();
337:
338: final OWLClassB result = em.find(OWLClassB.class, entityB.getUri());
339: assertNotNull(result.getProperties());
340: assertFalse(result.getProperties().containsKey(property));
341: }
342:
343: @Test
344: public void testRemoveTypedUnmappedPropertyValue() {
345: this.em = getEntityManager("RemoveUnmappedPropertyValueTyped", false);
346: entityP.setProperties(Generators.createTypedProperties(10));
347: persist(entityP);
348:
349: em.getTransaction().begin();
350: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
351: for (Set<Object> set : toUpdate.getProperties().values()) {
352: final Iterator<Object> it = set.iterator();
353: while (it.hasNext()) {
354: it.next();
355: if (Generators.randomBoolean()) {
356: it.remove();
357: }
358: }
359: }
360: em.getTransaction().commit();
361:
362: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
363: assertEquals(toUpdate.getProperties(), res.getProperties());
364: }
365:
366: @Test
367: public void testRemoveAllValuesOfTypedUnmappedProperty() {
368: this.em = getEntityManager("RemoveAllValuesOfUnmappedPropertyTyped", false);
369: entityP.setProperties(Generators.createTypedProperties(15));
370: persist(entityP);
371:
372: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
373: em.detach(toUpdate);
374: // Copy the keys to prevent concurrent modification
375: final Set<URI> keys = new HashSet<>(toUpdate.getProperties().keySet());
376: keys.stream().filter(k -> Generators.randomBoolean())
377: .forEach(key -> toUpdate.getProperties().remove(key));
378: em.getTransaction().begin();
379: em.merge(toUpdate);
380: em.getTransaction().commit();
381:
382: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
383: assertEquals(toUpdate.getProperties(), res.getProperties());
384: }
385:
386: @Test
387: public void testRemoveAllValuesOfPluralPlainIdentifierObjectProperty() {
388: this.em = getEntityManager("RemoveAllValuesOfPluralPlainIdentifierOP", false);
389: entityP.setIndividuals(Generators.createUrls());
390: persist(entityP);
391:
392: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
393: em.getTransaction().begin();
394: toUpdate.getIndividuals().clear();
395: em.getTransaction().commit();
396:
397: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
398: assertNull(res.getIndividuals());
399: }
400:
401: @Test
402: public void testSetAnnotationPropertyValueToNull() {
403: this.em = getEntityManager("SetAnnotationPropertyValueToNull", false);
404: entityN.setAnnotationProperty("annotationPropertyValue");
405: persist(entityN);
406:
407: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
408: assertNotNull(update.getAnnotationProperty());
409: em.getTransaction().begin();
410: update.setAnnotationProperty(null);
411: em.getTransaction().commit();
412:
413: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
414: assertNull(res.getAnnotationProperty());
415: }
416:
417: @Test
418: public void testSetAnnotationPropertyValueContainingUriToNull() {
419: this.em = getEntityManager("SetAnnotationPropertyValueContainingUriToNull", false);
420: entityN.setAnnotationUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#annotationPropertyValue"));
421: persist(entityN);
422:
423: final OWLClassN update = em.find(OWLClassN.class, entityN.getId());
424: assertNotNull(update.getAnnotationUri());
425: em.getTransaction().begin();
426: update.setAnnotationUri(null);
427: em.getTransaction().commit();
428:
429: final OWLClassN res = em.find(OWLClassN.class, entityN.getId());
430: assertNull(res.getAnnotationUri());
431: }
432: }