Search in sources :

Example 6 with ForeignDescriptor

use of org.glassfish.jersey.internal.inject.ForeignDescriptor in project jersey by jersey.

the class RequestScopeTest method testScopeWithTwoInternalTasks.

@Test
public void testScopeWithTwoInternalTasks() throws Exception {
    final RequestScope requestScope = new RequestScope();
    assertNull(requestScope.suspendCurrent());
    ForeignDescriptor inhab = ForeignDescriptor.wrap(new TestProvider("a"));
    final Instance instance = requestScope.runInScope(new Callable<Instance>() {

        @Override
        public Instance call() throws Exception {
            final Instance internalInstance = requestScope.suspendCurrent();
            final Instance anotherInstance = requestScope.runInScope(new Callable<Instance>() {

                @Override
                public Instance call() throws Exception {
                    final Instance currentInstance = requestScope.suspendCurrent();
                    assertTrue(!currentInstance.equals(internalInstance));
                    currentInstance.put(inhab, "1");
                    return currentInstance;
                }
            });
            assertTrue(!anotherInstance.equals(internalInstance));
            assertEquals("1", anotherInstance.get(inhab));
            anotherInstance.release();
            assertNull(anotherInstance.get(inhab));
            return internalInstance;
        }
    });
    instance.release();
    assertNull(instance.get(inhab));
}
Also used : Instance(org.glassfish.jersey.process.internal.RequestScope.Instance) ForeignDescriptor(org.glassfish.jersey.internal.inject.ForeignDescriptor) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 7 with ForeignDescriptor

use of org.glassfish.jersey.internal.inject.ForeignDescriptor in project jersey by jersey.

the class RequestScopeTest method testScopeWithImplicitInstance.

@Test
public void testScopeWithImplicitInstance() throws Exception {
    final RequestScope requestScope = new RequestScope();
    assertNull(requestScope.suspendCurrent());
    ForeignDescriptor inhab = ForeignDescriptor.wrap(new TestProvider("a"));
    final Instance instance = requestScope.runInScope(new Callable<Instance>() {

        @Override
        public Instance call() throws Exception {
            final Instance internalInstance = requestScope.suspendCurrent();
            assertNull(internalInstance.get(inhab));
            internalInstance.put(inhab, "1");
            assertEquals("1", internalInstance.get(inhab));
            return internalInstance;
        }
    });
    assertEquals("1", instance.get(inhab));
    instance.release();
    assertNull(instance.get(inhab));
}
Also used : Instance(org.glassfish.jersey.process.internal.RequestScope.Instance) ForeignDescriptor(org.glassfish.jersey.internal.inject.ForeignDescriptor) Test(org.junit.Test)

Aggregations

ForeignDescriptor (org.glassfish.jersey.internal.inject.ForeignDescriptor)7 Instance (org.glassfish.jersey.process.internal.RequestScope.Instance)5 Test (org.junit.Test)5 Callable (java.util.concurrent.Callable)1 InjecteeImpl (org.glassfish.jersey.internal.inject.InjecteeImpl)1