Skip to content

Commit

Permalink
WELD-2794 Add an automated test covering a related issue
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jul 31, 2024
1 parent 0bad05e commit 71ad977
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jboss.weld.tests.accessibility.bce;

import static org.junit.Assert.assertTrue;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;

import jakarta.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.BeanDiscoveryMode;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.BeansXml;
import org.jboss.weld.test.util.Utils;
import org.jboss.weld.tests.accessibility.bce.lib.MyBeanCreator;
import org.jboss.weld.tests.accessibility.bce.lib.MyBce;
import org.jboss.weld.tests.accessibility.bce.lib.SomeType;
import org.jboss.weld.tests.category.Integration;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

/**
* Tests a scenario in which an application (WAR) has a library (/lib) which isn't a bean archive but has registered BCE.
* While such BCE creates synth bean, its {@code Instance} should be able to access beans from the application (WAR)
*/
@RunWith(Arquillian.class)
@Category(Integration.class)
public class BceInLibTest {

@Deployment
public static WebArchive getDeployment() {
WebArchive war = ShrinkWrap
.create(WebArchive.class, Utils.getDeploymentNameAsHash(BceInLibTest.class, Utils.ARCHIVE_TYPE.WAR))
.addClasses(MyBean.class)
.addAsWebInfResource(new BeansXml(BeanDiscoveryMode.ANNOTATED), "beans.xml");
// archive with extension and no beans.xml == not a bean archive
JavaArchive lib = ShrinkWrap.create(JavaArchive.class)
.addClasses(MyBce.class, MyBeanCreator.class, SomeType.class)
.addAsServiceProvider(BuildCompatibleExtension.class, MyBce.class);
return war.addAsLibrary(lib);
}

@Inject
Instance<Object> instance;

@Test
public void testVisibility() {
// assert dummy bean is available
assertTrue(instance.select(MyBean.class).isResolvable());
// assert we can select type added through BCE from here
assertTrue(instance.select(SomeType.class).isResolvable());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.weld.tests.accessibility.bce;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.weld.tests.accessibility.bce.lib;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.Synthesis;
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents;

public class MyBce implements BuildCompatibleExtension {


@Synthesis
public void registerSynthBean(SyntheticComponents syntheticComponents) {
syntheticComponents.addBean(SomeType.class)
.type(SomeType.class)
.createWith(MyBeanCreator.class)
.scope(Dependent.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.weld.tests.accessibility.bce.lib;

import static org.junit.Assert.assertTrue;

import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.build.compatible.spi.Parameters;
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator;
import org.jboss.weld.tests.accessibility.bce.MyBean;

public class MyBeanCreator implements SyntheticBeanCreator<SomeType> {
@Override
public SomeType create(Instance<Object> lookup, Parameters params) {
// assert that Instance provided here (BCE in non bean archive lib) can "see" MyBean
assertTrue(lookup.select(MyBean.class).isResolvable());
return new SomeType();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jboss.weld.tests.accessibility.bce.lib;

public class SomeType {
}

0 comments on commit 71ad977

Please sign in to comment.