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 java.lang.IllegalStateException;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24
25 import org.codehaus.jackson.map.ObjectMapper;
26 import org.cometd.Client;
27 import org.cometd.Message;
28 import org.cometd.MessageListener;
29 import org.mortbay.cometd.client.BayeuxClient;
30 import org.mortbay.jetty.client.Address;
31 import org.mortbay.jetty.client.HttpClient;
32
33 public class AjaxFunctionalJsonBindingsTestCase extends DynamicPortTestCase
34 {
35 public static int SERVER_PORT = -1;
36
37 private BayeuxClient client;
38
39 @Override
40 protected String getConfigResources()
41 {
42 return "ajax-embedded-functional-json-bindings-test.xml";
43 }
44
45 @Override
46 protected void doSetUp() throws Exception
47 {
48 SERVER_PORT = getPorts().get(0);
49 HttpClient http = new HttpClient();
50 http.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
51
52 client = new BayeuxClient(http, new Address("localhost", SERVER_PORT), "/ajax/cometd");
53 http.start();
54
55 client.start();
56 }
57
58 @Override
59 protected void doTearDown() throws Exception
60 {
61
62
63
64 try
65 {
66 client.stop();
67 }
68 catch (IllegalStateException e)
69 {
70 logger.info("caught an IllegalStateException during tearDown", e);
71 }
72 catch(Exception e1)
73 {
74 fail("unexpected exception during tearDown :" + e1.getMessage());
75 }
76 }
77
78 public void testClientSubscribeWithJsonObjectResponse() throws Exception
79 {
80 final Latch latch = new Latch();
81
82 final AtomicReference<String> data = new AtomicReference<String>();
83 client.addListener(new MessageListener()
84 {
85 public void deliver(Client fromClient, Client toClient, Message message)
86 {
87 if (message.getData() != null)
88 {
89
90 data.set(message.toString());
91 latch.release();
92 }
93 }
94 });
95 client.subscribe("/test1");
96
97 MuleClient muleClient = new MuleClient(muleContext);
98 muleClient.dispatch("vm://in1", "Ross", null);
99 latch.await(10, TimeUnit.SECONDS);
100
101 assertNotNull(data.get());
102
103
104
105 ObjectMapper mapper = new ObjectMapper();
106 Map<?, ?> result = mapper.readValue(data.get(), Map.class);
107 assertEquals("/test1", result.get("channel"));
108 assertEquals("Ross", ((Map<?, ?>)result.get("data")).get("name"));
109 }
110
111 public void testClientPublishWithJsonObject() throws Exception
112 {
113 client.publish("/test2", "{\"name\":\"Ross\"}", null);
114 MuleClient muleClient = new MuleClient(muleContext);
115 MuleMessage msg = muleClient.request("vm://in2", 5000L);
116
117 assertNotNull(msg);
118 assertEquals("Received: DummyJsonBean{name='Ross'}", msg.getPayloadAsString());
119 }
120
121 @Override
122 protected int getNumPortsToFind()
123 {
124 return 1;
125 }
126 }