1   /*
2    * $Id: RmiInvocationTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.rmi;
12  
13  import org.mule.config.ConfigurationBuilder;
14  import org.mule.config.builders.QuickConfigurationBuilder;
15  import org.mule.config.i18n.Message;
16  import org.mule.impl.ImmutableMuleEndpoint;
17  import org.mule.impl.endpoint.MuleEndpoint;
18  import org.mule.impl.jndi.MuleInitialContextFactory;
19  import org.mule.providers.rmi.i18n.RmiMessages;
20  import org.mule.tck.FunctionalTestCase;
21  import org.mule.tck.services.MatchingMethodsComponent;
22  import org.mule.umo.UMOException;
23  import org.mule.umo.UMOMessage;
24  import org.mule.umo.endpoint.UMOEndpoint;
25  import org.mule.umo.endpoint.UMOImmutableEndpoint;
26  import org.mule.umo.provider.DispatchException;
27  
28  import java.util.Hashtable;
29  
30  import javax.naming.Context;
31  import javax.naming.InitialContext;
32  
33  /**
34   * test RMI object invocations
35   */
36  public class RmiInvocationTestCase extends FunctionalTestCase
37  {
38  
39      RmiConnector rmiConnector;
40  
41      protected String getConfigResources()
42      {
43          return null;
44      }
45  
46      protected ConfigurationBuilder getBuilder() throws Exception
47      {
48          QuickConfigurationBuilder builder = new QuickConfigurationBuilder();
49          builder.disableAdminAgent();
50  
51          // create RMI connector
52          rmiConnector = new RmiConnector();
53          rmiConnector.setName("rmi");
54          rmiConnector.setJndiInitialFactory(MuleInitialContextFactory.class.getName());
55          rmiConnector.setSecurityPolicy("rmi.policy");
56  
57          // Required if connectoring to a Remote Jndi context
58          // builder.getManager().registerAgent(new RmiRegistryAgent());
59  
60          // Create a local Jndi Context
61          Hashtable env = new Hashtable();
62          // env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
63          env.put(Context.INITIAL_CONTEXT_FACTORY, MuleInitialContextFactory.class.getName());
64          InitialContext ic = new InitialContext(env);
65          // Bind our servcie object
66          ic.bind("TestService", new MatchingMethodsComponent());
67  
68          rmiConnector.setJndiContext(ic);
69          builder.getManager().registerConnector(rmiConnector);
70          return builder;
71      }
72  
73      public void testReverseString() throws Exception
74      {
75          UMOImmutableEndpoint ep = new ImmutableMuleEndpoint(
76              "rmi://localhost/TestService?method=reverseString", false);
77          UMOMessage message = ep.send(getTestEvent("hello", ep));
78          assertNotNull(message.getPayload());
79          assertEquals("olleh", message.getPayloadAsString());
80      }
81  
82      public void testUpperCaseString() throws Exception
83      {
84          UMOImmutableEndpoint ep = new ImmutableMuleEndpoint(
85              "rmi://localhost/TestService?method=upperCaseString", false);
86          UMOMessage message = ep.send(getTestEvent("hello", ep));
87          assertNotNull(message.getPayload());
88          assertEquals("HELLO", message.getPayloadAsString());
89      }
90  
91      public void testNoMethodSet() throws Exception
92      {
93          UMOImmutableEndpoint ep = new ImmutableMuleEndpoint("rmi://localhost/TestService", false);
94          try
95          {
96              ep.send(getTestEvent("hello", ep));
97          }
98          catch (UMOException e)
99          {
100             assertTrue(e instanceof DispatchException);
101             
102             Message message = RmiMessages.messageParamServiceMethodNotSet();
103             assertTrue(e.getMessage().startsWith(message.toString()));
104         }
105     }
106 
107     public void testBadMethodName() throws Exception
108     {
109         UMOImmutableEndpoint ep = new ImmutableMuleEndpoint("rmi://localhost/TestService?method=foo", false);
110         try
111         {
112             ep.send(getTestEvent("hello", ep));
113         }
114         catch (UMOException e)
115         {
116             assertTrue(e.getCause() instanceof NoSuchMethodException);
117         }
118     }
119 
120     public void testBadMethodType() throws Exception
121     {
122         UMOEndpoint ep = new MuleEndpoint("rmi://localhost/TestService?method=reverseString", false);
123         ep.setProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, StringBuffer.class.getName());
124         try
125         {
126             ep.send(getTestEvent("hello", ep));
127         }
128         catch (UMOException e)
129         {
130             assertTrue(e.getCause() instanceof NoSuchMethodException);
131         }
132     }
133 
134     public void testCorrectMethodType() throws Exception
135     {
136         UMOEndpoint ep = new MuleEndpoint("rmi://localhost/TestService?method=reverseString", false);
137         ep.setProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, String.class.getName());
138         try
139         {
140             ep.send(getTestEvent("hello", ep));
141         }
142         catch (UMOException e)
143         {
144             assertTrue(e.getCause() instanceof NoSuchMethodException);
145         }
146     }
147 
148 }