On Apr 22, 2009, at 15:29 , James Strachan wrote:

2009/4/22 Peter Maas <pfmm...@gmail.com>:
Hi,

is it possible the programmatically replace parts of a route (for testing
purposes) to temporarily add processor in the middle of the route for
example? One case I (think) I need it for is for testing unexpected errors, I'd like to mimic this by 'injecting' a processor which throws a unchecked exception; the ErrorHandlerTest does do this but only with custom routes...

Two thoughts;

use conditional logic when building the route...

f = from("something");
if (testing) {
f.to(someTestingEndpoint);
}
else {
 f.to(productionEndpoint);
}

this just involves assigning where you are in the Java DSL to a
variable - then using Java logical branching.


The other option is to just modify the AST

 List<RouteDefinition> routes = camelContext.getRouteDefinitions();
 RouteDefinition route = routes.get(0);
 // modify AST
 camelContext.startRoute(route);

I tried to do this, but didn't really manage to get what I wanted:


public class UnicodeSplitTest extends CamelTestSupport{
  public void testShouldCombineMessages() throws Exception{
    List<RouteDefinition> routes = context.getRouteDefinitions();
    RouteDefinition route = routes.get(0);

ProcessorDefinition direct = Helper.firstNonNull(route.getOutputs()); ProcessorDefinition splitComponent = (ProcessorDefinition)Helper.firstNonNull(direct.getOutputs());
    splitComponent.process(new Processor(){

      public void process(Exchange arg0) throws Exception {
        System.out.println("hi");
        //throw new IllegalAccessError("we've got explosives!");
      }

    }).to("mock:end");
    context.startRoute(route);

MockEndpoint resultEndpoint = getMandatoryEndpoint("mock:end", MockEndpoint.class);
    resultEndpoint.setExpectedMessageCount(2);
template.sendBody("direct:goSplit", getClass().getResourceAsStream("utf8_test_document.xml"));
    resultEndpoint.assertIsSatisfied();
resultEndpoint.expectedBodiesReceived("<item>ŽŽn</ item>","<item>twee</item>");
  }

  @Override
  protected RouteBuilder[] createRouteBuilders() throws Exception {
final RouteBuilder[] builders = new RouteBuilder[]{new RouteBuilder() {
          @Override
          public void configure() throws Exception {
              errorHandler(loggingErrorHandler());
from("direct:goSplit").split().xpath("// item").to("mock:deadEnd");
          }
      }};
      return builders;
  }
}



The "hi" message gets printed, and the test succeeds. If I throw an exception however it is not handled bij the errorhandler as I expected.

I suppose I'm doing it wrong?

-P


--
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Reply via email to