Search in sources :

Example 1 with ArcContainer

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

the class RepeatingQualifierClassTest method testRepeatingQualifiers.

@Test
public void testRepeatingQualifiers() {
    ArcContainer container = Arc.container();
    // simple resolution with just one instance of repeatable qualifier
    InstanceHandle<SomePlace> home = container.instance(SomePlace.class, new Location.Literal("home"));
    Assertions.assertTrue(home.isAvailable());
    // resolution when we select a bean having two repeatable qualifiers but only using one
    InstanceHandle<SomePlace> farAway = container.instance(SomePlace.class, new Location.Literal("farAway"));
    Assertions.assertTrue(farAway.isAvailable());
    // resolution where we select a bean having two repeatable qualifiers using both
    InstanceHandle<SomePlace> work = container.instance(SomePlace.class, new Location.Literal("work"), new Location.Literal("office"));
    Assertions.assertTrue(work.isAvailable());
    InjectingBean injectingBean = container.instance(InjectingBean.class).get();
    Assertions.assertNotNull(injectingBean.getFarAway());
    Assertions.assertNotNull(injectingBean.getHome());
    Assertions.assertNotNull(injectingBean.getWork());
    Assertions.assertNotNull(injectingBean.getLocationFromInitializer());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Test(org.junit.jupiter.api.Test)

Example 2 with ArcContainer

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

the class RepeatingQualifierObserverTest method testRepeatingQualifiers.

@Test
public void testRepeatingQualifiers() {
    ArcContainer container = Arc.container();
    Event<String> event = container.beanManager().getEvent().select(String.class);
    ObservingBean bean = container.instance(ObservingBean.class).get();
    List<String> expectedList = new ArrayList<>();
    assertListsAreEqual(expectedList, bean.getEvents());
    event.select(new Location.Literal("home")).fire("home");
    expectedList.add("home");
    assertListsAreEqual(expectedList, bean.getEvents());
    event.select(new Location.Literal("farAway"), new Location.Literal("dreamland")).fire("farAway");
    expectedList.add("farAway");
    assertListsAreEqual(expectedList, bean.getEvents());
    event.select(new Location.Literal("work"), new Location.Literal("office")).fire("work");
    expectedList.add("work");
    assertListsAreEqual(expectedList, bean.getEvents());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with ArcContainer

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

the class RepeatingQualifierProducerTest method testRepeatingQualifiers.

@Test
public void testRepeatingQualifiers() {
    ArcContainer container = Arc.container();
    container.requestContext().activate();
    // all beans need to be invoked so that they are created, only then can we test disposers work as well
    // simple resolution with just one instance of repeatable qualifier
    InstanceHandle<SomePlace> home = container.instance(SomePlace.class, new Location.Literal("home"));
    Assertions.assertTrue(home.isAvailable());
    home.get().ping();
    // resolution when we select a bean having two repeatable qualifiers but only using one
    InstanceHandle<SomePlace> farAway = container.instance(SomePlace.class, new Location.Literal("farAway"));
    Assertions.assertTrue(farAway.isAvailable());
    farAway.get().ping();
    // resolution where we select a bean having two repeatable qualifiers using both
    InstanceHandle<SomePlace> work = container.instance(SomePlace.class, new Location.Literal("work"), new Location.Literal("office"));
    Assertions.assertTrue(work.isAvailable());
    work.get().ping();
    // same as before but backed by field producer
    InstanceHandle<SomePlace> matrix = container.instance(SomePlace.class, new Location.Literal("alternativeReality"), new Location.Literal("matrix"));
    Assertions.assertTrue(matrix.isAvailable());
    matrix.get().ping();
    // deactivate req. context which will trigger disposers
    container.requestContext().terminate();
    // assert all disposers were invoked
    ProducerBean producerBean = container.instance(ProducerBean.class).get();
    Assertions.assertTrue(producerBean.isFarAwayDisposerInvoked());
    Assertions.assertTrue(producerBean.isHomeDisposerInvoked());
    Assertions.assertTrue(producerBean.isMatrixDisposerInvoked());
    Assertions.assertTrue(producerBean.isWorkDisposerInvoked());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Test(org.junit.jupiter.api.Test)

Example 4 with ArcContainer

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

the class TypedTest method testEmptyTyped.

@Test
public void testEmptyTyped() throws IOException {
    ArcContainer container = Arc.container();
    assertFalse(container.instance(MyBean.class).isAvailable());
    assertNull(EVENT.get());
    container.beanManager().getEvent().fire("foo");
    assertEquals("foo", EVENT.get());
    InstanceHandle<Stage> stage = container.instance(Stage.class);
    assertTrue(stage.isAvailable());
    assertEquals("produced", stage.get().id);
    assertTrue(container.instance(MyOtherBean.class).isAvailable());
    boolean found = false;
    for (Bean<?> bean : container.beanManager().getBeans(Object.class)) {
        InjectableBean<?> injectable = (InjectableBean<?>) bean;
        if (injectable.getDeclaringBean() == null && injectable.getBeanClass().equals(MyOtherBean.class)) {
            found = true;
            break;
        }
    }
    assertTrue(found, "MyOtherBean not found");
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) InjectableBean(io.quarkus.arc.InjectableBean) Test(org.junit.jupiter.api.Test)

Example 5 with ArcContainer

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

the class RemoveUnusedDecoratorTest method testRemoval.

@Test
public void testRemoval() {
    ArcContainer container = Arc.container();
    assertPresent(HasObserver.class);
    assertNotPresent(Converter.class);
    assertTrue(container.beanManager().resolveDecorators(Set.of(Converter.class)).isEmpty());
}
Also used : ArcContainer(io.quarkus.arc.ArcContainer) Test(org.junit.jupiter.api.Test)

Aggregations

ArcContainer (io.quarkus.arc.ArcContainer)80 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 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 MethodCreator (io.quarkus.gizmo.MethodCreator)2