{
@Test
public void load_application_context_externally()
{
ServletContext servletContext = mockServletContext();
ConfigurableWebApplicationContext ac = newMock(ConfigurableWebApplicationContext.class);
Runnable fred = mockRunnable();
Runnable barney = mockRunnable();
ServiceBuilderResources resources = mockServiceBuilderResources();
train_getInitParameter(servletContext, SpringConstants.USE_EXTERNAL_SPRING_CONTEXT, "true");
train_getAttribute(servletContext, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
expect(ac.getBeanDefinitionNames()).andReturn(new String[] {"fred", "&barney"});
replay();
SpringModuleDef moduleDef = new SpringModuleDef(servletContext);
ServiceDef serviceDef = moduleDef.getServiceDef(SpringModuleDef.SERVICE_ID);
ObjectCreator serviceCreator = serviceDef.createServiceCreator(resources);
assertSame(serviceCreator.createObject(), ac);
verify();
// Now, let's test for some of the services.
ServiceDef sd = moduleDef.getServiceDef("ApplicationContext");
assertEquals(sd.getServiceInterface(), ac.getClass());
assertEquals(sd.createServiceCreator(null).toString(),
"<ObjectCreator for externally configured Spring ApplicationContext>");
expect((Class)ac.getType("fred")).andReturn(Runnable.class);
expect(ac.getBean("fred")).andReturn(fred);
sd = moduleDef.getServiceDef("fred");
replay();
assertEquals(sd.getServiceId(), "fred");
assertEquals(sd.getServiceInterface(), Runnable.class);
assertEquals(sd.getServiceScope(), ScopeConstants.DEFAULT);
assertSame(sd.createServiceCreator(null).createObject(), fred);
assertTrue(sd.getMarkers().isEmpty());
assertFalse(sd.isEagerLoad());
assertEquals(sd.createServiceCreator(null).toString(), "ObjectCreator<Spring Bean 'fred'>");
verify();
expect((Class)ac.getType("barney")).andReturn(Runnable.class);
expect(ac.getBean("barney")).andReturn(barney);
replay();
sd = moduleDef.getServiceDef("barney");