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