View Javadoc

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