if (ex.contains(BeanCurrentlyInCreationException.class)) { if (logger.isTraceEnabled()) { logger.trace("Bean currently in creation on FactoryBean type check: " + ex);
@Test public void testArgNamesError() { try { new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-error.xml", getClass()); fail("Expected BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(IllegalArgumentException.class)); } }
@Test public void testWithInlineScriptWithLeadingWhitespace() throws Exception { try { new ClassPathXmlApplicationContext("lwspBadGroovyContext.xml", getClass()); fail("Must have thrown a BeanCreationException ('inline:' prefix was preceded by whitespace"); } catch (BeanCreationException expected) { assertTrue(expected.contains(FileNotFoundException.class)); } }
if (ex.contains(BeanCurrentlyInCreationException.class)) { if (logger.isTraceEnabled()) { logger.trace("Bean currently in creation on FactoryBean type check: " + ex);
@Test public void testPrototypeCircleLeadsToException() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.(singleton)", "false"); p.setProperty("kerry.age", "35"); p.setProperty("kerry.spouse", "*rod"); p.setProperty("rod.(class)", TestBean.class.getName()); p.setProperty("rod.(singleton)", "false"); p.setProperty("rod.age", "34"); p.setProperty("rod.spouse", "*kerry"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); try { lbf.getBean("kerry"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Test public void testContextWithInvalidValueType() throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {INVALID_VALUE_TYPE_CONTEXT}, false); try { context.refresh(); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(TypeMismatchException.class)); assertTrue(ex.toString().contains("someMessageSource")); assertTrue(ex.toString().contains("useCodeAsDefaultMessage")); checkExceptionFromInvalidValueType(ex); checkExceptionFromInvalidValueType(new ExceptionInInitializerError(ex)); assertFalse(context.isActive()); } }
@Test public void circleLeadsToException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { String name = "requestScopedObjectCircle1"; assertNull(request.getAttribute(name)); this.beanFactory.getBean(name); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Test public void testCircularReferencesWithNotAllowed() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); xbf.setAllowCircularReferences(false); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.loadBeanDefinitions(REFTYPES_CONTEXT); try { xbf.getBean("jenny"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Test public void testCircularReferencesWithWrapping() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.loadBeanDefinitions(REFTYPES_CONTEXT); xbf.addBeanPostProcessor(new WrappingPostProcessor()); try { xbf.getBean("jenny"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Test public void testBeanNotAutowiredWithAnnotationConfigDisabled() { GenericApplicationContext context = new GenericApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.setIncludeAnnotationConfig(false); scanner.setBeanNameGenerator(new TestBeanNameGenerator()); int beanCount = scanner.scan(BASE_PACKAGE); assertEquals(7, beanCount); context.refresh(); try { context.getBean("fooService"); } catch (BeanCreationException expected) { assertTrue(expected.contains(BeanInstantiationException.class)); // @Lookup method not substituted } }
/** * Mark the specified bean as already created (or about to be created). * <p>This allows the bean factory to optimize its caching for repeated * creation of the specified bean. * @param beanName the name of the bean */ protected void markBeanAsCreated(String beanName) { if (!this.alreadyCreated.contains(beanName)) { synchronized (this.mergedBeanDefinitions) { if (!this.alreadyCreated.contains(beanName)) { // Let the bean definition get re-merged now that we're actually creating // the bean... just in case some of its metadata changed in the meantime. clearMergedBeanDefinition(beanName); this.alreadyCreated.add(beanName); } } } }
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans) throws BeansException { String[] beanNames = getBeanNamesForType(type, includePrototypes, includeFactoryBeans); Map result = CollectionFactory.createLinkedMapIfPossible(beanNames.length); for (int i = 0; i < beanNames.length; i++) { String beanName = beanNames[i]; try { result.put(beanName, getBean(beanName)); } catch (BeanCreationException ex) { if (ex.contains(BeanCurrentlyInCreationException.class)) { if (logger.isDebugEnabled()) { logger.debug("Ignoring match to currently created bean '" + beanName + "'", ex); } // Ignore: indicates a circular reference when autowiring constructors. // We want to find matches other than the currently created bean itself. } else { throw ex; } } } return result; }
@Test public void testBootstrapWhenThrowsInvalidPropertyExceptionShouldLogException() { //GIVEN given(applicationContext.getBean(WilmaEngine.class)).willThrow(beanCreationException); given(beanCreationException.getMostSpecificCause()).willReturn(invalidPropertyException); given(beanCreationException.contains(InvalidPropertyException.class)).willReturn(true); given(beanCreationException.getCause()).willReturn(invalidPropertyException); given(systemExceptionSelector.getSystemException(beanCreationException)).willReturn(invalidPropertyException); //WHEN underTest.bootstrap(); //THEN verify(systemExceptionSelector).getSystemException(beanCreationException); verify(logger).error(WILMA_CANNOT_BE_STARTED_ERROR_MSG + beanCreationException.getMostSpecificCause().getMessage(), invalidPropertyException); }
@Test public void testBootstrapWhenThrowsSchedulingCannotBeStartedExceptionShouldLogException() { //GIVEN given(applicationContext.getBean(WilmaEngine.class)).willThrow(beanCreationException); given(beanCreationException.getMostSpecificCause()).willReturn(schedulingCannotBeStartedException); given(beanCreationException.contains(SchedulingCannotBeStartedException.class)).willReturn(true); given(beanCreationException.getCause()).willReturn(schedulingCannotBeStartedException); given(systemExceptionSelector.getSystemException(beanCreationException)).willReturn(schedulingCannotBeStartedException); //WHEN underTest.bootstrap(); //THEN verify(systemExceptionSelector).getSystemException(beanCreationException); verify(logger).error(WILMA_CANNOT_BE_STARTED_ERROR_MSG + beanCreationException.getMostSpecificCause().getMessage(), schedulingCannotBeStartedException); }
@Test public void testBootstrapWhenThrowsPropertiesNotAvailableExceptionShouldLogException() { //GIVEN given(applicationContext.getBean(WilmaEngine.class)).willThrow(beanCreationException); given(beanCreationException.getMostSpecificCause()).willReturn(schedulingCannotBeStartedException); given(beanCreationException.contains(PropertiesNotAvailableException.class)).willReturn(true); given(beanCreationException.getCause()).willReturn(schedulingCannotBeStartedException); given(systemExceptionSelector.getSystemException(beanCreationException)).willReturn(schedulingCannotBeStartedException); //WHEN underTest.bootstrap(); //THEN verify(systemExceptionSelector).getSystemException(beanCreationException); verify(logger).error(WILMA_CANNOT_BE_STARTED_ERROR_MSG + beanCreationException.getMostSpecificCause().getMessage(), schedulingCannotBeStartedException); }
@Test public void testBootstrapShouldLogAnyNonWilmaSpecificException() { //GIVEN given(applicationContext.getBean(WilmaEngine.class)).willThrow(beanCreationException); given(beanCreationException.getMostSpecificCause()).willReturn(schedulingCannotBeStartedException); given(beanCreationException.contains(RuntimeException.class)).willReturn(true); given(systemExceptionSelector.getSystemException(beanCreationException)).willReturn(null); given(beanCreationException.getCause()).willReturn(null); //WHEN underTest.bootstrap(); //THEN verify(systemExceptionSelector).getSystemException(beanCreationException); verify(logger).error(WILMA_CANNOT_BE_STARTED_ERROR_MSG, beanCreationException); } }