View Javadoc

1   /*
2    * $Id: MultiStreamMule1692TestCase.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.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.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.AtomicReference;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  public class MultiStreamMule1692TestCase extends DynamicPortTestCase
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(muleContext);
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(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
81              TEST_MESSAGE, new HashMap());
82          latch.await(10, TimeUnit.SECONDS);
83          assertEquals(RESULT, message.get());
84  
85          final CountDownLatch latch2 = new CountDownLatch(1);
86          final AtomicReference message2 = new AtomicReference();
87          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
88          client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
89              TEST_MESSAGE_2, new HashMap());
90          latch2.await(10, TimeUnit.SECONDS);
91          assertEquals(RESULT_2, message2.get());
92      }
93  
94      @Override
95      protected int getNumPortsToFind()
96      {
97          return 2;
98      }
99  }
100