View Javadoc

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