1   /*
2    * $Id: AbstractFunctionalTestCase.java 10789 2008-02-12 20:04:43Z 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;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transport.DispatchException;
16  import org.mule.config.i18n.Message;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.FunctionalTestCase;
19  import org.mule.transport.rmi.i18n.RmiMessages;
20  
21  import java.util.HashMap;
22  
23  public abstract class AbstractFunctionalTestCase extends FunctionalTestCase
24  {
25  
26      private String prefix;
27      private String config;
28  
29      public AbstractFunctionalTestCase(String prefix, String config)
30      {
31          this.prefix = prefix;
32          this.config = config;
33      }
34  
35      // from earlier multiple target test case
36  
37      public void testCase() throws Exception
38      {
39          MuleClient client = new MuleClient();
40  
41          // send Echo String
42          MuleMessage message = client.send("vm://testin", new Integer(12), null);
43          assertNotNull(message);
44          Integer payload = (Integer)message.getPayload();
45          assertEquals(payload, new Integer(22));
46  
47          // send String
48          message = client.send("vm://testin", "test matching component first time", null);
49          assertNotNull(message);
50          assertEquals((String)message.getPayload(), "emit tsrif tnenopmoc gnihctam tset");
51  
52          // send String
53          message = client.send("vm://testin", "test mathching component second time", null);
54          assertNotNull(message);
55          assertEquals((String)message.getPayload(), "emit dnoces tnenopmoc gnihchtam tset");
56  
57          // send Integer
58          message = client.send("vm://testin", new Integer(15), null);
59          assertNotNull(message);
60          payload = (Integer)message.getPayload();
61          assertEquals(payload, new Integer(25));
62      }
63  
64      // from earlier invocation test case
65  
66      private MuleMessage send(String uri, String message) throws Exception
67      {
68          MuleClient client = new MuleClient();
69          return client.send(prefix + uri, message, new HashMap());
70      }
71  
72      public void testReverseString() throws Exception
73      {
74          MuleMessage message = send("://localhost/TestService?method=reverseString", "hello");
75          assertNotNull(message.getPayload());
76          assertEquals("olleh", message.getPayloadAsString());
77      }
78  
79      public void testUpperCaseString() throws Exception
80      {
81          MuleMessage message = send("://localhost/TestService?method=upperCaseString", "hello");
82          assertNotNull(message.getPayload());
83          assertEquals("HELLO", message.getPayloadAsString());
84      }
85  
86      public void testNoMethodSet() throws Exception
87      {
88          try
89          {
90              send("://localhost/TestService", "hello");
91          }
92          catch (MuleException e)
93          {
94              assertTrue(e instanceof DispatchException);
95  
96              Message message = RmiMessages.messageParamServiceMethodNotSet();
97              assertTrue(e.getMessage().startsWith(message.toString()));
98          }
99      }
100 
101     public void testBadMethodName() throws Exception
102     {
103         try
104         {
105             send("://localhost/TestService?method=foo", "hello");
106             fail("expected error");
107         }
108         catch (MuleException e)
109         {
110             assertTrue(e.getCause() instanceof NoSuchMethodException);
111         }
112     }
113 
114     protected String getConfigResources()
115     {
116         return config;
117     }
118 
119     public void testBadMethodType() throws Exception
120     {
121         try
122         {
123             new MuleClient().send("BadType", "hello", null);
124             fail("expected error");
125         }
126         catch (MuleException e)
127         {
128             assertTrue(e.getCause() instanceof NoSuchMethodException);
129         }
130     }
131 
132     public void testCorrectMethodType() throws Exception
133     {
134         MuleMessage message = new MuleClient().send("GoodType", "hello", null);
135         assertNotNull(message);
136         assertEquals("olleh", message.getPayloadAsString());
137     }
138     
139 }