Search in sources :

Example 1 with ExcludingPackageScanClassResolver

use of org.apache.camel.test.ExcludingPackageScanClassResolver in project camel by apache.

the class CamelSpringTestSupport method getRouteExcludingApplicationContext.

/**
     * Create a parent context that initializes a
     * {@link org.apache.camel.spi.PackageScanClassResolver} to exclude a set of given classes from
     * being resolved. Typically this is used at test time to exclude certain routes,
     * which might otherwise be just noisy, from being discovered and initialized.
     * <p/>
     * To use this filtering mechanism it is necessary to provide the
     * {@link ApplicationContext} returned from here as the parent context to
     * your test context e.g.
     *
     * <pre>
     * protected AbstractXmlApplicationContext createApplicationContext() {
     *     return new ClassPathXmlApplicationContext(new String[] {&quot;test-context.xml&quot;}, getRouteExcludingApplicationContext());
     * }
     * </pre>
     *
     * This will, in turn, call the template methods <code>excludedRoutes</code>
     * and <code>excludedRoute</code> to determine the classes to be excluded from scanning.
     *
     * @return ApplicationContext a parent {@link ApplicationContext} configured
     *         to exclude certain classes from package scanning
     */
protected ApplicationContext getRouteExcludingApplicationContext() {
    GenericApplicationContext routeExcludingContext = new GenericApplicationContext();
    routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
    routeExcludingContext.refresh();
    ExcludingPackageScanClassResolver excludingResolver = routeExcludingContext.getBean("excludingResolver", ExcludingPackageScanClassResolver.class);
    List<Class<?>> excluded = Arrays.asList(excludeRoutes());
    excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
    return routeExcludingContext;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ExcludingPackageScanClassResolver(org.apache.camel.test.ExcludingPackageScanClassResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AfterClass(org.testng.annotations.AfterClass)

Example 2 with ExcludingPackageScanClassResolver

use of org.apache.camel.test.ExcludingPackageScanClassResolver in project camel by apache.

the class CamelSpringTestContextLoader method createContext.

/**
     * Creates and starts the Spring context while optionally starting any loaded Camel contexts.
     *
     * @param testClass the test class that is being executed
     * @return the loaded Spring context
     */
protected GenericApplicationContext createContext(Class<?> testClass, MergedContextConfiguration mergedConfig) {
    ApplicationContext parentContext = null;
    GenericApplicationContext routeExcludingContext = null;
    if (mergedConfig != null) {
        parentContext = mergedConfig.getParentApplicationContext();
    }
    if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
        Class<?>[] excludedClasses = testClass.getAnnotation(ExcludeRoutes.class).value();
        if (excludedClasses.length > 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting up package scanning excluded classes as ExcludeRoutes " + "annotation was found. Excluding [" + StringUtils.arrayToCommaDelimitedString(excludedClasses) + "].");
            }
            if (parentContext == null) {
                routeExcludingContext = new GenericApplicationContext();
            } else {
                routeExcludingContext = new GenericApplicationContext(parentContext);
            }
            routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
            routeExcludingContext.refresh();
            ExcludingPackageScanClassResolver excludingResolver = routeExcludingContext.getBean("excludingResolver", ExcludingPackageScanClassResolver.class);
            List<Class<?>> excluded = Arrays.asList(excludedClasses);
            excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Not enabling package scanning excluded classes as ExcludeRoutes " + "annotation was found but no classes were excluded.");
            }
        }
    }
    GenericApplicationContext context;
    if (routeExcludingContext != null) {
        context = new GenericApplicationContext(routeExcludingContext);
    } else {
        if (parentContext != null) {
            context = new GenericApplicationContext(parentContext);
        } else {
            context = new GenericApplicationContext();
        }
    }
    return context;
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ExcludingPackageScanClassResolver(org.apache.camel.test.ExcludingPackageScanClassResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Example 3 with ExcludingPackageScanClassResolver

use of org.apache.camel.test.ExcludingPackageScanClassResolver in project camel by apache.

the class CamelSpringTestSupport method getRouteExcludingApplicationContext.

/**
     * Create a parent context that initializes a
     * {@link org.apache.camel.spi.PackageScanClassResolver} to exclude a set of given classes from
     * being resolved. Typically this is used at test time to exclude certain routes,
     * which might otherwise be just noisy, from being discovered and initialized.
     * <p/>
     * To use this filtering mechanism it is necessary to provide the
     * {@link org.springframework.context.ApplicationContext} returned from here as the parent context to
     * your test context e.g.
     *
     * <pre>
     * protected AbstractXmlApplicationContext createApplicationContext() {
     *     return new ClassPathXmlApplicationContext(new String[] {&quot;test-context.xml&quot;}, getRouteExcludingApplicationContext());
     * }
     * </pre>
     *
     * This will, in turn, call the template methods <code>excludedRoutes</code>
     * and <code>excludedRoute</code> to determine the classes to be excluded from scanning.
     *
     * @return ApplicationContext a parent {@link org.springframework.context.ApplicationContext} configured
     *         to exclude certain classes from package scanning
     */
protected ApplicationContext getRouteExcludingApplicationContext() {
    GenericApplicationContext routeExcludingContext = new GenericApplicationContext();
    routeExcludingContext.registerBeanDefinition("excludingResolver", new RootBeanDefinition(ExcludingPackageScanClassResolver.class));
    routeExcludingContext.refresh();
    ExcludingPackageScanClassResolver excludingResolver = routeExcludingContext.getBean("excludingResolver", ExcludingPackageScanClassResolver.class);
    List<Class<?>> excluded = Arrays.asList(excludeRoutes());
    excludingResolver.setExcludedClasses(new HashSet<Class<?>>(excluded));
    return routeExcludingContext;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ExcludingPackageScanClassResolver(org.apache.camel.test.ExcludingPackageScanClassResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AfterClass(org.junit.AfterClass)

Aggregations

ExcludingPackageScanClassResolver (org.apache.camel.test.ExcludingPackageScanClassResolver)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 AfterClass (org.junit.AfterClass)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AfterClass (org.testng.annotations.AfterClass)1