org.apache.deltaspike.cdise.api.CdiContainerLoader Java Examples

The following examples show how to use org.apache.deltaspike.cdise.api.CdiContainerLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CdiCucumberTestRunner.java    From database-rider with Apache License 2.0 6 votes vote down vote up
void applyBeforeFeatureConfig(Class testClass) {
    CdiContainer container = CdiContainerLoader.getCdiContainer();

    if (!isContainerStarted()) {
        container.boot(CdiTestSuiteRunner.getTestContainerConfig());
        containerStarted = true;
        bootExternalContainers(testClass);
    }

    List<Class<? extends Annotation>> restrictedScopes = new ArrayList<Class<? extends Annotation>>();

    //controlled by the container and not supported by weld:
    restrictedScopes.add(ApplicationScoped.class);
    restrictedScopes.add(Singleton.class);

    if (this.parent == null && this.testControl.getClass().equals(TestControlLiteral.class)) {
        //skip scope-handling if @TestControl isn't used explicitly on the test-class -> TODO re-visit it
        restrictedScopes.add(RequestScoped.class);
        restrictedScopes.add(SessionScoped.class);
    }

    this.previousProjectStage = ProjectStageProducer.getInstance().getProjectStage();
    ProjectStageProducer.setProjectStage(this.projectStage);

    startScopes(container, testClass, null, restrictedScopes.toArray(new Class[restrictedScopes.size()]));
}
 
Example #2
Source File: OpenEJbContainerControlConfigurationTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void basicInjection() // useless because of tcks but nice to have when working on this specific container
{
    final CdiContainer container = CdiContainerLoader.getCdiContainer();
    container.boot();

    try
    {
        final BeanManager beanManager = container.getBeanManager();
        assertEquals("foo", Foo.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(Foo.class)), Foo.class, null)).name());
    }
    finally
    {
        container.shutdown();
    }
}
 
Example #3
Source File: ContainerCtrlTckTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void testContainerBoot()
{
    CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);

    cc.boot();
    cc.getContextControl().startContexts();

    BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);
    
    Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
    Bean<?> bean = bm.resolve(beans);
    
    CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
    Assert.assertNotNull(carRepair);

    Assert.assertNotNull(carRepair.getCar());
    Assert.assertNotNull(carRepair.getCar().getUser());

    cc.shutdown();
}
 
Example #4
Source File: RequestServlet.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private RequestScopedBean getRequestScopedBean()
{
    BeanManager beanManager = CdiContainerLoader.getCdiContainer().getBeanManager();

    if (beanManager == null)
    {
        return null;
    }
    Set<Bean<?>> beans = beanManager.getBeans(RequestScopedBean.class);
    Bean<RequestScopedBean> reqScpdBean = (Bean<RequestScopedBean>) beanManager.resolve(beans);

    CreationalContext<RequestScopedBean> reqScpdCC =
            beanManager.createCreationalContext(reqScpdBean);

    RequestScopedBean instance = (RequestScopedBean)
            beanManager.getReference(reqScpdBean, RequestScopedBean.class, reqScpdCC);
    return instance;
}
 
Example #5
Source File: EmbeddedServletContainer.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void testBootRequest() throws Exception
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();
    int port = createServer();
    testRead(port);

    try
    {
        shutdown();
    }
    finally
    {
        cdiContainer.shutdown(); //also calls #stopContexts
    }
}
 
Example #6
Source File: ConfigExample.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();

    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);

    SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);

    LOG.info("configured int-value #1: " + settingsBean.getIntProperty1());
    LOG.info("configured long-value #2: " + settingsBean.getProperty2());
    LOG.info("configured inverse-value #2: " + settingsBean.getInverseProperty());
    LOG.info("configured location (custom config): " + settingsBean.getLocationId().name());
    
    cdiContainer.shutdown();
}
 
