Search in sources :

Example 21 with TestContainer

use of org.jboss.arquillian.container.weld.embedded.mock.TestContainer in project core by weld.

the class NonBdaExtensionTest method testEventsSentOnceOnly.

/*
    * description = "WELD-258"
    */
@Test
public void testEventsSentOnceOnly() {
    // Create the BDA in which we will deploy Observer1 and Foo. This is equivalent to a war or ejb jar
    final BeanDeploymentArchiveImpl bda1 = new BeanDeploymentArchiveImpl("1", CountingObserver1.class, Foo.class);
    // Create the BDA to return from loadBeanDeploymentArchive for Observer2, this is probably a library, though could be another war or ejb jar
    // bda2 is accessible from bda1, but isn't added to it's accessibility graph by default. This similar to an archive which doesn't contain a beans.xml but does contain an extension
    final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("2", CountingObserver2.class);
    // Create a deployment, that we can use to mirror the structure of one Extension inside a BDA, and one outside
    Deployment deployment = new FlatDeployment(bda1, new Observer1(), new Observer2(), new CountingObserver1(), new CountingObserver2()) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            // Return bda2 if it is Observer2. Stick anything else which this test isn't about in bda1
            if (beanClass.equals(CountingObserver2.class) || beanClass.equals(Bar.class)) {
                // If Observer2 is requested, then we need to add bda2 to the accessibility graph of bda1
                bda1.getBeanDeploymentArchives().add(bda2);
                return bda2;
            } else {
                return bda1;
            }
        }
    };
    TestContainer container = new TestContainer(deployment);
    // Cause the container to deploy the beans etc.
    container.startContainer();
    // Get the bean manager for bda1 and bda2
    BeanManager beanManager1 = container.getBeanManager(bda1);
    CountingObserver1 observer1 = Utils.getReference(beanManager1, CountingObserver1.class);
    CountingObserver2 observer2 = Utils.getReference(beanManager1, CountingObserver2.class);
    Assert.assertEquals(1, observer1.getBeforeBeanDiscovery());
    Assert.assertEquals(1, observer1.getProcessFooManagedBean());
    Assert.assertEquals(1, observer1.getProcessBarManagedBean());
    Assert.assertEquals(1, observer2.getBeforeBeanDiscovery());
    Assert.assertEquals(1, observer2.getProcessFooManagedBean());
    Assert.assertEquals(1, observer2.getProcessBarManagedBean());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Example 22 with TestContainer

use of org.jboss.arquillian.container.weld.embedded.mock.TestContainer in project core by weld.

the class InjectionTargetCreationTest method test.

@Test
public void test() {
    // Create the BDA in which we will deploy FooExtension. This BDA does not have access to bda2
    final BeanDeploymentArchive bda1 = new BeanDeploymentArchiveImpl("1", FooExtension.class);
    // Create the BDA in which we will deploy FooTarget and SimpleBean. This BDA does not have access to bda1
    final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("2", SimpleBean.class, FooTarget.class);
    final FooExtension extension = new FooExtension();
    // Create a deployment, that we can use to mirror the structure of one Extension inside a BDA, and one outside
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { bda1, bda2 }, extension) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            // Return bda2 if it is Observer2. Stick anything else which this test isn't about in bda1
            if (beanClass.equals(FooExtension.class)) {
                return bda1;
            } else {
                return bda2;
            }
        }

        public BeanDeploymentArchive getBeanDeploymentArchive(Class<?> beanClass) {
            return loadBeanDeploymentArchive(beanClass);
        }
    };
    TestContainer container = new TestContainer(deployment);
    // Cause the container to deploy the beans etc.
    container.startContainer();
    InjectionTarget<FooTarget> target = extension.getTarget();
    CreationalContext<FooTarget> ctx = container.getBeanManager(bda2).createCreationalContext(null);
    FooTarget instance = target.produce(ctx);
    target.postConstruct(instance);
    target.inject(instance, ctx);
    /*
         * Verify that the BeanManager for BDA2 (which can see SimpleBean) is used to inject FooTarget
         */
    assertNotNull(instance.getBean());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) Test(org.testng.annotations.Test)

Example 23 with TestContainer

use of org.jboss.arquillian.container.weld.embedded.mock.TestContainer in project core by weld.

the class TransitiveResolutionTest method testTypicalEarStructure.

/*
    * description = "WELD-236"
    */
