View Javadoc

1   /*
2    * $Id: CoreNamespaceRoutersTestCase.java 19640 2010-09-13 22:00:05Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.processor.MessageProcessor;
18  import org.mule.api.routing.OutboundRouterCollection;
19  import org.mule.api.service.Service;
20  import org.mule.routing.IdempotentMessageFilter;
21  import org.mule.routing.IdempotentSecureHashMessageFilter;
22  import org.mule.routing.MessageFilter;
23  import org.mule.routing.outbound.AbstractOutboundRouter;
24  import org.mule.service.ServiceCompositeMessageSource;
25  import org.mule.tck.FunctionalTestCase;
26  import org.mule.util.store.InMemoryObjectStore;
27  import org.mule.util.store.TextFileObjectStore;
28  
29  import java.util.List;
30  
31  public class CoreNamespaceRoutersTestCase extends FunctionalTestCase
32  {
33      @Override
34      public String getConfigResources()
35      {
36          return "core-namespace-routers.xml";
37      }
38  
39      public void testIdempotentSecureHashReceiverRouter() throws Exception
40      {
41          MessageProcessor router = lookupInboundRouterFromService("IdempotentSecureHashReceiverRouter");
42          assertTrue(router instanceof IdempotentSecureHashMessageFilter);
43  
44          IdempotentSecureHashMessageFilter filter = (IdempotentSecureHashMessageFilter)router;
45          assertEquals("SHA-128", filter.getMessageDigestAlgorithm());
46          assertNotNull(filter.getStore());
47          assertTrue(filter.getStore() instanceof InMemoryObjectStore);
48  
49          InMemoryObjectStore<String> store = (InMemoryObjectStore<String>)filter.getStore();
50          assertEquals(1001, store.getEntryTTL());
51          assertEquals(1001, store.getExpirationInterval());
52          assertEquals(1001, store.getMaxEntries());
53          assertEquals("xyz", store.getName());
54          assertNotNull(store.getScheduler());
55      }
56  
57      public void testIdempotentReceiverRouter() throws Exception
58      {
59          MessageProcessor router = lookupInboundRouterFromService("IdempotentReceiverRouter");
60          assertTrue(router instanceof IdempotentMessageFilter);
61  
62          IdempotentMessageFilter filter = (IdempotentMessageFilter)router;
63          assertEquals("#[message:id]-#[message:correlationId]", filter.getIdExpression());
64          assertNotNull(filter.getStore());
65          assertTrue(filter.getStore() instanceof TextFileObjectStore);
66  
67          TextFileObjectStore store = (TextFileObjectStore)filter.getStore();
68          assertEquals(-1, store.getEntryTTL());
69          assertEquals(1000, store.getExpirationInterval());
70          assertEquals(10000000, store.getMaxEntries());
71          assertEquals("foo", store.getDirectory());
72          assertNotNull(store.getName());
73          assertNotNull(store.getScheduler());
74      }
75  
76      public void testSelectiveConsumerRouter() throws Exception
77      {
78          MessageProcessor router = lookupInboundRouterFromService("SelectiveConsumerRouter");
79          assertTrue(router instanceof MessageFilter);
80      }
81  
82      public void testCustomRouter() throws Exception
83      {
84          MessageProcessor router = lookupOutboundRouterFromService("CustomRouter");
85          assertTrue(router instanceof CustomOutboundRouter);
86      }
87  
88      protected MessageProcessor lookupOutboundRouterFromService(String serviceName) throws Exception
89      {
90          Service service = lookupService(serviceName);
91          OutboundRouterCollection routerCollection = 
92              (OutboundRouterCollection) service.getOutboundMessageProcessor();
93          return routerCollection.getRoutes().get(0);
94      }
95  
96      protected MessageProcessor lookupInboundRouterFromService(String serviceName) throws Exception
97      {
98          Service service = lookupService(serviceName);
99          List<MessageProcessor> routers = 
100             ((ServiceCompositeMessageSource) service.getMessageSource()).getMessageProcessors();
101         assertEquals(1, routers.size());
102         return routers.get(0);
103     }
104 
105     protected Service lookupService(String serviceName)
106     {
107         Service service = muleContext.getRegistry().lookupService(serviceName);
108         assertNotNull(service);
109         return service;
110     }
111     
112     public static class CustomOutboundRouter extends AbstractOutboundRouter
113     {
114         public boolean isMatch(MuleMessage message) throws MuleException
115         {
116             return true;
117         }
118 
119         @Override
120         protected MuleEvent route(MuleEvent event) throws MessagingException
121         {
122             return event;
123         }
124     }
125 }