View Javadoc

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