|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 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.e2e.common; |
| 18 | + |
| 19 | +import org.glassfish.jersey.server.ResourceConfig; |
| 20 | +import org.glassfish.jersey.server.Uri; |
| 21 | +import org.glassfish.jersey.test.JerseyTest; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.hamcrest.CoreMatchers.is; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | + |
| 27 | +import javax.ws.rs.ConstrainedTo; |
| 28 | +import javax.ws.rs.GET; |
| 29 | +import javax.ws.rs.Path; |
| 30 | +import javax.ws.rs.RuntimeType; |
| 31 | +import javax.ws.rs.client.WebTarget; |
| 32 | +import javax.ws.rs.core.Application; |
| 33 | +import javax.ws.rs.core.Configuration; |
| 34 | +import javax.ws.rs.core.Context; |
| 35 | +import javax.ws.rs.core.Feature; |
| 36 | +import javax.ws.rs.core.FeatureContext; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
| 38 | + |
| 39 | +public class FeatureConstraintTest extends JerseyTest { |
| 40 | + |
| 41 | + public static AtomicInteger clientEnvironmentHitCount = new AtomicInteger(0); |
| 42 | + public static AtomicInteger serverEnvironmentHitCount = new AtomicInteger(0); |
| 43 | + public static AtomicInteger clientServerEnvironmentHitCount = new AtomicInteger(0); |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Application configure() { |
| 47 | + return new ResourceConfig(PropagatedConfigResource.class, ServerConstrainedClassFeature.class, |
| 48 | + ClientConstrainedClassFeature.class, ClientServerConstrainedClassFeature.class) |
| 49 | + .register(new ServerConstrainedInstanceFeature()) |
| 50 | + .register(new ClientConstrainedInstanceFeature()) |
| 51 | + .register(new ClientServerConstrainedInstanceFeature()); |
| 52 | + } |
| 53 | + |
| 54 | + @ConstrainedTo(RuntimeType.SERVER) |
| 55 | + public static class ServerConstrainedClassFeature implements Feature { |
| 56 | + protected int increment = 10; |
| 57 | + @Override |
| 58 | + public boolean configure(FeatureContext context) { |
| 59 | + if (context.getConfiguration().getRuntimeType().equals(RuntimeType.CLIENT)) { |
| 60 | + clientEnvironmentHitCount.set(clientEnvironmentHitCount.get() + increment); |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @ConstrainedTo(RuntimeType.SERVER) |
| 67 | + public static class ServerConstrainedInstanceFeature extends ServerConstrainedClassFeature { |
| 68 | + { |
| 69 | + increment = 100; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + @ConstrainedTo(RuntimeType.CLIENT) |
| 74 | + public static class ClientConstrainedClassFeature implements Feature { |
| 75 | + protected int increment = 10; |
| 76 | + @Override |
| 77 | + public boolean configure(FeatureContext context) { |
| 78 | + if (context.getConfiguration().getRuntimeType().equals(RuntimeType.SERVER)) { |
| 79 | + serverEnvironmentHitCount.set(serverEnvironmentHitCount.get() + increment); |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @ConstrainedTo(RuntimeType.CLIENT) |
| 86 | + public static class ClientConstrainedInstanceFeature extends ClientConstrainedClassFeature { |
| 87 | + { |
| 88 | + increment = 100; |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + public static class ClientServerConstrainedClassFeature implements Feature { |
| 93 | + protected int increment = 10; |
| 94 | + @Override |
| 95 | + public boolean configure(FeatureContext context) { |
| 96 | + if (context.getConfiguration().getRuntimeType().equals(RuntimeType.SERVER)) { |
| 97 | + clientServerEnvironmentHitCount.set(clientServerEnvironmentHitCount.get() + increment); |
| 98 | + } |
| 99 | + if (context.getConfiguration().getRuntimeType().equals(RuntimeType.CLIENT)) { |
| 100 | + clientServerEnvironmentHitCount.set(clientServerEnvironmentHitCount.get() + (100 * increment)); |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static class ClientServerConstrainedInstanceFeature extends ClientServerConstrainedClassFeature { |
| 107 | + { |
| 108 | + increment = 100; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + @Path("/") |
| 113 | + public static class PropagatedConfigResource { |
| 114 | + @Uri("/isRegistered") |
| 115 | + WebTarget target; |
| 116 | + |
| 117 | + @Path("isRegistered") |
| 118 | + @GET |
| 119 | + public boolean isRegisteredOnServer(@Context Configuration config) { |
| 120 | + return config.isRegistered(ServerConstrainedClassFeature.class) |
| 121 | + && config.isRegistered(ServerConstrainedInstanceFeature.class) |
| 122 | + && config.isRegistered(ClientConstrainedClassFeature.class) |
| 123 | + && config.isRegistered(ClientConstrainedInstanceFeature.class) |
| 124 | + && config.isRegistered(ClientServerConstrainedClassFeature.class) |
| 125 | + && config.isRegistered(ClientServerConstrainedInstanceFeature.class); |
| 126 | + } |
| 127 | + |
| 128 | + @Path("isInherited") |
| 129 | + @GET |
| 130 | + public boolean isInheritedInInjectedClientConfig() { |
| 131 | + final Configuration config = target.getConfiguration(); |
| 132 | + return isRegisteredOnServer(config); |
| 133 | + } |
| 134 | + |
| 135 | + @Path("featureConfigurationNotInvoked") |
| 136 | + @GET |
| 137 | + public boolean featureConfigurationNotInvoked() { |
| 138 | + boolean ret = target |
| 139 | + .register(ServerConstrainedClassFeature.class) |
| 140 | + .register(new ServerConstrainedInstanceFeature()) |
| 141 | + .register(ClientConstrainedClassFeature.class) |
| 142 | + .register(new ClientConstrainedInstanceFeature()) |
| 143 | + .register(ClientServerConstrainedClassFeature.class) |
| 144 | + .register(new ClientServerConstrainedInstanceFeature()) |
| 145 | + .request().get().readEntity(boolean.class); |
| 146 | + return ret; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void test() { |
| 152 | + assertThat("*Constrained*Feature must be registered in a server configuration", |
| 153 | + target("isRegistered") |
| 154 | + .register(ServerConstrainedClassFeature.class) |
| 155 | + .register(new ServerConstrainedInstanceFeature()) |
| 156 | + .register(ClientConstrainedClassFeature.class) |
| 157 | + .register(new ClientConstrainedInstanceFeature()) |
| 158 | + .register(ClientServerConstrainedClassFeature.class) |
| 159 | + .register(new ClientServerConstrainedInstanceFeature()) |
| 160 | + .request().get().readEntity(boolean.class), |
| 161 | + is(true)); |
| 162 | + |
| 163 | + assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); |
| 164 | + assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); |
| 165 | + assertThat("ClientSever Features should have been configured", clientServerEnvironmentHitCount.get(), is(11110)); |
| 166 | + clientServerEnvironmentHitCount.set(0); //reset configuration invoked on a server, it won't happen again |
| 167 | + |
| 168 | + assertThat("*Constrained*Feature must be in an application classes set", |
| 169 | + target("isInherited").request().get().readEntity(boolean.class), |
| 170 | + is(true)); |
| 171 | + |
| 172 | + assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); |
| 173 | + assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); |
| 174 | + assertThat("ClientSever Features should not have been configured", clientServerEnvironmentHitCount.get(), is(0)); |
| 175 | + |
| 176 | + assertThat("ServerConstrainedFeature must be in an application classes set", |
| 177 | + target("featureConfigurationNotInvoked") |
| 178 | + .register(ServerConstrainedClassFeature.class) |
| 179 | + .register(new ServerConstrainedInstanceFeature()) |
| 180 | + .register(ClientConstrainedClassFeature.class) |
| 181 | + .register(new ClientConstrainedInstanceFeature()) |
| 182 | + .register(ClientServerConstrainedClassFeature.class) |
| 183 | + .register(new ClientServerConstrainedInstanceFeature()) |
| 184 | + .request().get().readEntity(boolean.class), |
| 185 | + is(true)); |
| 186 | + |
| 187 | + assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); |
| 188 | + assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); |
| 189 | + assertThat("ClientSever Features should have been configured", clientServerEnvironmentHitCount.get(), is(22000)); |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | +} |
0 commit comments