View Javadoc

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