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