-
Notifications
You must be signed in to change notification settings - Fork 340
/
AsyncApplicationBuildingTest.java
259 lines (204 loc) · 8.47 KB
/
AsyncApplicationBuildingTest.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
/*
* Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.server;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyReader;
import javax.inject.Inject;
import javax.inject.Provider;
import org.glassfish.jersey.process.Inflector;
import org.glassfish.jersey.server.model.Resource;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for creating an application with asynchronously handled request processing
* via {@link Resource}'s programmatic API.
*
* @author Marek Potociar
*/
public class AsyncApplicationBuildingTest {
private static final String BASE_URI = "http://localhost:8080/base/";
private static class AsyncInflector implements Inflector<ContainerRequestContext, Response> {
@Inject
private Provider<AsyncContext> asyncContextProvider;
private final String responseContent;
public AsyncInflector() {
this.responseContent = "DEFAULT";
}
public AsyncInflector(String responseContent) {
this.responseContent = responseContent;
}
@Override
public Response apply(final ContainerRequestContext req) {
// Suspend current request
final AsyncContext asyncContext = asyncContextProvider.get();
asyncContext.suspend();
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace(System.err);
}
// Returning will enter the suspended request
asyncContext.resume(Response.ok().entity(responseContent).build());
}
});
return null;
}
}
public ApplicationHandler setupApplication1() {
final ResourceConfig rc = new ResourceConfig();
Resource.Builder rb;
rb = Resource.builder("a/b/c");
rb.addMethod("GET").handledBy(new AsyncInflector("A-B-C"));
rc.registerResources(rb.build());
rb = Resource.builder("a/b/d");
rb.addMethod("GET").handledBy(new AsyncInflector("A-B-D"));
rc.registerResources(rb.build());
return new ApplicationHandler(rc);
}
@Test
public void testAsyncApp1() throws InterruptedException, ExecutionException {
ContainerRequest req = RequestContextBuilder.from(
BASE_URI, BASE_URI + "a/b/c", "GET").build();
Future<ContainerResponse> res = setupApplication1().apply(req);
assertEquals("A-B-C", res.get().getEntity());
}
@Test
public void testAsyncApp2() throws InterruptedException, ExecutionException {
ContainerRequest req = RequestContextBuilder.from(
BASE_URI, BASE_URI + "a/b/d", "GET").build();
Future<ContainerResponse> res = setupApplication1().apply(req);
assertEquals("A-B-D", res.get().getEntity());
}
@Path("/")
public static class ResourceA {
@GET
public String get() {
return "get!";
}
}
@Test
public void testappBuilderClasses() throws InterruptedException, ExecutionException {
final ResourceConfig resourceConfig = new ResourceConfig(ResourceA.class);
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();
assertEquals("get!", application.apply(req).get().getEntity());
}
@Test
public void testEmptyAppCreationPasses() throws InterruptedException, ExecutionException {
final ResourceConfig resourceConfig = new ResourceConfig();
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();
assertEquals(404, application.apply(req).get().getStatus());
}
@Test
public void testAppBuilderJaxRsApplication() throws InterruptedException, ExecutionException {
Application jaxRsApplication = new Application() {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(ResourceA.class);
return set;
}
@Override
public Set<Object> getSingletons() {
return super.getSingletons();
}
};
final ApplicationHandler application = new ApplicationHandler(jaxRsApplication);
ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();
assertEquals("get!", application.apply(req).get().getEntity());
}
@Path("/")
public static class ResourceB {
@Context
javax.ws.rs.core.Application application;
@GET
public String get() {
assertTrue(application != null);
assertTrue(application.getClasses().contains(ResourceB.class));
assertTrue(application.getSingletons().size() > 0);
assertTrue(application.getSingletons().iterator().next().getClass().equals(ResourceAReader.class));
return "get!";
}
}
public static class ResourceAReader implements MessageBodyReader<ResourceA> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return false;
}
@Override
public ResourceA readFrom(Class<ResourceA> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
return null;
}
}
@Test
public void testJaxrsApplicationInjection() throws InterruptedException, ExecutionException {
final ResourceConfig resourceConfig = new ResourceConfig(ResourceB.class)
.registerInstances(new ResourceAReader());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
ContainerRequest req = RequestContextBuilder.from(BASE_URI, BASE_URI, "GET").build();
assertEquals("get!", application.apply(req).get().getEntity());
}
@Path("/")
@Consumes("text/plain")
public static class ErrornousResource {
@POST
@Produces("text/plain")
public String postOne(String s) {
return "One";
}
@POST
@Produces("text/plain")
public String postTwo(String s) {
return "Two";
}
}
@Test
public void testDeploymentFailsForAmbiguousResource() {
final ResourceConfig resourceConfig = new ResourceConfig(ErrornousResource.class);
try {
ApplicationHandler server = new ApplicationHandler(resourceConfig);
assertTrue(false, "Jersey server initialization should have failed: " + server);
} catch (Exception e) {
}
}
}