View Javadoc

1   /*
2    * $Id: MuleMessageFactoryUsagePatternsTestCase.java 19191 2010-08-25 21:05:23Z 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.transport;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.lifecycle.CreateException;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.transport.MessageDispatcher;
20  import org.mule.api.transport.MessageReceiver;
21  import org.mule.api.transport.MessageRequester;
22  import org.mule.api.transport.MuleMessageFactory;
23  import org.mule.tck.AbstractMuleTestCase;
24  
25  /**
26   * This test verifies and illustrates various usage patterns with {@link MuleMessageFactory}. It 
27   * uses {@link MessageDispatcher} instances for the test but the same patterns apply to 
28   * {@link MessageReceiver} and {@link MessageRequester} as well as all of the code resides in their
29   * abstract superclasses.
30   */
31  public class MuleMessageFactoryUsagePatternsTestCase extends AbstractMuleTestCase
32  {
33      private OutboundEndpoint endpoint;
34      private AbstractConnector connector;
35      private MuleMessageFactory factoryFromConnector;
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41          
42          endpoint = getTestOutboundEndpoint("test");
43          connector = (AbstractConnector) endpoint.getConnector();
44          factoryFromConnector = connector.getMuleMessageFactory();
45      }
46  
47      public void testSharedMuleMessageFactoryWithConnector() throws Exception
48      {
49          connector.setDispatcherFactory(new FakeDispatcherFactory());
50          
51          MockMessageDispatcher dispatcher = 
52              (MockMessageDispatcher) connector.getDispatcherFactory().create(endpoint);
53          dispatcher.initialise();
54          
55          MuleMessageFactory factoryFromDispatcher = dispatcher.getMuleMessageFactory();
56          assertNotNull(factoryFromDispatcher);
57          assertSame(factoryFromConnector, factoryFromDispatcher);
58      }
59      
60      public void testMessageDispatcherCreatesOwnMuleMessageFactory() throws Exception
61      {
62          connector.setDispatcherFactory(new CustomDispatcherFactory());
63          
64          CustomMessageDispatcher dispatcher = 
65              (CustomMessageDispatcher) connector.getDispatcherFactory().create(endpoint);
66          dispatcher.initialise();
67          
68          MuleMessageFactory factoryFromDispatcher = dispatcher.getMuleMessageFactory();
69          assertNotNull(factoryFromDispatcher);
70          assertNotSame(factoryFromConnector, factoryFromDispatcher);
71      }
72      
73      private static class FakeDispatcherFactory extends AbstractMessageDispatcherFactory
74      {
75          public FakeDispatcherFactory()
76          {
77              super();
78          }
79          
80          @Override
81          public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
82          {
83              return new MockMessageDispatcher(endpoint);
84          }
85      }
86      
87      private static class CustomDispatcherFactory extends AbstractMessageDispatcherFactory
88      {
89          public CustomDispatcherFactory()
90          {
91              super();
92          }
93  
94          @Override
95          public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
96          {
97              return new CustomMessageDispatcher(endpoint);
98          }
99      }
100     
101     private static class MockMessageDispatcher extends AbstractMessageDispatcher
102     {
103         public MockMessageDispatcher(OutboundEndpoint endpoint)
104         {
105             super(endpoint);
106         }
107 
108         /**
109          * open up access for testing
110          */
111         public MuleMessageFactory getMuleMessageFactory()
112         {
113             return muleMessageFactory;
114         }
115 
116         @Override
117         protected void doDispatch(MuleEvent event) throws Exception
118         {
119             throw new UnsupportedOperationException("doDispatch");
120         }
121 
122         @Override
123         protected MuleMessage doSend(MuleEvent event) throws Exception
124         {
125             throw new UnsupportedOperationException("doSend");
126         }
127     }
128     
129     private static class CustomMessageDispatcher extends AbstractMessageDispatcher
130     {
131         public CustomMessageDispatcher(OutboundEndpoint endpoint)
132         {
133             super(endpoint);
134         }
135 
136         @Override
137         protected void initializeMessageFactory() throws InitialisationException
138         {
139             try
140             {
141                 muleMessageFactory = connector.createMuleMessageFactory();
142             }
143             catch (CreateException e)
144             {
145                 throw new InitialisationException(e, this);
146             }
147         }
148 
149         /**
150          * open up access for testing
151          */
152         public MuleMessageFactory getMuleMessageFactory()
153         {
154             return muleMessageFactory;
155         }
156 
157         @Override
158         protected void doDispatch(MuleEvent event) throws Exception
159         {
160             throw new UnsupportedOperationException("doDispatch");
161         }
162 
163         @Override
164         protected MuleMessage doSend(MuleEvent event) throws Exception
165         {
166             throw new UnsupportedOperationException("doSend");
167         }
168     }
169 }