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