Search in sources :

Example 21 with WeldContainer

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

the class WeldSEDeployableContainer method deploy.

@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
    final URL[] urls = writeFiles(archive);
    final ClassLoader classLoader = new URLClassLoader(urls, getClass().getClassLoader()) {

        @Override
        public Enumeration<URL> getResources(String name) throws IOException {
            Enumeration<URL> resources = super.getResources(name);
            Set<URL> urls = new HashSet<URL>();
            while (resources.hasMoreElements()) {
                final URL url = resources.nextElement();
                // ignore any resources on classpath outside of the deployment
                if (url.getProtocol().equalsIgnoreCase("jar")) {
                    urls.add(url);
                }
            }
            return Collections.enumeration(urls);
        }
    };
    this.weld = new Weld().setClassLoader(classLoader);
    final WeldContainer container = this.weld.initialize();
    beanManagerProducer.set(container.getBeanManager());
    return new ProtocolMetaData();
}
Also used : URLClassLoader(java.net.URLClassLoader) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLClassLoader(java.net.URLClassLoader) ProtocolMetaData(org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData) URL(java.net.URL) HashSet(java.util.HashSet) Weld(org.jboss.weld.environment.se.Weld)

Example 22 with WeldContainer

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

the class StartMainEmptyArgsTest method testMainEmptyArgs.

/**
 * Test of main method, of class StartMain when no command-line args are
 * provided.
 */
@Test
public void testMainEmptyArgs() {
    WeldContainer container = startMain.go();
    MainTestBean mainTestBean = container.instance().select(MainTestBean.class).get();
    assertNotNull(mainTestBean);
    ParametersTestBean paramsBean = mainTestBean.getParametersTestBean();
    assertNotNull(paramsBean);
    assertNotNull(paramsBean.getParameters());
}
Also used : ParametersTestBean(org.jboss.weld.environment.se.test.beans.ParametersTestBean) WeldContainer(org.jboss.weld.environment.se.WeldContainer) MainTestBean(org.jboss.weld.environment.se.test.beans.MainTestBean) Test(org.junit.Test)

Example 23 with WeldContainer

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

the class ContainerLifecyleObserverTest method testExtensionBuilder.

@SuppressWarnings({ "serial" })
@Test
public void testExtensionBuilder() {
    ActionSequence.reset();
    Extension myExtension = ContainerLifecycleObserver.extensionBuilder().add(beforeBeanDiscovery((e) -> addAction(BeforeBeanDiscovery.class.getSimpleName()))).add(afterTypeDiscovery().notify((e, b) -> {
        addAction(AfterTypeDiscovery.class.getSimpleName());
        e.addAnnotatedType(b.createAnnotatedType(Charlie.class), Charlie.class.getName());
    })).add(afterBeanDiscovery((e) -> {
        addAction(AfterBeanDiscovery.class.getSimpleName());
        e.addObserverMethod().beanClass(Foo.class).observedType(Foo.class).notifyWith((ctx) -> {
        });
        e.addBean().beanClass(Integer.class).addType(Integer.class).addQualifier(Juicy.Literal.INSTANCE).createWith((ctx) -> Integer.valueOf(10));
    })).add(afterDeploymentValidation((e) -> addAction(AfterDeploymentValidation.class.getSimpleName()))).add(beforeShutdown((e) -> addAction(BeforeShutdown.class.getSimpleName()))).build();
    Extension myExtension2 = ContainerLifecycleObserver.extensionBuilder().add(processAnnotatedType().withAnnotations(RequestScoped.class).notify((e) -> e.veto())).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processSyntheticAnnotatedType(new TypeLiteral<ProcessSyntheticAnnotatedType<?>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticAnnotatedType.class.getSimpleName()))).add(processInjectionPoint().notify((e) -> addAction(ProcessInjectionPoint.class.getSimpleName()))).add(processProducer().notify((e) -> addAction(ProcessProducer.class.getSimpleName()))).add(processBean().notify((e) -> addAction(ProcessBean.class.getSimpleName()))).add(processManagedBean().notify((e) -> addAction(ProcessManagedBean.class.getSimpleName()))).add(processProducerField().notify((e) -> addAction(ProcessProducerField.class.getSimpleName()))).add(processProducerMethod().notify((e) -> {
        // Weld SE defines some producer methods, e.g. ParametersFactory
        addAction(ProcessProducerMethod.class.getSimpleName());
    })).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processObserverMethod().notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName()))).add(processObserverMethod(new TypeLiteral<ProcessObserverMethod<String, ?>>() {
    }.getType()).notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName()))).add(processSyntheticObserverMethod(new TypeLiteral<ProcessSyntheticObserverMethod<Foo, ?>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName()))).add(processSyntheticBean(new TypeLiteral<ProcessSyntheticBean<Integer>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName()))).build();
    try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bravo.class).addExtension(myExtension).addExtension(myExtension2).initialize()) {
        assertTrue(container.select(Foo.class).isUnsatisfied());
        assertFalse(container.select(Bravo.class).isUnsatisfied());
        Assert.assertEquals(Integer.valueOf(10), container.select(Integer.class, Juicy.Literal.INSTANCE).get());
    }
    ActionSequence.assertSequenceDataContainsAll(BeforeBeanDiscovery.class, AfterTypeDiscovery.class, AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class);
    ActionSequence.assertSequenceDataContainsAll(ProcessBeanAttributes.class, ProcessSyntheticAnnotatedType.class, ProcessInjectionPoint.class, ProcessObserverMethod.class, ProcessBeanAttributes.class, ProcessProducer.class);
    ActionSequence.assertSequenceDataContainsAll(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName(), ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName(), ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName());
    ActionSequence.assertSequenceDataContainsAll(ProcessBean.class, ProcessManagedBean.class, ProcessProducerMethod.class, ProcessProducerField.class);
}
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) WeldContainer(org.jboss.weld.environment.se.WeldContainer) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) ProcessObserverMethod(javax.enterprise.inject.spi.ProcessObserverMethod) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) Weld(org.jboss.weld.environment.se.Weld) Extension(javax.enterprise.inject.spi.Extension) ProcessManagedBean(javax.enterprise.inject.spi.ProcessManagedBean) ProcessSyntheticBean(javax.enterprise.inject.spi.ProcessSyntheticBean) TypeLiteral(javax.enterprise.util.TypeLiteral) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Test(org.junit.Test)

