Search in sources :

Example 1 with DefaultModelJAXBContextFactory

use of org.apache.camel.impl.DefaultModelJAXBContextFactory in project camel by apache.

the class SpringLoadRouteFromXmlTest method testLoadRouteFromXml.

@Test
public void testLoadRouteFromXml() throws Exception {
    assertNotNull("Existing foo route should be there", context.getRoute("foo"));
    assertEquals(1, context.getRoutes().size());
    // test that existing route works
    MockEndpoint foo = getMockEndpoint("mock:foo");
    foo.expectedBodiesReceived("Hello World");
    template.sendBody("direct:foo", "Hello World");
    foo.assertIsSatisfied();
    // load bar route from classpath using JAXB
    JAXBContext jaxb = new DefaultModelJAXBContextFactory().newJAXBContext();
    Unmarshaller unmarshaller = jaxb.createUnmarshaller();
    Resource rs = new ClassPathResource("org/apache/camel/itest/jaxb/BarRoute.xml");
    Object value = unmarshaller.unmarshal(rs.getInputStream());
    // it should be a RoutesDefinition (we can have multiple routes in the same XML file)
    RoutesDefinition routes = (RoutesDefinition) value;
    assertNotNull("Should load routes from XML", routes);
    assertEquals(1, routes.getRoutes().size());
    // add the routes to existing CamelContext
    context.addRouteDefinitions(routes.getRoutes());
    assertNotNull("Loaded bar route should be there", context.getRoute("bar"));
    assertEquals(2, context.getRoutes().size());
    // test that loaded route works
    MockEndpoint bar = getMockEndpoint("mock:bar");
    bar.expectedBodiesReceived("Bye World");
    template.sendBody("direct:bar", "Bye World");
    bar.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) RoutesDefinition(org.apache.camel.model.RoutesDefinition) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) DefaultModelJAXBContextFactory(org.apache.camel.impl.DefaultModelJAXBContextFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 DefaultModelJAXBContextFactory (org.apache.camel.impl.DefaultModelJAXBContextFactory)1 RoutesDefinition (org.apache.camel.model.RoutesDefinition)1 Test (org.junit.Test)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1