Skip to content

Method: testRemoveFromReferencedList()

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