Search in sources :

Example 11 with RequestScope

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());
}
Also used : Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Aggregations

RequestScope (org.glassfish.jersey.process.internal.RequestScope)11 Test (org.junit.Test)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Supplier (java.util.function.Supplier)4 DisposableSupplier (org.glassfish.jersey.internal.inject.DisposableSupplier)4 ProcessingException (javax.ws.rs.ProcessingException)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)3 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 Singleton (javax.inject.Singleton)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 Type (java.lang.reflect.Type)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 GenericType (javax.ws.rs.core.GenericType)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 Providers (javax.ws.rs.ext.Providers)1 Binder (org.glassfish.jersey.internal.inject.Binder)1