Example #7
Source File: SimpleSchedulerExample.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();

    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);

    GlobalResultHolder globalResultHolder =
        BeanProvider.getContextualReference(GlobalResultHolder.class);

    while (globalResultHolder.getCount() < 100)
    {
        Thread.sleep(500);
        LOG.info("current count: " + globalResultHolder.getCount());
    }
    LOG.info("completed!");

    contextControl.stopContext(ApplicationScoped.class);
    cdiContainer.shutdown();
}
 
Example #8
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
void applyAfterClassConfig()
{
    CdiContainer container = CdiContainerLoader.getCdiContainer();

    stopStartedScopes(container);

    if (this.containerStarted)
    {
        if (CdiTestSuiteRunner.isStopContainerAllowed())
        {
            shutdownExternalContainers();

            container.shutdown(); //stop the container on the same level which started it
            CdiTestSuiteRunner.setContainerStarted(false);
        }
    }
}
 
Example #9
Source File: CdiCucumberTestRunner.java    From database-rider with Apache License 2.0 6 votes vote down vote up
void applyAfterFeatureConfig() {
    ProjectStageProducer.setProjectStage(previousProjectStage);
    previousProjectStage = null;

    CdiContainer container = CdiContainerLoader.getCdiContainer();

    stopStartedScopes(container);

    if (this.containerStarted) {
        if (isStopContainerAllowed()) {
            shutdownExternalContainers();

            container.shutdown(); //stop the container on the same level which started it
            containerStarted = false;
        }
    }
}
 
Example #10
Source File: CdiCucumberTestRunner.java    From database-rider with Apache License 2.0 5 votes vote down vote up
@Override
public void run(RunNotifier runNotifier)
{
    CdiContainer container = CdiContainerLoader.getCdiContainer();

    if (!containerStarted)
    {
        container.boot(CdiTestSuiteRunner.getTestContainerConfig());
        containerStarted = true;
    }

    super.run(runNotifier);
}
 
Example #11
Source File: StandardContextTestControlValidator.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private void checkActiveContextControlImplementation()
{
    if (customContextControlDetected != null)
    {
        return;
    }

    customContextControlDetected = !CdiContainerLoader.getCdiContainer().getContextControl()
        .getClass().getName().startsWith("org.apache.deltaspike.");
}
 
Example #12
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
void applyAfterMethodConfig()
{
    try
    {
        stopStartedScopes(CdiContainerLoader.getCdiContainer());
    }
    finally
    {
        setCurrentTestMethod(null);
        ProjectStageProducer.setProjectStage(previousProjectStage);
        previousProjectStage = null;
        currentTestRunner.set(null);
        currentTestRunner.remove();
    }
}
 
Example #13
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
void applyBeforeMethodConfig(Method testMethod)
{
    this.previousProjectStage = ProjectStageProducer.getInstance().getProjectStage();
    ProjectStageProducer.setProjectStage(this.projectStage);

    setCurrentTestMethod(testMethod);
    startScopes(CdiContainerLoader.getCdiContainer(), testMethod.getDeclaringClass(), testMethod);
}
 
Example #14
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
void applyBeforeClassConfig(Class<?> testClass)
{
    CdiContainer container = CdiContainerLoader.getCdiContainer();

    if (!isContainerStarted())
    {
        if (!CdiTestSuiteRunner.isContainerStarted())
        {
            // We are setting this system property to make the deployment for Weld "flat"
            // This (amongst other things) means that alternatives enabled via beans.xml will be 
            // enabled globally
            // Beginning with Weld 2.x you could use Weld.property(), but here we depend on Weld 1.x API
            // Note that Weld 1 was "flat" anyway, so this property only affects newer versions of Weld
            System.setProperty("org.jboss.weld.se.archive.isolation", "false");

            CdiTestSuiteRunner.applyTestSpecificMetaData(testClass);

            container.boot(CdiTestSuiteRunner.getTestContainerConfig());
            setContainerStarted();

            bootExternalContainers(testClass);
        }
    }

    List<Class<? extends Annotation>> restrictedScopes = new ArrayList<Class<? extends Annotation>>();

    //controlled by the container and not supported by weld:
    restrictedScopes.add(ApplicationScoped.class);
    restrictedScopes.add(Singleton.class);

    if (this.parent == null && this.testControl.getClass().equals(TestControlLiteral.class))
    {
        //skip scope-handling if @TestControl isn't used explicitly on the test-class -> TODO re-visit it
        restrictedScopes.add(RequestScoped.class);
        restrictedScopes.add(SessionScoped.class);
    }

    startScopes(container, testClass, null, restrictedScopes.toArray(new Class[restrictedScopes.size()]));
}
 
