View Javadoc

1   /*
2    * $Id: StreamingTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.integration;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.DynamicPortTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalStreamingTestComponent;
19  
20  import java.util.HashMap;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
25  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
26  
27  /**
28   * This test is more about testing the streaming model than the TCP provider, really.
29   */
30  public class StreamingTestCase extends DynamicPortTestCase
31  {
32  
33      public static final int TIMEOUT = 300000;
34      public static final String TEST_MESSAGE = "Test TCP Request";
35      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
36      
37      protected String getConfigResources()
38      {
39          return "tcp-streaming-test.xml";
40      }
41  
42      public void testSend() throws Exception
43      {
44          final CountDownLatch latch = new CountDownLatch(1);
45          final AtomicReference message = new AtomicReference();
46          final AtomicInteger loopCount = new AtomicInteger(0);
47  
48          EventCallback callback = new EventCallback()
49          {
50              public synchronized void eventReceived(MuleEventContext context, Object component)
51              {
52                  try
53                  {
54                      logger.info("called " + loopCount.incrementAndGet() + " times");
55                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
56                      // without this we may have problems with the many repeats
57                      if (1 == latch.getCount())
58                      {
59                          message.set(ftc.getSummary());
60                          assertEquals(RESULT, message.get());
61                          latch.countDown();
62                      }
63                  }
64                  catch (Exception e)
65                  {
66                      logger.error(e.getMessage(), e);
67                  }
68              }
69          };
70  
71          MuleClient client = new MuleClient(muleContext);
72  
73          // this works only if singleton set in descriptor
74          Object ftc = getComponent("testComponent");
75          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
76          assertNotNull(ftc);
77  
78          ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback, TEST_MESSAGE.length());
79  
80          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
81              TEST_MESSAGE, new HashMap());
82  
83          latch.await(10, TimeUnit.SECONDS);
84          assertEquals(RESULT, message.get());
85      }
86  
87      @Override
88      protected int getNumPortsToFind()
89      {
90          return 2;
91      }
92  }