@Test public void testExceptionOnConfigParsingWithAmbiguousAdviceMethod() { try { new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ambiguous.xml", getClass()); } catch (BeanCreationException ex) { Throwable cause = ex.getRootCause(); assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException); assertTrue("Cannot resolve method 'myBeforeAdvice' to a unique method", cause.getMessage().contains("Cannot resolve method 'myBeforeAdvice' to a unique method")); } }
@Test public void testExceptionOnConfigParsingWithMismatchedAdviceMethod() { try { new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); } catch (BeanCreationException ex) { Throwable cause = ex.getRootCause(); assertTrue("Should be IllegalArgumentException", cause instanceof IllegalArgumentException); assertTrue("invalidAbsoluteTypeName should be detected by AJ", cause.getMessage().contains("invalidAbsoluteTypeName")); } }
@Test public void multipleCachingConfigurers() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(MultiCacheManagerConfigurer.class, EnableCachingConfig.class); try { ctx.refresh(); } catch (BeanCreationException ex) { Throwable root = ex.getRootCause(); assertTrue(root instanceof IllegalStateException); assertTrue(root.getMessage().contains("implementations of CachingConfigurer")); } }
@Test public void testNotNullConstraint() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class)); ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class)); ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class)); try { ac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause().getMessage().contains("testBean")); assertTrue(ex.getRootCause().getMessage().contains("invalid")); } ac.close(); }
@Test public void testNotNullConstraint() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class)); ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class)); ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class)); try { ac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause().getMessage().contains("testBean")); assertTrue(ex.getRootCause().getMessage().contains("invalid")); } ac.close(); }
@Test public void failsWhenJdkProxyAndScheduledMethodNotPresentOnInterface() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigA.class); try { ctx.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof IllegalStateException); } }
@Test public void multipleCachingConfigurers() { try { load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class); } catch (BeanCreationException ex) { Throwable root = ex.getRootCause(); assertTrue(root instanceof IllegalStateException); assertTrue(ex.getMessage().contains("implementations of CachingConfigurer")); } }
@Test public void testSizeConstraint() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class)); RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class); bd.getPropertyValues().add("testBean", new TestBean()); bd.getPropertyValues().add("stringValue", "s"); ac.registerBeanDefinition("bean", bd); try { ac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause().getMessage().contains("stringValue")); assertTrue(ex.getRootCause().getMessage().contains("invalid")); } ac.close(); }
@Test public void testSizeConstraint() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class)); RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class); bd.getPropertyValues().add("testBean", new TestBean()); bd.getPropertyValues().add("stringValue", "s"); ac.registerBeanDefinition("bean", bd); try { ac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause().getMessage().contains("stringValue")); assertTrue(ex.getRootCause().getMessage().contains("invalid")); } ac.close(); }
@Test public void testIncompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); try { bf.getBean("testBean"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof IllegalStateException); } }
@Test public void testProxyingDecoratorNoInstance() throws Exception { String[] beanNames = this.beanFactory.getBeanNamesForType(ApplicationListener.class); assertTrue(Arrays.asList(beanNames).contains("debuggingTestBeanNoInstance")); assertEquals(ApplicationListener.class, this.beanFactory.getType("debuggingTestBeanNoInstance")); try { this.beanFactory.getBean("debuggingTestBeanNoInstance"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof BeanInstantiationException); } }
@Test public void testIncompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); try { bf.getBean("testBean"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getRootCause() instanceof IllegalStateException); } }
@Test public void autowiredMethodParameterWithSingleNonQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testAutowiredFieldWithSingleNonQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testAutowiredMethodParameterWithSingleNonQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void autowiredFieldWithSingleNonQualifiedCandidate() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); context.registerBeanDefinition(JUERGEN, person); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testAutowiredFieldWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void testAutowiredMethodParameterWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void autowiredFieldWithMultipleNonQualifiedCandidates() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }
@Test public void autowiredFieldDoesNotResolveCandidateWithDefaultValueAndConflictingValueOnBeanDefinition() { GenericApplicationContext context = new GenericApplicationContext(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); // qualifier added, and non-default value specified person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "not the default")); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); context.registerBeanDefinition(JUERGEN, person1); context.registerBeanDefinition(MARK, person2); context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class)); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); try { context.refresh(); fail("expected BeanCreationException"); } catch (BeanCreationException e) { assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException); assertEquals("autowired", e.getBeanName()); } }