View Javadoc

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