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