use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class CustomInjectablesApplicationTest method plainHK2DynamicTest.
@Test
public void plainHK2DynamicTest() throws Exception {
InjectionManager injectionManager = Injections.createInjectionManager(new RequestScope.Binder());
Binder binder = new AbstractBinder() {
@Override
protected void configure() {
bindAsContract(MyInjectablePerRequest.class).in(RequestScoped.class);
bindAsContract(MyInjectableSingleton.class).in(Singleton.class);
}
};
injectionManager.register(binder);
final RequestScope requestScope = injectionManager.getInstance(RequestScope.class);
final MyInjectableSingleton myInjectableSingleton = injectionManager.getInstance(MyInjectableSingleton.class);
assertEquals(myInjectableSingleton, injectionManager.getInstance(MyInjectableSingleton.class));
final MyInjectablePerRequest myInjectablePerRequest = requestScope.runInScope(new Callable<MyInjectablePerRequest>() {
@Override
public MyInjectablePerRequest call() throws Exception {
final MyInjectablePerRequest myInjectablePerRequest = injectionManager.getInstance(MyInjectablePerRequest.class);
assertEquals(myInjectablePerRequest, injectionManager.getInstance(MyInjectablePerRequest.class));
return myInjectablePerRequest;
}
});
requestScope.runInScope(new Runnable() {
@Override
public void run() {
assertNotSame(myInjectablePerRequest, injectionManager.getInstance(MyInjectablePerRequest.class));
}
});
}
use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class CustomInjectablesApplicationTest method plainHK2Test.
@Test
public void plainHK2Test() throws Exception {
final InjectionManager locator = Injections.createInjectionManager(new RequestScope.Binder(), new AbstractBinder() {
@Override
protected void configure() {
bindAsContract(MyInjectablePerRequest.class).in(RequestScoped.class);
bindAsContract(MyInjectableSingleton.class).in(Singleton.class);
}
});
final RequestScope requestScope = locator.getInstance(RequestScope.class);
final MyInjectableSingleton myInjectableSingleton = locator.getInstance(MyInjectableSingleton.class);
assertEquals(myInjectableSingleton, locator.getInstance(MyInjectableSingleton.class));
final MyInjectablePerRequest myInjectablePerRequest = requestScope.runInScope(new Callable<MyInjectablePerRequest>() {
@Override
public MyInjectablePerRequest call() throws Exception {
final MyInjectablePerRequest myInjectablePerRequest = locator.getInstance(MyInjectablePerRequest.class);
assertEquals(myInjectablePerRequest, locator.getInstance(MyInjectablePerRequest.class));
return myInjectablePerRequest;
}
});
requestScope.runInScope(new Runnable() {
@Override
public void run() {
assertNotSame(myInjectablePerRequest, locator.getInstance(MyInjectablePerRequest.class));
}
});
}
use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class DisposableSupplierTest method testDisposeSingletonSupplierMultiRequestScoped.
/**
* Tests that inherited request scoped is also cleaned by disposing the objects.
*/
@Test
public void testDisposeSingletonSupplierMultiRequestScoped() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> firstSupplier = new AtomicReference<>();
AtomicReference<Supplier<String>> secondSupplier = new AtomicReference<>();
request.runInScope(() -> {
Supplier<String> supplier1 = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
firstSupplier.set(supplier1);
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier2 = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
secondSupplier.set(supplier2);
Object instance2 = injectionManager.getInstance(String.class);
// 1-2 because the same static class is used in inherited runInScope
assertEquals("1", instance2);
});
});
Supplier<String> cleanedSupplier1 = firstSupplier.get();
Supplier<String> cleanedSupplier2 = secondSupplier.get();
// Next should be 1-1
assertEquals("1", cleanedSupplier1.get());
// 1-2 because the same static class is used but the instance is cleaned.
assertEquals("1", cleanedSupplier2.get());
}
use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class DisposableSupplierTest method testDisposeSingletonSupplierRequestScopedInstance.
@Test
public void testDisposeSingletonSupplierRequestScopedInstance() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class, Singleton.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
atomicSupplier.set(supplier);
// All instances should be the same because they are request scoped.
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
Object instance2 = injectionManager.getInstance(String.class);
assertEquals("1", instance2);
});
Supplier<String> cleanedSupplier = atomicSupplier.get();
// Next should be 1-1
assertEquals("1", cleanedSupplier.get());
}
use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class DisposableSupplierTest method testDisposePerLookupSupplierRequestScopedInstance.
/**
* Tests that object created in request scope is disposing at the time of ending the scope.
*/
@Test
public void testDisposePerLookupSupplierRequestScopedInstance() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
atomicSupplier.set(supplier);
// All instances should be the same because they are request scoped.
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
Object instance2 = injectionManager.getInstance(String.class);
assertEquals("1", instance2);
});
Supplier<String> cleanedSupplier = atomicSupplier.get();
// Next should be 1
assertEquals("1", cleanedSupplier.get());
}
Aggregations