Search in sources :

Example 1 with RequestScope

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));
        }
    });
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with RequestScope

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));
        }
    });
}
Also used : Singleton(javax.inject.Singleton) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) RequestScoped(org.glassfish.jersey.process.internal.RequestScoped) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 3 with RequestScope

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());
}
Also used : 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)

Example 4 with RequestScope

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());
}
Also used : 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)

Example 5 with RequestScope

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());
}
Also used : 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