Search in sources :

Example 1 with RequestContextController

use of javax.enterprise.context.control.RequestContextController in project quarkus by quarkusio.

the class RequestContextTest method testRequestContextEvents.

@Test
public void testRequestContextEvents() {
    // reset counters since other tests might have triggered it already
    ContextObserver.reset();
    // firstly test manual activation
    ArcContainer arc = Arc.container();
    ManagedContext requestContext = arc.requestContext();
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    requestContext.activate();
    assertEquals(1, ContextObserver.initializedObserved);
    assertEquals(0, ContextObserver.beforeDestroyedObserved);
    assertEquals(0, ContextObserver.destroyedObserved);
    // dummy check that bean is available
    arc.instance(Controller.class).get().getId();
    requestContext.terminate();
    assertEquals(1, ContextObserver.initializedObserved);
    assertEquals(1, ContextObserver.beforeDestroyedObserved);
    assertEquals(1, ContextObserver.destroyedObserved);
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    // now test the same but activate context via interceptor (@ActivateRequestContext)
    arc.instance(ControllerClient.class).get().getControllerId();
    assertEquals(2, ContextObserver.initializedObserved);
    assertEquals(2, ContextObserver.beforeDestroyedObserved);
    assertEquals(2, ContextObserver.destroyedObserved);
    // lastly, use RequestContextController bean to handle the context
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    RequestContextController controller = arc.instance(RequestContextController.class).get();
    controller.activate();
    assertEquals(3, ContextObserver.initializedObserved);
    assertEquals(2, ContextObserver.beforeDestroyedObserved);
    assertEquals(2, ContextObserver.destroyedObserved);
    // dummy check that bean is available
    arc.instance(Controller.class).get().getId();
    controller.deactivate();
    assertEquals(3, ContextObserver.initializedObserved);
    assertEquals(3, ContextObserver.beforeDestroyedObserved);
    assertEquals(3, ContextObserver.destroyedObserved);
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ManagedContext(io.quarkus.arc.ManagedContext) RequestContextController(javax.enterprise.context.control.RequestContextController) Test(org.junit.jupiter.api.Test)

Example 2 with RequestContextController

use of javax.enterprise.context.control.RequestContextController in project quarkus by quarkusio.

the class RequestContextTest method testRequestContextController.

@Test
public void testRequestContextController() {
    Controller.DESTROYED.set(false);
    ArcContainer arc = Arc.container();
    RequestContextController controller = Arc.container().instance(RequestContextController.class).get();
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    controller.activate();
    assertFalse(Controller.DESTROYED.get());
    Controller controller1 = arc.instance(Controller.class).get();
    Controller controller2 = arc.instance(Controller.class).get();
    String controller2Id = controller2.getId();
    assertEquals(controller1.getId(), controller2Id);
    controller.deactivate();
    assertTrue(Controller.DESTROYED.get());
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    // Id must be different in a different request
    Controller.DESTROYED.set(false);
    controller.activate();
    assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
    controller.deactivate();
    assertTrue(Controller.DESTROYED.get());
    Controller.DESTROYED.set(false);
    controller.activate();
    assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
    controller.deactivate();
    assertTrue(Controller.DESTROYED.get());
    // @ActivateRequestContext
    Controller.DESTROYED.set(false);
    ControllerClient client = arc.instance(ControllerClient.class).get();
    assertNotEquals(controller2Id, client.getControllerId());
    assertTrue(Controller.DESTROYED.get());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) RequestContextController(javax.enterprise.context.control.RequestContextController) RequestContextController(javax.enterprise.context.control.RequestContextController) Test(org.junit.jupiter.api.Test)

Example 3 with RequestContextController

use of javax.enterprise.context.control.RequestContextController in project openwebbeans by apache.

the class ActivateRequestContextInterceptorBean method intercept.

@Override
public Object intercept(final InterceptionType type, final InterceptorClass instance, final InvocationContext ctx) throws Exception {
    final RequestContextController contextController = new OwbRequestContextController(webBeansContext);
    contextController.activate();
    try {
        return ctx.proceed();
    } finally {
        contextController.deactivate();
    }
}
Also used : RequestContextController(javax.enterprise.context.control.RequestContextController)

Example 4 with RequestContextController

use of javax.enterprise.context.control.RequestContextController in project openwebbeans by apache.

the class OwbRequestContextControllerTest method check.

@Test
public void check() {
    startContainer();
    final ContextsService cs = getWebBeansContext().getContextsService();
    // it is started by AbstractUnitTest
    cs.endContext(RequestScoped.class, null);
    final RequestContextController controller = getInstance(RequestContextController.class);
    assertNull(cs.getCurrentContext(RequestScoped.class));
    assertTrue(controller.activate());
    final RequestContextController i2 = getInstance(RequestContextController.class);
    assertFalse(i2.activate());
    final RequestContextController i3 = getInstance(RequestContextController.class);
    assertFalse(i3.activate());
    assertTrue(cs.getCurrentContext(RequestScoped.class).isActive());
    i2.deactivate();
    // skip i3 to ensure we deactivate by the "owner"
    controller.deactivate();
    assertNull(cs.getCurrentContext(RequestScoped.class));
}
Also used : ContextsService(org.apache.webbeans.spi.ContextsService) RequestScoped(javax.enterprise.context.RequestScoped) RequestContextController(javax.enterprise.context.control.RequestContextController) AbstractUnitTest(org.apache.webbeans.test.AbstractUnitTest) Test(org.junit.Test)

Aggregations

RequestContextController (javax.enterprise.context.control.RequestContextController)4 ArcContainer (io.quarkus.arc.ArcContainer)2 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)2 Test (org.junit.jupiter.api.Test)2 ManagedContext (io.quarkus.arc.ManagedContext)1 RequestScoped (javax.enterprise.context.RequestScoped)1 ContextsService (org.apache.webbeans.spi.ContextsService)1 AbstractUnitTest (org.apache.webbeans.test.AbstractUnitTest)1 Test (org.junit.Test)1