Guest User

Untitled

a guest
Dec 17th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package testIntercept;
  2.  
  3. import org.apache.camel.Exchange;
  4. import org.apache.camel.Processor;
  5. import org.apache.camel.builder.AdviceWithRouteBuilder;
  6. import org.apache.camel.builder.RouteBuilder;
  7. import org.apache.camel.test.junit4.CamelTestSupport;
  8. import org.junit.Test;
  9.  
  10. public class TestMultipleAdvice extends CamelTestSupport {
  11.  
  12. @Override
  13. public boolean isUseAdviceWith() {
  14. return true;
  15. }
  16.  
  17. @Override
  18. protected RouteBuilder createRouteBuilder() throws Exception {
  19. return new RouteBuilder() {
  20. @Override
  21. public void configure() throws Exception {
  22. onException(Exception.class).handled(true).to("mock:error");
  23.  
  24. from("direct:startA").routeId("RouteA").to("mock:resultA");
  25. from("direct:startB").routeId("RouteB").to("mock:resultB");
  26. }
  27. };
  28. }
  29.  
  30. /**
  31. * This test is success.
  32. * @throws Exception
  33. */
  34. @Test
  35. public void testSimpleMultipleAdvice() throws Exception {
  36.  
  37. context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
  38. @Override
  39. public void configure() throws Exception {
  40. interceptSendToEndpoint("mock:resultA").process(new Processor() {
  41. @Override
  42. public void process(Exchange exchange) throws Exception {
  43. }
  44. });
  45. }
  46. });
  47.  
  48. context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {
  49. @Override
  50. public void configure() throws Exception {
  51. }
  52. });
  53.  
  54. context.start();
  55.  
  56. getMockEndpoint("mock:resultA").expectedMessageCount(1);
  57. template.sendBody("direct:startA", "a trigger");
  58. assertMockEndpointsSatisfied();
  59. }
  60.  
  61. /**
  62. * This test is success.
  63. * @throws Exception
  64. */
  65. @Test
  66. public void testMultipleAdviceWithExceptionThrown() throws Exception {
  67. context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
  68. @Override
  69. public void configure() throws Exception {
  70. interceptSendToEndpoint("mock:resultA").process(new Processor() {
  71. @Override
  72. public void process(Exchange exchange) throws Exception {
  73. throw new Exception("my exception");
  74. }
  75. });
  76. }
  77. });
  78.  
  79. context.start();
  80.  
  81. getMockEndpoint("mock:resultA").expectedMessageCount(0);
  82. template.sendBody("direct:startA", "a trigger");
  83. assertMockEndpointsSatisfied();
  84. }
  85.  
  86. /**
  87. * This test will fail with the extra Advice on the second route, even though it should not affect anything.
  88. * @throws Exception
  89. */
  90. @Test
  91. public void testMultipleAdvice() throws Exception {
  92.  
  93. context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {
  94. @Override
  95. public void configure() throws Exception {
  96. interceptSendToEndpoint("mock:resultA").process(new Processor() {
  97. @Override
  98. public void process(Exchange exchange) throws Exception {
  99. throw new Exception("my exception");
  100. }
  101. });
  102. }
  103. });
  104.  
  105. context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {
  106. @Override
  107. public void configure() throws Exception {
  108. }
  109. });
  110.  
  111. context.start();
  112.  
  113. getMockEndpoint("mock:resultA").expectedMessageCount(0);
  114. template.sendBody("direct:startA", "a trigger");
  115. assertMockEndpointsSatisfied();
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment