Search in sources :

Example 16 with SpringCamelContext

use of org.apache.camel.spring.SpringCamelContext 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 17 with SpringCamelContext

use of org.apache.camel.spring.SpringCamelContext in project camel by apache.

the class ServiceCallConfigurationTest method testServiceDiscoveryConfiguration.

@Test
public void testServiceDiscoveryConfiguration() {
    SpringCamelContext context = createContext("org/apache/camel/spring/cloud/ServiceCallConfigurationTest.xml");
    ServiceCallConfigurationDefinition conf1 = context.getServiceCallConfiguration("conf1");
    assertNotNull("No ServiceCallConfiguration (1)", conf1);
    assertNotNull("No ServiceDiscoveryConfiguration (1)", conf1.getServiceDiscoveryConfiguration());
    assertNotNull("No ServiceCallLoadBalancerConfiguration (1)", conf1.getLoadBalancerConfiguration());
    assertTrue(conf1.getLoadBalancerConfiguration() instanceof DefaultServiceCallLoadBalancerConfiguration);
    StaticServiceCallServiceDiscoveryConfiguration discovery1 = (StaticServiceCallServiceDiscoveryConfiguration) conf1.getServiceDiscoveryConfiguration();
    assertEquals(1, discovery1.getServers().size());
    assertEquals("localhost:9091", discovery1.getServers().get(0));
    ServiceCallConfigurationDefinition conf2 = context.getServiceCallConfiguration("conf2");
    assertNotNull("No ServiceCallConfiguration (2)", conf2);
    assertNotNull("No ServiceDiscoveryConfiguration (2)", conf2.getServiceDiscoveryConfiguration());
    assertNull(conf2.getLoadBalancerConfiguration());
    ChainedServiceCallServiceDiscoveryConfiguration discovery2 = (ChainedServiceCallServiceDiscoveryConfiguration) conf2.getServiceDiscoveryConfiguration();
    assertEquals(2, discovery2.getServiceDiscoveryConfigurations().size());
    assertTrue(discovery2.getServiceDiscoveryConfigurations().get(0) instanceof StaticServiceCallServiceDiscoveryConfiguration);
    assertTrue(discovery2.getServiceDiscoveryConfigurations().get(1) instanceof StaticServiceCallServiceDiscoveryConfiguration);
    StaticServiceCallServiceDiscoveryConfiguration sconf1 = (StaticServiceCallServiceDiscoveryConfiguration) discovery2.getServiceDiscoveryConfigurations().get(0);
    assertEquals(1, sconf1.getServers().size());
    assertEquals("localhost:9092", sconf1.getServers().get(0));
    StaticServiceCallServiceDiscoveryConfiguration sconf2 = (StaticServiceCallServiceDiscoveryConfiguration) discovery2.getServiceDiscoveryConfigurations().get(1);
    assertEquals(1, sconf2.getServers().size());
    assertEquals("localhost:9093,localhost:9094,localhost:9095,localhost:9096", sconf2.getServers().get(0));
    ChainedServiceCallServiceFilterConfiguration filter = (ChainedServiceCallServiceFilterConfiguration) conf2.getServiceFilterConfiguration();
    assertEquals(2, filter.getServiceFilterConfigurations().size());
    assertTrue(filter.getServiceFilterConfigurations().get(0) instanceof HealthyServiceCallServiceFilterConfiguration);
    assertTrue(filter.getServiceFilterConfigurations().get(1) instanceof BlacklistServiceCallServiceFilterConfiguration);
}
Also used : DefaultServiceCallLoadBalancerConfiguration(org.apache.camel.model.cloud.DefaultServiceCallLoadBalancerConfiguration) ChainedServiceCallServiceFilterConfiguration(org.apache.camel.model.cloud.ChainedServiceCallServiceFilterConfiguration) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) BlacklistServiceCallServiceFilterConfiguration(org.apache.camel.model.cloud.BlacklistServiceCallServiceFilterConfiguration) HealthyServiceCallServiceFilterConfiguration(org.apache.camel.model.cloud.HealthyServiceCallServiceFilterConfiguration) ServiceCallConfigurationDefinition(org.apache.camel.model.cloud.ServiceCallConfigurationDefinition) StaticServiceCallServiceDiscoveryConfiguration(org.apache.camel.model.cloud.StaticServiceCallServiceDiscoveryConfiguration) ChainedServiceCallServiceDiscoveryConfiguration(org.apache.camel.model.cloud.ChainedServiceCallServiceDiscoveryConfiguration) Test(org.junit.Test)

