-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable @ConstrainedTo on Features #4055
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/common/FeatureConstraintTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* Copyright (c) 2019 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.tests.e2e.common; | ||
|
||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.server.Uri; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import javax.ws.rs.ConstrainedTo; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Configuration; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Feature; | ||
import javax.ws.rs.core.FeatureContext; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class FeatureConstraintTest extends JerseyTest { | ||
|
||
public static AtomicInteger clientEnvironmentHitCount = new AtomicInteger(0); | ||
public static AtomicInteger serverEnvironmentHitCount = new AtomicInteger(0); | ||
public static AtomicInteger clientServerEnvironmentHitCount = new AtomicInteger(0); | ||
|
||
@Override | ||
protected Application configure() { | ||
return new ResourceConfig(PropagatedConfigResource.class, ServerConstrainedClassFeature.class, | ||
ClientConstrainedClassFeature.class, ClientServerConstrainedClassFeature.class) | ||
.register(new ServerConstrainedInstanceFeature()) | ||
.register(new ClientConstrainedInstanceFeature()) | ||
.register(new ClientServerConstrainedInstanceFeature()); | ||
} | ||
|
||
@ConstrainedTo(RuntimeType.SERVER) | ||
public static class ServerConstrainedClassFeature implements Feature { | ||
protected int increment = 10; | ||
@Override | ||
public boolean configure(FeatureContext context) { | ||
if (context.getConfiguration().getRuntimeType().equals(RuntimeType.CLIENT)) { | ||
clientEnvironmentHitCount.addAndGet(increment); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@ConstrainedTo(RuntimeType.SERVER) | ||
public static class ServerConstrainedInstanceFeature extends ServerConstrainedClassFeature { | ||
{ | ||
increment = 100; | ||
} | ||
}; | ||
|
||
@ConstrainedTo(RuntimeType.CLIENT) | ||
public static class ClientConstrainedClassFeature implements Feature { | ||
protected int increment = 10; | ||
@Override | ||
public boolean configure(FeatureContext context) { | ||
if (context.getConfiguration().getRuntimeType().equals(RuntimeType.SERVER)) { | ||
serverEnvironmentHitCount.addAndGet(increment); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
@ConstrainedTo(RuntimeType.CLIENT) | ||
public static class ClientConstrainedInstanceFeature extends ClientConstrainedClassFeature { | ||
{ | ||
increment = 100; | ||
} | ||
}; | ||
|
||
public static class ClientServerConstrainedClassFeature implements Feature { | ||
protected int increment = 10; | ||
@Override | ||
public boolean configure(FeatureContext context) { | ||
if (context.getConfiguration().getRuntimeType().equals(RuntimeType.SERVER)) { | ||
clientServerEnvironmentHitCount.addAndGet(increment); | ||
} | ||
if (context.getConfiguration().getRuntimeType().equals(RuntimeType.CLIENT)) { | ||
clientServerEnvironmentHitCount.addAndGet(100 * increment); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public static class ClientServerConstrainedInstanceFeature extends ClientServerConstrainedClassFeature { | ||
{ | ||
increment = 100; | ||
} | ||
}; | ||
|
||
@Path("/") | ||
public static class PropagatedConfigResource { | ||
@Uri("/isRegistered") | ||
WebTarget target; | ||
|
||
@Path("isRegistered") | ||
@GET | ||
public boolean isRegisteredOnServer(@Context Configuration config) { | ||
return config.isRegistered(ServerConstrainedClassFeature.class) | ||
&& config.isRegistered(ServerConstrainedInstanceFeature.class) | ||
&& config.isRegistered(ClientConstrainedClassFeature.class) | ||
&& config.isRegistered(ClientConstrainedInstanceFeature.class) | ||
&& config.isRegistered(ClientServerConstrainedClassFeature.class) | ||
&& config.isRegistered(ClientServerConstrainedInstanceFeature.class); | ||
} | ||
|
||
@Path("isInherited") | ||
@GET | ||
public boolean isInheritedInInjectedClientConfig() { | ||
final Configuration config = target.getConfiguration(); | ||
return isRegisteredOnServer(config); | ||
} | ||
|
||
@Path("featureConfigurationNotInvoked") | ||
@GET | ||
public boolean featureConfigurationNotInvoked() { | ||
return target | ||
.register(ServerConstrainedClassFeature.class) | ||
.register(new ServerConstrainedInstanceFeature()) | ||
.register(ClientConstrainedClassFeature.class) | ||
.register(new ClientConstrainedInstanceFeature()) | ||
.register(ClientServerConstrainedClassFeature.class) | ||
.register(new ClientServerConstrainedInstanceFeature()) | ||
.request().get().readEntity(boolean.class); | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
assertThat("*Constrained*Feature must be registered in a server configuration", | ||
target("isRegistered") | ||
.register(ServerConstrainedClassFeature.class) | ||
.register(new ServerConstrainedInstanceFeature()) | ||
.register(ClientConstrainedClassFeature.class) | ||
.register(new ClientConstrainedInstanceFeature()) | ||
.register(ClientServerConstrainedClassFeature.class) | ||
.register(new ClientServerConstrainedInstanceFeature()) | ||
.request().get().readEntity(boolean.class), | ||
is(true)); | ||
|
||
assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); | ||
assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); | ||
assertThat("ClientSever Features should have been configured", clientServerEnvironmentHitCount.get(), is(11110)); | ||
clientServerEnvironmentHitCount.set(0); //reset configuration invoked on a server, it won't happen again | ||
|
||
assertThat("*Constrained*Feature must be in an application classes set", | ||
target("isInherited").request().get().readEntity(boolean.class), | ||
is(true)); | ||
|
||
assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); | ||
assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); | ||
assertThat("ClientSever Features should not have been configured", clientServerEnvironmentHitCount.get(), is(0)); | ||
|
||
assertThat("ServerConstrainedFeature must be in an application classes set", | ||
target("featureConfigurationNotInvoked") | ||
.register(ServerConstrainedClassFeature.class) | ||
.register(new ServerConstrainedInstanceFeature()) | ||
.register(ClientConstrainedClassFeature.class) | ||
.register(new ClientConstrainedInstanceFeature()) | ||
.register(ClientServerConstrainedClassFeature.class) | ||
.register(new ClientServerConstrainedInstanceFeature()) | ||
.request().get().readEntity(boolean.class), | ||
is(true)); | ||
|
||
assertThat("Server Features should not have been configured the Client", clientEnvironmentHitCount.get(), is(0)); | ||
assertThat("Client Features should not have been configured the Server", serverEnvironmentHitCount.get(), is(0)); | ||
assertThat("ClientSever Features should have been configured", clientServerEnvironmentHitCount.get(), is(22000)); | ||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check should be performed before line 674, so we do not create an injected instance of a Feature that should not exist in this type of runtime.