-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TestAnnotationConfiguration.java
400 lines (352 loc) · 18.8 KB
/
TestAnnotationConfiguration.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
398
399
400
//
// ========================================================================
// 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.annotations;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContainerInitializer;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.JAR;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.RelativeOrdering;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebDescriptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestAnnotationConfiguration
{
public class TestableAnnotationConfiguration extends AnnotationConfiguration
{
public void assertAnnotationDiscovery(boolean b)
{
if (!b)
assertTrue(_discoverableAnnotationHandlers.isEmpty());
else
assertFalse(_discoverableAnnotationHandlers.isEmpty());
}
}
public File web25;
public File web31false;
public File web31true;
public File jarDir;
public File testSciJar;
public File testContainerSciJar;
public File testWebInfClassesJar;
public File unpacked;
public URLClassLoader containerLoader;
public URLClassLoader webAppLoader;
public List<Resource> classes;
public Resource targetClasses;
public Resource webInfClasses;
@BeforeEach
public void setup() throws Exception
{
web25 = MavenTestingUtils.getTestResourceFile("web25.xml");
web31false = MavenTestingUtils.getTestResourceFile("web31false.xml");
web31true = MavenTestingUtils.getTestResourceFile("web31true.xml");
// prepare an sci that will be on the webapp's classpath
jarDir = new File(MavenTestingUtils.getTestResourcesDir().getParentFile(), "jar");
testSciJar = new File(jarDir, "test-sci.jar");
assertTrue(testSciJar.exists());
testContainerSciJar = new File(jarDir, "test-sci-for-container-path.jar");
testWebInfClassesJar = new File(jarDir, "test-sci-for-webinf.jar");
// unpack some classes to pretend that are in WEB-INF/classes
unpacked = new File(MavenTestingUtils.getTargetTestingDir(), "test-sci-for-webinf");
unpacked.mkdirs();
FS.cleanDirectory(unpacked);
JAR.unpack(testWebInfClassesJar, unpacked);
webInfClasses = Resource.newResource(unpacked);
containerLoader = new URLClassLoader(new URL[]{
testContainerSciJar.toURI().toURL()
}, Thread.currentThread().getContextClassLoader());
targetClasses = Resource.newResource(MavenTestingUtils.getTargetDir().toURI()).addPath("/test-classes");
classes = Arrays.asList(new Resource[]{webInfClasses, targetClasses});
webAppLoader = new URLClassLoader(new URL[]{
testSciJar.toURI().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL()
},
containerLoader);
}
@Test
public void testAnnotationScanControl() throws Exception
{
//check that a 2.5 webapp with configurationDiscovered will discover annotations
TestableAnnotationConfiguration config25 = new TestableAnnotationConfiguration();
WebAppContext context25 = new WebAppContext();
context25.setClassLoader(Thread.currentThread().getContextClassLoader());
context25.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE);
context25.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0);
context25.setConfigurationDiscovered(false);
context25.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web25)));
context25.getServletContext().setEffectiveMajorVersion(2);
context25.getServletContext().setEffectiveMinorVersion(5);
config25.configure(context25);
config25.assertAnnotationDiscovery(false);
//check that a 2.5 webapp discover annotations
TestableAnnotationConfiguration config25b = new TestableAnnotationConfiguration();
WebAppContext context25b = new WebAppContext();
context25b.setClassLoader(Thread.currentThread().getContextClassLoader());
context25b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE);
context25b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0);
context25b.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web25)));
context25b.getServletContext().setEffectiveMajorVersion(2);
context25b.getServletContext().setEffectiveMinorVersion(5);
config25b.configure(context25b);
config25b.assertAnnotationDiscovery(true);
//check that a 3.x webapp with metadata true won't discover annotations
TestableAnnotationConfiguration config31 = new TestableAnnotationConfiguration();
WebAppContext context31 = new WebAppContext();
context31.setClassLoader(Thread.currentThread().getContextClassLoader());
context31.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE);
context31.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0);
context31.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31true)));
context31.getServletContext().setEffectiveMajorVersion(3);
context31.getServletContext().setEffectiveMinorVersion(1);
config31.configure(context31);
config31.assertAnnotationDiscovery(false);
//check that a 3.x webapp with metadata false will discover annotations
TestableAnnotationConfiguration config31b = new TestableAnnotationConfiguration();
WebAppContext context31b = new WebAppContext();
context31b.setClassLoader(Thread.currentThread().getContextClassLoader());
context31b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE);
context31b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0);
context31b.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31false)));
context31b.getServletContext().setEffectiveMajorVersion(3);
context31b.getServletContext().setEffectiveMinorVersion(1);
config31b.configure(context31b);
config31b.assertAnnotationDiscovery(true);
}
@Test
public void testServerAndWebappSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
//test 3.1 webapp loads both server and app scis
context.setClassLoader(webAppLoader);
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31true)));
context.getMetaData().setWebInfClassesResources(classes);
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testClassScanHandlersForSCIs() throws Exception
{
//test that SCIs with a @HandlesTypes that is an annotation registers
//handlers for the scanning phase that will capture the class hierarchy,
//and also capture all classes that contain the annotation
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
class MyAnnotationConfiguration extends AnnotationConfiguration
{
@Override
public void createServletContainerInitializerAnnotationHandlers(WebAppContext context, List<ServletContainerInitializer> scis) throws Exception
{
super.createServletContainerInitializerAnnotationHandlers(context, scis);
//check class hierarchy scanner handler is registered
assertNotNull(_classInheritanceHandler);
//check
assertEquals(1, _containerInitializerAnnotationHandlers.size());
ContainerInitializerAnnotationHandler handler = _containerInitializerAnnotationHandlers.get(0);
assertThat(handler._holder.toString(), containsString("com.acme.initializer.FooInitializer"));
assertEquals("com.acme.initializer.Foo", handler._annotation.getName());
}
}
MyAnnotationConfiguration config = new MyAnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setClassLoader(webAppLoader);
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31true)));
context.getMetaData().setWebInfClassesResources(classes);
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
config.createServletContainerInitializerAnnotationHandlers(context, scis);
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testMetaDataCompleteSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
// test a 3.1 webapp with metadata-complete=false loads both server
// and webapp scis
context.setClassLoader(webAppLoader);
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31false)));
context.getMetaData().setWebInfClassesResources(classes);
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); // container
// path
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); // web-inf
// jar
// no
// web-fragment
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testRelativeOrderingWithSCIs() throws Exception
{
// test a 3.1 webapp with RELATIVE ORDERING loads sci from
// equivalent of WEB-INF/classes first as well as container path
ClassLoader old = Thread.currentThread().getContextClassLoader();
File orderedFragmentJar = new File(jarDir, "test-sci-with-ordering.jar");
assertTrue(orderedFragmentJar.exists());
URLClassLoader orderedLoader = new URLClassLoader(new URL[]{
orderedFragmentJar.toURI().toURL(), testSciJar.toURI().toURL(),
targetClasses.getURI().toURL(), webInfClasses.getURI().toURL()
},
containerLoader);
Thread.currentThread().setContextClassLoader(orderedLoader);
try
{
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setClassLoader(orderedLoader);
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web31true)));
RelativeOrdering ordering = new RelativeOrdering(context.getMetaData());
context.getMetaData().setOrdering(ordering);
context.getMetaData().addWebInfResource(Resource.newResource(orderedFragmentJar.toURI().toURL()));
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getMetaData().setWebInfClassesResources(classes);
context.getMetaData().orderFragments();
context.getServletContext().setEffectiveMajorVersion(3);
context.getServletContext().setEffectiveMinorVersion(1);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(4, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.ordering.AcmeServletContainerInitializer", scis.get(2).getClass().getName()); // first
assertEquals("com.acme.initializer.FooInitializer", scis.get(3).getClass().getName()); //other in ordering
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testDiscoveredFalseWithSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
//test 2.5 webapp with configurationDiscovered=false loads only server scis
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setConfigurationDiscovered(false);
context.setClassLoader(webAppLoader);
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web25)));
context.getMetaData().setWebInfClassesResources(classes);
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(2);
context.getServletContext().setEffectiveMinorVersion(5);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
for (ServletContainerInitializer s : scis)
{
//should not have any of the web-inf lib scis in here
assertFalse(s.getClass().getName().equals("com.acme.ordering.AcmeServletContainerInitializer"));
assertFalse(s.getClass().getName().equals("com.acme.initializer.FooInitializer"));
//NOTE: should also not have the web-inf classes scis in here either, but due to the
//way the test is set up, the sci we're pretending is in web-inf classes will actually
//NOT be loaded by the webapp's classloader, but rather by the junit classloader, so
//it looks as if it is a container class.
}
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
@Test
public void testDiscoveredTrueWithSCIs() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(webAppLoader);
try
{
//test 2.5 webapp with configurationDiscovered=true loads both server and webapp scis
AnnotationConfiguration config = new AnnotationConfiguration();
WebAppContext context = new WebAppContext();
List<ServletContainerInitializer> scis;
context.setConfigurationDiscovered(true);
context.setClassLoader(webAppLoader);
context.getMetaData().setWebDescriptor(new WebDescriptor(Resource.newResource(web25)));
context.getMetaData().setWebInfClassesResources(classes);
context.getMetaData().addWebInfResource(Resource.newResource(testSciJar.toURI().toURL()));
context.getServletContext().setEffectiveMajorVersion(2);
context.getServletContext().setEffectiveMinorVersion(5);
scis = config.getNonExcludedInitializers(context);
assertNotNull(scis);
assertEquals(3, scis.size());
assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path
assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf
assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
}