-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityLifecycleCallbackResolverTest.java
332 lines (282 loc) · 14 KB
/
EntityLifecycleCallbackResolverTest.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
/**
* 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.model.metamodel;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassR;
import cz.cvut.kbss.jopa.environment.OWLClassS;
import cz.cvut.kbss.jopa.environment.Vocabulary;
import cz.cvut.kbss.jopa.environment.listener.ParentListener;
import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.IRI;
import cz.cvut.kbss.jopa.model.annotations.*;
import cz.cvut.kbss.jopa.model.lifecycle.LifecycleEvent;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("unused")
public class EntityLifecycleCallbackResolverTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void resolveDiscoversEntityLifecycleListeners() throws Exception {
final AbstractIdentifiableType<OWLClassR> type = typeFor(OWLClassR.class);
final EntityLifecycleListenerManager callbackManager = resolve(type);
assertTrue(callbackManager.getLifecycleCallbacks().containsKey(LifecycleEvent.POST_LOAD));
assertEquals(OWLClassR.class.getDeclaredMethod("postLoad"),
callbackManager.getLifecycleCallbacks().get(LifecycleEvent.POST_LOAD));
}
private EntityLifecycleListenerManager resolve(AbstractIdentifiableType<?> et) {
return new EntityLifecycleCallbackResolver(et).resolve();
}
private <T> AbstractIdentifiableType<T> typeFor(Class<T> cls) {
final String name = cls.getName();
final String iri = cls.getDeclaredAnnotation(OWLClass.class).iri();
return new EntityTypeImpl<>(name, cls, IRI.create(iri));
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenMultipleListenersForOnePhaseAreDeclaredOnType() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The type [" + ClassWithMultipleListeners.class.getName() +
"] has multiple lifecycle callbacks for the lifecycle event [" + LifecycleEvent.POST_LOAD +
"].");
resolve(typeFor(ClassWithMultipleListeners.class));
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "ClassWithMultipleListeners")
private static class ClassWithMultipleListeners {
@PostLoad
private void one() {
}
@PostLoad
private void two() {
}
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerTakesArguments() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [takesArguments] in type [" +
ClassWithLifecycleListenerTakingArguments.class.getName() +
"] has incorrect signature. It should not have any arguments.");
resolve(typeFor(ClassWithLifecycleListenerTakingArguments.class));
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "ClassWithLifecycleListenerTakingArguments")
private static class ClassWithLifecycleListenerTakingArguments {
@PrePersist
private void takesArguments(EntityManager em) {
}
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleIsNotVoid() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [returnsData] in type [" +
ClassWithNonVoidLifecycleListener.class.getName() +
"] has incorrect signature. Its return type should be void.");
resolve(typeFor(ClassWithNonVoidLifecycleListener.class));
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "ClassWithNonVoidLifecycleListener")
private static class ClassWithNonVoidLifecycleListener {
@PostPersist
private String returnsData() {
return "test";
}
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerIsFinal() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [finalListener] in type [" +
ClassWithFinalLifecycleListener.class.getName() +
"] has incorrect signature. It should not be static or final.");
resolve(typeFor(ClassWithFinalLifecycleListener.class));
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "ClassWithFinalLifecycleListener")
private static class ClassWithFinalLifecycleListener {
@PostPersist
public final void finalListener() {
}
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerIsStatic() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [staticListener] in type [" +
ClassWithStaticLifecycleListener.class.getName() +
"] has incorrect signature. It should not be static or final.");
resolve(typeFor(ClassWithStaticLifecycleListener.class));
}
@OWLClass(iri = Vocabulary.CLASS_BASE + "ClassWithStaticLifecycleListener")
private static class ClassWithStaticLifecycleListener {
@PostPersist
public static void staticListener() {
}
}
@Test
public void resolveSetsUpReferenceToSupertypeLifecycleManager() throws Exception {
final AbstractIdentifiableType<OWLClassS> parent = typeFor(OWLClassS.class);
parent.setLifecycleListenerManager(resolve(parent));
final AbstractIdentifiableType<OWLClassR> child = typeFor(OWLClassR.class);
child.setSupertype(parent);
final EntityLifecycleListenerManager result = resolve(child);
final Field parentField = EntityLifecycleListenerManager.class.getDeclaredField("parent");
parentField.setAccessible(true);
assertEquals(parent.getLifecycleListenerManager(), parentField.get(result));
}
@Test
public void resolveCreatesInstanceOfEntityListenerDeclaredBySpecifiedEntityType() {
final AbstractIdentifiableType<OWLClassS> et = typeFor(OWLClassS.class);
final EntityLifecycleListenerManager result = resolve(et);
assertEquals(1, result.getEntityListeners().size());
assertTrue(result.getEntityListeners().get(0) instanceof ParentListener);
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenUnableToInstantiateEntityListener() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("Unable to instantiate entity listener of type " + InvalidListener.class
+ ". The listener has to have a public no-arg constructor.");
final AbstractIdentifiableType<EntityWithInvalidListener> et = typeFor(EntityWithInvalidListener.class);
resolve(et);
}
private static class InvalidListener {
private InvalidListener() {
}
}
@EntityListeners(InvalidListener.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithInvalidListener")
private static class EntityWithInvalidListener {
}
@Test
public void resolveRegistersCallbacksDeclaredInEntityListener() throws Exception {
final AbstractIdentifiableType<OWLClassS> et = typeFor(OWLClassS.class);
final EntityLifecycleListenerManager result = resolve(et);
assertEquals(1, result.getEntityListenerCallbacks().size());
final Map<LifecycleEvent, Method> callbacks = result.getEntityListenerCallbacks().values().iterator().next();
assertTrue(callbacks.containsKey(LifecycleEvent.PRE_PERSIST));
assertEquals(ParentListener.getPrePersistMethod(), callbacks.get(LifecycleEvent.PRE_PERSIST));
assertTrue(callbacks.containsKey(LifecycleEvent.POST_PERSIST));
assertEquals(ParentListener.getPostPersistMethod(), callbacks.get(LifecycleEvent.POST_PERSIST));
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackHasInvalidNumberOfParameters() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [invalidArgumentCount] in entity listener [" +
ListenerWithInvalidArgumentCount.class.getName() +
"] has incorrect signature. It should take exactly one argument.");
final AbstractIdentifiableType<EntityWithListenerWithInvalidArgumentCount> et = typeFor(
EntityWithListenerWithInvalidArgumentCount.class);
resolve(et);
}
private static class ListenerWithInvalidArgumentCount {
public ListenerWithInvalidArgumentCount() {
}
@PostLoad
public void invalidArgumentCount(Object one, Object two) {
}
}
@EntityListeners(ListenerWithInvalidArgumentCount.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithListenerWithInvalidArgumentCount")
private static class EntityWithListenerWithInvalidArgumentCount {
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackIsNotVoid() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [nonVoid] in entity listener [" +
ListenerWithNonVoidCallback.class.getName() +
"] has incorrect signature. Its return type should be void.");
final AbstractIdentifiableType<EntityWithListenerWithNonVoidCallback> et = typeFor(
EntityWithListenerWithNonVoidCallback.class);
resolve(et);
}
private static class ListenerWithNonVoidCallback {
public ListenerWithNonVoidCallback() {
}
@PostLoad
public int nonVoid(Object arg) {
return 117;
}
}
@EntityListeners(ListenerWithNonVoidCallback.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithListenerWithNonVoidCallback")
private static class EntityWithListenerWithNonVoidCallback {
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackIsStatic() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [staticCallback] in entity listener [" +
ListenerWithStaticCallback.class.getName() +
"] has incorrect signature. It should not be static or final.");
final AbstractIdentifiableType<EntityWithListenerWithStaticCallback> et = typeFor(
EntityWithListenerWithStaticCallback.class);
resolve(et);
}
private static class ListenerWithStaticCallback {
public ListenerWithStaticCallback() {
}
@PostLoad
public static void staticCallback(Object arg) {
}
}
@EntityListeners(ListenerWithStaticCallback.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithListenerWithStaticCallback")
private static class EntityWithListenerWithStaticCallback {
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerHasMultipleCallbacksForSameEvent() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The entity listener [" + ListenerWithMultipleCallbacksForSameEvent.class.getName() +
"] has multiple callbacks for the lifecycle event [" + LifecycleEvent.POST_LOAD + "].");
final AbstractIdentifiableType<EntityWithListenerWithMultipleConflictingCallbacks> et = typeFor(
EntityWithListenerWithMultipleConflictingCallbacks.class);
resolve(et);
}
private static class ListenerWithMultipleCallbacksForSameEvent {
public ListenerWithMultipleCallbacksForSameEvent() {
}
@PostLoad
public void firstCallback(Object arg) {
}
@PostLoad
public void secondCallback(Object arg) {
}
}
@EntityListeners(ListenerWithMultipleCallbacksForSameEvent.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithListenerWithMultipleConflictingCallbacks")
private static class EntityWithListenerWithMultipleConflictingCallbacks {
}
@Test
public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackHasInvalidParameterType() {
thrown.expect(MetamodelInitializationException.class);
thrown.expectMessage("The callback method [callback] in entity listener [" +
ListenerWithInvalidParameterTypeCallback.class.getName() +
"] has incorrect signature. Its parameter should be of type [" + Object.class.getName() + "] or [" +
EntityWithListenerWithInvalidParameterTypeCallback.class.getName() + "].");
final AbstractIdentifiableType<EntityWithListenerWithInvalidParameterTypeCallback> et = typeFor(
EntityWithListenerWithInvalidParameterTypeCallback.class);
resolve(et);
}
private static class ListenerWithInvalidParameterTypeCallback {
public ListenerWithInvalidParameterTypeCallback() {
}
@PostLoad
public void callback(OWLClassA arg) {
}
}
@EntityListeners(ListenerWithInvalidParameterTypeCallback.class)
@OWLClass(iri = Vocabulary.CLASS_BASE + "EntityWithListenerWithInvalidParameterTypeCallback")
private static class EntityWithListenerWithInvalidParameterTypeCallback {
}
}