@Test public void testInvalidBeanNameReference() throws Exception { try { this.beanFactory.getBean("jumble2"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof BeanDefinitionStoreException); assertTrue(ex.getCause().getMessage().contains("rod2")); } }
@Test public void equivalentMappingsWithSameMethodName() throws Exception { try { initServletWithControllers(ChildController.class); fail("Expected 'method already mapped' error"); } catch (BeanCreationException e) { assertTrue(e.getCause() instanceof IllegalStateException); assertTrue(e.getCause().getMessage().contains("Ambiguous mapping")); } }
@Test public void testWithRequiredPropertyOmitted() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .addPropertyValue("name", "Rob Harrop") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("age")); assertTrue(message.contains("testBean")); } }
@Test public void testSpringInitBean() throws Exception { try { beanFactory.getBean("spring-init"); fail("expected security exception"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof SecurityException); } }
@Test public void testWithStaticFactoryMethod() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .setFactoryMethod("create") .addPropertyValue("name", "Rob Harrop") .addPropertyValue("favouriteColour", "Blue") .addPropertyValue("jobTitle", "Grand Poobah") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("age")); assertTrue(message.contains("testBean")); } }
private void testDoubleTargetSourceIsRejected(String name) { try { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS)); bf.getBean(name); fail("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property"); } catch (BeanCreationException ex) { // Root cause of the problem must be an AOP exception AopConfigException aex = (AopConfigException) ex.getCause(); assertTrue(aex.getMessage().contains("TargetSource")); } }
@Test public void testCustomInitBean() throws Exception { try { beanFactory.getBean("custom-init"); fail("expected security exception"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof SecurityException); } }
@Test public void testCustomFactoryObject() throws Exception { try { beanFactory.getBean("spring-factory"); fail("expected security exception"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof SecurityException); } }
@Test public void testWithThreeRequiredPropertiesOmitted() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .addPropertyValue("name", "Rob Harrop") .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Properties")); assertTrue(message.contains("age")); assertTrue(message.contains("favouriteColour")); assertTrue(message.contains("jobTitle")); assertTrue(message.contains("testBean")); } }
public void xtestTypeMismatch() { try { getBeanFactory().getBean("typeMismatch"); fail("Shouldn't succeed with type mismatch"); } catch (BeanCreationException wex) { assertEquals("typeMismatch", wex.getBeanName()); assertTrue(wex.getCause() instanceof PropertyBatchUpdateException); PropertyBatchUpdateException ex = (PropertyBatchUpdateException) wex.getCause(); // Further tests assertTrue("Has one error ", ex.getExceptionCount() == 1); assertTrue("Error is for field age", ex.getPropertyAccessException("age") != null); assertTrue("We have rejected age in exception", ex.getPropertyAccessException("age").getPropertyChangeEvent().getNewValue().equals("34x")); } }
@Test public void testWithCustomAnnotation() { try { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); BeanDefinition beanDef = BeanDefinitionBuilder .genericBeanDefinition(RequiredTestBean.class) .getBeanDefinition(); factory.registerBeanDefinition("testBean", beanDef); RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor(); rabpp.setRequiredAnnotationType(MyRequired.class); factory.addBeanPostProcessor(rabpp); factory.preInstantiateSingletons(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { String message = ex.getCause().getMessage(); assertTrue(message.contains("Property")); assertTrue(message.contains("name")); assertTrue(message.contains("testBean")); } }
@Test public void testJobLaunchingGatewayNoJobLauncher() throws Exception { try { setUp("JobLaunchingGatewayParserTestsNoJobLauncher-context.xml", getClass()); } catch(BeanCreationException e) { assertEquals("No bean named 'jobLauncher' available", e.getCause().getMessage()); return; } fail("Expected a NoSuchBeanDefinitionException to be thrown."); }
@Test public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() { try { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS)); bf.getBean("targetSourceNotLast"); fail("TargetSource or non-advised object must be last in interceptorNames"); } catch (BeanCreationException ex) { // Root cause of the problem must be an AOP exception AopConfigException aex = (AopConfigException) ex.getCause(); assertTrue(aex.getMessage().contains("interceptorNames")); } }
@Test @SuppressWarnings("resource") public void testServletContextAttributeFactoryBeanWithAttributeNotFound() { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("attributeName", "myAttr"); wac.registerSingleton("importedAttr", ServletContextAttributeFactoryBean.class, pvs); try { wac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getMessage().contains("myAttr")); } }
@Test @SuppressWarnings("resource") public void testServletContextParameterFactoryBeanWithAttributeNotFound() { MockServletContext sc = new MockServletContext(); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("initParamName", "myParam"); wac.registerSingleton("importedParam", ServletContextParameterFactoryBean.class, pvs); try { wac.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); assertTrue(ex.getCause().getMessage().contains("myParam")); } }
/** * Globals must be followed by a target. */ @Test public void testGlobalsWithoutTarget() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(INVALID_CONTEXT, CLASS)); try { bf.getBean("globalsWithoutTarget"); fail("Should require target name"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof AopConfigException); } }
@Test public void testPossibleMatches() { try { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("ag", "foobar"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("tb", bd); lbf.getBean("tb"); fail("Should throw exception on invalid property"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof NotWritablePropertyException); NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); // expected assertEquals(1, cause.getPossibleMatches().length); assertEquals("age", cause.getPossibleMatches()[0]); } }
/** * Test that if a custom initializer throws an exception, it's handled correctly */ @Test public void testInitMethodThrowsException() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT); try { xbf.getBean("init-method2"); fail(); } catch (BeanCreationException ex) { assertTrue(ex.getResourceDescription().contains("initializers.xml")); assertEquals("init-method2", ex.getBeanName()); assertTrue(ex.getCause() instanceof IOException); } }
@Test public void testDefaultLazyInit() throws Exception { InitAndIB.constructed = false; DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_LAZY_CONTEXT); assertFalse(InitAndIB.constructed); xbf.preInstantiateSingletons(); assertTrue(InitAndIB.constructed); try { xbf.getBean("lazy-and-bad"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof IOException); } }
@Test public void eventListenerWorksWithCustomScope() throws Exception { load(CustomScopeTestBean.class); CustomScope customScope = new CustomScope(); this.context.getBeanFactory().registerScope("custom", customScope); CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class); assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy)); this.eventCollector.assertNoEventReceived(proxy.getId()); this.context.publishEvent(new ContextRefreshedEvent(this.context)); this.eventCollector.assertNoEventReceived(proxy.getId()); customScope.active = false; this.context.publishEvent(new ContextRefreshedEvent(this.context)); customScope.active = true; this.eventCollector.assertNoEventReceived(proxy.getId()); TestEvent event = new TestEvent(); this.context.publishEvent(event); this.eventCollector.assertEvent(proxy.getId(), event); this.eventCollector.assertTotalEventsCount(1); try { customScope.active = false; this.context.publishEvent(new TestEvent()); fail("Should have thrown IllegalStateException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); } }