View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.issues;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.functional.EventCallback;
13  import org.mule.tck.functional.FunctionalStreamingTestComponent;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.tck.junit4.rule.DynamicPort;
16  
17  import java.util.HashMap;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
20  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
21  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.junit.Rule;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  
30  public class MultiStreamMule1692TestCase extends FunctionalTestCase
31  {
32  
33      private static final Log logger = LogFactory.getLog(MultiStreamMule1692TestCase.class);
34      public static final int TIMEOUT = 3000;
35      public static final String TEST_MESSAGE = "Test TCP Request";
36      public static final String TEST_MESSAGE_2 = "Second test TCP Request";
37      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
38      public static final String RESULT_2 = "Received stream; length: 23; 'Seco...uest'";
39  
40      @Rule
41      public DynamicPort dynamicPort1 = new DynamicPort("port1");
42  
43      @Rule
44      public DynamicPort dynamicPort2 = new DynamicPort("port2");
45  
46      @Override
47      protected String getConfigResources()
48      {
49          return "tcp-streaming-test.xml";
50      }
51  
52      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
53      {
54          return new EventCallback()
55          {
56              public synchronized void eventReceived(MuleEventContext context, Object component)
57              {
58                  try
59                  {
60                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
61                      // without this we may have problems with the many repeats
62                      if (1 == latch.getCount())
63                      {
64                          message.set(ftc.getSummary());
65                          latch.countDown();
66                      }
67                  }
68                  catch (Exception e)
69                  {
70                      logger.error(e.getMessage(), e);
71                  }
72              }
73          };
74      }
75  
76      @Test
77      public void testSend() throws Exception
78      {
79          MuleClient client = new MuleClient(muleContext);
80  
81          Object ftc = getComponent("testComponent");
82          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
83  
84  
85          final CountDownLatch latch = new CountDownLatch(1);
86          final AtomicReference message = new AtomicReference();
87          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch, message), TEST_MESSAGE.length());
88          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
89              TEST_MESSAGE, new HashMap());
90          latch.await(10, TimeUnit.SECONDS);
91          assertEquals(RESULT, message.get());
92  
93          final CountDownLatch latch2 = new CountDownLatch(1);
94          final AtomicReference message2 = new AtomicReference();
95          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
96          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
97              TEST_MESSAGE_2, new HashMap());
98          latch2.await(10, TimeUnit.SECONDS);
99          assertEquals(RESULT_2, message2.get());
100     }
101 
102 }
103