Search in sources :

Example 91 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class WeldBuilderTest method testMultipleWeldInstancesCreated.

@Test
public void testMultipleWeldInstancesCreated() {
    Baz.DESTROYED.clear();
    Weld weld = new Weld().disableDiscovery();
    int loop = 5;
    List<WeldContainer> containers = new ArrayList<WeldContainer>();
    for (int i = 0; i < loop; i++) {
        containers.add(weld.containerId("" + i).beanClasses(Baz.class).initialize());
        WeldContainer.getRunningContainerIds().contains(String.valueOf(i));
        assertTrue(WeldContainer.getRunningContainerIds().size() == i + 1);
    }
    for (WeldContainer container : containers) {
        assertTrue(container.isRunning());
        assertEquals(container.getId(), container.select(Baz.class).get().getVal());
    }
    weld.shutdown();
    assertEquals(loop, Baz.DESTROYED.size());
    for (int i = 0; i < loop; i++) {
        assertTrue(Baz.DESTROYED.contains("" + i));
    }
    for (WeldContainer container : containers) {
        assertFalse(container.isRunning());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) ArrayList(java.util.ArrayList) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 92 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class SimpleInstanceTest method testSelect.

@Test
public void testSelect() throws Exception {
    Weld weld = new Weld();
    try {
        WeldContainer wc = weld.initialize();
        Assert.assertNotNull(wc.instance().select(KPT.class).select(new KPQLiteral()).get());
        Assert.assertNotNull(wc.instance().select(KPT.class, new KPQLiteral()).get());
    } finally {
        weld.shutdown();
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 93 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class SyntheticBeanArchiveFromDirectoryTest method testAddPackageFromDirectoryRecursively.

@Test
public void testAddPackageFromDirectoryRecursively() {
    try (WeldContainer container = new Weld().disableDiscovery().addPackages(true, AlphaFromDirectory.class.getPackage()).initialize()) {
        AlphaFromDirectory alpha = container.select(AlphaFromDirectory.class).get();
        Instance<BetaFromDirectory> betaInstance = container.select(BetaFromDirectory.class);
        assertFalse(betaInstance.isUnsatisfied());
        assertNotNull(alpha);
        assertNotNull(betaInstance.get());
        assertEquals(1, alpha.ping());
        assertEquals(1, betaInstance.get().ping());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) AlphaFromDirectory(org.jboss.weld.environment.se.test.synthethic.testpackage.AlphaFromDirectory) BetaFromDirectory(org.jboss.weld.environment.se.test.synthethic.testpackage.nested.BetaFromDirectory) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 94 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class WeldInstanceSelectTypeTest method testDestroy.

@Test
public void testDestroy() {
    Type type = DestroyedBean.class;
    try (WeldContainer container = new Weld().initialize()) {
        WeldInstance<DestroyedBean> instance = container.<DestroyedBean>select(type);
        DestroyedBean bean = instance.get();
        Assert.assertNotNull(bean);
        // destroy and verify
        Assert.assertFalse(DestroyedBean.DESTROY_INVOKED);
        instance.destroy(bean);
        Assert.assertTrue(DestroyedBean.DESTROY_INVOKED);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 95 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class WeldInstanceSelectTypeTest method testValidSelect.

@Test
public void testValidSelect() {
    try (WeldContainer container = new Weld().initialize()) {
        // Class<T> implements Type, so let's make use of that
        Type firstType = SomeInterface.class;
        // Let's also grab a parameterized type
        ParameterizedType pType = (ParameterizedType) new TypeLiteral<Foo<Bar>>() {
        }.getType();
        // Instance<Object> -> Instance<SomeInterface>
        WeldInstance<Object> instanceObject = container.select(firstType);
        Object object = instanceObject.get();
        // this can be casted if needed
        Assert.assertTrue(object instanceof SomeInterface);
        SomeInterface si = (SomeInterface) object;
        Assert.assertEquals(SomeOtherBean.class.getSimpleName(), si.ping());
        WeldInstance<Foo<Bar>> parameterizedInstance = container.<Foo<Bar>>select(pType);
        Foo<Bar> fooBar = parameterizedInstance.get();
        Assert.assertNotNull(fooBar);
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(javax.enterprise.util.TypeLiteral) Test(org.junit.Test)

Aggregations

WeldContainer (org.jboss.weld.environment.se.WeldContainer)115 Weld (org.jboss.weld.environment.se.Weld)100 Test (org.junit.Test)98 CountDownLatch (java.util.concurrent.CountDownLatch)8 Bean (javax.enterprise.inject.spi.Bean)8 BeanManager (javax.enterprise.inject.spi.BeanManager)8 Type (java.lang.reflect.Type)7 URLClassLoader (java.net.URLClassLoader)7 ArrayList (java.util.ArrayList)7 URL (java.net.URL)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 Fabric8Extension (io.fabric8.cdi.Fabric8Extension)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 PrintStream (java.io.PrintStream)3 ServiceRegistry (org.jboss.weld.bootstrap.api.ServiceRegistry)3