View Javadoc

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