use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testSyntheticBeanArchivePackages.
@Test
public void testSyntheticBeanArchivePackages() throws Exception {
Weld weld = new Weld().disableDiscovery();
try (WeldContainer container = weld.packages(Alpha1.class).initialize()) {
assertEquals(1, container.select(Alpha1.class).get().getVal());
assertEquals(2, container.select(Alpha2.class).get().getVal());
assertTrue(container.select(Beta1.class).isUnsatisfied());
assertTrue(container.select(Beta2.class).isUnsatisfied());
}
try (WeldContainer container = weld.reset().addPackage(true, Alpha2.class).initialize()) {
assertEquals(1, container.select(Alpha1.class).get().getVal());
assertEquals(2, container.select(Alpha2.class).get().getVal());
assertEquals(11, container.select(Beta1.class).get().getVal());
assertEquals(22, container.select(Beta2.class).get().getVal());
}
try (WeldContainer container = weld.reset().addPackages(true, Beta1.class).initialize()) {
assertTrue(container.select(Alpha1.class).isUnsatisfied());
assertTrue(container.select(Alpha2.class).isUnsatisfied());
assertEquals(11, container.select(Beta1.class).get().getVal());
assertEquals(22, container.select(Beta2.class).get().getVal());
}
// Scan the package from cdi-api.jar
try (WeldContainer container = weld.reset().packages(ObserverException.class).initialize()) {
assertFalse(container.select(ObserverException.class).isUnsatisfied());
}
// Scan the package recursively from cdi-api.jar
try (WeldContainer container = weld.reset().addPackage(true, Any.class).initialize()) {
// There is no managed bean discovered, therefore we only check the the bean class was found
boolean found = false;
for (BeanDeploymentArchive beanDeploymentArchive : Container.instance(container.getId()).beanDeploymentArchives().keySet()) {
if (beanDeploymentArchive.getBeanClasses().contains(DefinitionException.class.getName())) {
found = true;
break;
}
}
assertTrue(found);
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testResetAll.
@Test
public void testResetAll() {
Weld weld = new Weld().containerId("FOO").disableDiscovery().property(ConfigurationKey.RELAXED_CONSTRUCTION.get(), false).beanClasses(Foo.class);
weld.resetAll();
assertTrue(weld.isDiscoveryEnabled());
assertNull(weld.getContainerId());
weld.disableDiscovery();
try (WeldContainer container = weld.beanClasses(Bar.class).initialize()) {
assertTrue(container.select(Foo.class).isUnsatisfied());
assertTrue(container.select(BeanManagerImpl.class).get().getServices().get(WeldConfiguration.class).getBooleanProperty(ConfigurationKey.BEAN_IDENTIFIER_INDEX_OPTIMIZATION));
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testExtensions.
@Test
public void testExtensions() {
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Bar.class).extensions(new TestExtension()).initialize()) {
assertEquals(10, container.select(Foo.class).get().getVal());
assertFalse(container.select(TestExtension.class).isUnsatisfied());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testLifecycle.
@Test
public void testLifecycle() {
Weld weld = new Weld().disableDiscovery().beanClasses(Foo.class, DependentFoo.class);
try (WeldContainer container = weld.initialize()) {
container.select(DependentFoo.class).get().getVal();
}
assertTrue(DependentFoo.destroyCallbackCalled.get());
DependentFoo.reset();
try (WeldContainer container = weld.initialize()) {
DependentFoo dependentFoo = container.select(DependentFoo.class).get();
dependentFoo.getVal();
container.destroy(dependentFoo);
assertTrue(DependentFoo.destroyCallbackCalled.get());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testInterceptorBuilder.
@Test
public void testInterceptorBuilder() {
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Coorge.class, BuilderInterceptorBinding.class).addContainerLifecycleObserver(ContainerLifecycleObserver.afterBeanDiscovery((e) -> e.addInterceptor().addBinding(new BuilderInterceptorBinding.BuilderInterceptorBindingLiteral()).priority(2500).intercept(InterceptionType.AROUND_INVOKE, invocationContext -> {
try {
Integer result = ((Integer) invocationContext.proceed());
return result + 10;
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}))).initialize()) {
Coorge coorge = container.select(Coorge.class).get();
assertEquals(coorge.ping(), 11);
}
}
Aggregations