use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldBuilderTest method testMultipleWeldInstancesCreated.
@Test
public void testMultipleWeldInstancesCreated() {
Baz.DESTROYED.clear();
Weld weld = new Weld().disableDiscovery();
int loop = 5;
List<WeldContainer> containers = new ArrayList<WeldContainer>();
for (int i = 0; i < loop; i++) {
containers.add(weld.containerId("" + i).beanClasses(Baz.class).initialize());
WeldContainer.getRunningContainerIds().contains(String.valueOf(i));
assertTrue(WeldContainer.getRunningContainerIds().size() == i + 1);
}
for (WeldContainer container : containers) {
assertTrue(container.isRunning());
assertEquals(container.getId(), container.select(Baz.class).get().getVal());
}
weld.shutdown();
assertEquals(loop, Baz.DESTROYED.size());
for (int i = 0; i < loop; i++) {
assertTrue(Baz.DESTROYED.contains("" + i));
}
for (WeldContainer container : containers) {
assertFalse(container.isRunning());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class SimpleInstanceTest method testSelect.
@Test
public void testSelect() throws Exception {
Weld weld = new Weld();
try {
WeldContainer wc = weld.initialize();
Assert.assertNotNull(wc.instance().select(KPT.class).select(new KPQLiteral()).get());
Assert.assertNotNull(wc.instance().select(KPT.class, new KPQLiteral()).get());
} finally {
weld.shutdown();
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class SyntheticBeanArchiveFromDirectoryTest method testAddPackageFromDirectoryRecursively.
@Test
public void testAddPackageFromDirectoryRecursively() {
try (WeldContainer container = new Weld().disableDiscovery().addPackages(true, AlphaFromDirectory.class.getPackage()).initialize()) {
AlphaFromDirectory alpha = container.select(AlphaFromDirectory.class).get();
Instance<BetaFromDirectory> betaInstance = container.select(BetaFromDirectory.class);
assertFalse(betaInstance.isUnsatisfied());
assertNotNull(alpha);
assertNotNull(betaInstance.get());
assertEquals(1, alpha.ping());
assertEquals(1, betaInstance.get().ping());
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldInstanceSelectTypeTest method testDestroy.
@Test
public void testDestroy() {
Type type = DestroyedBean.class;
try (WeldContainer container = new Weld().initialize()) {
WeldInstance<DestroyedBean> instance = container.<DestroyedBean>select(type);
DestroyedBean bean = instance.get();
Assert.assertNotNull(bean);
// destroy and verify
Assert.assertFalse(DestroyedBean.DESTROY_INVOKED);
instance.destroy(bean);
Assert.assertTrue(DestroyedBean.DESTROY_INVOKED);
}
}
use of org.jboss.weld.environment.se.WeldContainer in project core by weld.
the class WeldInstanceSelectTypeTest method testValidSelect.
@Test
public void testValidSelect() {
try (WeldContainer container = new Weld().initialize()) {
// Class<T> implements Type, so let's make use of that
Type firstType = SomeInterface.class;
// Let's also grab a parameterized type
ParameterizedType pType = (ParameterizedType) new TypeLiteral<Foo<Bar>>() {
}.getType();
// Instance<Object> -> Instance<SomeInterface>
WeldInstance<Object> instanceObject = container.select(firstType);
Object object = instanceObject.get();
// this can be casted if needed
Assert.assertTrue(object instanceof SomeInterface);
SomeInterface si = (SomeInterface) object;
Assert.assertEquals(SomeOtherBean.class.getSimpleName(), si.ping());
WeldInstance<Foo<Bar>> parameterizedInstance = container.<Foo<Bar>>select(pType);
Foo<Bar> fooBar = parameterizedInstance.get();
Assert.assertNotNull(fooBar);
}
}
Aggregations