SpringBasedContainer extends AbstractComponentRegistry implements Container { public <T> T instanceFor(Class<T> type) { try { return parentContext.getBean(type); } catch (NoSuchBeanDefinitionException e) { Map<String, T> beans = parentContext.getBeansOfType(type); for (Entry<String, T> def : beans.entrySet()) { BeanDefinition definition = parentContext.getBeanFactory().getBeanDefinition(def.getKey()); if (isPrimary(definition) || hasGreaterRoleThanInfrastructure(definition)) { return def.getValue(); } } throw e; } } SpringBasedContainer(ConfigurableWebApplicationContext parentContext); void register(Class<?> requiredType, Class<?> componentType); T instanceFor(Class<T> type); boolean canProvide(Class<T> type); void start(ServletContext context); void stop();  }
@Test public void twoClassesWithSameNameButDifferentPackages() throws Exception { SameName instance1 = container.instanceFor(SameName.class); br.com.caelum.vraptor.ioc.spring.components.sub.SameName instance2 = container .instanceFor(br.com.caelum.vraptor.ioc.spring.components.sub.SameName.class); assertNotNull(instance1); assertNotNull(instance2); }
@Test public void shouldScanAndRegisterAnnotatedBeans() { DummyComponent component = container.instanceFor(DummyComponent.class); assertNotNull("can instantiate", component); assertTrue("is the right implementation", component instanceof DummyImplementation); }
@Test public void shouldSupportOtherStereotypeAnnotations() { SpecialImplementation component = container.instanceFor(SpecialImplementation.class); assertNotNull("can instantiate", component); }
@Test public void shouldSupportConstructorInjection() { ConstructorInjection component = container.instanceFor(ConstructorInjection.class); assertNotNull("can instantiate", component); assertNotNull("inject dependencies", component.getDependecy()); }
@Test public void shouldProvideCurrentHttpRequest() { ServletRequest httpRequest = container.instanceFor(ServletRequest.class); assertNotNull("can provide request", httpRequest); }
@Test public void shouldProvideCurrentVRaptorRequest() { RequestInfo vraptorRequest = container.instanceFor(RequestInfo.class); assertNotNull("can provide request", vraptorRequest); }
@Test public void shouldProvideServletContext() { ServletContext context = container.instanceFor(ServletContext.class); assertNotNull("can provide ServletContext", context); }
@Test public void shouldProvideTheContainer() { Container itself = this.container.instanceFor(Container.class); assertNotNull("can provide the container", itself); }
@Test public void shoudRegisterResourcesInRouter() { Router router = container.instanceFor(Router.class); Matcher<Iterable<? super Route>> hasItem = hasItem(canHandle(DummyResource.class, DummyResource.class .getDeclaredMethods()[0])); assertThat(router.allRoutes(), hasItem); }
@Test public void shoudRegisterConvertersInConverters() { Converters converters = container.instanceFor(Converters.class); Converter<?> converter = converters.to(Foo.class); assertThat(converter, is(instanceOf(DummyConverter.class))); }
@Test public void shoudRegisterInterceptorsInInterceptorRegistry() { InterceptorRegistry registry = container.instanceFor(InterceptorRegistry.class); assertThat(registry.all(), hasItem(DummyInterceptor.class)); }