Example #15
Source File: CdiTestSuiteRunner.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public void run(RunNotifier notifier)
{
    if (this.testSuiteClass == null)
    {
        throw new IllegalStateException("no test-suite class found");
    }

    CdiContainer container = CdiContainerLoader.getCdiContainer();

    if (!containerStarted)
    {
        applyTestSpecificMetaData(getTestClass().getJavaClass());

        container.boot(getTestContainerConfig());
        containerStarted = true;
    }

    notifier.addListener(new LogRunListener());

    try
    {
        super.run(notifier);
    }
    finally
    {
        if (STOP_CONTAINER)
        {
            container.shutdown();
            containerStarted = false;
        }
    }
}
 
Example #16
Source File: CdiCtrlLifecycle.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public CdiContainer start() {
    final CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot(configuration("cdictrl"));
    cdiContainer.getContextControl().startContexts();
    return cdiContainer;
}
 
Example #17
Source File: CdiContainerLifecycle.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart(final ITestContext iTestContext) {
    container = CdiContainerLoader.getCdiContainer();
    try {
        container.boot();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #18
Source File: HAAbstractUnitTest.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
protected void startContainer()
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);
    contextControl.startContext(SessionScoped.class);
    contextControl.startContext(RequestScoped.class);
}
 
Example #19
Source File: BeanProviderHelper.java    From BeanTest with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the CDI Container and initializes its contexts.
 */
private void bootstrapCdiContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();

}
 
Example #20
Source File: CucumberCdiFeature.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #21
Source File: ContainerCtrlTckTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Test
public void testParallelThreadExecution() throws Exception
{
    final CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);

    cc.boot();
    cc.getContextControl().startContexts();

    final BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);

    final AtomicInteger numErrors = new AtomicInteger(0);
    final ContextControl contextControl = cc.getContextControl();

    Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                contextControl.startContext(SessionScoped.class);
                contextControl.startContext(RequestScoped.class);


                Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
                Bean<?> bean = bm.resolve(beans);

                CarRepair carRepair = (CarRepair)
                        bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
                Assert.assertNotNull(carRepair);

                for (int i = 0; i < 100000; i++)
                {
                    // we need the threads doing something ;)
                    Assert.assertNotNull(carRepair.getCar());
                    Assert.assertNotNull(carRepair.getCar().getUser());
                    Assert.assertNull(carRepair.getCar().getUser().getName());
                }
                contextControl.stopContext(RequestScoped.class);
                contextControl.stopContext(SessionScoped.class);
            }
            catch (Throwable e)
            {
                log.log(Level.SEVERE, "An exception happened on a new worker thread", e);
                numErrors.incrementAndGet();
            }
        }
    };


    Thread[] threads = new Thread[NUM_THREADS];
    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i] = new Thread(runnable);
    }

    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i].start();
    }

    for (int i = 0 ; i < NUM_THREADS; i++)
    {
        threads[i].join();
    }

    Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get());


    cc.shutdown();
}
 
Example #22
Source File: CdiEnabledNewDepositorFixture.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeSuite
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #23
Source File: ContainerCtrlTckTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
/**
 * Stops and starts: application-, session- and request-scope.
 * <p/>
 * application-scoped instance has a ref to
 * request-scoped instance which has a ref to
 * session-scoped instance.
 * <p/>
 * If the deepest ref has the expected value, all levels in between were resetted correctly.
 */
