View Javadoc

1   /*
2    * $Id: JettyContinuationsTwoEndpointsSinglePortTestCase.java 22450 2011-07-19 08:20:41Z dirk.olmes $
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.junit4.FunctionalTestCase;
18  import org.mule.tck.junit4.rule.DynamicPort;
19  import org.mule.util.SystemUtils;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.junit.Rule;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  
30  public class JettyContinuationsTwoEndpointsSinglePortTestCase extends FunctionalTestCase
31  {
32  
33      @Rule
34      public DynamicPort dynamicPort = new DynamicPort("port1");
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "jetty-continuations-two-endpoints-single-port.xml";
40      }
41  
42      /**
43       * MULE-3992: this test hangs on JDK6
44       */
45      @Override
46      protected boolean isDisabledInThisEnvironment()
47      {
48          return SystemUtils.IS_JAVA_1_6;
49      }
50  
51      @Test
52      public void testSendToEach() throws Exception
53      {
54          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 10);
55          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 10);
56      }
57  
58      @Test
59      public void testSendToEachWithBadEndpoint() throws Exception
60      {
61  
62          MuleClient client = new MuleClient(muleContext);
63  
64          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent1", "test", "mycomponent1", 5);
65          sendWithResponse("http://localhost:" + dynamicPort.getNumber() + "/mycomponent2", "test", "mycomponent2", 5);
66  
67          MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/mycomponent-notfound", "test", null);
68          assertNotNull(result);
69          assertNotNull(result.getExceptionPayload());
70          final int status = result.getOutboundProperty("http.status", 0);
71          assertEquals(404, status);
72  
73          // Test that after the exception the endpoints still receive events
74          sendWithResponse("http://localhost:60211/mycomponent1", "test", "mycomponent1", 5);
75          sendWithResponse("http://localhost:60211/mycomponent2", "test", "mycomponent2", 5);
76      }
77  
78      protected void sendWithResponse(String endpoint, String message, String response, int noOfMessages)
79          throws MuleException
80      {
81          MuleClient client = new MuleClient(muleContext);
82  
83          List results = new ArrayList();
84          for (int i = 0; i < noOfMessages; i++)
85          {
86              results.add(client.send(endpoint, message, null).getPayload(DataType.BYTE_ARRAY_DATA_TYPE));
87          }
88          assertEquals(noOfMessages, results.size());
89          for (int i = 0; i < noOfMessages; i++)
90          {
91              assertEquals(response, new String((byte[])results.get(i)));
92          }
93      }
94  
95  }