use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class RequestScopedActiveInterceptorTest method requestScopedActiveInNestedInvocation.
@Test
public void requestScopedActiveInNestedInvocation() {
try (WeldContainer container = new Weld().initialize()) {
Foo foo = container.select(Foo.class).get();
Assert.assertEquals(3, foo.pingNested());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class ContainerLifecyleObserverTest method testAddContainerLifecycleObserver.
@SuppressWarnings("serial")
@Test
public void testAddContainerLifecycleObserver() {
final AtomicBoolean called = new AtomicBoolean(false);
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class).addContainerLifecycleObserver(processAnnotatedType(new TypeLiteral<ProcessAnnotatedType<Foo>>() {
}.getType()).notify((e) -> e.veto())).addContainerLifecycleObserver(afterBeanDiscovery((e) -> called.set(true))).initialize()) {
assertTrue(called.get());
assertTrue(container.select(Foo.class).isUnsatisfied());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class ExplicitClassLoaderScanningTest method testScanningExplicitClassLoader.
@Test
public void testScanningExplicitClassLoader() throws IOException {
// this is the application
final Archive<?> archive = create(BeanArchive.class).addClass(EmbeddedApplication.class);
final File jar = File.createTempFile("weld-se-test", ".jar");
jar.deleteOnExit();
archive.as(ZipExporter.class).exportTo(jar, true);
/*
* Special classloader that hides BDAs in parent classloaders. This would not be needed normally. We need this here because
* , since this testsuite defines a top-level beans.xml file, each file in this testsuite is already part of this single giant BDA.
* Since we are adding the EmbeddedApplication class to the special BDA we test, we do not want the class to be found twice. We cannot just leave
* out the parent classloader as we need CDI classes to be loadable from the application.
*/
ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (AbstractWeldDeployment.BEANS_XML.equals(name)) {
return findResources(name);
}
return super.getResources(name);
}
};
try (WeldContainer weld = new Weld().setClassLoader(classLoader).initialize()) {
AtomicInteger payload = new AtomicInteger();
weld.event().fire(payload);
Assert.assertEquals(10, payload.intValue());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class ExplicitResourceLoaderScanningTest method testScanning.
@Test
public void testScanning() throws IOException {
// this is the application
final Archive<?> archive = create(BeanArchive.class).addClass(EmbeddedApplication.class);
final File jar = File.createTempFile("weld-se-resource-loader-test", ".jar");
jar.deleteOnExit();
archive.as(ZipExporter.class).exportTo(jar, true);
/*
* Special classloader that hides BDAs in parent classloaders. This would not be needed normally. We need this here because
* , since this testsuite defines a top-level beans.xml file, each file in this testsuite is already part of this single giant BDA.
* Since we are adding the EmbeddedApplication class to the special BDA we test, we do not want the class to be found twice. We cannot just leave
* out the parent classloader as we need CDI classes to be loadable from the application.
*/
ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (AbstractWeldDeployment.BEANS_XML.equals(name)) {
return findResources(name);
}
return super.getResources(name);
}
};
try (WeldContainer weld = new Weld().setResourceLoader(new ClassLoaderResourceLoader(classLoader)).initialize()) {
AtomicInteger payload = new AtomicInteger();
weld.event().fire(payload);
Assert.assertEquals(10, payload.intValue());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldContainerCurrentTest method testExceptionThrownWithMultipleContainers.
@Test
public void testExceptionThrownWithMultipleContainers() {
String containerIdOne = "007";
String containerIdTwo = "008";
WeldContainer containerOne = new Weld(containerIdOne).disableDiscovery().addBeanClass(DumbBean.class).initialize();
WeldContainer containerTwo = new Weld(containerIdTwo).disableDiscovery().addBeanClass(DumbBean.class).initialize();
try {
WeldContainer.current();
} catch (IllegalStateException e) {
// expected
return;
} finally {
containerOne.close();
containerTwo.close();
}
Assert.fail();
}
Aggregations