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