1   /*
2    * $Id: StreamingTestCase.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.integration;
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.AtomicInteger;
26  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * This test is more about testing the streaming model than the TCP provider, really.
32   */
33  public class StreamingTestCase extends FunctionalTestCase
34  {
35  
36      private static final Log logger = LogFactory.getLog(StreamingTestCase.class);
37      public static final int TIMEOUT = 3000;
38      public static final String TEST_MESSAGE = "Test TCP Request";
39      public static final String RESULT = "Received stream; length: 16; 'Test...uest'";
40  
41      public StreamingTestCase()
42      {
43          setDisposeManagerPerSuite(true);
44      }
45  
46      protected String getConfigResources()
47      {
48          return "tcp-streaming-test.xml";
49      }
50  
51      public void testSend() throws Exception
52      {
53          final CountDownLatch latch = new CountDownLatch(1);
54          final AtomicReference message = new AtomicReference();
55          final AtomicInteger loopCount = new AtomicInteger(0);
56  
57          EventCallback callback = new EventCallback()
58          {
59              public synchronized void eventReceived(UMOEventContext context, Object component)
60              {
61                  try
62                  {
63                      logger.info("called " + loopCount.incrementAndGet() + " times");
64                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
65                      // without this we may have problems with the many repeats
66                      if (1 == latch.getCount())
67                      {
68                          message.set(ftc.getSummary());
69                          assertEquals(RESULT, message.get());
70                          latch.countDown();
71                      }
72                  }
73                  catch (Exception e)
74                  {
75                      logger.error(e.getMessage(), e);
76                  }
77              }
78          };
79  
80          MuleClient client = new MuleClient();
81  
82          // this works only if singleton set in descriptor
83          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("echo");
84          FunctionalStreamingTestComponent ftc =
85                  (FunctionalStreamingTestComponent) model.getComponent("testComponent").getInstance();
86  //       assertNotNull(ftc);
87  //       assertEquals(1, ftc.getNumber());
88  
89          // this works with or without singleton, but required adding getComponent method
90  //        UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("echo");
91  //        UMOSession session = model.getComponentSession("testComponent");
92  //        StreamingComponent component = (StreamingComponent) session.getComponent();
93  //        FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component.getComponent();
94  
95          ftc.setEventCallback(callback, TEST_MESSAGE.length());
96  
97          client.dispatch("tcp://localhost:65432", TEST_MESSAGE, new HashMap());
98  
99          latch.await(10, TimeUnit.SECONDS);
100         assertEquals(RESULT, message.get());
101     }
102 
103 }