View Javadoc

1   /*
2    * $Id: MultiStreamMule1692TestCase.java 22452 2011-07-19 09:42:56Z justin.calleja $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.Arrays;
17  import java.util.Collection;
18  import java.util.HashMap;
19  import java.util.concurrent.CountDownLatch;
20  import java.util.concurrent.TimeUnit;
21  import java.util.concurrent.atomic.AtomicReference;
22  
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  import org.mule.api.MuleEventContext;
27  import org.mule.api.endpoint.InboundEndpoint;
28  import org.mule.module.client.MuleClient;
29  import org.mule.tck.AbstractServiceAndFlowTestCase;
30  import org.mule.tck.functional.EventCallback;
31  import org.mule.tck.functional.FunctionalStreamingTestComponent;
32  import org.mule.tck.junit4.rule.DynamicPort;
33  
34  public class MultiStreamMule1692TestCase extends AbstractServiceAndFlowTestCase
35  {
36      
37      public static final int TIMEOUT = 3000;
38      public static final String TEST_MESSAGE = "Test TCP Request";
39      public static final String TEST_MESSAGE_2 = "Second test TCP Request";
40      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
41      public static final String RESULT_2 = "Received stream; length: 23; 'Seco...uest'";
42  
43      @Rule
44      public DynamicPort dynamicPort1 = new DynamicPort("port1");
45  
46      @Rule
47      public DynamicPort dynamicPort2 = new DynamicPort("port2");
48  
49      public MultiStreamMule1692TestCase(ConfigVariant variant, String configResources)
50      {
51          super(variant, configResources);
52      }
53      
54      @Parameters
55      public static Collection<Object[]> parameters()
56      {
57          return Arrays.asList(new Object[][]{
58              {ConfigVariant.SERVICE, "tcp-streaming-test-service.xml"},
59              {ConfigVariant.FLOW, "tcp-streaming-test-flow.xml"}
60          });
61      }
62      
63      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference<String> message)
64      {
65          return new EventCallback()
66          {
67              @Override
68              public synchronized void eventReceived(MuleEventContext context, Object component)
69              {
70                  try
71                  {
72                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
73                      // without this we may have problems with the many repeats
74                      if (1 == latch.getCount())
75                      {
76                          message.set(ftc.getSummary());
77                          latch.countDown();
78                      }
79                  }
80                  catch (Exception e)
81                  {
82                      logger.error(e.getMessage(), e);
83                  }
84              }
85          };
86      }
87  
88      @Test
89      public void testSend() throws Exception
90      {
91          MuleClient client = new MuleClient(muleContext);
92  
93          Object ftc = getComponent("testComponent");
94          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
95  
96  
97          final CountDownLatch latch = new CountDownLatch(1);
98          final AtomicReference<String> message = new AtomicReference<String>();
99          ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch, message), TEST_MESSAGE.length());
100         client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
101             TEST_MESSAGE, new HashMap<Object, Object>());
102         latch.await(10, TimeUnit.SECONDS);
103         assertEquals(RESULT, message.get());
104 
105         final CountDownLatch latch2 = new CountDownLatch(1);
106         final AtomicReference<String> message2 = new AtomicReference<String>();
107         ((FunctionalStreamingTestComponent) ftc).setEventCallback(newCallback(latch2, message2), TEST_MESSAGE_2.length());
108         client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("testInbound")).getAddress(),
109             TEST_MESSAGE_2, new HashMap<Object, Object>());
110         latch2.await(10, TimeUnit.SECONDS);
111         assertEquals(RESULT_2, message2.get());
112     }
113 }