1   /*
2    * $Id: MultiStreamMule1692TestCase.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.issues;
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.AtomicReference;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  public class MultiStreamMule1692TestCase extends FunctionalTestCase
29  {
30  
31      private static final Log logger = LogFactory.getLog(MultiStreamMule1692TestCase.class);
32      public static final int TIMEOUT = 3000;
33      public static final String TEST_MESSAGE = "Test TCP Request";
34      public static final String TEST_MESSAGE_2 = "Second test TCP Request";
35      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
36      public static final String RESULT_2 = "Received stream; length: 23; 'Seco...uest'";
37  
38      protected String getConfigResources()
39      {
40          return "tcp-streaming-test.xml";
41      }
42  
43      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
44      {
45          return new EventCallback()
46          {
47              public synchronized void eventReceived(MuleEventContext context, Object component)
48              {
49                  try
50                  {
51                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
52                      // without this we may have problems with the many repeats
53                      if (1 == latch.getCount())
54                      {
55                          message.set(ftc.getSummary());
56                          latch.countDown();
57                      }
58                  }
59                  catch (Exception e)
60                  {
61                      logger.error(e.getMessage(), e);
62                  }
63              }
64          };
65      }
66      
67      public void testSend() throws Exception
68      {
69          MuleClient client = new MuleClient();
70  
71          Object ftc = getComponent("testComponent");
72          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
73  //        assertNotNull(ftc);
74  //        assertEquals(1, ftc.getNumber());
75  
76  
77          final CountDownLatch latch = new CountDownLatch(1);
78          final AtomicReference message = new AtomicReference();
79          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch, message), TEST_MESSAGE.length());
80          client.dispatch("tcp://localhost:65432", TEST_MESSAGE, new HashMap());
81          latch.await(10, TimeUnit.SECONDS);
82          assertEquals(RESULT, message.get());
83  
84          final CountDownLatch latch2 = new CountDownLatch(1);
85          final AtomicReference message2 = new AtomicReference();
86          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
87          client.dispatch("tcp://localhost:65432", TEST_MESSAGE_2, new HashMap());
88          latch2.await(10, TimeUnit.SECONDS);
89          assertEquals(RESULT_2, message2.get());
90      }
91  
92  }
93