View Javadoc

1   /*
2    * $Id: AjaxFunctionalTestCase.java 20762 2010-12-16 00:00:31Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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          // need to start the client before you can add subscriptions
54          bayeuxClient.start();
55          
56          assertTrue("httpClient is not running", httpClient.isRunning());
57          assertTrue("bayeuxClient is not running", bayeuxClient.isRunning());
58      }
59  
60      @Override
61      protected void doTearDown() throws Exception
62      {
63          if (httpClient.isRunning())
64          {
65              httpClient.stop();
66          }
67  
68          try
69          {
70              /*
71               * always try to stop the client as I think there is a timing issue of it
72               * staying up between tests and even if it thinks it's running, calling
73               * stop sometimes throws an exception
74               */
75              bayeuxClient.stop();
76          }
77          catch (Exception e)
78          {
79              // dont do anything
80          }
81      }
82  
83      public void testClientSubscribeWithString() throws Exception
84      {
85          /*
86           * Give mule and the clients time to warm up; we get an intermittent failure,
87           * see if this helps
88           */
89          Thread.sleep(5000); 
90          final Latch latch = new Latch();
91  
92          final AtomicReference<Object> data = new AtomicReference<Object>();
93          bayeuxClient.addListener(new MessageListener()
94          {
95              public void deliver(Client fromClient, Client toClient, Message message)
96              {
97  
98                  if (message.getData() != null)
99                  {
100                     // This simulates what the browser would receive
101                     data.set(message.toString());
102                     latch.release();
103                 }
104             }
105         });
106         bayeuxClient.subscribe("/test1");
107 
108         MuleClient muleClient = new MuleClient(muleContext);
109         muleClient.dispatch("vm://in1", "Ross", null);
110 
111         latch.await(10, TimeUnit.SECONDS);
112         assertNotNull(data.get());
113         
114         // parse the result string into java objects.  different jvms return it in different order, so we can't do a straight string comparison 
115         ObjectMapper mapper = new ObjectMapper();
116         Map result  = mapper.readValue((String) data.get(), Map.class);
117         assertEquals("/test1", result.get("channel"));
118         assertEquals("Ross Received", result.get("data"));
119     }
120 
121     public void testClientPublishWithString() throws Exception
122     {
123         MuleClient muleClient = new MuleClient(muleContext);
124 
125         bayeuxClient.publish("/test2", "Ross", null);
126         MuleMessage msg = muleClient.request("vm://in2", RECEIVE_TIMEOUT);
127 
128         assertNotNull(msg);
129         assertEquals("Ross Received", msg.getPayloadAsString());
130     }
131 
132     @Override
133     protected int getNumPortsToFind()
134     {
135         return 1;
136     }
137 }