Assert.assertNotNull(cdiContainer);
cdiContainer.boot();
cdiContainer.getContextControl().startContexts();
BeanManager beanManager = cdiContainer.getBeanManager();
Assert.assertNotNull(beanManager);
Set<Bean<?>> beans = beanManager.getBeans(CarRepair.class);
Bean<?> bean = beanManager.resolve(beans);
CarRepair carRepair = (CarRepair)
beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));
Assert.assertNotNull(carRepair);
Car car = carRepair.getCar();
Assert.assertNotNull(car);
Assert.assertNotNull(car.getUser());
carRepair.getCar().getUser().setName("tester");
Assert.assertEquals("tester", car.getUser().getName());
Assert.assertFalse(CarRepair.isPreDestroyCalled());
Assert.assertFalse(Car.isPreDestroyCalled());
Assert.assertFalse(TestUser.isPreDestroyCalled());
cdiContainer.getContextControl().stopContexts();
Assert.assertTrue(CarRepair.isPreDestroyCalled());
Assert.assertTrue(Car.isPreDestroyCalled());
Assert.assertTrue(TestUser.isPreDestroyCalled());
try
{
car.getUser();
// accessing the car should have triggered a ContextNotActiveException
Assert.fail();
}
catch (ContextNotActiveException e)
{
//do nothing - exception expected
}
cdiContainer.getContextControl().startContexts();
carRepair = (CarRepair)
beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));
Assert.assertNotNull(carRepair.getCar());
Assert.assertNotNull(carRepair.getCar().getUser());
Assert.assertNull(carRepair.getCar().getUser().getName());