View Javadoc

1   /*
2    * $Id: AjaxRPCFunctionalTestCase.java 19866 2010-10-08 20:17:09Z 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.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          // FIXME DZ: we don't use the inherited SERVER_PORT here because it's not set
45          // at this point and we can't move super.doSetUp() above this
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          //need to start the client before you can add subscriptions
53          client.start();
54      }
55  
56      @Override
57      protected void doTearDown() throws Exception
58      {
59          //9 times out of 10 this throws a "ava.lang.IllegalStateException: Not running" exception, it can be ignored
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                      //This simulate what the browser would receive
75                      data.set((message.getData()));
76                      latch.release();
77                  }
78              }
79          });
80          //The '/response' channel is set on the request message
81          client.subscribe("/response");
82          //Simulates dispatching from the browser
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  }