MockExternalContext externalContext = new MockExternalContext(servletContext);
Context appContext = new ApplicationContext( externalContext.getApplicationMap() );
appContext.set( Seam.getComponentName(Init.class), new Init() );
appContext.set(
Seam.getComponentName(ConversationEntries.class) + ".component",
new Component(ConversationEntries.class, appContext)
);
appContext.set(
Seam.getComponentName(Manager.class) + ".component",
new Component(Manager.class, appContext)
);
appContext.set(
Seam.getComponentName(Foo.class) + ".component",
new Component(Foo.class, appContext)
);
appContext.set(
Seam.getComponentName(Factory.class) + ".component",
new Component(Factory.class, appContext)
);
FacesLifecycle.beginRequest(externalContext);
Manager.instance().setCurrentConversationId("1");
FacesLifecycle.resumeConversation(externalContext);
FacesLifecycle.setPhaseId(PhaseId.RENDER_RESPONSE);
final Bar bar = new Bar();
final Foo foo = new Foo();
Contexts.getSessionContext().set("otherFoo", foo);
BijectionInterceptor bi = new BijectionInterceptor();
bi.setComponent( new Component(Bar.class, appContext) );
String result = (String) bi.aroundInvoke( new MockInvocationContext() {
@Override
public Object getTarget()
{
return bar;
}
@Override
public Object proceed() throws Exception
{
assert bar.otherFoo==foo;
assert bar.foo!=null;
return bar.foo();
}
});
assert "foo".equals(result);
assert Contexts.getEventContext().get("otherString").equals("outAgain");
assert Contexts.getConversationContext().get("string").equals("out");
assert Contexts.getSessionContext().isSet("foo");
assert bar.foo==null;
assert bar.otherFoo==null;
final Method method;
try
{
method = Bar.class.getMethod("foo");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
bi.aroundInvoke( new MockInvocationContext() {
@Override
public Object getTarget()
{
return bar;
}
@Override
public Object proceed() throws Exception
{
assert bar.otherFoo==foo;
assert bar.foo!=null;
return bar.foo();
}
@Override
public Method getMethod()
{
return method;
}
});
assert bar.foo==null;
assert bar.otherFoo==null;
try
{
Contexts.getSessionContext().remove("otherFoo");
bi.aroundInvoke( new MockInvocationContext() {
@Override
public Object getTarget()
{
return bar;
}
@Override
public Object proceed() throws Exception
{
assert false;
return null;
}
@Override
public Method getMethod()
{
return method;
}
});
assert false;
}
catch (Exception e)
{
assert e instanceof RequiredException;
}
final Method method2;
try
{
method2 = BrokenAction.class.getMethod("go");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
final BrokenAction brokenAction = new BrokenAction();
BijectionInterceptor biba = new BijectionInterceptor();
biba.setComponent( new Component(BrokenAction.class, appContext) );
try
{
biba.aroundInvoke( new MockInvocationContext() {
@Override
public Object getTarget() {
return brokenAction;
}
@Override
public Object proceed() throws Exception {
assert false;
return null;
}
@Override
public Method getMethod()
{
return method2;
}
} );
assert false;
}
catch (Exception e)
{
assert e instanceof RequiredException;
}
final Method method3;
try
{
method3 = Action.class.getMethod("go");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
final Action action = new Action();
BijectionInterceptor bia = new BijectionInterceptor();
bia.setComponent( new Component(Action.class, appContext) );
result = (String) bia.aroundInvoke( new MockInvocationContext() {
@Override
public Object getTarget() {
return action;