/**
* Tests runtime context registration
*/
public void testRegisterContextAfterSession() throws Exception {
EventContext ctx = new EventContextImpl();
SessionScopeContext scope = new SessionScopeContext(ctx);
scope.registerFactories(createConfigurations());
scope.start();
Object session = new Object();
// first request
ctx.setIdentifier(HttpSessionEvent.HTTP_IDENTIFIER, session);
SessionScopeComponent comp1 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null);
Assert.assertNotNull(comp1);
ctx.clearIdentifier(HttpSessionEvent.HTTP_IDENTIFIER);
scope.registerFactory(createConfiguration("NewTestService"));
// second request
ctx.setIdentifier(HttpSessionEvent.HTTP_IDENTIFIER, session);
SessionScopeInitDestroyComponent comp2 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService")
.getInstance(null);
Assert.assertNotNull(comp2);
Assert.assertTrue(comp2.isInitialized());
ctx.clearIdentifier(HttpSessionEvent.HTTP_IDENTIFIER);
// third request different session
Object session2 = new Object();
ctx.setIdentifier(HttpSessionEvent.HTTP_IDENTIFIER, session2);
SessionScopeInitDestroyComponent comp3 = (SessionScopeInitDestroyComponent) scope.getContext("NewTestService")
.getInstance(null);
Assert.assertNotNull(comp3);
Assert.assertNotSame(comp2, comp3);
Assert.assertTrue(comp3.isInitialized());
ctx.clearIdentifier(HttpSessionEvent.HTTP_IDENTIFIER);
scope.onEvent(new HttpSessionEnd(this, session));
Assert.assertTrue(comp2.isDestroyed());
scope.onEvent(new HttpSessionEnd(this, session2));