2012-11-30 96 views
2

我試圖攔截一條消息來跳過Http請求並繼續我的路線。 以下是您可以複製/粘貼以進行試用的課程。駱駝測試使用攔截

使用駱駝測試,駱駝核心,駱駝http4 2.10.2和HttpClient的OSGi的,的HttpCore OSGi的4.2.2

下面是代碼:

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; 

/** 
* Created with IntelliJ IDEA. 
* User: lleclerc 
* Date: 12-11-28 
* Time: 16:34 
* To change this template use File | Settings | File Templates. 
*/ 
public class IsUseAdviceWithJUnit4Test extends CamelTestSupport { 

    private String providerEndPointURI = "http://stackoverflow.com"; 
    private String timerEndPointURI = "timer://myTimer"; 
    private String mockEndPointURI = "mock:myMock"; 
    private String directEndPointURI = "direct:myDirect"; 
    private boolean messageIntercepted; 

    @Override 
    protected RouteBuilder createRouteBuilder() throws Exception { 

     return new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 

       from(timerEndPointURI + "?fixedRate=true&delay=1000&period=1000") 
         .to(providerEndPointURI + "?throwExceptionOnFailure=false") 
         .to(mockEndPointURI); 
      } 
     }; 
    } 

    @Test 
    public void testIsUseAdviceWith() throws Exception { 

     messageIntercepted = false; 

     context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { 
      @Override 
      public void configure() throws Exception { 

       replaceFromWith(directEndPointURI); 

       mockEndpoints(); 

       interceptSendToEndpoint(providerEndPointURI) 
         .skipSendToOriginalEndpoint() 
         .process(new Processor() { 
          @Override 
          public void process(Exchange exchange) throws Exception { 
           messageIntercepted = true; 
           System.out.println("INTERCEPTED"); 
          } 
         }); 
      } 
     }); 

     // we must manually start when we are done with all the advice with 
     context.start(); 

     getMockEndpoint(mockEndPointURI).expectedMessageCount(1); 

     template.sendBody(directEndPointURI, "a trigger"); 

     assertMockEndpointsSatisfied(); 

     assertEquals(true, messageIntercepted); 

     assertNotNull(context.hasEndpoint(directEndPointURI)); 
     assertNotNull(context.hasEndpoint("mock:" + directEndPointURI)); 

     assertNotNull(context.hasEndpoint(mockEndPointURI)); 
    } 

    @Override 
    public boolean isUseAdviceWith() { 
     return true; 
    } 

    @Override 
    public boolean isUseRouteBuilder() { 
     return true; 
    } 
} 

感謝您幫幫我 !

+0

這是關係到使用「http4」 ......它更改爲「HTTP」,它工作得很好......不知道爲什麼,這將是尚未雖然 –

+0

我更新了測試當郵件沒有被截取時失敗,你可以再試一次,http或http4,兩者都不起作用...... :( – lleclerc

回答