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.rmi;
8   
9   import org.mule.api.endpoint.EndpointBuilder;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.api.lifecycle.InitialisationException;
12  import org.mule.config.i18n.MessageFactory;
13  import org.mule.endpoint.EndpointURIEndpointBuilder;
14  import org.mule.transport.AbstractMessageReceiverTestCase;
15  import org.mule.util.concurrent.Latch;
16  
17  import java.rmi.Naming;
18  import java.rmi.registry.LocateRegistry;
19  import java.rmi.registry.Registry;
20  import java.rmi.server.UnicastRemoteObject;
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.Hashtable;
24  import java.util.Map;
25  
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  
29  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.junit.Test;
33  
34  import static org.junit.Assert.assertNotNull;
35  import static org.junit.Assert.assertNull;
36  import static org.junit.Assert.assertTrue;
37  
38  /**
39   * This is a UNIT TEST case, not a functional test case.
40   * 
41   * @author Yuen-Chi Lian
42   */
43  public class RmiMessageReceiverTestCase extends AbstractMessageReceiverTestCase
44  {
45      private static Log LOGGER = LogFactory.getLog(RmiMessageReceiverTestCase.class);
46  
47      private RmiConnector connector = null;
48      private RmiMessageReceiver messageReceiver = null;
49      private Registry rmiRegistry = null;
50  
51      @Override
52      protected void doSetUp() throws Exception
53      {
54          registerRmi();
55  
56          connector = new RmiConnector(muleContext);
57          connector.setName("TestConnector:" + this.getClass());
58          connector.setSecurityPolicy(ClassLoader.getSystemResource("rmi.policy").getPath());
59          connector.setJndiInitialFactory("com.sun.jndi.rmi.registry.RegistryContextFactory");
60          connector.setJndiProviderUrl("rmi://localhost:11099");
61  
62          muleContext.getRegistry().registerConnector(connector);
63  
64          super.doSetUp();
65      }
66  
67      @SuppressWarnings("unchecked")
68      @Override
69      public InboundEndpoint getEndpoint() throws Exception
70      {
71          if (this.endpoint == null)
72          {
73              EndpointBuilder builder = new EndpointURIEndpointBuilder(
74                  "rmi://localhost:11099/TestMatchingMethodsComponent?method=reverseString", muleContext);
75  
76              if (connector == null)
77              {
78                  throw new InitialisationException(
79                      MessageFactory.createStaticMessage("Connector has not been initialized."), null);
80              }
81              builder.setConnector(connector);
82  
83              Map properties = new HashMap();
84              properties.put("methodArgumentTypes", "java.lang.String");
85              properties.put("methodArgumentsList", Arrays.asList(new String[]{"test"}));
86  
87              builder.setProperties(properties);
88              endpoint = muleContext.getEndpointFactory().getInboundEndpoint(builder);
89  
90              return endpoint;
91          }
92          return this.endpoint;
93      }
94  
95      @Override
96      public RmiMessageReceiver getMessageReceiver() throws Exception
97      {
98          if (messageReceiver == null)
99          {
100             messageReceiver = new RmiMessageReceiver(this.connector, getTestService(),
101                 this.getEndpoint(), 5000)
102             {
103                 @Override
104                 public void poll()
105                 {
106                     super.poll();
107                     RmiMessageReceiverTestCase.this.callbackCalled.countDown();
108                 }
109 
110             };
111             messageReceiver.initialise();
112         }
113         return messageReceiver;
114     }
115 
116     @Test
117     public void testReceive() throws Exception
118     {
119         RmiMessageReceiver messageReceiver = this.getMessageReceiver();
120 
121         // Before connect(), let's do some assertion
122         assertNull(messageReceiver.invokeMethod);
123         messageReceiver.connect();
124 
125         // Make sure that the transport could find the method
126         assertNotNull(messageReceiver.invokeMethod);
127 
128         // Poll once
129         callbackCalled = new Latch();
130         messageReceiver.poll();
131         assertTrue(callbackCalled.await(1000, TimeUnit.MILLISECONDS));
132     }
133 
134     private void registerRmi() throws Exception
135     {
136         if (null == rmiRegistry)
137         {
138             rmiRegistry = LocateRegistry.createRegistry(11099);
139             Naming.rebind("//localhost:11099/TestMatchingMethodsComponent",
140                 new SerializedMatchingMethodsComponent());
141 
142             Hashtable<String, String> env = new Hashtable<String, String>();
143             env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
144 
145             Context context = new InitialContext(env);
146             SerializedMatchingMethodsComponent obj = 
147                 (SerializedMatchingMethodsComponent)context.lookup("rmi://localhost:11099/TestMatchingMethodsComponent");
148 
149             if (obj == null)
150             {
151                 throw new RuntimeException("Could not start RMI properly");
152             }
153         }
154     }
155 
156     @Override
157     protected void doTearDown() throws Exception
158     {
159         try
160         {
161             messageReceiver.disconnect();
162             messageReceiver.dispose();
163 
164             connector.disconnect();
165             connector.dispose();
166 
167             UnicastRemoteObject.unexportObject(rmiRegistry, true);
168         }
169         catch (Exception e)
170         {
171             LOGGER.warn(e.toString(), e);
172         }
173 
174         super.doTearDown();
175     }
176 }