use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class CustomEditorTests method testCustomNumberEditorWithAllowEmpty.
@Test
public void testCustomNumberEditorWithAllowEmpty() {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
NumberTestBean tb = new NumberTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
bw.setPropertyValue("long1", "5");
bw.setPropertyValue("long2", "6");
assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
assertTrue("Correct long1 value", tb.getLong1() == 5);
assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
bw.setPropertyValue("long2", "");
assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
assertTrue("Correct long2 value", tb.getLong2() == null);
try {
bw.setPropertyValue("long1", "");
fail("Should have thrown BeansException");
} catch (BeansException ex) {
// expected
assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
assertTrue("Correct long1 value", tb.getLong1() == 5);
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class AbstractApplicationContext method refresh.
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testWithDuplicateName.
@Test
public void testWithDuplicateName() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
try {
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(TEST_WITH_DUP_NAMES_CONTEXT);
fail("Duplicate name not detected");
} catch (BeansException ex) {
assertTrue(ex.getMessage().contains("Bean name 'foo'"));
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class SchemaValidationTests method withAutodetection.
@Test
public void withAutodetection() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
try {
reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()));
fail("Should not be able to parse a file with errors");
} catch (BeansException ex) {
assertTrue(ex.getCause() instanceof SAXParseException);
}
}
use of org.springframework.beans.BeansException in project spring-framework by spring-projects.
the class SchemaValidationTests method withExplicitValidationMode.
@Test
public void withExplicitValidationMode() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
try {
reader.loadBeanDefinitions(new ClassPathResource("invalidPerSchema.xml", getClass()));
fail("Should not be able to parse a file with errors");
} catch (BeansException ex) {
assertTrue(ex.getCause() instanceof SAXParseException);
}
}
Aggregations