View Javadoc

1   /*
2    * $Id: LengthProtocolLengthTestCase.java 22552 2011-07-25 07:18:19Z claude.mamo $
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.tcp.issues;
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import org.mule.api.MuleMessage;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  import org.mule.tck.junit4.rule.DynamicPort;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  public class LengthProtocolLengthTestCase extends AbstractServiceAndFlowTestCase
30  {
31  
32      @Rule
33      public DynamicPort dynamicPort1 = new DynamicPort("port1");
34  
35      @Rule
36      public DynamicPort dynamicPort2 = new DynamicPort("port2");
37  
38      public LengthProtocolLengthTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41      }
42  
43      @Parameters
44      public static Collection<Object[]> parameters()
45      {
46          return Arrays.asList(new Object[][]{
47              {ConfigVariant.SERVICE, "length-protocol-length-test-service.xml"},
48              {ConfigVariant.FLOW, "length-protocol-length-test-flow.xml"}
49          });
50      }      
51  
52      @Test
53      public void testLength() throws Exception
54      {
55          doTest("length", 5, true);
56          doTest("length", 15, false);
57      }
58  
59      @Test
60      public void testSafe() throws Exception
61      {
62          doTest("safe", 5, true);
63          doTest("safe", 15, false);
64      }
65  
66      protected void doTest(String endpoint, int length, boolean ok) throws Exception
67      {
68          byte[] message = new byte[length];
69          for (int i = 0; i < length; ++i)
70          {
71              message[i] = (byte)(i % 255);
72          }
73          MuleClient client = new MuleClient(muleContext);
74          if (ok)
75          {
76              MuleMessage response = client.send(endpoint, message, null);
77              assertNotNull(response);
78              assertNotNull(response.getPayload());
79              assertTrue(Arrays.equals(message, response.getPayloadAsBytes()));
80          }
81          else
82          {
83              assertResponseBad(client.send(endpoint, message, null));
84          }
85      }
86  
87      protected void assertResponseBad(MuleMessage message)
88      {
89          try
90          {
91              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
92              {
93                  fail("expected error");
94              }
95          }
96          catch (Exception e)
97          {
98              // expected
99          }
100     }
101 
102 }