Search in sources :

Example 1 with CdiContainer

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

the class OpenEJbContainerControlConfigurationTest method basicInjection.

@Test
public // useless because of tcks but nice to have when working on this specific container
void basicInjection() {
    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();
    }
}
Also used : Foo(org.apache.deltaspike.cdise.openejb.bean.Foo) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 2 with CdiContainer

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

the class OpenEJbContainerControlConfigurationTest method ensureDataSourceExist.

@Test
public void ensureDataSourceExist() {
    final CdiContainer container = CdiContainerLoader.getCdiContainer();
    container.boot(new HashMap<Object, Object>() {

        {
            put("foo", "new://Resource?type=DataSource");
            put("foo.JdbcUrl", "jdbc:hsqldb:mem:foo");
            put("foo.JtaManaged", "false");
        }
    });
    try {
        final DataSource ds = DataSource.class.cast(SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("java:openejb/Resource/foo"));
        Connection c = null;
        try {
            c = ds.getConnection();
            assertEquals("jdbc:hsqldb:mem:foo", c.getMetaData().getURL());
        } catch (SQLException e) {
            fail(e.getMessage());
        } finally {
            try {
                if (c != null) {
                    c.close();
                }
            } catch (SQLException e) {
            // no-op
            }
        }
    } catch (final NamingException e) {
        fail(e.getMessage());
    } finally {
        container.shutdown();
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) SQLException(java.sql.SQLException) Connection(java.sql.Connection) NamingException(javax.naming.NamingException) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with CdiContainer

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

the class ContainerCtrlTckTest method testShutdownWithInactiveContexts.

@Test
@LockedCDIImplementation(versions = { @LockedVersionRange(implementation = CdiImplementation.WELD11, versionRange = "[1.1.14,1.2)"), @LockedVersionRange(implementation = CdiImplementation.WELD20, versionRange = "[2.0.1.Final,2.1)") })
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();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) Car(org.apache.deltaspike.cdise.tck.beans.Car) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test) LockedCDIImplementation(org.apache.deltaspike.cdise.tck.control.LockedCDIImplementation)

Example 4 with CdiContainer

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

the class ContainerCtrlTckTest method testParallelThreadExecution.

@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();
}
Also used : Bean(javax.enterprise.inject.spi.Bean) CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextControl(org.apache.deltaspike.cdise.api.ContextControl) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 5 with CdiContainer

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

the class ContainerCtrlTckTest method testContainerBoot.

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

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