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());
}
}
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());
}
}
}
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();
}
Aggregations