/**
* Tests instance identity is properly maintained
*/
public void testInstanceManagement() throws Exception {
EventContext ctx = new EventContextImpl();
SessionScopeContext scope = new SessionScopeContext(ctx);
scope.registerFactories(createConfigurations());
scope.start();
Object session = new Object();
Object session2 = 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);
// second request
ctx.setIdentifier(HttpSessionEvent.HTTP_IDENTIFIER, session);
SessionScopeComponent comp2 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null);
Assert.assertNotNull(comp2);
Assert.assertSame(comp1, comp2);
ctx.clearIdentifier(HttpSessionEvent.HTTP_IDENTIFIER);
// third request, different session
ctx.setIdentifier(HttpSessionEvent.HTTP_IDENTIFIER, session2);
SessionScopeComponent comp3 = (SessionScopeComponent) scope.getContext("TestService1").getInstance(null);
Assert.assertNotNull(comp3);
Assert.assertNotSame(comp1, comp3); // should be different instances
ctx.clearIdentifier(HttpSessionEvent.HTTP_IDENTIFIER);
scope.onEvent(new HttpSessionEnd(this, session));
scope.onEvent(new HttpSessionEnd(this, session2));
scope.stop();
}