-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LifeCycleCallbackCollectionTest.java
317 lines (274 loc) · 11 KB
/
LifeCycleCallbackCollectionTest.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
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.plus.annotation;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class LifeCycleCallbackCollectionTest
{
public static class TestServlet extends HttpServlet
{
public static int postConstructCount = 0;
public static int preDestroyCount = 0;
public void postconstruct()
{
++postConstructCount;
}
public void predestroy()
{
++preDestroyCount;
}
}
/**
* An unsupported lifecycle callback type
*/
public class TestLifeCycleCallback extends LifeCycleCallback
{
public TestLifeCycleCallback(Class<?> clazz, String methodName)
{
super(clazz, methodName);
}
public TestLifeCycleCallback(String className, String methodName)
{
super(className, methodName);
}
@Override
public void validate(Class<?> clazz, Method m)
{
throw new IllegalStateException("TEST!");
}
}
/**
* A class that we can use to simulate having PostConstruct and
* PreDestroy annotations on.
*/
public class SomeTestClass
{
public void afterConstruct()
{
//Empty method, we just want to refer to its name
}
}
@Test
public void testAddForPostConstruct() throws Exception
{
//test empty PostConstruct
String nullName = null;
Class<?> clazz = null;
PostConstructCallback pc1 = null;
try
{
pc1 = new PostConstructCallback(nullName, null);
fail("Null class arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PostConstructCallback(clazz, null);
fail("Null class arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PostConstructCallback(SomeTestClass.class, null);
fail("Null method arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PostConstructCallback("foo", null);
fail("Null method arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
LifeCycleCallbackCollection collection = new LifeCycleCallbackCollection();
//test ignoring duplicate adds for callbacks for same classname and method
PostConstructCallback pc2 = new PostConstructCallback("foo", "bar");
collection.add(pc2);
assertThat(collection.getPostConstructCallbackMap().get("foo"), Matchers.contains(pc2));
PostConstructCallback pc3 = new PostConstructCallback("foo", "bar");
collection.add(pc3);
assertThat(collection.getPostConstructCallbackMap().get("foo"), Matchers.contains(pc2));
assertThat(collection.getPostConstructCallbackMap().values(), hasSize(1));
//test ignoring duplicate adds by class and method name
collection = new LifeCycleCallbackCollection();
PostConstructCallback pc4 = new PostConstructCallback(SomeTestClass.class, "afterConstruct");
collection.add(pc4);
assertThat(collection.getPostConstructCallbackMap().get(SomeTestClass.class.getName()), Matchers.contains(pc4));
assertThat(collection.getPostConstructCallbackMap().values(), hasSize(1));
PostConstructCallback pc5 = new PostConstructCallback(SomeTestClass.class, "afterConstruct");
collection.add(pc5);
assertThat(collection.getPostConstructCallbackMap().get(SomeTestClass.class.getName()), Matchers.contains(pc4));
assertThat(collection.getPostConstructCallbackMap().values(), hasSize(1));
}
@Test
public void testUnsupportedType() throws Exception
{
//test that we currently only support PostConstruct and PreDestroy
LifeCycleCallbackCollection collection = new LifeCycleCallbackCollection();
try
{
TestLifeCycleCallback tcb = new TestLifeCycleCallback("abc", "def");
collection.add(tcb);
fail("Support only PostConstruct and PreDestroy");
}
catch (IllegalArgumentException e)
{
//expected
}
}
@Test
public void testServletPostConstructPreDestroy() throws Exception
{
Server server = new Server();
WebAppContext context = new WebAppContext();
context.setResourceBase(MavenTestingUtils.getTargetTestingDir("predestroy-test").toURI().toURL().toString());
context.setContextPath("/");
server.setHandler(context);
//add a non-async servlet
ServletHolder notAsync = new ServletHolder();
notAsync.setHeldClass(TestServlet.class);
notAsync.setName("notAsync");
notAsync.setAsyncSupported(false);
notAsync.setInitOrder(1);
context.getServletHandler().addServletWithMapping(notAsync, "/notasync/*");
//add an async servlet
ServletHolder async = new ServletHolder();
async.setHeldClass(TestServlet.class);
async.setName("async");
async.setAsyncSupported(true);
async.setInitOrder(1);
context.getServletHandler().addServletWithMapping(async, "/async/*");
//add a run-as servlet
ServletHolder runas = new ServletHolder();
runas.setHeldClass(TestServlet.class);
runas.setName("runas");
runas.setRunAsRole("admin");
runas.setInitOrder(1);
context.getServletHandler().addServletWithMapping(runas, "/runas/*");
//add both run-as and non async servlet
ServletHolder both = new ServletHolder();
both.setHeldClass(TestServlet.class);
both.setName("both");
both.setRunAsRole("admin");
both.setAsyncSupported(false);
both.setInitOrder(1);
context.getServletHandler().addServletWithMapping(both, "/both/*");
//Make fake lifecycle callbacks for all servlets
LifeCycleCallbackCollection collection = new LifeCycleCallbackCollection();
context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, collection);
PostConstructCallback pcNotAsync = new PostConstructCallback(TestServlet.class, "postconstruct");
collection.add(pcNotAsync);
PreDestroyCallback pdNotAsync = new PreDestroyCallback(TestServlet.class, "predestroy");
collection.add(pdNotAsync);
PostConstructCallback pcAsync = new PostConstructCallback(TestServlet.class, "postconstruct");
collection.add(pcAsync);
PreDestroyCallback pdAsync = new PreDestroyCallback(TestServlet.class, "predestroy");
collection.add(pdAsync);
PostConstructCallback pcRunAs = new PostConstructCallback(TestServlet.class, "postconstruct");
collection.add(pcRunAs);
PreDestroyCallback pdRunAs = new PreDestroyCallback(TestServlet.class, "predestroy");
collection.add(pdRunAs);
PostConstructCallback pcBoth = new PostConstructCallback(TestServlet.class, "postconstruct");
collection.add(pcBoth);
PreDestroyCallback pdBoth = new PreDestroyCallback(TestServlet.class, "predestroy");
collection.add(pdBoth);
server.start();
assertEquals(4, TestServlet.postConstructCount);
server.stop();
assertEquals(4, TestServlet.preDestroyCount);
}
@Test
public void testAddForPreDestroy() throws Exception
{
//test empty PreDestroy
String nullName = null;
Class<?> clazz = null;
PreDestroyCallback pc1 = null;
try
{
pc1 = new PreDestroyCallback(nullName, null);
fail("Null class arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PreDestroyCallback(clazz, null);
fail("Null class arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PreDestroyCallback(SomeTestClass.class, null);
fail("Null method arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
try
{
pc1 = new PreDestroyCallback("foo", null);
fail("Null method arg should not be allowed");
}
catch (NullPointerException e)
{
//expected
}
LifeCycleCallbackCollection collection = new LifeCycleCallbackCollection();
//test ignoring duplicate adds for callbacks for same classname and method
PreDestroyCallback pc2 = new PreDestroyCallback("foo", "bar");
collection.add(pc2);
assertThat(collection.getPreDestroyCallbackMap().get("foo"), Matchers.contains(pc2));
PreDestroyCallback pc3 = new PreDestroyCallback("foo", "bar");
collection.add(pc3);
assertThat(collection.getPreDestroyCallbackMap().get("foo"), Matchers.contains(pc2));
assertThat(collection.getPreDestroyCallbackMap().values(), hasSize(1));
//test ignoring duplicate adds by class and method name
collection = new LifeCycleCallbackCollection();
PreDestroyCallback pc4 = new PreDestroyCallback(SomeTestClass.class, "afterConstruct");
collection.add(pc4);
assertThat(collection.getPreDestroyCallbackMap().get(SomeTestClass.class.getName()), Matchers.contains(pc4));
assertThat(collection.getPreDestroyCallbackMap().values(), hasSize(1));
PreDestroyCallback pc5 = new PreDestroyCallback(SomeTestClass.class, "afterConstruct");
collection.add(pc5);
assertThat(collection.getPreDestroyCallbackMap().get(SomeTestClass.class.getName()), Matchers.contains(pc4));
assertThat(collection.getPreDestroyCallbackMap().values(), hasSize(1));
}
}