/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.cdi.tck.tests.deployment.packaging.ear.modules;
import static org.jboss.cdi.tck.TestGroups.JAVAEE_FULL;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Extension;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.cdi.tck.AbstractTest;
import org.jboss.cdi.tck.shrinkwrap.EnterpriseArchiveBuilder;
import org.jboss.cdi.tck.shrinkwrap.WebArchiveBuilder;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.beans10.BeansDescriptor;
import org.jboss.shrinkwrap.descriptor.api.spec.se.manifest.ManifestDescriptor;
import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.test.audit.annotations.SpecAssertions;
import org.testng.annotations.Test;
/**
* This test is aimed to verify packaging-related issues in a little bit more complex deployment scenario. The assertions are
* rather informative and redundant as they're already verified in elementary tests.
*
* Note that we DO NOT include test class in EJB module since we wouldn't be able to inject bean from web module (Java EE
* classloading requirements)!
*
* @author Martin Kouba
*/
@Test(groups = JAVAEE_FULL)
public class EnterpriseArchiveModulesTest extends AbstractTest {
/**
* Modules:
* <ul>
* <li>A - EJB jar BDA: Foo, BusinessOperationEventInspector</li>
* <li>B - EJB jar BDA: LegacyServiceProducer</li>
* <li>C - lib BDA: Bar, AlternativeBar, BarInspector, Util, Business, BusinessOperationEvent,
* BusinessOperationObservedEvent, NonEnterprise, Secured, SecurityInterceptor, LoggingDecorator</li>
* <li>D - lib non-BDA: LegacyService, ContainerEventsObserver</li>
* <li>E - web archive
* <ul>
* <li>F - WEB-INF/classes BDA: Baz, Bazinga</li>
* <li>G - WEB-INF/lib BDA: Qux</li>
* <li>H - WEB-INF/classes non-BDA:</li>
* </ul>
* </li>
* </ul>
*
* @return test archive
*/
@Deployment
public static EnterpriseArchive createTestArchive() {
EnterpriseArchive enterpriseArchive = new EnterpriseArchiveBuilder()
// A - default EJB jar
.withTestClassDefinition(EnterpriseArchiveModulesTest.class)
.withClasses(Foo.class, BusinessOperationEventInspector.class)
// C - lib visible to all
.withBeanLibrary(
Descriptors.create(BeansDescriptor.class).createInterceptors()
.clazz(SecurityInterceptor.class.getName()).up(), Bar.class, AlternativeBar.class, Util.class,
Business.class, BusinessOperationEvent.class, BusinessOperationObservedEvent.class,
NonEnterprise.class, Secured.class, SecurityInterceptor.class, LoggingDecorator.class)
// D - lib visible to all
.withLibrary(LegacyService.class).noDefaultWebModule().build();
// B - not visible for ACDE
JavaArchive barArchive = ShrinkWrap.create(JavaArchive.class, "bar.jar")
.addClasses(BarInspector.class, ContainerEventsObserver.class, LegacyServiceProducer.class)
.addAsServiceProvider(Extension.class, ContainerEventsObserver.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
enterpriseArchive.addAsModule(barArchive);
// E - not visible for ABCD
WebArchive bazArchive = new WebArchiveBuilder()
.notTestArchive()
// F - with enabled decorator
.withClasses(Baz.class, EnterpriseArchiveModulesTest.class)
.withBeansXml(
Descriptors.create(BeansDescriptor.class).createDecorators().clazz(LoggingDecorator.class.getName())
.up().createAlternatives().clazz(AlternativeBar.class.getName()).up())
// G
.withBeanLibrary(Qux.class)
.build()
// Make A and B visible in a portable way
.setManifest(
new StringAsset(Descriptors.create(ManifestDescriptor.class)
.addToClassPath(EnterpriseArchiveBuilder.DEFAULT_EJB_MODULE_NAME).addToClassPath("bar.jar")
.exportAsString()));
enterpriseArchive.addAsModule(bazArchive);
return enterpriseArchive;
}
@Inject
private Business foo;
@Inject
private BusinessOperationEventInspector inspector;
@Inject
private BarInspector barInspector;
@NonEnterprise
@Inject
private Business baz;
@Test
@SpecAssertions({ @SpecAssertion(section = "11.5", id = "bb") })
public void testExtensionAndContainerEvents() throws Exception {
// Test extension registration and container lifecycle events
assertTrue(ContainerEventsObserver.allEventsOk());
}
@Test
@SpecAssertions({ @SpecAssertion(section = "12.1", id = "bba"), @SpecAssertion(section = "12.1", id = "bbb"),
@SpecAssertion(section = "12.1", id = "bbc"), @SpecAssertion(section = "12.1", id = "bbe"),
@SpecAssertion(section = "5.2.1", id = "n") })
public void testVisibilityAndInterceptorEnablement() throws Exception {
SecurityInterceptor.reset();
inspector.reset();
foo.businessOperation1();
foo.businessOperation2();
assertEquals(inspector.getBusinessOperationObservations(), 2);
// Interceptor is enabled in C only; observed method intercepted
assertEquals(SecurityInterceptor.getNumberOfInterceptions(), 1);
}
@Test
@SpecAssertions({ @SpecAssertion(section = "8.3", id = "aa"), @SpecAssertion(section = "8.2", id = "a") })
public void testDecoratorEnablement() throws Exception {
// Test LoggingDecorator is enabled in F only
LoggingDecorator.reset();
baz.businessOperation1();
foo.businessOperation1();
assertEquals(LoggingDecorator.getNumberOfDecorationsPerformed(), 1);
}
@Test
@SpecAssertions({ @SpecAssertion(section = "3.3", id = "aa"), @SpecAssertion(section = "3.3", id = "c"),
@SpecAssertion(section = "10.2", id = "i") })
public void testProducerAndEventDuringDisposal() throws Exception {
// Test legacy service producer in B, bean from F is observing event fired during legacy service disposal
Bean<LegacyService> bean = getUniqueBean(LegacyService.class);
CreationalContext<LegacyService> ctx = getCurrentManager().createCreationalContext(bean);
LegacyService instance = bean.create(ctx);
bean.destroy(instance, ctx);
assertTrue(LegacyService.disposalObserved);
}
@Test
@SpecAssertions({ @SpecAssertion(section = "5.1.1", id = "c") })
public void testAlternatives() throws Exception {
Set<Bean<?>> beans = getCurrentManager().getBeans(AlternativeBar.class);
assertEquals(beans.size(), 1);
assertEquals(beans.iterator().next().getBeanClass(), AlternativeBar.class);
// Bar alternative is enabled in A only
assertFalse(barInspector.getBar().isAlternative());
}
}