1   /*
2    * $Id: EjbInvocationTestCase.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.ejb;
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.container.DummyEjbHomeProxy;
18  import org.mule.impl.endpoint.MuleEndpoint;
19  import org.mule.impl.jndi.MuleInitialContextFactory;
20  import org.mule.providers.rmi.RmiConnector;
21  import org.mule.providers.rmi.i18n.RmiMessages;
22  import org.mule.tck.FunctionalTestCase;
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 EjbInvocationTestCase extends FunctionalTestCase
38  {
39  
40      private EjbConnector ejbConnector;
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 RMI connector
53          ejbConnector = new EjbConnector();
54          ejbConnector.setName("ejb");
55          ejbConnector.setJndiInitialFactory(MuleInitialContextFactory.class.getName());
56          ejbConnector.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 DummyEjbHomeProxy());
68  
69          ejbConnector.setJndiContext(ic);
70          builder.getManager().registerConnector(ejbConnector);
71          return builder;
72      }
73  
74      public void testReverseString() throws Exception
75      {
76          UMOImmutableEndpoint ep = new ImmutableMuleEndpoint(
77              "ejb://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              "ejb://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("ejb://localhost/TestService", false);
95          try
96          {
97              ep.send(getTestEvent("hello", ep));
98  
99          }
100         catch (UMOException e)
101         {
102             assertTrue(e instanceof DispatchException);
103             
104             Message message = RmiMessages.messageParamServiceMethodNotSet();
105             assertTrue(e.getMessage().startsWith(message.toString()));
106         }
107     }
108 
109     public void testBadMethodName() throws Exception
110     {
111         UMOImmutableEndpoint ep = new ImmutableMuleEndpoint("ejb://localhost/TestService?method=foo", false);
112         try
113         {
114             ep.send(getTestEvent("hello", ep));
115         }
116         catch (UMOException e)
117         {
118             assertTrue(e.getCause() instanceof NoSuchMethodException);
119         }
120     }
121 
122     public void testBadMethodType() throws Exception
123     {
124         UMOEndpoint ep = new MuleEndpoint("ejb://localhost/TestService?method=reverseString", false);
125         ep.setProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, StringBuffer.class.getName());
126         try
127         {
128             ep.send(getTestEvent("hello", ep));
129         }
130         catch (UMOException e)
131         {
132             assertTrue(e.getCause() instanceof NoSuchMethodException);
133         }
134     }
135 
136     public void testCorrectMethodType() throws Exception
137     {
138         UMOEndpoint ep = new MuleEndpoint("ejb://localhost/TestService?method=reverseString", false);
139         ep.setProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, String.class.getName());
140         try
141         {
142             ep.send(getTestEvent("hello", ep));
143         }
144         catch (UMOException e)
145         {
146             assertTrue(e.getCause() instanceof NoSuchMethodException);
147         }
148     }
149 
150 }