Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Last active September 27, 2018 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamsotha/d7f782fff4a9be7b04f80fdde416294e to your computer and use it in GitHub Desktop.
Save psamsotha/d7f782fff4a9be7b04f80fdde416294e to your computer and use it in GitHub Desktop.
import java.util.function.Supplier;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
// import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* https://stackoverflow.com/q/52513611/2587435
*
* Reproduces problem for Stack Overflow question. Seems the problem is with Jersey and not HK2.
* If we use the Jersey `org.glassfish.jersey.internal.inject.AbstractBinder`, when we use
* `ranked()` with the `bindFactory()`, it does not work. But if we switch over to using the HK2
* `org.glassfish.hk2.utilities.binding.AbstractBinder` it works.
*
* In this test, we are testing that the service either has the message "Test Service" or
* "Production Service". If the ranking is working correctly, it should show "Test Service" as
* we want to override the production service during test.
*
* The default setup is using the HK2 `AbstractBinder`. To see the Jersey one, comment out the
* HK2 `AbstractBinder` import and uncomment the Jersey import. Also uncomment the commented out
* `bindFactory()` in both `AbstractBinders` and comment out the one that is not commented.
*
* Two required dependency to run this test.
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
* <version>2.27</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.glassfish.jersey.inject</groupId>
* <artifactId>jersey-hk2</artifactId>
* <version>2.27</version>
* </dependency>
*
*/
public class FactoryRankTest extends JerseyTest {
public static class Service {
public String msg;
}
public static class ProductionServiceSupplier implements Supplier<Service> {
@Override
public Service get() {
Service service = new Service();
service.msg = "Production Service";
return service;
}
}
public static class ProductionServiceFactory implements Factory<Service> {
@Override
public Service provide() {
Service service = new Service();
service.msg = "Production Service";
return service;
}
@Override
public void dispose(Service service) {
}
}
@Path("test")
public static class TestResource {
@Inject
private Service service;
@GET
public String get() {
return service.msg;
}
}
public static class ProductionConfig extends ResourceConfig {
public ProductionConfig() {
register(TestResource.class);
register(new AbstractBinder() {
@Override
protected void configure() {
// bindFactory(ProductionServiceSupplier.class)
// .to(Service.class)
// .in(Singleton.class);
bindFactory(ProductionServiceFactory.class)
.to(Service.class)
.in(Singleton.class);
}
});
}
}
public static class TestServiceSupplier implements Supplier<Service> {
@Override
public Service get() {
Service service = new Service();
service.msg = "Test Service";
return service;
}
}
public static class TestServiceFactory implements Factory<Service> {
@Override
public Service provide() {
Service service = new Service();
service.msg = "Test Service";
return service;
}
@Override
public void dispose(Service service) {
}
}
public static class TestBinder extends AbstractBinder {
@Override
protected void configure() {
// bindFactory(TestServiceSupplier.class)
// .to(Service.class)
// .in(Singleton.class)
// .ranked(1);
bindFactory(TestServiceFactory.class)
.to(Service.class)
.in(Singleton.class)
.ranked(1);
}
}
@Override
public ResourceConfig configure() {
return new ProductionConfig()
.register(new TestBinder());
}
@Test
public void testIt() {
Response res = target("test")
.request()
.get();
String result = res.readEntity(String.class);
System.out.println(result);
assertEquals("Test Service", result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment