Skip to content

Instantly share code, notes, and snippets.

@birkland
Created August 25, 2015 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birkland/1b12261521dd5d5c79ef to your computer and use it in GitHub Desktop.
Save birkland/1b12261521dd5d5c79ef to your computer and use it in GitHub Desktop.
package org.example.test
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.ExplicitCamelContextNameStrategy;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class ContextComponentTest
extends CamelTestSupport {
@Produce
protected ProducerTemplate template;
@EndpointInject(uri = "mock:end")
private MockEndpoint mock_end;
private JndiRegistry registry;
private CamelContext parentContext;
@Test
public void testBlackBoxRoutes() throws Exception {
/* CamelContext names for the black box contexts */
final String BLACK_BOX_1 = "bb1";
final String BLACK_BOX_2 = "bb2";
/* These are the context component URIs (e.g. bb1:in, bb2:out, etc) */
final String BLACK_BOX_1_IN = BLACK_BOX_1 + ":in";
final String BLACK_BOX_1_OUT = BLACK_BOX_1 + ":out";
final String BLACK_BOX_2_IN = BLACK_BOX_2 + ":in";
final String BLACK_BOX_2_OUT = BLACK_BOX_2 + ":out";
/* Create first black box context */
newBlackBoxContext(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:in").setBody(constant("one")).to("direct:out");
}
}, BLACK_BOX_1);
/* Create second black box context */
newBlackBoxContext(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:in").setBody(constant("two")).to("direct:out");
}
}, BLACK_BOX_2);
/* Now wire the two black boxes together */
parentContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to(BLACK_BOX_1_IN);
from(BLACK_BOX_1_OUT).to(BLACK_BOX_2_IN);
from(BLACK_BOX_2_OUT).to("mock:end");
}
});
/*
* Send a message to 'direct:start'. It should be routed through the
* black boxes, and end up in mock:end
*/
template.sendBody("testing", "direct:start");
mock_end.expectedMessageCount(1);
}
private CamelContext newBlackBoxContext(RoutesBuilder routes, String id)
throws Exception {
CamelContext cxt = new DefaultCamelContext(registry);
cxt.setNameStrategy(new ExplicitCamelContextNameStrategy(id));
cxt.addRoutes(routes);
cxt.start();
registry.bind(id, cxt);
return cxt;
}
protected JndiRegistry createRegistry() throws Exception {
return registry = super.createRegistry();
}
@Override
protected CamelContext createCamelContext() throws Exception {
return parentContext = super.createCamelContext();
}
}
@terrisgit
Copy link

Reading the thread on camel-users.. Awesome find and fix. Can't wait to get a release with this. Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment