View Javadoc

1   /*
2    * $Id: SafeProtocolTestCase.java 22485 2011-07-21 08:26:15Z justin.calleja $
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.protocols;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import java.util.Arrays;
19  import java.util.Collection;
20  
21  import org.junit.Rule;
22  import org.junit.Test;
23  import org.junit.runners.Parameterized.Parameters;
24  import org.mule.api.MuleException;
25  import org.mule.api.MuleMessage;
26  import org.mule.module.client.MuleClient;
27  import org.mule.tck.AbstractServiceAndFlowTestCase;
28  import org.mule.tck.junit4.rule.DynamicPort;
29  
30  public class SafeProtocolTestCase extends AbstractServiceAndFlowTestCase
31  {
32  
33      protected static String TEST_MESSAGE = "Test TCP Request";
34  
35      @Rule
36      public DynamicPort dynamicPort1 = new DynamicPort("port1");
37  
38      @Rule
39      public DynamicPort dynamicPort2 = new DynamicPort("port2");
40  
41      public SafeProtocolTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44      }
45  
46      @Parameters
47      public static Collection<Object[]> parameters()
48      {
49          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "safe-protocol-test-service.xml"},
50              {ConfigVariant.FLOW, "safe-protocol-test-flow.xml"}});
51      }
52  
53      @Test
54      public void testSafeToSafe() throws MuleException
55      {
56          MuleClient client = new MuleClient(muleContext);
57          assertResponseOk(client.send("tcp://localhost:" + dynamicPort1.getNumber() + "?connector=safe",
58              TEST_MESSAGE, null));
59      }
60  
61      @Test
62      public void testUnsafeToSafe() throws MuleException
63      {
64          MuleClient client = new MuleClient(muleContext);
65          assertResponseBad(client.send("tcp://localhost:" + dynamicPort1.getNumber() + "?connector=unsafe",
66              TEST_MESSAGE, null));
67      }
68  
69      private void assertResponseOk(MuleMessage message)
70      {
71          assertNotNull("Null message", message);
72          Object payload = message.getPayload();
73          assertNotNull("Null payload", payload);
74          assertTrue("Payload not byte[]", payload instanceof byte[]);
75          assertEquals(TEST_MESSAGE + " Received", new String((byte[]) payload));
76      }
77  
78      protected void assertResponseBad(MuleMessage message)
79      {
80          try
81          {
82              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
83              {
84                  fail("expected error");
85              }
86          }
87          catch (Exception e)
88          {
89              // expected
90          }
91      }
92  
93  }