Search in sources :

Example 6 with BeansException

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);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) NumberFormat(java.text.NumberFormat) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 7 with BeansException

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();
        }
    }
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeansException(org.springframework.beans.BeansException)

Example 8 with BeansException

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'"));
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 9 with BeansException

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);
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 10 with BeansException

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);
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Aggregations

BeansException (org.springframework.beans.BeansException)64 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 BeanWrapper (org.springframework.beans.BeanWrapper)11 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)9 BeanCreationException (org.springframework.beans.factory.BeanCreationException)8 MalformedURLException (java.net.MalformedURLException)7 ArrayList (java.util.ArrayList)7 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)7 ApplicationContext (org.springframework.context.ApplicationContext)7 Map (java.util.Map)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5 IOException (java.io.IOException)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)4 UrlResource (org.springframework.core.io.UrlResource)4 File (java.io.File)3 HashMap (java.util.HashMap)3