Example 24 with WeldContainer

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

the class WeldContainerCurrentTest method testCorrectContainerReturned.

@Test
public void testCorrectContainerReturned() {
    String containerId = "007";
    try (WeldContainer container = new Weld(containerId).disableDiscovery().addBeanClass(DumbBean.class).initialize()) {
        Assert.assertTrue(container.isRunning());
        Assert.assertTrue(WeldContainer.current().getId().equals(containerId));
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 25 with WeldContainer

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

the class ContainerEventsTest method testEventsFired.

@Test
public void testEventsFired() {
    String id = "EVENT";
    ActionSequence.reset();
    try (WeldContainer container = new Weld(id).disableDiscovery().beanClasses(ContainerObserver.class).initialize()) {
        assertFalse(container.select(ContainerObserver.class).isUnsatisfied());
    }
    List<String> sequenceData = ActionSequence.getSequenceData();
    assertEquals(6, sequenceData.size());
    assertTrue(sequenceData.contains(ContainerInitialized.class.getName() + id));
    assertTrue(sequenceData.contains(ContainerInitialized.class.getName() + ApplicationScoped.class.getName() + id));
    assertTrue(sequenceData.contains(ContainerBeforeShutdown.class.getName() + id));
    assertTrue(sequenceData.contains(ContainerBeforeShutdown.class.getName() + ApplicationScoped.class.getName() + id));
    assertTrue(sequenceData.contains(ContainerShutdown.class.getName() + id));
    assertTrue(sequenceData.contains(ContainerShutdown.class.getName() + ApplicationScoped.class.getName() + id));
    sequenceData = ActionSequence.getSequenceData(ApplicationScoped.class.getName());
    assertEquals(3, sequenceData.size());
    assertTrue(sequenceData.get(0).equals(ContainerInitialized.class.getName() + ApplicationScoped.class.getName()));
    assertTrue(sequenceData.get(1).equals(ContainerBeforeShutdown.class.getName() + ApplicationScoped.class.getName()));
    assertTrue(sequenceData.get(2).equals(ContainerShutdown.class.getName() + ApplicationScoped.class.getName()));
}
Also used : ContainerShutdown(org.jboss.weld.environment.se.events.ContainerShutdown) WeldContainer(org.jboss.weld.environment.se.WeldContainer) ContainerBeforeShutdown(org.jboss.weld.environment.se.events.ContainerBeforeShutdown) ContainerInitialized(org.jboss.weld.environment.se.events.ContainerInitialized) ApplicationScoped(javax.enterprise.context.ApplicationScoped) 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