|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.tests.api; |
| 18 | + |
| 19 | +import org.glassfish.jersey.server.ServerProperties; |
| 20 | +import org.glassfish.jersey.test.JerseyTest; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import javax.ws.rs.GET; |
| 25 | +import javax.ws.rs.Path; |
| 26 | +import javax.ws.rs.container.ContainerRequestContext; |
| 27 | +import javax.ws.rs.container.ContainerRequestFilter; |
| 28 | +import javax.ws.rs.core.Application; |
| 29 | +import javax.ws.rs.core.Response; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.concurrent.atomic.AtomicInteger; |
| 36 | + |
| 37 | +public class ApplicationCachingTest extends JerseyTest { |
| 38 | + |
| 39 | + private static AtomicInteger singletonCounter = new AtomicInteger(0); |
| 40 | + |
| 41 | + public static class ApplicationCachingTestFilter implements ContainerRequestFilter { |
| 42 | + @Override |
| 43 | + public void filter(ContainerRequestContext requestContext) throws IOException { |
| 44 | + |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Path("/") |
| 49 | + public static class ApplicationCachingTestResource { |
| 50 | + @GET |
| 51 | + public String get() { |
| 52 | + return "GET"; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static class OneTimeCalledApplication extends Application { |
| 57 | + @Override |
| 58 | + public Map<String, Object> getProperties() { |
| 59 | + Map<String, Object> map = new HashMap<>(); |
| 60 | + map.put(ServerProperties.WADL_FEATURE_DISABLE, true); |
| 61 | + return map; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Set<Object> getSingletons() { |
| 66 | + singletonCounter.incrementAndGet(); |
| 67 | + return Collections.singleton(new ApplicationCachingTestFilter()); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Set<Class<?>> getClasses() { |
| 72 | + return Collections.singleton(ApplicationCachingTestResource.class); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected Application configure() { |
| 78 | + return new OneTimeCalledApplication(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testOneTimeCalled() { |
| 83 | + try (Response r = target().request().get()) { |
| 84 | + Assert.assertEquals(200, r.getStatus()); |
| 85 | + } |
| 86 | + Assert.assertEquals(1, singletonCounter.get()); |
| 87 | + } |
| 88 | +} |
0 commit comments