Search in sources :

Example 81 with WeldContainer

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

the class RequestScopedActiveInterceptorTest method requestScopedActiveInNestedInvocation.

@Test
public void requestScopedActiveInNestedInvocation() {
    try (WeldContainer container = new Weld().initialize()) {
        Foo foo = container.select(Foo.class).get();
        Assert.assertEquals(3, foo.pingNested());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 82 with WeldContainer

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

the class ContainerLifecyleObserverTest method testAddContainerLifecycleObserver.

@SuppressWarnings("serial")
@Test
public void testAddContainerLifecycleObserver() {
    final AtomicBoolean called = new AtomicBoolean(false);
    try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class).addContainerLifecycleObserver(processAnnotatedType(new TypeLiteral<ProcessAnnotatedType<Foo>>() {
    }.getType()).notify((e) -> e.veto())).addContainerLifecycleObserver(afterBeanDiscovery((e) -> called.set(true))).initialize()) {
        assertTrue(called.get());
        assertTrue(container.select(Foo.class).isUnsatisfied());
    }
}
Also used : ContainerLifecycleObserver.processObserverMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processObserverMethod) ContainerLifecycleObserver.processSyntheticBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticBean) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) ContainerLifecycleObserver.beforeBeanDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.beforeBeanDiscovery) ContainerLifecycleObserver.afterBeanDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterBeanDiscovery) ContainerLifecycleObserver.afterTypeDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterTypeDiscovery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessSyntheticBean(javax.enterprise.inject.spi.ProcessSyntheticBean) ProcessBean(javax.enterprise.inject.spi.ProcessBean) ContainerLifecycleObserver.processProducer(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducer) ContainerLifecycleObserver.processBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processBean) ContainerLifecycleObserver.beforeShutdown(org.jboss.weld.environment.se.ContainerLifecycleObserver.beforeShutdown) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) ProcessProducerField(javax.enterprise.inject.spi.ProcessProducerField) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) TypeLiteral(javax.enterprise.util.TypeLiteral) ActionSequence(org.jboss.weld.test.util.ActionSequence) ContainerLifecycleObserver.processInjectionPoint(org.jboss.weld.environment.se.ContainerLifecycleObserver.processInjectionPoint) ProcessObserverMethod(javax.enterprise.inject.spi.ProcessObserverMethod) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) ContainerLifecycleObserver.processSyntheticAnnotatedType(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticAnnotatedType) ContainerLifecycleObserver.processProducerField(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducerField) Weld(org.jboss.weld.environment.se.Weld) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) ContainerLifecycleObserver.processSyntheticObserverMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticObserverMethod) Extension(javax.enterprise.inject.spi.Extension) ProcessManagedBean(javax.enterprise.inject.spi.ProcessManagedBean) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ContainerLifecycleObserver.processManagedBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processManagedBean) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) WeldContainer(org.jboss.weld.environment.se.WeldContainer) ProcessProducerMethod(javax.enterprise.inject.spi.ProcessProducerMethod) ContainerLifecycleObserver.afterDeploymentValidation(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterDeploymentValidation) ContainerLifecycleObserver.processProducerMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducerMethod) AfterTypeDiscovery(javax.enterprise.inject.spi.AfterTypeDiscovery) ContainerLifecycleObserver(org.jboss.weld.environment.se.ContainerLifecycleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) RequestScoped(javax.enterprise.context.RequestScoped) BeforeShutdown(javax.enterprise.inject.spi.BeforeShutdown) ActionSequence.addAction(org.jboss.weld.test.util.ActionSequence.addAction) ProcessBeanAttributes(javax.enterprise.inject.spi.ProcessBeanAttributes) ContainerLifecycleObserver.processAnnotatedType(org.jboss.weld.environment.se.ContainerLifecycleObserver.processAnnotatedType) Assert(org.junit.Assert) ContainerLifecycleObserver.processBeanAttributes(org.jboss.weld.environment.se.ContainerLifecycleObserver.processBeanAttributes) ProcessSyntheticObserverMethod(javax.enterprise.inject.spi.ProcessSyntheticObserverMethod) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TypeLiteral(javax.enterprise.util.TypeLiteral) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 83 with WeldContainer

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

