use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.
the class DisposableSupplierTest method testDisposeComposedObjectWithPerLookupFields.
/**
* PerLookup fields are not disposed therefore they should never be used as a DisposedSupplier because the field stay in
* {@link SupplierFactoryBridge} forever.
*/
@Test
public void testDisposeComposedObjectWithPerLookupFields() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class, Singleton.class).to(String.class);
binder.bindAsContract(ComposedObject.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.
ComposedObject instance = injectionManager.getInstance(ComposedObject.class);
assertEquals("1", instance.first);
assertEquals("2", instance.second);
assertEquals("3", instance.third);
});
Supplier<String> cleanedSupplier = atomicSupplier.get();
// Next should be 4
assertEquals("4", cleanedSupplier.get());
}
Aggregations