@Test
public void testTypicalEarStructure() {
    // Create the BDA in which we will deploy Foo. This is equivalent to a ejb
    // jar
    final BeanDeploymentArchiveImpl ejbJar = new BeanDeploymentArchiveImpl("ejb-jar", Foo.class);
    // Create the BDA in which we will deploy Bar. This is equivalent to a war
    final BeanDeploymentArchiveImpl war = new BeanDeploymentArchiveImpl("war", Bar.class);
    // The war can access the ejb jar
    war.getBeanDeploymentArchives().add(ejbJar);
    // Create a deployment, any other classes are put into the ejb-jar (not
    // relevant to test)
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { war, ejbJar }) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return ejbJar;
        }
    };
    TestContainer container = new TestContainer(deployment);
    container.startContainer();
    container.ensureRequestActive();
    // Get the bean manager for war and ejb jar
    BeanManager warBeanManager = container.getBeanManager(war);
    BeanManager ejbJarBeanManager = container.getBeanManager(ejbJar);
    Assert.assertEquals(1, warBeanManager.getBeans(Bar.class).size());
    Assert.assertEquals(1, warBeanManager.getBeans(Foo.class).size());
    Assert.assertEquals(1, ejbJarBeanManager.getBeans(Foo.class).size());
    Assert.assertEquals(0, ejbJarBeanManager.getBeans(Bar.class).size());
    Bar bar = Utils.getReference(warBeanManager, Bar.class);
    Assert.assertNotNull(bar.getFoo());
    Assert.assertNotNull(bar.getBeanManager());
    Assert.assertEquals(warBeanManager, bar.getBeanManager());
    Assert.assertEquals(ejbJarBeanManager, bar.getFoo().getBeanManager());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Example 24 with TestContainer

use of org.jboss.arquillian.container.weld.embedded.mock.TestContainer in project core by weld.

the class TransitiveResolutionTest method testInterceptorEnabledInWarButPackagedInEjbJar.

/*
    * WELD-507
    */
@Test
public void testInterceptorEnabledInWarButPackagedInEjbJar() {
    // Create the BDA in which we will deploy Foo. This is equivalent to a ejb
    // jar
    final BeanDeploymentArchiveImpl ejbJar = new BeanDeploymentArchiveImpl("ejb-jar", Basic.class, BasicInterceptor.class, Simple.class);
    // Create the BDA in which we will deploy Bar. This is equivalent to a war
    BeansXml beansXml = new BeansXmlImpl(null, null, null, Collections.singletonList(BasicInterceptor.class.getName()));
    final BeanDeploymentArchiveImpl war = new BeanDeploymentArchiveImpl("war", beansXml, Complex.class);
    // The war can access the ejb jar
    war.getBeanDeploymentArchives().add(ejbJar);
    // Create a deployment, any other classes are put into the ejb-jar (not
    // relevant to test)
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { ejbJar, war }) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return ejbJar;
        }
    };
    TestContainer container = new TestContainer(deployment);
    container.startContainer();
    container.ensureRequestActive();
    // Get the bean manager for war and ejb jar
    BeanManager warBeanManager = container.getBeanManager(war);
    BeanManager ejbJarBeanManager = container.getBeanManager(ejbJar);
    BasicInterceptor.reset();
    Simple simple = getReference(ejbJarBeanManager, Simple.class);
    simple.ping("14");
    Assert.assertNull(BasicInterceptor.getTarget());
    BasicInterceptor.reset();
    Complex complex = getReference(warBeanManager, Complex.class);
    complex.ping("14");
    Assert.assertNotNull(BasicInterceptor.getTarget());
    Assert.assertTrue(BasicInterceptor.getTarget() instanceof Complex);
    assertEquals("14", ((Complex) BasicInterceptor.getTarget()).getId());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Aggregations

TestContainer (org.jboss.arquillian.container.weld.embedded.mock.TestContainer)24 Test (org.testng.annotations.Test)21 BeanDeploymentArchiveImpl (org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl)11 FlatDeployment (org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment)11 Deployment (org.jboss.weld.bootstrap.spi.Deployment)10 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)10 BeanManager (javax.enterprise.inject.spi.BeanManager)9 AbstractClusterTest (org.jboss.weld.mock.cluster.AbstractClusterTest)8 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)6 BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)2 Conversation (javax.enterprise.context.Conversation)1 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)1 Car (org.atinject.tck.auto.Car)1 Environment (org.jboss.weld.bootstrap.api.Environment)1 BoundConversationContext (org.jboss.weld.context.bound.BoundConversationContext)1 BoundRequest (org.jboss.weld.context.bound.BoundRequest)1 EjbDescriptor (org.jboss.weld.ejb.spi.EjbDescriptor)1 MockEjbServices (org.jboss.weld.mock.MockEjbServices)1 AfterClass (org.testng.annotations.AfterClass)1 BeforeClass (org.testng.annotations.BeforeClass)1