the class ExplicitClassLoaderScanningTest method testScanningExplicitClassLoader.

@Test
public void testScanningExplicitClassLoader() throws IOException {
    // this is the application
    final Archive<?> archive = create(BeanArchive.class).addClass(EmbeddedApplication.class);
    final File jar = File.createTempFile("weld-se-test", ".jar");
    jar.deleteOnExit();
    archive.as(ZipExporter.class).exportTo(jar, true);
    /*
         * Special classloader that hides BDAs in parent classloaders. This would not be needed normally. We need this here because
         * , since this testsuite defines a top-level beans.xml file, each file in this testsuite is already part of this single giant BDA.
         * Since we are adding the EmbeddedApplication class to the special BDA we test, we do not want the class to be found twice. We cannot just leave
         * out the parent classloader as we need CDI classes to be loadable from the application.
         */
    ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }) {

        @Override
        public Enumeration<URL> getResources(String name) throws IOException {
            if (AbstractWeldDeployment.BEANS_XML.equals(name)) {
                return findResources(name);
            }
            return super.getResources(name);
        }
    };
    try (WeldContainer weld = new Weld().setClassLoader(classLoader).initialize()) {
        AtomicInteger payload = new AtomicInteger();
        weld.event().fire(payload);
        Assert.assertEquals(10, payload.intValue());
    }
}
Also used : BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) URLClassLoader(java.net.URLClassLoader) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 84 with WeldContainer

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

the class ExplicitResourceLoaderScanningTest method testScanning.

@Test
public void testScanning() throws IOException {
    // this is the application
    final Archive<?> archive = create(BeanArchive.class).addClass(EmbeddedApplication.class);
    final File jar = File.createTempFile("weld-se-resource-loader-test", ".jar");
    jar.deleteOnExit();
    archive.as(ZipExporter.class).exportTo(jar, true);
    /*
         * Special classloader that hides BDAs in parent classloaders. This would not be needed normally. We need this here because
         * , since this testsuite defines a top-level beans.xml file, each file in this testsuite is already part of this single giant BDA.
         * Since we are adding the EmbeddedApplication class to the special BDA we test, we do not want the class to be found twice. We cannot just leave
         * out the parent classloader as we need CDI classes to be loadable from the application.
         */
    ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }) {

        @Override
        public Enumeration<URL> getResources(String name) throws IOException {
            if (AbstractWeldDeployment.BEANS_XML.equals(name)) {
                return findResources(name);
            }
            return super.getResources(name);
        }
    };
    try (WeldContainer weld = new Weld().setResourceLoader(new ClassLoaderResourceLoader(classLoader)).initialize()) {
        AtomicInteger payload = new AtomicInteger();
        weld.event().fire(payload);
        Assert.assertEquals(10, payload.intValue());
    }
}
Also used : BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) URLClassLoader(java.net.URLClassLoader) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLClassLoader(java.net.URLClassLoader) ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader) File(java.io.File) URL(java.net.URL) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 85 with WeldContainer

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

the class WeldContainerCurrentTest method testExceptionThrownWithMultipleContainers.

@Test
public void testExceptionThrownWithMultipleContainers() {
    String containerIdOne = "007";
    String containerIdTwo = "008";
    WeldContainer containerOne = new Weld(containerIdOne).disableDiscovery().addBeanClass(DumbBean.class).initialize();
    WeldContainer containerTwo = new Weld(containerIdTwo).disableDiscovery().addBeanClass(DumbBean.class).initialize();
    try {
        WeldContainer.current();
    } catch (IllegalStateException e) {
        // expected
        return;
    } finally {
        containerOne.close();
        containerTwo.close();
    }
    Assert.fail();
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) 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