1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ajax;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.DynamicPortTestCase;
16 import org.mule.util.concurrent.Latch;
17
18 import java.util.Map;
19 import java.util.concurrent.atomic.AtomicReference;
20
21 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22
23 import org.codehaus.jackson.map.ObjectMapper;
24 import org.cometd.Client;
25 import org.cometd.Message;
26 import org.cometd.MessageListener;
27 import org.mortbay.cometd.client.BayeuxClient;
28 import org.mortbay.jetty.client.Address;
29 import org.mortbay.jetty.client.HttpClient;
30
31 public class AjaxFunctionalTestCase extends DynamicPortTestCase
32 {
33 public static int SERVER_PORT = -1;
34
35 private HttpClient httpClient;
36 private BayeuxClient bayeuxClient;
37
38 @Override
39 protected String getConfigResources()
40 {
41 return "ajax-embedded-functional-test.xml";
42 }
43
44 @Override
45 protected void doSetUp() throws Exception
46 {
47 SERVER_PORT = getPorts().get(0);
48 httpClient = new HttpClient();
49 httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
50 httpClient.start();
51
52 bayeuxClient = new BayeuxClient(httpClient, new Address("localhost", SERVER_PORT), "/ajax/cometd");
53
54 bayeuxClient.start();
55 }
56
57 @Override
58 protected void doTearDown() throws Exception
59 {
60 if (httpClient.isRunning())
61 {
62 httpClient.stop();
63 }
64 }
65
66 public void testClientSubscribeWithString() throws Exception
67 {
68 final Latch latch = new Latch();
69
70 final AtomicReference<Object> data = new AtomicReference<Object>();
71 bayeuxClient.addListener(new MessageListener()
72 {
73 public void deliver(Client fromClient, Client toClient, Message message)
74 {
75 if (message.getData() != null)
76 {
77
78 data.set(message.toString());
79 latch.release();
80 }
81 }
82 });
83 bayeuxClient.subscribe("/test1");
84
85 MuleClient muleClient = new MuleClient(muleContext);
86 muleClient.dispatch("vm://in1", "Ross", null);
87 latch.await(10, TimeUnit.SECONDS);
88
89 assertNotNull(data.get());
90
91
92 ObjectMapper mapper = new ObjectMapper();
93 Map result = mapper.readValue((String) data.get(), Map.class);
94 assertEquals("/test1", result.get("channel"));
95 assertEquals("Ross Received", result.get("data"));
96 }
97
98 public void testClientPublishWithString() throws Exception
99 {
100 MuleClient muleClient = new MuleClient(muleContext);
101
102 bayeuxClient.publish("/test2", "Ross", null);
103 MuleMessage msg = muleClient.request("vm://in2", RECEIVE_TIMEOUT);
104
105 assertNotNull(msg);
106 assertEquals("Ross Received", msg.getPayloadAsString());
107 }
108
109 @Override
110 protected int getNumPortsToFind()
111 {
112 return 1;
113 }
114 }