@After public void tearDown() { if (ctx != null) { ctx.close(); } }
@After public void close() { if (context != null) { context.close(); } }
@Test public void importXmlWithAutowiredConfig() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class); String name = ctx.getBean("xmlBeanName", String.class); assertThat(name, equalTo("xml.declared")); ctx.close(); }
@Test public void txManagerIsResolvedOnInvocationOfTransactionalMethod() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( EnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); ctx.close(); }
@Test public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( EnableTxConfig.class, MultiTxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); ctx.close(); }
@Test public void testSelfInjectHierarchy() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ChildConfig.class); assertNotNull(context.getBean(MyComponent.class)); context.close(); }
@Test public void spr14322FindsOnInterfaceWithInterfaceProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14322ConfigA.class); TransactionalTestInterface bean = ctx.getBean(TransactionalTestInterface.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); bean.saveFoo(); bean.saveBar(); assertThat(txManager.begun, equalTo(2)); assertThat(txManager.commits, equalTo(2)); assertThat(txManager.rollbacks, equalTo(0)); ctx.close(); }
@Test public void spr14322FindsOnInterfaceWithCglibProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14322ConfigB.class); TransactionalTestInterface bean = ctx.getBean(TransactionalTestInterface.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); bean.saveFoo(); bean.saveBar(); assertThat(txManager.begun, equalTo(2)); assertThat(txManager.commits, equalTo(2)); assertThat(txManager.rollbacks, equalTo(0)); ctx.close(); }
@Test public void importNonXmlResource() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class); assertTrue(ctx.containsBean("propertiesDeclaredBean")); ctx.close(); }
@Test public void providesBeanMethodBeanDefinition() throws Exception { AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(Conf.class); BeanDefinition beanDefinition = context.getBeanDefinition("myBean"); assertThat("should provide AnnotatedBeanDefinition", beanDefinition, instanceOf(AnnotatedBeanDefinition.class)); Map<String, Object> annotationAttributes = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata().getAnnotationAttributes(MyAnnotation.class.getName()); assertThat(annotationAttributes.get("value"), equalTo("test")); context.close(); }
@Test public void asyncProcessorIsOrderedLowestPrecedenceByDefault() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfig.class); ctx.refresh(); AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE)); ctx.close(); }
@Test public void orderAttributeIsPropagated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(OrderedAsyncConfig.class); ctx.refresh(); AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE)); ctx.close(); }
@Test public void transactionProxyIsCreated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( EnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); Map<?,?> services = ctx.getBeansWithAnnotation(Service.class); assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); ctx.close(); }
@Test public void transactionProxyIsCreatedWithEnableOnSuperclass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( InheritedEnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); Map<?,?> services = ctx.getBeansWithAnnotation(Service.class); assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); ctx.close(); }
@Test public void proxyingOccurs() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfig.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); ctx.close(); }
@Test public void spr12233() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PropertySourcesPlaceholderConfigurer.class); ctx.register(ImportConfiguration.class); ctx.refresh(); ctx.close(); }
@Test public void spr11124MultipleAnnotations() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr11124Config.class); Spr11124Service bean = context.getBean(Spr11124Service.class); bean.single(2); bean.single(2); bean.multiple(2); bean.multiple(2); context.close(); }
@Test public void proxyingOccursWithMockitoStub() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfigWithMockito.class, AsyncBeanUser.class); ctx.refresh(); AsyncBeanUser asyncBeanUser = ctx.getBean(AsyncBeanUser.class); AsyncBean asyncBean = asyncBeanUser.getAsyncBean(); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); ctx.close(); }
@Test public void importWithPlaceholder() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); PropertySource<?> propertySource = new MapPropertySource("test", Collections.<String, Object> singletonMap("test", "springframework")); ctx.getEnvironment().getPropertySources().addFirst(propertySource); ctx.register(ImportXmlConfig.class); ctx.refresh(); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); ctx.close(); }
@Test public void importXml() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class); assertTrue("did not contain java-declared bean", ctx.containsBean("javaDeclaredBean")); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); assertEquals("myName", tb.getName()); ctx.close(); }