Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Last active August 29, 2015 14:09
Show Gist options
  • Save MoriTanosuke/d1fda2e44cda57553db0 to your computer and use it in GitHub Desktop.
Save MoriTanosuke/d1fda2e44cda57553db0 to your computer and use it in GitHub Desktop.
Simple JUnit test to demonstrate how to get a RouteDefinition object from XML for Apache Camel.
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class ConvertXmlToRouteDefinitionTest {
private final String XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<route group=\"de.kopis.examples.camel.MyRouteBuilder\" customId=\"true\" id=\"myRoute\" xmlns=\"http://camel.apache.org/schema/spring\">" +
"<from uri=\"file:src/data?noop=true\"/>" +
"<choice id=\"choice1\">" +
"<when id=\"when1\">" +
"<xpath>/person/city = 'London'</xpath>" +
"<log message=\"UK message\" id=\"log1\"/>" +
"<to uri=\"file:target/messages/uk\" id=\"to1\"/>" +
"</when>" +
"<otherwise id=\"otherwise1\">" +
"<log message=\"Other message\" id=\"log2\"/>" +
"<to uri=\"file:target/messages/others\" id=\"to2\"/>" +
"</otherwise>" +
"</choice>" +
"</route>";
@Test
public void importsXmlAndCreatesRouteDefinition() throws Exception {
DefaultCamelContext context = new DefaultCamelContext();
RoutesDefinition routes = context.loadRoutesDefinition(new ByteArrayInputStream(XML.getBytes()));
RouteDefinition r = getRoute(routes, "myRoute");
assertNotNull(r);
assertEquals("file:src/data?noop=true", r.getInputs().get(0).getUri());
}
private RouteDefinition getRoute(RoutesDefinition routes, String routeId) {
for (RouteDefinition route : routes.getRoutes()) {
if (routeId.equalsIgnoreCase(route.getId())) {
return route;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment