1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ajax;
12
13 import org.mule.tck.DynamicPortTestCase;
14 import org.mule.util.concurrent.Latch;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
19
20 import org.cometd.Client;
21 import org.cometd.Message;
22 import org.cometd.MessageListener;
23 import org.mortbay.cometd.client.BayeuxClient;
24 import org.mortbay.jetty.client.Address;
25 import org.mortbay.jetty.client.HttpClient;
26
27 public class AjaxRPCFunctionalTestCase extends DynamicPortTestCase
28 {
29 public static final String TEST_JSON_MESSAGE = "{\"data\" : {\"value1\" : \"foo\", \"value2\" : \"bar\"}, \"replyTo\" : \"/response\"}";
30
31 public static int SERVER_PORT = -1;
32
33 private BayeuxClient client;
34
35 @Override
36 protected String getConfigResources()
37 {
38 return "ajax-rpc-test.xml";
39 }
40
41 @Override
42 protected void doSetUp() throws Exception
43 {
44
45
46 SERVER_PORT = getPorts().get(0);
47 HttpClient http = new HttpClient();
48 http.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
49
50 client = new BayeuxClient(http, new Address("localhost", SERVER_PORT), "/ajax/cometd");
51 http.start();
52
53 client.start();
54 }
55
56 @Override
57 protected void doTearDown() throws Exception
58 {
59
60 client.stop();
61 }
62
63 public void testDispatchReceiveSimple() throws Exception
64 {
65 final Latch latch = new Latch();
66
67 final AtomicReference<Object> data = new AtomicReference<Object>();
68 client.addListener(new MessageListener()
69 {
70 public void deliver(Client client, Client client1, Message message)
71 {
72 if (message.getData() != null)
73 {
74
75 data.set((message.getData()));
76 latch.release();
77 }
78 }
79 });
80
81 client.subscribe("/response");
82
83 client.publish("/request", TEST_JSON_MESSAGE, null);
84 latch.await(10, TimeUnit.SECONDS);
85
86 assertNotNull(data.get());
87 assertEquals("{\"value1\":\"foo\",\"value2\":\"bar\"}", data.get());
88 }
89
90 @Override
91 protected int getNumPortsToFind()
92 {
93 return 1;
94 }
95 }