View Javadoc

1   /*
2    * $Id: MultiStreamMule1692TestCase.java 19817 2010-10-04 18:10:39Z dzapata $
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.issues;
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.FunctionalTestCase;
18  import org.mule.tck.functional.EventCallback;
19  import org.mule.tck.functional.FunctionalStreamingTestComponent;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  
24  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
25  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
26  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  public class MultiStreamMule1692TestCase extends DynamicPortTestCase
32  {
33  
34      private static final Log logger = LogFactory.getLog(MultiStreamMule1692TestCase.class);
35      public static final int TIMEOUT = 3000;
36      public static final String TEST_MESSAGE = "Test TCP Request";
37      public static final String TEST_MESSAGE_2 = "Second test TCP Request";
38      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
39      public static final String RESULT_2 = "Received stream; length: 23; 'Seco...uest'"; 
40      
41      protected String getConfigResources()
42      {
43          return "tcp-streaming-test.xml";
44      }
45  
46      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
47      {
48          return new EventCallback()
49          {
50              public synchronized void eventReceived(MuleEventContext context, Object component)
51              {
52                  try
53                  {
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                          latch.countDown();
60                      }
61                  }
62                  catch (Exception e)
63                  {
64                      logger.error(e.getMessage(), e);
65                  }
66              }
67          };
68      }
69      
70      public void testSend() throws Exception
71      {
72          MuleClient client = new MuleClient(muleContext);
73  
74          Object ftc = getComponent("testComponent");
75          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
76  //        assertNotNull(ftc);
77  //        assertEquals(1, ftc.getNumber());
78  
79  
80          final CountDownLatch latch = new CountDownLatch(1);
81          final AtomicReference message = new AtomicReference();
82          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch, message), TEST_MESSAGE.length());
83          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
84              TEST_MESSAGE, new HashMap());
85          latch.await(10, TimeUnit.SECONDS);
86          assertEquals(RESULT, message.get());
87  
88          final CountDownLatch latch2 = new CountDownLatch(1);
89          final AtomicReference message2 = new AtomicReference();
90          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
91          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
92              TEST_MESSAGE_2, new HashMap());
93          latch2.await(10, TimeUnit.SECONDS);
94          assertEquals(RESULT_2, message2.get());
95      }
96  
97      @Override
98      protected int getNumPortsToFind()
99      {
100         return 2;
101     }
102 }
103