Search in sources :

Example 1 with Instance

use of org.glassfish.jersey.process.internal.RequestScope.Instance in project jersey by jersey.

the class RequestScopeTest method testMultipleGetInstanceCalls.

@Test
public void testMultipleGetInstanceCalls() 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();
            internalInstance.put(inhab, "1");
            requestScope.suspendCurrent();
            requestScope.suspendCurrent();
            requestScope.suspendCurrent();
            requestScope.suspendCurrent();
            return internalInstance;
        }
    });
    assertEquals("1", instance.get(inhab));
    instance.release();
    assertEquals("1", instance.get(inhab));
    instance.release();
    assertEquals("1", instance.get(inhab));
    instance.release();
    assertEquals("1", instance.get(inhab));
    instance.release();
    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)

Example 2 with Instance

use of org.glassfish.jersey.process.internal.RequestScope.Instance in project jersey by jersey.

the class RequestScopeTest method testScopeWithCreatedInstance.

@Test
public void testScopeWithCreatedInstance() {
    final RequestScope requestScope = new RequestScope();
    assertNull(requestScope.suspendCurrent());
    final Instance instance = requestScope.createInstance();
    ForeignDescriptor inhab = ForeignDescriptor.wrap(new TestProvider("a"));
    instance.put(inhab, "1");
    requestScope.runInScope(instance, new Runnable() {

        @Override
        public void run() {
            assertEquals("1", instance.get(inhab));
            instance.release();
            assertEquals("1", instance.get(inhab));
        }
    });
    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)

Example 3 with Instance

use of org.glassfish.jersey.process.internal.RequestScope.Instance in project jersey by jersey.

the class RequestScopeTest method testScopeReleaseInsideScope.

@Test
public void testScopeReleaseInsideScope() {
    final RequestScope requestScope = new RequestScope();
    assertNull(requestScope.suspendCurrent());
    final Instance instance = requestScope.createInstance();
    ForeignDescriptor inhab = ForeignDescriptor.wrap(new TestProvider("a"));
    instance.put(inhab, "1");
    requestScope.runInScope(instance, new Runnable() {

        @Override
        public void run() {
            final Instance internalInstance = requestScope.suspendCurrent();
            assertEquals(internalInstance, instance);
            assertEquals("1", instance.get(inhab));
            instance.release();
            assertEquals("1", instance.get(inhab));
        }
    });
    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)

Example 4 with Instance

use of org.glassfish.jersey.process.internal.RequestScope.Instance 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 5 with Instance

use of org.glassfish.jersey.process.internal.RequestScope.Instance 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)5 Instance (org.glassfish.jersey.process.internal.RequestScope.Instance)5 Test (org.junit.Test)5 Callable (java.util.concurrent.Callable)1