Search in sources :

Example 11 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method handlerBeanNotFound.

@Test
public void handlerBeanNotFound() throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext root = new XmlWebApplicationContext();
    root.setServletContext(sc);
    root.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map1.xml" });
    root.refresh();
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setParent(root);
    wac.setServletContext(sc);
    wac.setNamespace("map2err");
    wac.setConfigLocations(new String[] { "/org/springframework/web/servlet/handler/map2err.xml" });
    try {
        wac.refresh();
        fail("Should have thrown NoSuchBeanDefinitionException");
    } catch (FatalBeanException ex) {
        NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause();
        assertEquals("mainControlle", nestedEx.getBeanName());
    }
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 12 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolverTests method resolveExceptionWithAssertionErrorAsRootCause.

@Test
public void resolveExceptionWithAssertionErrorAsRootCause() throws Exception {
    AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
    this.resolver.setApplicationContext(cxt);
    this.resolver.afterPropertiesSet();
    AssertionError err = new AssertionError("argh");
    FatalBeanException ex = new FatalBeanException("wrapped", err);
    HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
    ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
    assertNotNull("Exception was not handled", mav);
    assertTrue(mav.isEmpty());
    assertEquals(err.toString(), this.response.getContentAsString());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 13 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project grails-core by grails.

the class GrailsClassUtils method getPropertyDescriptorForValue.

/**
     * Retrieves a PropertyDescriptor for the specified instance and property value
     *
     * @param instance The instance
     * @param propertyValue The value of the property
     * @return The PropertyDescriptor
     */
public static PropertyDescriptor getPropertyDescriptorForValue(Object instance, Object propertyValue) {
    if (instance == null || propertyValue == null) {
        return null;
    }
    PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(instance.getClass());
    for (PropertyDescriptor pd : descriptors) {
        if (isAssignableOrConvertibleFrom(pd.getPropertyType(), propertyValue.getClass())) {
            Object value;
            try {
                ReflectionUtils.makeAccessible(pd.getReadMethod());
                value = pd.getReadMethod().invoke(instance);
            } catch (Exception e) {
                throw new FatalBeanException("Problem calling readMethod of " + pd, e);
            }
            if (propertyValue.equals(value)) {
                return pd;
            }
        }
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) FatalBeanException(org.springframework.beans.FatalBeanException) ConfigObject(groovy.util.ConfigObject) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) FatalBeanException(org.springframework.beans.FatalBeanException) BeansException(org.springframework.beans.BeansException)

Example 14 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method doTestAutowire.

private void doTestAutowire(DefaultListableBeanFactory xbf) throws Exception {
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    TestBean kerry = (TestBean) xbf.getBean("spouse");
    // should have been autowired
    assertEquals(kerry, rod1.getSpouse());
    DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
    // should have been autowired
    assertEquals(kerry, rod1a.getSpouse());
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertEquals(kerry, rod2.getSpouse());
    DependenciesBean rod2a = (DependenciesBean) xbf.getBean("rod2a");
    // should have been set explicitly
    assertEquals(kerry, rod2a.getSpouse());
    ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertEquals(kerry, rod3.getSpouse1());
    assertEquals(kerry, rod3.getSpouse2());
    assertEquals(other, rod3.getOther());
    ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
    // should have been autowired
    assertEquals(kerry, rod3a.getSpouse1());
    assertEquals(kerry, rod3a.getSpouse2());
    assertEquals(other, rod3a.getOther());
    try {
        xbf.getBean("rod4", ConstructorDependenciesBean.class);
        fail("Must have thrown a FatalBeanException");
    } catch (FatalBeanException expected) {
    // expected
    }
    DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
    // Should not have been autowired
    assertNull(rod5.getSpouse());
    BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
    assertTrue(appCtx.containsBean("rod1"));
    assertTrue(appCtx.containsBean("jenny"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean)

Example 15 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessorTests method testForRefreshedScriptHavingErrorPickedUpOnFirstCall.

@Test
public void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
    BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
    BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
    BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
    collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
    ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
    final String collaboratorBeanName = "collaborator";
    ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
    ctx.refresh();
    Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    assertEquals(MESSAGE_TEXT, messenger.getMessage());
    // cool; now let's change the script and check the refresh behaviour...
    pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
    StaticScriptSource source = getScriptSource(ctx);
    // needs The Sundays compiler; must NOT throw any exception here...
    source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
    Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    try {
        refreshedMessenger.getMessage();
        fail("Must have thrown an Exception (invalid script)");
    } catch (FatalBeanException expected) {
        assertTrue(expected.contains(ScriptCompilationException.class));
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) Messenger(org.springframework.scripting.Messenger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Aggregations

FatalBeanException (org.springframework.beans.FatalBeanException)17 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 BeansException (org.springframework.beans.BeansException)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ReversePropertySourcesStandardServletEnvironment (com.hevelian.activemq.support.ReversePropertySourcesStandardServletEnvironment)1 ConfigObject (groovy.util.ConfigObject)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 ResourceXmlApplicationContext (org.apache.xbean.spring.context.ResourceXmlApplicationContext)1 BeanInstantiationException (org.springframework.beans.BeanInstantiationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1