@Test
public void testRestartContexts()
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cdiContainer);

    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();

    BeanManager beanManager = cdiContainer.getBeanManager();
    Assert.assertNotNull(beanManager);

    Set<Bean<?>> beans = beanManager.getBeans(CarRepair.class);
    Bean<?> bean = beanManager.resolve(beans);

    CarRepair carRepair = (CarRepair)
        beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));

    Assert.assertNotNull(carRepair);

    Car car = carRepair.getCar();

    Assert.assertNotNull(car);
    Assert.assertNotNull(car.getUser());


    carRepair.getCar().getUser().setName("tester");
    Assert.assertEquals("tester", car.getUser().getName());

    Assert.assertFalse(CarRepair.isPreDestroyCalled());
    Assert.assertFalse(Car.isPreDestroyCalled());
    Assert.assertFalse(TestUser.isPreDestroyCalled());

    cdiContainer.getContextControl().stopContexts();

    Assert.assertTrue(CarRepair.isPreDestroyCalled());
    Assert.assertTrue(Car.isPreDestroyCalled());
    Assert.assertTrue(TestUser.isPreDestroyCalled());

    try
    {
        car.getUser();

        // accessing the car should have triggered a ContextNotActiveException
        Assert.fail();
    }
    catch (ContextNotActiveException e)
    {
        //do nothing - exception expected
    }

    cdiContainer.getContextControl().startContexts();

    carRepair = (CarRepair)
        beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));

    Assert.assertNotNull(carRepair.getCar());
    Assert.assertNotNull(carRepair.getCar().getUser());
    Assert.assertNull(carRepair.getCar().getUser().getName());

    cdiContainer.shutdown();
}
 
Example #24
Source File: ContainerCtrlTckTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@LockedCDIImplementation(versions = {
        @LockedVersionRange(implementation = CdiImplementation.WELD11, versionRange = "[1.1.14,1.2)"),
        @LockedVersionRange(implementation = CdiImplementation.WELD20, versionRange = "[2.0.1.Final,2.1)")
    })
@Test
public void testShutdownWithInactiveContexts()
{
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cdiContainer);

    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();

    // now do some random stuff
    BeanManager beanManager = cdiContainer.getBeanManager();
    Assert.assertNotNull(beanManager);

    Set<Bean<?>> beans = beanManager.getBeans(CarRepair.class);
    Bean<?> bean = beanManager.resolve(beans);

    CarRepair carRepair = (CarRepair)
            beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));

    Assert.assertNotNull(carRepair);

    Car car = carRepair.getCar();

    Assert.assertNotNull(car);
    Assert.assertNotNull(car.getUser());


    carRepair.getCar().getUser().setName("tester");
    Assert.assertEquals("tester", car.getUser().getName());

    Assert.assertFalse(CarRepair.isPreDestroyCalled());
    Assert.assertFalse(Car.isPreDestroyCalled());
    Assert.assertFalse(TestUser.isPreDestroyCalled());

    cdiContainer.getContextControl().stopContexts();

    Assert.assertTrue(CarRepair.isPreDestroyCalled());
    Assert.assertTrue(Car.isPreDestroyCalled());
    Assert.assertTrue(TestUser.isPreDestroyCalled());

    cdiContainer.shutdown();
}
 
Example #25
Source File: CucumberCdiTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #26
Source File: CdiServletRequestListener.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private ContextControl getContextControl()
{
    CdiContainer container = CdiContainerLoader.getCdiContainer();
    return container.getContextControl();
}
 
Example #27
Source File: CucumberCdiTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #28
Source File: CdiEnabledNewDepositorFixture.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeSpecification
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #29
Source File: CdiEnabledNewDepositorFixture.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeSpecification
public static void startContainer() {
    cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
}
 
Example #30
Source File: HAAbstractUnitTest.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
protected void shutDownContainer()
{
    CdiContainerLoader.getCdiContainer().shutdown();
}