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.integration;
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.AtomicInteger;
22  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
23  import org.junit.Rule;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  /**
31   * This test is more about testing the streaming model than the TCP provider, really.
32   */
33  public class StreamingTestCase extends FunctionalTestCase
34  {
35  
36      public static final int TIMEOUT = 300000;
37      public static final String TEST_MESSAGE = "Test TCP Request";
38      public static final String RESULT = "Received stream; length: 16; 'Test...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      @Test
53      public void testSend() throws Exception
54      {
55          final CountDownLatch latch = new CountDownLatch(1);
56          final AtomicReference message = new AtomicReference();
57          final AtomicInteger loopCount = new AtomicInteger(0);
58  
59          EventCallback callback = new EventCallback()
60          {
61              public synchronized void eventReceived(MuleEventContext context, Object component)
62              {
63                  try
64                  {
65                      logger.info("called " + loopCount.incrementAndGet() + " times");
66                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
67                      // without this we may have problems with the many repeats
68                      if (1 == latch.getCount())
69                      {
70                          message.set(ftc.getSummary());
71                          assertEquals(RESULT, message.get());
72                          latch.countDown();
73                      }
74                  }
75                  catch (Exception e)
76                  {
77                      logger.error(e.getMessage(), e);
78                  }
79              }
80          };
81  
82          MuleClient client = new MuleClient(muleContext);
83  
84          // this works only if singleton set in descriptor
85          Object ftc = getComponent("testComponent");
86          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
87          assertNotNull(ftc);
88  
89          ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback, TEST_MESSAGE.length());
90  
91          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
92              TEST_MESSAGE, new HashMap());
93  
94          latch.await(10, TimeUnit.SECONDS);
95          assertEquals(RESULT, message.get());
96      }
97  
98  }