Search in sources :

Example 1 with RequestContextController

use of jakarta.enterprise.context.control.RequestContextController in project core by weld.

the class GetContextUtilMethodsTest method getActiveWeldAlterableContextsTest.

@Test
public void getActiveWeldAlterableContextsTest() {
    try (WeldContainer container = new Weld().initialize()) {
        // TheLoneBean is just to have some bean in the archive
        container.select(TheLoneBean.class).get().ping();
        WeldManager manager = container.select(WeldManager.class).get();
        RequestContextController controller = manager.instance().select(RequestContextController.class).get();
        controller.activate();
        Collection<WeldAlterableContext> activeContexts = manager.getActiveWeldAlterableContexts();
        // there are 7 scopes by default in SE, only 3 have active contexts by default
        // it is dependent, singleton and application -> none of these implements WeldAlterableContext
        // therefore we activated request scope and assume on that one
        controller.deactivate();
        Assert.assertEquals(1, activeContexts.size());
        Assert.assertEquals(RequestScoped.class, activeContexts.iterator().next().getScope());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Example 2 with RequestContextController

use of jakarta.enterprise.context.control.RequestContextController in project core by weld.

the class ContextPropagationSETest method testRequestScopedBean.

@Test
public void testRequestScopedBean() {
    try (WeldContainer container = new Weld().initialize()) {
        // in SE we need to controll req context activation/deactivation
        RequestContextController controller = container.select(RequestContextController.class).get();
        controller.activate();
        Future<Integer> futureResult = pingBeanAndOffloadTask(container, ReqScopedBean.class);
        // block until we have result
        try {
            Integer result = futureResult.get();
            controller.deactivate();
            Assert.assertEquals(2, result.intValue());
        } catch (InterruptedException e) {
            Assert.fail("Encountered InterruptedException while waiting for result from a different thread!");
        } catch (ExecutionException e) {
            Assert.fail(e.toString());
        }
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) ExecutionException(java.util.concurrent.ExecutionException) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 3 with RequestContextController

use of jakarta.enterprise.context.control.RequestContextController in project helidon by oracle.

the class HelidonMethodExecutor method invoke.

/**
 * Invoke method after enrichment.
 *
 * - JUnit: Inexplicably, the {@code @Before} and {@code @After} methods are
 * not called when running this executor, so we call them manually.
 *
 * - TestNG: Methods decorated with {@code @BeforeMethod} and {@code AfterMethod}
 * are called too early, before enrichment takes places. Here we call them
 * again to make sure instances are initialized properly.
 *
 * @param testMethodExecutor Method executor.
 * @return Test result.
 */
public TestResult invoke(TestMethodExecutor testMethodExecutor) {
    RequestContextController controller = enricher.getRequestContextController();
    try {
        controller.activate();
        Object instance = testMethodExecutor.getInstance();
        Method method = testMethodExecutor.getMethod();
        LOGGER.info("Invoking '" + method + "' on " + instance);
        enricher.enrich(instance);
        jUnitTestNameRule(testMethodExecutor);
        invokeBefore(instance, method);
        testMethodExecutor.invoke(enricher.resolve(method));
        invokeAfter(instance, method);
    } catch (Throwable t) {
        return TestResult.failed(t);
    } finally {
        controller.deactivate();
    }
    return TestResult.passed();
}
Also used : BeforeMethod(org.testng.annotations.BeforeMethod) AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) RequestContextController(jakarta.enterprise.context.control.RequestContextController)

Aggregations

RequestContextController (jakarta.enterprise.context.control.RequestContextController)3 Weld (org.jboss.weld.environment.se.Weld)2 WeldContainer (org.jboss.weld.environment.se.WeldContainer)2 Test (org.junit.Test)2 Method (java.lang.reflect.Method)1 ExecutionException (java.util.concurrent.ExecutionException)1 WeldAlterableContext (org.jboss.weld.context.WeldAlterableContext)1 WeldManager (org.jboss.weld.manager.api.WeldManager)1 AfterMethod (org.testng.annotations.AfterMethod)1 BeforeMethod (org.testng.annotations.BeforeMethod)1