Search in sources :

Example 6 with FatalBeanException

use of cn.taketoday.beans.FatalBeanException in project today-framework by TAKETODAY.

the class NoSuchBeanDefinitionFailureAnalyzerTests method createFailure.

private FatalBeanException createFailure(Class<?> config, String... environment) {
    try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
        this.analyzer = new NoSuchBeanDefinitionFailureAnalyzer(context.getBeanFactory());
        TestPropertyValues.of(environment).applyTo(context);
        context.register(config);
        context.refresh();
        return null;
    } catch (FatalBeanException ex) {
        return ex;
    }
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) FatalBeanException(cn.taketoday.beans.FatalBeanException)

Example 7 with FatalBeanException

use of cn.taketoday.beans.FatalBeanException in project today-infrastructure by TAKETODAY.

the class NoSuchBeanDefinitionFailureAnalyzerTests method createFailure.

private FatalBeanException createFailure(Class<?> config, String... environment) {
    try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
        this.analyzer = new NoSuchBeanDefinitionFailureAnalyzer(context.getBeanFactory());
        TestPropertyValues.of(environment).applyTo(context);
        context.register(config);
        context.refresh();
        return null;
    } catch (FatalBeanException ex) {
        return ex;
    }
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) FatalBeanException(cn.taketoday.beans.FatalBeanException)

Example 8 with FatalBeanException

use of cn.taketoday.beans.FatalBeanException in project today-infrastructure by TAKETODAY.

the class NoSuchBeanDefinitionFailureAnalyzerTests method failureAnalysisForExcludedAutoConfigurationType.

@Test
void failureAnalysisForExcludedAutoConfigurationType() {
    FatalBeanException failure = createFailure(StringHandler.class);
    addExclusions(this.analyzer, TestPropertyAutoConfiguration.class);
    FailureAnalysis analysis = analyzeFailure(failure);
    assertDescriptionConstructorMissingType(analysis, StringHandler.class, 0, String.class);
    String configClass = ClassUtils.getShortName(TestPropertyAutoConfiguration.class.getName());
    assertClassDisabled(analysis, String.format("auto-configuration '%s' was excluded", configClass), "string", ClassUtils.getShortName(TestPropertyAutoConfiguration.class));
    assertActionMissingType(analysis, String.class);
}
Also used : FatalBeanException(cn.taketoday.beans.FatalBeanException) FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis) Test(org.junit.jupiter.api.Test)

Example 9 with FatalBeanException

use of cn.taketoday.beans.FatalBeanException in project today-infrastructure by TAKETODAY.

the class ServiceLocatorFactoryBeanTests method testRequiresListableBeanFactoryAndChokesOnAnythingElse.

@Test
public void testRequiresListableBeanFactoryAndChokesOnAnythingElse() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    try {
        ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
        factory.setBeanFactory(beanFactory);
    } catch (FatalBeanException ex) {
    // expected
    }
}
Also used : ServiceLocatorFactoryBean(cn.taketoday.beans.factory.config.ServiceLocatorFactoryBean) BeanFactory(cn.taketoday.beans.factory.BeanFactory) FatalBeanException(cn.taketoday.beans.FatalBeanException) Test(org.junit.jupiter.api.Test)

Example 10 with FatalBeanException

use of cn.taketoday.beans.FatalBeanException in project today-infrastructure by TAKETODAY.

the class DefaultNamespaceHandlerResolver method resolve.

/**
 * Locate the {@link NamespaceHandler} for the supplied namespace URI
 * from the configured mappings.
 *
 * @param namespaceUri the relevant namespace URI
 * @return the located {@link NamespaceHandler}, or {@code null} if none found
 */
@Override
@Nullable
public NamespaceHandler resolve(String namespaceUri) {
    Map<String, Object> handlerMappings = getHandlerMappings();
    Object handlerOrClassName = handlerMappings.get(namespaceUri);
    if (handlerOrClassName == null) {
        return null;
    } else if (handlerOrClassName instanceof NamespaceHandler) {
        return (NamespaceHandler) handlerOrClassName;
    } else {
        String className = (String) handlerOrClassName;
        try {
            Class<NamespaceHandler> handlerClass = ClassUtils.forName(className, this.classLoader);
            if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
                throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri + "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
            }
            NamespaceHandler namespaceHandler = BeanUtils.newInstance(handlerClass);
            namespaceHandler.init();
            handlerMappings.put(namespaceUri, namespaceHandler);
            return namespaceHandler;
        } catch (ClassNotFoundException ex) {
            throw new FatalBeanException("Could not find NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "]", ex);
        } catch (LinkageError err) {
            throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "]", err);
        }
    }
}
Also used : FatalBeanException(cn.taketoday.beans.FatalBeanException) Nullable(cn.taketoday.lang.Nullable)

Aggregations

FatalBeanException (cn.taketoday.beans.FatalBeanException)10 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)4 Test (org.junit.jupiter.api.Test)4 BeanFactory (cn.taketoday.beans.factory.BeanFactory)2 ServiceLocatorFactoryBean (cn.taketoday.beans.factory.config.ServiceLocatorFactoryBean)2 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)2 Nullable (cn.taketoday.lang.Nullable)2