Advertisement
Guest User

Untitled

a guest
Dec 10th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. package testIntercept;
  2.  
  3. import org.apache.camel.*;
  4. import org.apache.camel.builder.RouteBuilder;
  5. import org.apache.camel.component.mock.MockComponent;
  6. import org.apache.camel.component.mock.MockEndpoint;
  7. import org.apache.camel.component.seda.SedaComponent;
  8. import org.apache.camel.component.seda.SedaEndpoint;
  9. import org.apache.camel.test.junit4.CamelTestSupport;
  10. import org.junit.Ignore;
  11. import org.junit.Test;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Map;
  15. import java.util.concurrent.LinkedBlockingQueue;
  16.  
  17. public class TestRemoveInvalidRecipient2 extends CamelTestSupport {
  18.  
  19. String directEndPointURI = "direct:myDirect";
  20. String finalEndpointURI = "mock:finalEndpoint";
  21. RecipientBean recipientBean = new RecipientBean();
  22.  
  23. @Override
  24. public boolean isUseRouteBuilder() {
  25. return false;
  26. }
  27.  
  28. @Test
  29. @Ignore
  30. public void testIntegrationDynamicRecipient() throws Exception {
  31.  
  32. context.addRoutes(new RouteBuilder() {
  33. @Override
  34. public void configure() throws Exception {
  35.  
  36. from(directEndPointURI)
  37. .doTry()
  38. .recipientList(method(recipientBean, "getRecipientList")).parallelProcessing().ignoreInvalidEndpoints().end()
  39. .doCatch(Exception.class)
  40. .process(new Processor() {
  41. @Override
  42. public void process(Exchange exchange) throws Exception {
  43. String failureEndpoint = exchange.getProperty(Exchange.FAILURE_ENDPOINT, String.class);
  44. // TODO : Get the failure endpoint.
  45. // failureEndpoint contains "direct:myDirect"....
  46. if (failureEndpoint != null) {
  47. recipientBean.removeRecipient(failureEndpoint);
  48. }
  49. }
  50. })
  51. .doFinally()
  52. .process(new Processor() {
  53. @Override
  54. public void process(Exchange exchange) throws Exception {
  55. System.out.println("There is " + recipientBean.getRecipientList().length + " endpoint.");
  56. }
  57. })
  58. .end().to(finalEndpointURI);
  59. }
  60. });
  61.  
  62. context.start();
  63.  
  64. MockEndpoint mockEndpoint = new MockEndpoint("mock:abc", new MockComponent());
  65. Endpoint sedaEndpoint = new SedaEndpoint("seda:mySeda", new SedaComponent(), new LinkedBlockingQueue<Exchange>());
  66. sedaEndpoint.setCamelContext(context);
  67.  
  68. recipientBean.addRecipient(mockEndpoint);
  69. recipientBean.addRecipient(sedaEndpoint);
  70.  
  71. // Do a simple go through the route
  72. getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
  73. template.sendBody(directEndPointURI, "MY_MESSAGE");
  74. getMockEndpoint(finalEndpointURI).assertIsSatisfied();
  75.  
  76. // Make sure we have the 2 recipients.
  77. assertEquals(2, recipientBean.getRecipientList().length);
  78.  
  79. // Add the bad endpoint
  80. Endpoint fake = new MyFakeEndpoint();
  81. recipientBean.addRecipient(fake);
  82.  
  83. // Run the second test
  84. getMockEndpoint(finalEndpointURI).reset();
  85. getMockEndpoint(finalEndpointURI).expectedMessageCount(1);
  86. template.sendBody(directEndPointURI, "MY_MESSAGE2");
  87. getMockEndpoint(finalEndpointURI).assertIsSatisfied();
  88.  
  89. // The fake endpoint should have been deleted by the time finalEndpointURI is satisfied.
  90. assertEquals(2, recipientBean.getRecipientList().length);
  91. }
  92.  
  93. public class RecipientBean {
  94.  
  95. private ArrayList<Endpoint> recipientList = new ArrayList<Endpoint>();
  96.  
  97. public void addRecipient(Endpoint endpoint) {
  98. recipientList.add(endpoint);
  99. }
  100.  
  101. public void removeRecipient(Endpoint endpoint) {
  102. recipientList.remove(endpoint);
  103. }
  104.  
  105. public void removeRecipient(String uri) {
  106. Endpoint toDeleteEndpoint = null;
  107.  
  108. for (Endpoint endpoint : recipientList) {
  109. if (endpoint.getEndpointUri().equals(uri))
  110. toDeleteEndpoint = endpoint;
  111. }
  112. if (toDeleteEndpoint != null)
  113. recipientList.remove(toDeleteEndpoint);
  114. }
  115.  
  116. public Endpoint[] getRecipientList() {
  117. return recipientList.toArray(new Endpoint[0]);
  118. }
  119. }
  120.  
  121. public class MyFakeEndpoint implements Endpoint {
  122.  
  123. @Override
  124. public String getEndpointUri() {
  125. return "myEvilURI";
  126. }
  127.  
  128. @Override
  129. public EndpointConfiguration getEndpointConfiguration() {
  130. return null; //To change body of implemented methods use File | Settings | File Templates.
  131. }
  132.  
  133. @Override
  134. public String getEndpointKey() {
  135. return null; //To change body of implemented methods use File | Settings | File Templates.
  136. }
  137.  
  138. @Override
  139. public Exchange createExchange() {
  140. return null; //To change body of implemented methods use File | Settings | File Templates.
  141. }
  142.  
  143. @Override
  144. public Exchange createExchange(ExchangePattern pattern) {
  145. return null; //To change body of implemented methods use File | Settings | File Templates.
  146. }
  147.  
  148. @Override
  149. public Exchange createExchange(Exchange exchange) {
  150. return null; //To change body of implemented methods use File | Settings | File Templates.
  151. }
  152.  
  153. @Override
  154. public CamelContext getCamelContext() {
  155. return null; //To change body of implemented methods use File | Settings | File Templates.
  156. }
  157.  
  158. @Override
  159. public Producer createProducer() throws Exception {
  160. return null; //To change body of implemented methods use File | Settings | File Templates.
  161. }
  162.  
  163. @Override
  164. public Consumer createConsumer(Processor processor) throws Exception {
  165. return null; //To change body of implemented methods use File | Settings | File Templates.
  166. }
  167.  
  168. @Override
  169. public PollingConsumer createPollingConsumer() throws Exception {
  170. return null; //To change body of implemented methods use File | Settings | File Templates.
  171. }
  172.  
  173. @Override
  174. public void configureProperties(Map<String, Object> options) {
  175. //To change body of implemented methods use File | Settings | File Templates.
  176. }
  177.  
  178. @Override
  179. public void setCamelContext(CamelContext context) {
  180. //To change body of implemented methods use File | Settings | File Templates.
  181. }
  182.  
  183. @Override
  184. public boolean isLenientProperties() {
  185. return false; //To change body of implemented methods use File | Settings | File Templates.
  186. }
  187.  
  188. @Override
  189. public boolean isSingleton() {
  190. return false; //To change body of implemented methods use File | Settings | File Templates.
  191. }
  192.  
  193. @Override
  194. public void start() throws Exception {
  195. //To change body of implemented methods use File | Settings | File Templates.
  196. }
  197.  
  198. @Override
  199. public void stop() throws Exception {
  200. //To change body of implemented methods use File | Settings | File Templates.
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement