View Javadoc

1   /*
2    * $Id: HttpPersistentQueueTestCase.java 19817 2010-10-04 18:10:39Z 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.http.functional;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.tck.DynamicPortTestCase;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  import org.mule.transport.http.HttpConstants;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.HttpMethod;
25  import org.apache.commons.httpclient.HttpStatus;
26  import org.apache.commons.httpclient.NameValuePair;
27  import org.apache.commons.httpclient.methods.GetMethod;
28  import org.apache.commons.httpclient.methods.PostMethod;
29  
30  public class HttpPersistentQueueTestCase extends DynamicPortTestCase
31  {
32      private CountDownLatch messageDidArrive = new CountDownLatch(1);
33      private int port = -1;
34      
35      @Override
36      protected String getConfigResources()
37      {
38          return "http-persistent-queue.xml";
39      }
40  
41      @Override
42      protected void doSetUp() throws Exception
43      {
44          super.doSetUp();
45  
46          FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("PersistentQueueAsync");
47          assertNotNull(testComponent);
48          testComponent.setEventCallback(new Callback(messageDidArrive));
49          port = getPorts().get(0);
50      }
51  
52      public void testPersistentMessageDeliveryWithGet() throws Exception
53      {
54          GetMethod method = new GetMethod("http://localhost:" + port + "/services/Echo?foo=bar");
55          method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
56          doTestPersistentMessageDelivery(method);
57      }
58  
59      public void testPersistentMessageDeliveryWithPost() throws Exception
60      {        
61          PostMethod method = new PostMethod("http://localhost:" + port + "/services/Echo");        
62          method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
63          method.addParameter(new NameValuePair("foo", "bar"));
64          doTestPersistentMessageDelivery(method);
65      }
66      
67      private void doTestPersistentMessageDelivery(HttpMethod httpMethod) throws Exception
68      {
69          HttpClient client = new HttpClient();
70          int rc = client.executeMethod(httpMethod);
71          
72          assertEquals(HttpStatus.SC_OK, rc);
73          assertTrue(messageDidArrive.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
74      }
75      
76      private static class Callback implements EventCallback
77      {
78          private CountDownLatch messageDidArrive;
79          
80          public Callback(CountDownLatch latch)
81          {
82              super();
83              messageDidArrive = latch;
84          }
85          
86          public void eventReceived(MuleEventContext context, Object component) throws Exception
87          {
88              MuleMessage message = context.getMessage();
89  
90              Object httpMethod = message.getInboundProperty("http.method");
91              if (HttpConstants.METHOD_GET.equals(httpMethod))
92              {
93                  assertEquals("/services/Echo?foo=bar", message.getPayloadAsString());
94              }
95              else if (HttpConstants.METHOD_POST.equals(httpMethod))
96              {
97                  assertEquals("foo=bar", message.getPayloadAsString());
98              }
99              else
100             {
101                 fail("invalid HTTP method : " + httpMethod);
102             }
103             
104             assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_CONNECTION));
105             assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_KEEP_ALIVE));
106             
107             messageDidArrive.countDown();            
108         }
109     }
110 
111     @Override
112     protected int getNumPortsToFind()
113     {
114         return 1;
115     } 
116 }