View Javadoc

1   /*
2    * $Id: JettyTwoEndpointsSinglePortTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.servlet.jetty;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transformer.DataType;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.FunctionalTestCase;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  public class JettyTwoEndpointsSinglePortTestCase extends FunctionalTestCase
23  {
24  
25      protected String getConfigResources()
26      {
27          return "jetty-two-endpoints-single-port.xml";
28      }
29  
30      public void testSendToEach() throws Exception
31      {
32  
33          sendWithResponse("http://localhost:60211/mycomponent1", "test", "mycomponent1", 10);
34          sendWithResponse("http://localhost:60211/mycomponent2", "test", "mycomponent2", 10);
35      }
36  
37      public void testSendToEachWithBadEndpoint() throws Exception
38      {
39  
40          MuleClient client = new MuleClient(muleContext);
41  
42          sendWithResponse("http://localhost:60211/mycomponent1", "test", "mycomponent1", 5);
43          sendWithResponse("http://localhost:60211/mycomponent2", "test", "mycomponent2", 5);
44  
45          MuleMessage result = client.send("http://localhost:60211/mycomponent-notfound", "test", null);
46          assertNotNull(result);
47          assertNotNull(result.getExceptionPayload());
48          final int status = result.getInboundProperty("http.status", 0);
49          assertEquals(404, status);
50  
51          // Test that after the exception the endpoints still receive events
52          sendWithResponse("http://localhost:60211/mycomponent1", "test", "mycomponent1", 5);
53          sendWithResponse("http://localhost:60211/mycomponent2", "test", "mycomponent2", 5);
54      }
55  
56      protected void sendWithResponse(String endpoint, String message, String response, int noOfMessages)
57          throws MuleException
58      {
59          MuleClient client = new MuleClient(muleContext);
60  
61          List results = new ArrayList();
62          for (int i = 0; i < noOfMessages; i++)
63          {
64              MuleMessage result = client.send(endpoint, message, null);
65              results.add(result.getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
66          }
67  
68          assertEquals(noOfMessages, results.size());
69          for (int i = 0; i < noOfMessages; i++)
70          {
71              assertEquals(response, new String((byte[])results.get(i)));
72          }
73      }
74  }