-
Notifications
You must be signed in to change notification settings - Fork 14
/
CreateOperationsMultiContextRunner.java
280 lines (250 loc) · 13.4 KB
/
CreateOperationsMultiContextRunner.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
/**
* 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.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.descriptors.ObjectPropertyCollectionDescriptor;
import cz.cvut.kbss.jopa.test.*;
import cz.cvut.kbss.jopa.test.environment.Generators;
import org.junit.Test;
import org.slf4j.Logger;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
public abstract class CreateOperationsMultiContextRunner extends BaseRunner {
private OWLClassF entityF;
private OWLClassK entityK;
public CreateOperationsMultiContextRunner(Logger logger) {
super(logger);
initialize();
}
private void initialize() {
this.entityF = new OWLClassF();
entityF.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityF"));
this.entityK = new OWLClassK();
entityK.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityK"));
}
@Test
public void testPersistDataPropertyIntoContext() throws Exception {
logger.debug("Test: persist an entity into the default context and its data property into a different one.");
this.em = getEntityManager("MultiPersistDataPropertyIntoContext", false);
final Descriptor aDescriptor = new EntityDescriptor();
aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_ONE);
em.getTransaction().begin();
em.persist(entityA, aDescriptor);
em.getTransaction().commit();
final OWLClassA res = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
assertNotNull(res);
assertEquals(entityA.getUri(), res.getUri());
assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
assertEquals(entityA.getTypes().size(), res.getTypes().size());
assertTrue(entityA.getTypes().containsAll(res.getTypes()));
}
@Test
public void testPersistObjectPropertyIntoContext() throws Exception {
logger.debug(
"Test: persist entity into one context and its object property into another, along with its own attributes.");
this.em = getEntityManager("MultiPersistObjectPropertyIntoContext", false);
final Descriptor dDescriptor = new EntityDescriptor();
final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
em.getTransaction().begin();
em.persist(entityD, dDescriptor);
em.persist(entityA, aDescriptor);
em.getTransaction().commit();
final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
assertNotNull(resD);
assertNotNull(resD.getOwlClassA());
final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
assertNotNull(resA);
assertEquals(resD.getOwlClassA().getUri(), resA.getUri());
assertEquals(resD.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
assertTrue(resD.getOwlClassA().getTypes().containsAll(resA.getTypes()));
}
@Test
public void testPersistWithGeneratedIntoContext() {
logger.debug("Test: persist entity with generated ID into a context.");
this.em = getEntityManager("MultiPersistWithGeneratedIntoContext", false);
final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_ONE);
em.getTransaction().begin();
em.persist(entityE, eDescriptor);
em.getTransaction().commit();
final OWLClassE res = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
assertNotNull(res);
assertEquals(entityE.getUri(), res.getUri());
assertEquals(entityE.getStringAttribute(), res.getStringAttribute());
}
@Test
public void testPersistReferenceIntoContextAndThenOwnerIntoDefault() {
this.em = getEntityManager("ReferenceIntoContextThenOwnerIntoDefault", false);
final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_ONE);
em.getTransaction().begin();
em.persist(entityA, eDescriptor);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
entityD.setOwlClassA(entityA);
em.persist(entityD);
em.getTransaction().commit();
final OWLClassD res = em.find(OWLClassD.class, entityD.getUri());
assertNotNull(res);
assertNotNull(res.getOwlClassA());
}
@Test
public void persistTwiceIntoSameContextIsLegal() {
this.em = getEntityManager("MultiPersistTwiceIntoOneContext", false);
logger.debug("Test: persist an entity twice into the same context.");
final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
em.getTransaction().begin();
em.persist(entityA, aDescriptor);
em.getTransaction().commit();
em.getTransaction().begin();
em.persist(entityA, aDescriptor);
em.getTransaction().commit();
assertNotNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
}
@Test
public void persistTwiceIntoDifferentContextsIsLegal() {
this.em = getEntityManager("MultiPersistTwiceIntoDifferentContexts", false);
logger.debug("Test: persist an entity into two different contexts.");
final Descriptor aDescriptorOne = new EntityDescriptor(CONTEXT_ONE);
final Descriptor aDescriptorTwo = new EntityDescriptor(CONTEXT_TWO);
em.getTransaction().begin();
em.persist(entityA, aDescriptorOne);
em.persist(entityA, aDescriptorTwo);
em.getTransaction().commit();
final OWLClassA resOne = em.find(OWLClassA.class, entityA.getUri(), aDescriptorOne);
assertNotNull(resOne);
final OWLClassA resTwo = em.find(OWLClassA.class, entityA.getUri(), aDescriptorTwo);
assertNotNull(resTwo);
assertNotSame(resOne, resTwo);
assertEquals(resOne.getUri(), resTwo.getUri());
assertEquals(resOne.getStringAttribute(), resTwo.getStringAttribute());
}
@Test
public void testPersistPropertiesIntoDifferentContext() throws Exception {
this.em = getEntityManager("MultiPersistPropertiesIntoDifferentContext", false);
logger.debug("Test: persist an entity and persist its properties into a different context.");
final Descriptor bDescriptor = new EntityDescriptor();
entityB.setProperties(Generators.createProperties(10));
bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_ONE);
em.getTransaction().begin();
em.persist(entityB, bDescriptor);
em.getTransaction().commit();
final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
assertNotNull(res);
assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
assertEquals(entityB.getProperties().size(), res.getProperties().size());
for (Map.Entry<String, Set<String>> e : res.getProperties().entrySet()) {
assertTrue(entityB.getProperties().containsKey(e.getKey()));
assertEquals(e.getValue(), entityB.getProperties().get(e.getKey()));
}
}
@Test
public void testPersistCascadeIntoThreeContexts() throws Exception {
this.em = getEntityManager("MultiPersistCascadeIntoThreeContexts", false);
logger.debug("Test: persist three entities in cascaded relationship, each into a different context.");
final Descriptor gDescriptor = new EntityDescriptor();
final Descriptor hDescriptor = new EntityDescriptor(CONTEXT_ONE);
final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
hDescriptor.addAttributeDescriptor(OWLClassH.class.getDeclaredField("owlClassA"), aDescriptor);
gDescriptor.addAttributeDescriptor(OWLClassG.class.getDeclaredField("owlClassH"), hDescriptor);
em.getTransaction().begin();
em.persist(entityG, gDescriptor);
assertTrue(em.contains(entityG));
assertTrue(em.contains(entityH));
assertTrue(em.contains(entityA));
em.getTransaction().commit();
final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
assertNotNull(resA);
final OWLClassH resH = em.find(OWLClassH.class, entityH.getUri(), hDescriptor);
assertNotNull(resH);
assertSame(resA, resH.getOwlClassA());
final OWLClassG resG = em.find(OWLClassG.class, entityG.getUri(), gDescriptor);
assertNotNull(resG);
assertSame(resH, resG.getOwlClassH());
}
@Test
public void testPersistSetWithAttributeContexts() throws Exception {
logger.debug(
"Test: persist entity with simple set, the set will be in a different context and attributes of its element in another.");
this.em = getEntityManager("MultiPersistSetWithAttributeContexts", false);
entityF.setSimpleSet(Generators.createSimpleSet(20));
final Descriptor fDescriptor = new EntityDescriptor();
final Descriptor setDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
OWLClassF.class.getDeclaredField("simpleSet"));
fDescriptor.addAttributeDescriptor(OWLClassF.class.getDeclaredField("simpleSet"), setDescriptor);
setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
setDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("types"), CONTEXT_TWO);
em.getTransaction().begin();
em.persist(entityF, fDescriptor);
for (OWLClassA a : entityF.getSimpleSet()) {
em.persist(a, setDescriptor);
}
em.getTransaction().commit();
final OWLClassF resF = em.find(OWLClassF.class, entityF.getUri(), fDescriptor);
assertNotNull(resF);
assertEquals(entityF.getSimpleSet().size(), resF.getSimpleSet().size());
for (OWLClassA a : resF.getSimpleSet()) {
final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), setDescriptor);
assertNotNull(resA);
assertEquals(a.getStringAttribute(), resA.getStringAttribute());
assertEquals(a.getTypes(), resA.getTypes());
}
}
@Test
public void testPersistEntityWithObjectPropertyWithGeneratedIdentifierAndPutTheReferenceIntoContext()
throws Exception {
logger.debug("Test: persist entity with reference to an entity with generated identifier. "
+ "The identifier should be generated automatically before the referenced entity itself is persisted.");
this.em = getEntityManager("PersistEntityWithObjectPropertyWithGeneratedIdentifierContexts", true);
entityK.setOwlClassE(entityE);
assertNull(entityE.getUri());
final Descriptor eDescriptor = new EntityDescriptor(CONTEXT_TWO);
final Descriptor kDescriptor = new EntityDescriptor(CONTEXT_ONE);
kDescriptor.addAttributeDescriptor(OWLClassK.class.getDeclaredField("owlClassE"), eDescriptor);
em.getTransaction().begin();
em.persist(entityK, kDescriptor);
assertNotNull(entityE.getUri());
em.persist(entityE, eDescriptor);
em.getTransaction().commit();
final OWLClassE resE = em.find(OWLClassE.class, entityE.getUri(), eDescriptor);
assertNotNull(resE);
assertEquals(entityE.getStringAttribute(), resE.getStringAttribute());
final OWLClassK resK = em.find(OWLClassK.class, entityK.getUri(), kDescriptor);
assertNotNull(resK);
assertEquals(resE, resK.getOwlClassE());
}
@Test
public void testPersistEntityWithMappedSuperclassPuttingReferenceIntoDifferentContext() throws Exception {
this.em = getEntityManager("PersistEntityWithMappedSuperclassReferenceInContext", true);
final Descriptor qDescriptor = new EntityDescriptor(CONTEXT_ONE);
final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
em.getTransaction().begin();
em.persist(entityQ, qDescriptor);
em.persist(entityA, aDescriptor);
em.getTransaction().commit();
final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
assertNotNull(resA);
final OWLClassQ resQ = em.find(OWLClassQ.class, entityQ.getUri(), qDescriptor);
assertNotNull(resQ);
assertEquals(entityQ.getStringAttribute(), resQ.getStringAttribute());
assertEquals(entityQ.getParentString(), resQ.getParentString());
assertEquals(entityQ.getLabel(), resQ.getLabel());
assertNotNull(resQ.getOwlClassA());
assertEquals(resA, resQ.getOwlClassA());
}
}