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