1   /*
2    * $Id: EjbFunctionalTestCase.java 10961 2008-02-22 19:01:02Z dfeist $
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.transport.ejb;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.EndpointBuilder;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.transport.DispatchException;
18  import org.mule.config.i18n.Message;
19  import org.mule.endpoint.EndpointURIEndpointBuilder;
20  import org.mule.module.client.MuleClient;
21  import org.mule.tck.FunctionalTestCase;
22  import org.mule.transport.rmi.RmiConnector;
23  import org.mule.transport.rmi.i18n.RmiMessages;
24  
25  import java.util.HashMap;
26  import java.util.Properties;
27  
28  /**
29   * test RMI object invocations
30   */
31  public class EjbFunctionalTestCase extends FunctionalTestCase
32  {
33  
34      protected String getConfigResources()
35      {
36          return "ejb-functional-test.xml";
37      }
38  
39      private MuleMessage send(String uri, String message) throws Exception
40      {
41          MuleClient client = new MuleClient();
42          return client.send(uri, message, new HashMap());
43      }
44  
45      public void testReverseString() throws Exception
46      {
47          MuleMessage message = send("ejb://localhost/TestService?method=reverseString", "hello");
48          assertNotNull(message.getPayload());
49          assertEquals("olleh", message.getPayloadAsString());
50      }
51  
52      public void testUpperCaseString() throws Exception
53      {
54          MuleMessage message = send("ejb://localhost/TestService?method=upperCaseString", "hello");
55          assertNotNull(message.getPayload());
56          assertEquals("HELLO", message.getPayloadAsString());
57      }
58  
59      public void testNoMethodSet() throws Exception
60      {
61          try
62          {
63              send("ejb://localhost/TestService", "hello");
64  
65          }
66          catch (MuleException e)
67          {
68              assertTrue(e instanceof DispatchException);
69              
70              Message message = RmiMessages.messageParamServiceMethodNotSet();
71              assertTrue(e.getMessage().startsWith(message.toString()));
72          }
73      }
74  
75      public void testBadMethodName() throws Exception
76      {
77          try
78          {
79              send("ejb://localhost/TestService?method=foo", "hello");
80          }
81          catch (MuleException e)
82          {
83              assertTrue(e.getCause() instanceof NoSuchMethodException);
84          }
85      }
86  
87      public void testBadMethodType() throws Exception
88      {
89          // moving this to xml config requires endpoint properties
90          // MULE-1790
91          EndpointBuilder builder = new EndpointURIEndpointBuilder("ejb://localhost/TestService?method=reverseString",
92              muleContext);
93          Properties props = new Properties();
94          props.put(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, StringBuffer.class.getName());
95          builder.setProperties(props);
96  
97          OutboundEndpoint ep = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
98              builder);
99          try
100         {
101             ep.send(getTestEvent("hello", ep));
102         }
103         catch (MuleException e)
104         {
105             assertTrue(e.getCause() instanceof NoSuchMethodException);
106         }
107     }
108 
109     public void testCorrectMethodType() throws Exception
110     {
111         // moving this to xml config requires endpoint properties
112         // MULE-1790
113         EndpointBuilder builder = new EndpointURIEndpointBuilder("ejb://localhost/TestService?method=reverseString",
114             muleContext);
115         Properties props = new Properties();
116         props.put(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES, StringBuffer.class.getName());
117         builder.setProperties(props);
118         
119         OutboundEndpoint ep = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
120             builder);
121         
122         try
123         {
124             ep.send(getTestEvent("hello", ep));
125         }
126         catch (MuleException e)
127         {
128             assertTrue(e.getCause() instanceof NoSuchMethodException);
129         }
130     }
131 
132 }