Search in sources :

Example 1 with DoToSpringCamelContextsStrategy

use of org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleUseOverridePropertiesWithPropertiesComponent.

/**
     * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
    Collection<Method> methods = getAllMethods(testClass);
    final List<Properties> properties = new LinkedList<Properties>();
    for (Method method : methods) {
        if (AnnotationUtils.findAnnotation(method, UseOverridePropertiesWithPropertiesComponent.class) != null) {
            Class<?>[] argTypes = method.getParameterTypes();
            if (argTypes.length > 0) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not a no-argument method.");
            } else if (!Properties.class.isAssignableFrom(method.getReturnType())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but does not return a java.util.Properties.");
            } else if (!Modifier.isStatic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not static.");
            } else if (!Modifier.isPublic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not public.");
            }
            try {
                properties.add((Properties) method.invoke(null));
            } catch (Exception e) {
                throw new RuntimeException("Method [" + method.getName() + "] threw exception during evaluation.", e);
            }
        }
    }
    if (properties.size() != 0) {
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class);
                Properties extra = new Properties();
                for (Properties prop : properties) {
                    extra.putAll(prop);
                }
                if (!extra.isEmpty()) {
                    LOG.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), contextName);
                    pc.setOverrideProperties(extra);
                }
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) Method(java.lang.reflect.Method) Properties(java.util.Properties) LinkedList(java.util.LinkedList) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 2 with DoToSpringCamelContextsStrategy

use of org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleMockEndpointsAndSkip.

/**
     * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpointsAndSkip} and skipping the
     * original endpoint.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleMockEndpointsAndSkip(GenericApplicationContext context, Class<?> testClass) throws Exception {
    if (testClass.isAnnotationPresent(MockEndpointsAndSkip.class)) {
        final String mockEndpoints = testClass.getAnnotation(MockEndpointsAndSkip.class).value();
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            @Override
            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                // resovle the property place holders of the mockEndpoints 
                String mockEndpointsValue = camelContext.resolvePropertyPlaceholders(mockEndpoints);
                LOG.info("Enabling auto mocking and skipping of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpointsValue, contextName);
                camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpointsValue, true));
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy)

Example 3 with DoToSpringCamelContextsStrategy

use of org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleMockEndpoints.

/**
     * Handles auto-intercepting of endpoints with mocks based on {@link MockEndpoints}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleMockEndpoints(GenericApplicationContext context, Class<?> testClass) throws Exception {
    if (testClass.isAnnotationPresent(MockEndpoints.class)) {
        final String mockEndpoints = testClass.getAnnotation(MockEndpoints.class).value();
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            @Override
            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                LOG.info("Enabling auto mocking of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpoints, contextName);
                camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpoints));
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy)

Example 4 with DoToSpringCamelContextsStrategy

use of org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleProvidesBreakpoint.

/**
     * Handles the processing of the {@link ProvidesBreakpoint} annotation on a test class.  Exists here
     * as it is needed in 
     *
     * @param context the initialized Spring context containing the Camel context(s) to insert breakpoints into 
     * @param testClass the test class being processed
     *
     * @throws Exception if there is an error processing the class
     */
protected void handleProvidesBreakpoint(GenericApplicationContext context, Class<?> testClass) throws Exception {
    Collection<Method> methods = getAllMethods(testClass);
    final List<Breakpoint> breakpoints = new LinkedList<Breakpoint>();
    for (Method method : methods) {
        if (AnnotationUtils.findAnnotation(method, ProvidesBreakpoint.class) != null) {
            Class<?>[] argTypes = method.getParameterTypes();
            if (argTypes.length != 0) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with ProvidesBreakpoint but is not a no-argument method.");
            } else if (!Breakpoint.class.isAssignableFrom(method.getReturnType())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with ProvidesBreakpoint but does not return a Breakpoint.");
            } else if (!Modifier.isStatic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with ProvidesBreakpoint but is not static.");
            } else if (!Modifier.isPublic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with ProvidesBreakpoint but is not public.");
            }
            try {
                breakpoints.add((Breakpoint) method.invoke(null));
            } catch (Exception e) {
                throw new RuntimeException("Method [" + method.getName() + "] threw exception during evaluation.", e);
            }
        }
    }
    if (breakpoints.size() != 0) {
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            @Override
            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                Debugger debugger = camelContext.getDebugger();
                if (debugger == null) {
                    debugger = new DefaultDebugger();
                    camelContext.setDebugger(debugger);
                }
                for (Breakpoint breakpoint : breakpoints) {
                    LOG.info("Adding Breakpoint [{}] to CamelContext with name [{}].", breakpoint, contextName);
                    debugger.addBreakpoint(breakpoint);
                }
            }
        });
    }
}
Also used : Debugger(org.apache.camel.spi.Debugger) DefaultDebugger(org.apache.camel.impl.DefaultDebugger) Breakpoint(org.apache.camel.spi.Breakpoint) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) DefaultDebugger(org.apache.camel.impl.DefaultDebugger) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy)

Example 5 with DoToSpringCamelContextsStrategy

use of org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy in project camel by apache.

the class CamelSpringTestContextLoader method handleShutdownTimeout.

/**
     * Handles updating shutdown timeouts on Camel contexts based on {@link ShutdownTimeout}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleShutdownTimeout(GenericApplicationContext context, Class<?> testClass) throws Exception {
    final int shutdownTimeout;
    final TimeUnit shutdownTimeUnit;
    if (testClass.isAnnotationPresent(ShutdownTimeout.class)) {
        shutdownTimeout = testClass.getAnnotation(ShutdownTimeout.class).value();
        shutdownTimeUnit = testClass.getAnnotation(ShutdownTimeout.class).timeUnit();
    } else {
        shutdownTimeout = 10;
        shutdownTimeUnit = TimeUnit.SECONDS;
    }
    CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

        @Override
        public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
            LOG.info("Setting shutdown timeout to [{} {}] on CamelContext with name [{}].", new Object[] { shutdownTimeout, shutdownTimeUnit, contextName });
            camelContext.getShutdownStrategy().setTimeout(shutdownTimeout);
            camelContext.getShutdownStrategy().setTimeUnit(shutdownTimeUnit);
        }
    });
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) TimeUnit(java.util.concurrent.TimeUnit) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) Breakpoint(org.apache.camel.spi.Breakpoint)

Aggregations

SpringCamelContext (org.apache.camel.spring.SpringCamelContext)5 DoToSpringCamelContextsStrategy (org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy)5 Method (java.lang.reflect.Method)2 LinkedList (java.util.LinkedList)2 InterceptSendToMockEndpointStrategy (org.apache.camel.impl.InterceptSendToMockEndpointStrategy)2 Breakpoint (org.apache.camel.spi.Breakpoint)2 Properties (java.util.Properties)1 TimeUnit (java.util.concurrent.TimeUnit)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1 DefaultDebugger (org.apache.camel.impl.DefaultDebugger)1 Debugger (org.apache.camel.spi.Debugger)1