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