-
Notifications
You must be signed in to change notification settings - Fork 14
/
RetrieveOperationsRunner.java
148 lines (129 loc) · 5.4 KB
/
RetrieveOperationsRunner.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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.
*
* 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.test.*;
import org.junit.Test;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import static org.junit.Assert.*;
public abstract class RetrieveOperationsRunner extends BaseRunner {
public RetrieveOperationsRunner(Logger logger) {
super(logger);
}
@Test
public void testRetrieveSimple() {
logger.config("Test: retrieve a simple entity.");
this.em = getEntityManager("RetrieveSimple", false);
persist(entityA);
em.getEntityManagerFactory().getCache().evictAll();
final OWLClassA res = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(res);
assertEquals(entityA.getUri(), res.getUri());
assertEquals(entityA.getStringAttribute(), res.getStringAttribute());
assertTrue(entityA.getTypes().containsAll(res.getTypes()));
assertTrue(em.contains(res));
}
@Test(expected = NullPointerException.class)
public void testRetrieveNull() {
logger.config("Test: retrieve null.");
this.em = getEntityManager("RetrieveNull", false);
em.find(OWLClassA.class, null);
}
@Test
public void testRetrieveLazy() throws Exception {
logger.config("Test: retrieve entity with lazy loaded attribute.");
this.em = getEntityManager("RetrieveLazy", false);
persist(entityI);
final OWLClassI resI = em.find(OWLClassI.class, entityI.getUri());
assertNotNull(resI);
final Field f = OWLClassI.class.getDeclaredField("owlClassA");
f.setAccessible(true);
Object value = f.get(resI);
assertNull(value);
assertNotNull(resI.getOwlClassA());
value = f.get(resI);
assertNotNull(value);
assertEquals(entityA.getUri(), resI.getOwlClassA().getUri());
assertTrue(em.contains(resI.getOwlClassA()));
}
@Test
public void testRetrieveGenerated() throws Exception {
logger.config("Test: persist and retrieve several entities with generated identifiers.");
this.em = getEntityManager("RetrieveGenerated", false);
em.getTransaction().begin();
final int size = 10;
final List<OWLClassE> lst = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
final OWLClassE e = new OWLClassE();
e.setStringAttribute("blablabla" + i);
assertNull(e.getUri());
em.persist(e);
assertNotNull(e.getUri());
lst.add(e);
}
em.getTransaction().commit();
em.clear();
for (OWLClassE e : lst) {
final OWLClassE res = em.find(OWLClassE.class, e.getUri());
assertNotNull(res);
assertEquals(e.getStringAttribute(), res.getStringAttribute());
}
}
@Test
public void testRetrieveNotExisting() {
logger.config("Test: retrieve entity which does not exist in the specified context.");
this.em = getEntityManager("RetrieveNotExisting", false);
final OWLClassB res = em.find(OWLClassB.class, entityB.getUri());
assertNull(res);
}
@Test
public void testRefresh() {
logger.config("Test: refresh entity.");
this.em = getEntityManager("Refresh", false);
persist(entityD, entityA);
final OWLClassA newA = new OWLClassA();
newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
newA.setStringAttribute("newA");
final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
assertEquals(d.getOwlClassA(), a);
d.setOwlClassA(newA);
em.refresh(d);
assertEquals(a.getUri(), d.getOwlClassA().getUri());
}
@Test(expected = IllegalArgumentException.class)
public void testRefreshNotManaged() {
logger.config("Test: refresh entity which is not managed.");
this.em = getEntityManager("RefreshNotManaged", false);
persist(entityA);
final OWLClassA a = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(a);
final OWLClassA newA = new OWLClassA();
newA.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA"));
newA.setStringAttribute("newA");
em.refresh(newA);
}
@Test
public void testRetrieveDifferentType() {
logger.config("Test: persist entity but try to retrieve it as a different type.");
this.em = getEntityManager("RetrieveDifferentType", false);
persist(entityA);
final OWLClassB res = em.find(OWLClassB.class, entityA.getUri());
assertNull(res);
}
}