Skip to content

Method: testRemoveUnmappedPropertyValue()

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