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());
}
}
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());
}
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;
}
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"));
}
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));
}
}
Aggregations