* testInjectionAndSerialization()
*/
@Test
public void testInjectionAndSerialization()
{
MockApplication app = new MockApplication();
app.setServletContext(new MockServletContext(app, null));
try
{
ThreadContext.setApplication(app);
app.setName(getClass().getName());
app.initApplication();
Session session = new WebSession(new MockWebRequest(Url.parse("/")));
app.getSessionStore().bind(null, session);
ThreadContext.setSession(session);
GuiceComponentInjector injector = new GuiceComponentInjector(app, new Module()
{
public void configure(final Binder binder)
{
binder.bind(ITestService.class).to(TestService.class);
binder.bind(ITestService.class)
.annotatedWith(Red.class)
.to(TestServiceRed.class);
binder.bind(ITestService.class)
.annotatedWith(Blue.class)
.to(TestServiceBlue.class);
binder.bind(new TypeLiteral<Map<String, String>>()
{
}).toProvider(new Provider<Map<String, String>>()
{
public Map<String, String> get()
{
Map<String, String> strings = new HashMap<String, String>();
strings.put(ITestService.RESULT, ITestService.RESULT);
return strings;
}
});
}
});
app.getComponentInstantiationListeners().add(injector);
// Create a new component, which should be automatically injected,
// and test to make sure the injection has worked.
TestComponentInterface testComponent = newTestComponent("id");
doChecksForComponent(testComponent);
// Serialize and deserialize the object, and check it still works.
TestComponentInterface clonedComponent = (TestComponentInterface)WicketObjects.cloneObject(testComponent);
doChecksForComponent(clonedComponent);
// Test injection of a class that does not extend Component
TestNoComponentInterface noncomponent = newTestNoComponent();
doChecksForNoComponent(noncomponent);
}
finally
{
app.internalDestroy();
ThreadContext.detach();
}
}