Search in sources :

Example 6 with ArcContainer

use of io.quarkus.arc.ArcContainer 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 7 with ArcContainer

use of io.quarkus.arc.ArcContainer in project quarkus by quarkusio.

the class RequestContextTest method testRequestContext.

@Test
public void testRequestContext() {
    Controller.DESTROYED.set(false);
    ArcContainer arc = Arc.container();
    ManagedContext requestContext = arc.requestContext();
    try {
        arc.instance(Controller.class).get().getId();
        fail();
    } catch (ContextNotActiveException expected) {
    }
    requestContext.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);
    requestContext.terminate();
    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);
    requestContext.activate();
    assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
    requestContext.terminate();
    assertTrue(Controller.DESTROYED.get());
    Controller.DESTROYED.set(false);
    requestContext.activate();
    assertNotEquals(controller2Id, arc.instance(Controller.class).get().getId());
    requestContext.terminate();
    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) ManagedContext(io.quarkus.arc.ManagedContext) RequestContextController(javax.enterprise.context.control.RequestContextController) Test(org.junit.jupiter.api.Test)

Example 8 with ArcContainer

use of io.quarkus.arc.ArcContainer in project quarkus by quarkusio.

the class BridgeMethodInterceptionTest method testInterception.

@Test
public void testInterception() {
    ArcContainer container = Arc.container();
    Counter counter = container.instance(Counter.class).get();
    counter.reset();
    Submarine submarine = container.instance(Submarine.class).get();
    assertEquals("foo", submarine.echo("foo"));
    assertEquals(Submarine.class.getSimpleName(), submarine.getName());
    assertEquals(2, counter.get());
    // Now let's invoke the bridge method...
    Base<String> base = submarine;
    assertEquals("foo", base.echo("foo"));
    assertEquals(Submarine.class.getSimpleName(), base.getName());
    assertEquals(4, counter.get());
    counter.reset();
    Ubot ubot = container.instance(Ubot.class).get();
    assertEquals("1", ubot.echo(1));
    assertEquals(42, ubot.getName());
    assertEquals(2, counter.get());
    Base<Integer> baseUbot = ubot;
    assertEquals("1", baseUbot.echo(1));
    assertEquals(42, ubot.getName());
    assertEquals(4, counter.get());
    counter.reset();
    Ponorka ponorka = container.instance(Ponorka.class).get();
    assertEquals("TRUE", ponorka.echo(true));
    assertNull(ponorka.getName());
    assertEquals(2, counter.get());
    Base<Boolean> basePonorka = ponorka;
    assertEquals("TRUE", basePonorka.echo(true));
    assertNull(basePonorka.getName());
    assertEquals(4, counter.get());
    counter.reset();
    NextSubmarine nextSubmarine = container.instance(NextSubmarine.class).get();
    assertEquals("foo", nextSubmarine.echo("foo"));
    assertEquals(NextSubmarine.class.getSimpleName(), nextSubmarine.getName());
    assertEquals(2, counter.get());
    // Now let's invoke the bridge method...
    NextBase<String> nextBase = nextSubmarine;
    assertEquals("foo", nextBase.echo("foo"));
    assertEquals(NextSubmarine.class.getSimpleName(), nextBase.getName());
    assertEquals(4, counter.get());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Counter(io.quarkus.arc.test.interceptors.Counter) Test(org.junit.jupiter.api.Test)

Example 9 with ArcContainer

use of io.quarkus.arc.ArcContainer in project quarkus by quarkusio.

the class BridgeMethodInterceptionTest method testHierarchyWithInterface.

@Test
public void testHierarchyWithInterface() {
    ArcContainer container = Arc.container();
    Counter counter = container.instance(Counter.class).get();
    counter.reset();
    ExampleResource exampleResource = container.instance(ExampleResource.class).get();
    assertEquals("foo", exampleResource.create("foo"));
    assertEquals(1, counter.get());
    counter.reset();
    ExampleApi exampleApi = container.instance(ExampleApi.class).get();
    assertEquals("foo", exampleApi.create("foo"));
    assertEquals(1, counter.get());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Counter(io.quarkus.arc.test.interceptors.Counter) Test(org.junit.jupiter.api.Test)

Example 10 with ArcContainer

use of io.quarkus.arc.ArcContainer in project quarkus by quarkusio.

the class SimpleInterceptorTest method testInterception.

@Test
public void testInterception() {
    ArcContainer arc = Arc.container();
    LifecycleInterceptor.POST_CONSTRUCTS.clear();
    LifecycleInterceptor.PRE_DESTROYS.clear();
    LifecycleInterceptor.AROUND_CONSTRUCTS.clear();
    InstanceHandle<SimpleBean> handle = arc.instance(SimpleBean.class);
    SimpleBean simpleBean = handle.get();
    assertEquals(1, LifecycleInterceptor.AROUND_CONSTRUCTS.size());
    assertNotNull(LifecycleInterceptor.AROUND_CONSTRUCTS.get(0));
    assertEquals(1, LifecycleInterceptor.POST_CONSTRUCTS.size());
    assertEquals(simpleBean, LifecycleInterceptor.POST_CONSTRUCTS.get(0));
    Counter counter = arc.instance(Counter.class).get();
    LoggingInterceptor.LOG.set(null);
    assertEquals("0foo1", simpleBean.foo("0"));
    assertEquals("oof", simpleBean.bar());
    assertEquals(1, counter.get());
    assertEquals("foo", LoggingInterceptor.LOG.get());
    simpleBean.baz(42);
    assertEquals(2, counter.get());
    handle.destroy();
    assertEquals(1, LifecycleInterceptor.PRE_DESTROYS.size());
    assertEquals(simpleBean, LifecycleInterceptor.PRE_DESTROYS.get(0));
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Test(org.junit.jupiter.api.Test)

Aggregations

ArcContainer (io.quarkus.arc.ArcContainer)82 Test (org.junit.jupiter.api.Test)51 Arc (io.quarkus.arc.Arc)11 InstanceHandle (io.quarkus.arc.InstanceHandle)10 ResultHandle (io.quarkus.gizmo.ResultHandle)10 Annotation (java.lang.annotation.Annotation)9 ManagedContext (io.quarkus.arc.ManagedContext)7 DataSourcesHealthSupport (io.quarkus.datasource.runtime.DataSourcesHealthSupport)5 PostConstruct (javax.annotation.PostConstruct)5 BeanManager (javax.enterprise.inject.spi.BeanManager)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Counter (io.quarkus.arc.test.interceptors.Counter)3 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)3 Set (java.util.Set)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)3 AgroalDataSource (io.agroal.api.AgroalDataSource)2 InjectableBean (io.quarkus.arc.InjectableBean)2 ContextState (io.quarkus.arc.InjectableContext.ContextState)2 ClassCreator (io.quarkus.gizmo.ClassCreator)2 FieldDescriptor (io.quarkus.gizmo.FieldDescriptor)2