/**
* Tests a request-to-session scoped wire is setup properly by the runtime
*/
public void testRequestToSession() throws Exception {
WorkContext ctx = new WorkContextImpl();
final ScopeContainer requestScope = new RequestScopeContainer(ctx);
final ScopeContainer sessionScope = new HttpSessionScopeContainer(ctx);
requestScope.start();
sessionScope.start();
Object session1 = new Object();
ctx.setIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER, session1);
sessionScope.onEvent(new HttpSessionStart(this, session1));
Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", SourceImpl.class,
requestScope, members, "target", Target.class, TargetImpl.class, sessionScope);
final AtomicComponent sourceComponent = contexts.get("source");
final AtomicComponent targetComponent = contexts.get("target");
requestScope.onEvent(new RequestStart(this));
Source source = (Source) sourceComponent.getServiceInstance();
Target target = (Target) targetComponent.getServiceInstance();
assertNull(source.getTarget().getString());
assertNull(target.getString());
target.setString("foo");
assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
assertEquals("foo", source.getTarget().getString());
// spin off another request
Executor executor = Executors.newSingleThreadExecutor();
FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
public void run() {
requestScope.onEvent(new RequestStart(this));
Source source2 = (Source) sourceComponent.getServiceInstance();
Target target2 = (Target) targetComponent.getServiceInstance();
assertEquals("foo", target2.getString());
assertEquals("foo", source2.getTarget().getString());
source2.getTarget().setString("bar");
assertEquals("bar", target2.getString());
assertEquals("bar", source2.getTarget().getString());
requestScope.onEvent(new RequestEnd(this));
}
}, null);
executor.execute(future);
future.get();
assertEquals("bar", target.getString());
requestScope.onEvent(new RequestEnd(this));
requestScope.stop();
ctx.clearIdentifier(HttpSessionScopeContainer.HTTP_IDENTIFIER);
sessionScope.onEvent(new HttpSessionEnd(this, session1));
sessionScope.stop();
}