1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.construct;
12
13 import org.apache.commons.lang.RandomStringUtils;
14 import org.apache.commons.lang.math.RandomUtils;
15 import org.mule.api.MuleEventContext;
16 import org.mule.api.MuleException;
17 import org.mule.api.MuleMessage;
18 import org.mule.module.client.MuleClient;
19 import org.mule.tck.FunctionalTestCase;
20 import org.mule.tck.functional.EventCallback;
21 import org.mule.tck.functional.FunctionalTestComponent;
22 import org.mule.transport.NullPayload;
23 import org.mule.util.concurrent.Latch;
24
25 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
26
27 public class BridgeTestCase extends FunctionalTestCase
28 {
29 private MuleClient muleClient;
30
31 @Override
32 protected void doSetUp() throws Exception
33 {
34 super.setDisposeManagerPerSuite(true);
35 super.doSetUp();
36 muleClient = new MuleClient(muleContext);
37 }
38
39 @Override
40 protected String getConfigResources()
41 {
42 return "org/mule/test/integration/construct/bridge-config.xml";
43 }
44
45 public void testSynchronous() throws Exception
46 {
47 doTestMathsService("vm://synchronous-bridge.in");
48 }
49
50 public void testAsynchronous() throws Exception
51 {
52 final MuleMessage result = muleClient.send("vm://asynchronous-bridge.in", "foobar", null);
53 assertEquals(NullPayload.getInstance(), result.getPayload());
54 }
55
56 public void testTransformers() throws Exception
57 {
58 doTestStringMassager("vm://transforming-bridge.in");
59 }
60
61 public void testEndpointReferences() throws Exception
62 {
63 doTestMathsService("vm://endpoint-ref-bridge.in");
64 }
65
66 public void testChildEndpoints() throws Exception
67 {
68 doTestMathsService("vm://child-endpoint-bridge.in");
69 }
70
71 public void testExceptionHandler() throws Exception
72 {
73 doTestMathsService("vm://exception-bridge.in");
74 }
75
76 public void testVmTransacted() throws Exception
77 {
78 doTestMathsService("vm://transacted-bridge.in");
79 }
80
81 public void testInheritance() throws Exception
82 {
83 doTestMathsService("vm://concrete-child-bridge.in");
84 }
85
86 public void testHeterogeneousTransports() throws Exception
87 {
88 doJmsBasedTest("jms://myDlq", "dlq-file-picker");
89 }
90
91 public void testJmsTransactions() throws Exception
92 {
93 doJmsBasedTest("jms://myQueue", "topic-listener");
94 }
95
96 private void doJmsBasedTest(String jmsDestinationUri, String ftcName)
97 throws Exception, MuleException, InterruptedException
98 {
99 final FunctionalTestComponent ftc = getFunctionalTestComponent(ftcName);
100 final Latch latch = new Latch();
101 ftc.setEventCallback(new EventCallback()
102 {
103 public void eventReceived(MuleEventContext context, Object component) throws Exception
104 {
105 latch.countDown();
106 }
107 });
108
109 final String payload = RandomStringUtils.randomAlphabetic(10);
110 muleClient.dispatch(jmsDestinationUri, payload, null);
111 latch.await(getTestTimeoutSecs(), TimeUnit.SECONDS);
112 assertEquals(1, ftc.getReceivedMessagesCount());
113 assertEquals(payload, byteArrayOrStringtoString(ftc.getReceivedMessage(1)));
114 }
115
116 private void doTestMathsService(String url) throws MuleException
117 {
118 final int a = RandomUtils.nextInt(100);
119 final int b = RandomUtils.nextInt(100);
120 final int result = (Integer) muleClient.send(url, new int[]{a, b}, null).getPayload();
121 assertEquals(a + b, result);
122 }
123
124 private void doTestStringMassager(String url) throws Exception, MuleException
125 {
126 final String payload = RandomStringUtils.randomAlphabetic(10);
127 final String result = muleClient.send(url, payload.getBytes(), null).getPayloadAsString();
128 assertEquals(payload + "barbaz", result);
129 }
130
131 private String byteArrayOrStringtoString(Object o)
132 {
133 if (o instanceof String)
134 {
135 return (String) o;
136 }
137
138 return new String((byte[]) o);
139 }
140 }