Example 18 with SpringCamelContext

use of org.apache.camel.spring.SpringCamelContext in project camel by apache.

the class ErrorHandlerTest method testEndpointConfiguration.

public void testEndpointConfiguration() throws Exception {
    SpringCamelContext context = applicationContext.getBeansOfType(SpringCamelContext.class).values().iterator().next();
    List<Route> list = context.getRoutes();
    assertEquals("Number routes created" + list, 2, list.size());
    for (Route route : list) {
        EventDrivenConsumerRoute consumerRoute = assertIsInstanceOf(EventDrivenConsumerRoute.class, route);
        Channel channel = unwrapChannel(consumerRoute.getProcessor());
        DeadLetterChannel deadLetterChannel = assertIsInstanceOf(DeadLetterChannel.class, channel.getErrorHandler());
        RedeliveryPolicy redeliveryPolicy = deadLetterChannel.getRedeliveryPolicy();
        assertEquals("getMaximumRedeliveries()", 1, redeliveryPolicy.getMaximumRedeliveries());
        assertEquals("isUseExponentialBackOff()", true, redeliveryPolicy.isUseExponentialBackOff());
    }
}
Also used : DeadLetterChannel(org.apache.camel.processor.DeadLetterChannel) Channel(org.apache.camel.Channel) DeadLetterChannel(org.apache.camel.processor.DeadLetterChannel) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) RedeliveryPolicy(org.apache.camel.processor.RedeliveryPolicy) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute) Route(org.apache.camel.Route) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute)

Example 19 with SpringCamelContext

use of org.apache.camel.spring.SpringCamelContext in project camel by apache.

the class SpringXmlRouteBuilderTest method getRoutesFromContext.

protected List<Route> getRoutesFromContext(String classpathConfigFile) {
    applicationContext = new ClassPathXmlApplicationContext(classpathConfigFile);
    SpringCamelContext context = applicationContext.getBeansOfType(SpringCamelContext.class).values().iterator().next();
    assertNotNull("No Camel Context in file: " + classpathConfigFile, context);
    List<Route> routes = context.getRoutes();
    assertNotNull("No routes available for context: " + context.getName() + " in file: " + classpathConfigFile, routes);
    return routes;
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpringCamelContext(org.apache.camel.spring.SpringCamelContext) Route(org.apache.camel.Route)

Example 20 with SpringCamelContext

use of org.apache.camel.spring.SpringCamelContext in project camel by apache.

the class RouteAutoStartupPropertiesTest method testAutoStartupTrue.

public void testAutoStartupTrue() throws Exception {
    ac = new ClassPathXmlApplicationContext("org/apache/camel/spring/config/RouteAutoStartupTrueTest.xml");
    SpringCamelContext camel = ac.getBeansOfType(SpringCamelContext.class).values().iterator().next();
    assertEquals(true, camel.getRouteStatus("bar").isStarted());
    // and now we can send a message to the route and see that it works
    MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    ProducerTemplate template = camel.createProducerTemplate();
    template.start();
    template.sendBody("direct:start", "Hello World");
    template.stop();
    mock.assertIsSatisfied();
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SpringCamelContext(org.apache.camel.spring.SpringCamelContext)

Aggregations

SpringCamelContext (org.apache.camel.spring.SpringCamelContext)30 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 DoToSpringCamelContextsStrategy (org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy)5 Method (java.lang.reflect.Method)4 LinkedList (java.util.LinkedList)4 CamelContext (org.apache.camel.CamelContext)4 ProducerTemplate (org.apache.camel.ProducerTemplate)4 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)4 Breakpoint (org.apache.camel.spi.Breakpoint)4 Test (org.junit.Test)3 Properties (java.util.Properties)2 TimeUnit (java.util.concurrent.TimeUnit)2 Route (org.apache.camel.Route)2 DefaultDebugger (org.apache.camel.impl.DefaultDebugger)2 InterceptSendToMockEndpointStrategy (org.apache.camel.impl.InterceptSendToMockEndpointStrategy)2 Debugger (org.apache.camel.spi.Debugger)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 Channel (org.apache.camel.Channel)1