1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.tcp.issues;
12
13 import org.mule.MuleManager;
14 import org.mule.extras.client.MuleClient;
15 import org.mule.tck.FunctionalTestCase;
16 import org.mule.tck.functional.EventCallback;
17 import org.mule.tck.functional.FunctionalStreamingTestComponent;
18 import org.mule.umo.UMOEventContext;
19 import org.mule.umo.model.UMOModel;
20
21 import java.util.HashMap;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
24 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
25 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 public class MultiStreamMule1692TestCase extends FunctionalTestCase
30 {
31
32 private static final Log logger = LogFactory.getLog(MultiStreamMule1692TestCase.class);
33 public static final int TIMEOUT = 3000;
34 public static final String TEST_MESSAGE = "Test TCP Request";
35 public static final String TEST_MESSAGE_2 = "Second test TCP Request";
36 public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
37 public static final String RESULT_2 = "Received stream; length: 23; 'Seco...uest'";
38
39 public MultiStreamMule1692TestCase()
40 {
41 setDisposeManagerPerSuite(true);
42 }
43
44 protected String getConfigResources()
45 {
46 return "tcp-streaming-test.xml";
47 }
48
49 private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
50 {
51 return new EventCallback()
52 {
53 public synchronized void eventReceived(UMOEventContext context, Object component)
54 {
55 try
56 {
57 FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
58
59 if (1 == latch.getCount())
60 {
61 message.set(ftc.getSummary());
62 latch.countDown();
63 }
64 }
65 catch (Exception e)
66 {
67 logger.error(e.getMessage(), e);
68 }
69 }
70 };
71 }
72
73 public void testSend() throws Exception
74 {
75 MuleClient client = new MuleClient();
76
77 UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("echo");
78 FunctionalStreamingTestComponent ftc =
79 (FunctionalStreamingTestComponent) model.getComponent("testComponent").getInstance();
80
81
82
83 final CountDownLatch latch = new CountDownLatch(1);
84 final AtomicReference message = new AtomicReference();
85 ftc.setEventCallback(newCallback(latch, message), TEST_MESSAGE.length());
86 client.dispatch("tcp://localhost:65432", TEST_MESSAGE, new HashMap());
87 latch.await(10, TimeUnit.SECONDS);
88 assertEquals(RESULT, message.get());
89
90 final CountDownLatch latch2 = new CountDownLatch(1);
91 final AtomicReference message2 = new AtomicReference();
92 ftc.setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
93 client.dispatch("tcp://localhost:65432", TEST_MESSAGE_2, new HashMap());
94 latch2.await(10, TimeUnit.SECONDS);
95 assertEquals(RESULT_2, message2.get());
96 }
97
98 }
99