Search in sources :

Example 11 with CdiContainer

use of org.apache.deltaspike.cdise.api.CdiContainer in project deltaspike by apache.

the class ContainerCtrlTckTest method testRestartContexts.

/**
     * 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();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) Car(org.apache.deltaspike.cdise.tck.beans.Car) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 12 with CdiContainer

use of org.apache.deltaspike.cdise.api.CdiContainer in project deltaspike by apache.

the class ContainerCtrlTckTest method testNewRequests.

@Test
public void testNewRequests() {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cdiContainer);
    cdiContainer.boot();
    cdiContainer.getContextControl().startContext(SessionScoped.class);
    cdiContainer.getContextControl().startContext(RequestScoped.class);
    BeanManager beanManager = cdiContainer.getBeanManager();
    Assert.assertNotNull(beanManager);
    TestUser testUser = resolveInstance(beanManager, TestUser.class);
    Assert.assertNotNull(testUser);
    testUser.setName("tester");
    CarRepair carRepair = resolveInstance(beanManager, CarRepair.class);
    Assert.assertNotNull(carRepair);
    Car car = carRepair.getCar();
    Assert.assertNotNull(car);
    Assert.assertNotNull(car.getUser());
    Assert.assertEquals("tester", car.getUser().getName());
    carRepair.getCar().getUser().setName("tck-tester");
    Assert.assertEquals("tck-tester", testUser.getName());
    cdiContainer.getContextControl().stopContext(RequestScoped.class);
    cdiContainer.getContextControl().startContext(RequestScoped.class);
    try {
        testUser = resolveInstance(beanManager, TestUser.class);
        Assert.assertNotNull(testUser);
        Assert.assertNotNull(testUser.getName());
        Assert.assertEquals("tck-tester", testUser.getName());
    } catch (ContextNotActiveException e) {
        Assert.fail(e.getMessage());
    }
    try {
        carRepair = resolveInstance(beanManager, CarRepair.class);
        Assert.assertNotNull(carRepair);
        car = carRepair.getCar();
        Assert.assertNotNull(car);
        Assert.assertNotNull(car.getUser());
        Assert.assertNotNull(car.getUser().getName());
        Assert.assertEquals("tck-tester", car.getUser().getName());
    } catch (ContextNotActiveException e) {
        Assert.fail(e.getMessage());
    }
    cdiContainer.shutdown();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) Car(org.apache.deltaspike.cdise.tck.beans.Car) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanManager(javax.enterprise.inject.spi.BeanManager) TestUser(org.apache.deltaspike.cdise.tck.beans.TestUser) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 13 with CdiContainer

use of org.apache.deltaspike.cdise.api.CdiContainer in project deltaspike by apache.

the class EmbeddedServletContainer method testBootRequest.

@Test
public void testBootRequest() throws Exception {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();
    int port = createServer();
    testRead(port);
    try {
        shutdown();
    } finally {
        //also calls #stopContexts
        cdiContainer.shutdown();
    }
}
Also used : CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 14 with CdiContainer

use of org.apache.deltaspike.cdise.api.CdiContainer in project deltaspike by apache.

the class CdiTestSuiteRunner method run.

@Override
public void run(RunNotifier notifier) {
    if (this.testSuiteClass == null) {
        throw new IllegalStateException("no test-suite class found");
    }
    CdiContainer container = CdiContainerLoader.getCdiContainer();
    if (!containerStarted) {
        container.boot(getTestContainerConfig());
        containerStarted = true;
    }
    notifier.addListener(new LogRunListener());
    try {
        super.run(notifier);
    } finally {
        if (STOP_CONTAINER) {
            container.shutdown();
            containerStarted = false;
        }
    }
}
Also used : CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer)

Example 15 with CdiContainer

use of org.apache.deltaspike.cdise.api.CdiContainer in project activemq-artemis by apache.

the class CDIExample method main.

public static void main(final String[] args) throws Exception {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    cdiContainer.shutdown();
}
Also used : CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer)

Aggregations

CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)15 Test (org.junit.Test)8 BeanManager (javax.enterprise.inject.spi.BeanManager)6 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 Bean (javax.enterprise.inject.spi.Bean)4 Car (org.apache.deltaspike.cdise.tck.beans.Car)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 CdiContainerLoader.getCdiContainer (org.apache.deltaspike.cdise.api.CdiContainerLoader.getCdiContainer)1 Foo (org.apache.deltaspike.cdise.openejb.bean.Foo)1 TestUser (org.apache.deltaspike.cdise.tck.beans.TestUser)1 LockedCDIImplementation (org.apache.deltaspike.cdise.tck.control.LockedCDIImplementation)1 DefaultEchoService (org.apache.deltaspike.example.echo.DefaultEchoService)1 EchoService (org.apache.deltaspike.example.echo.EchoService)1 OptionalService (org.apache.deltaspike.example.optional.OptionalService)1