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