Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testIntercept;
- import org.apache.camel.Exchange;
- import org.apache.camel.Processor;
- import org.apache.camel.builder.AdviceWithRouteBuilder;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.test.junit4.CamelTestSupport;
- import org.junit.Test;
- public class TestMultipleAdvice extends CamelTestSupport {
- @Override
- public boolean isUseAdviceWith() {
- return true;
- }
- @Override
- protected RouteBuilder createRouteBuilder() throws Exception {
- return new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- onException(Exception.class).handled(true).to("mock:error");
- from("direct:startA").routeId("RouteA").to("mock:resultA");
- from("direct:startB").routeId("RouteB").to("mock:resultB");
- }
- };
- }
- /**
- * This test is success.
- * @throws Exception
- */
- @Test
- public void testSimpleMultipleAdvice() throws Exception {
- context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
- @Override
- public void configure() throws Exception {
- interceptSendToEndpoint("mock:resultA").process(new Processor() {
- @Override
- public void process(Exchange exchange) throws Exception {
- }
- });
- }
- });
- context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {
- @Override
- public void configure() throws Exception {
- }
- });
- context.start();
- getMockEndpoint("mock:resultA").expectedMessageCount(1);
- template.sendBody("direct:startA", "a trigger");
- assertMockEndpointsSatisfied();
- }
- /**
- * This test is success.
- * @throws Exception
- */
- @Test
- public void testMultipleAdviceWithExceptionThrown() throws Exception {
- context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
- @Override
- public void configure() throws Exception {
- interceptSendToEndpoint("mock:resultA").process(new Processor() {
- @Override
- public void process(Exchange exchange) throws Exception {
- throw new Exception("my exception");
- }
- });
- }
- });
- context.start();
- getMockEndpoint("mock:resultA").expectedMessageCount(0);
- template.sendBody("direct:startA", "a trigger");
- assertMockEndpointsSatisfied();
- }
- /**
- * This test will fail with the extra Advice on the second route, even though it should not affect anything.
- * @throws Exception
- */
- @Test
- public void testMultipleAdvice() throws Exception {
- context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
- @Override
- public void configure() throws Exception {
- interceptSendToEndpoint("mock:resultA").process(new Processor() {
- @Override
- public void process(Exchange exchange) throws Exception {
- throw new Exception("my exception");
- }
- });
- }
- });
- context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {
- @Override
- public void configure() throws Exception {
- }
- });
- context.start();
- getMockEndpoint("mock:resultA").expectedMessageCount(0);
- template.sendBody("direct:startA", "a trigger");
- assertMockEndpointsSatisfied();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment