1   /*
2    * $Id: StreamingScriptComponentTestCase.java 10430 2008-01-21 16:28:22Z 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.components.script;
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.AtomicReference;
26  
27  public class StreamingScriptComponentTestCase extends FunctionalTestCase
28  {
29      public static final int TIMEOUT = 3000;
30  
31      public static final String TEST_MESSAGE = "World";
32  
33      public static final String RESULT = "Received stream; length: 11; 'Hell...orld'";
34  
35      public StreamingScriptComponentTestCase()
36      {
37          setDisposeManagerPerSuite(true);
38      }
39  
40      protected String getConfigResources()
41      {
42          return "groovy-streaming-test.xml";
43      }
44  
45      public void testFunctionBehaviour() throws Exception
46      {
47          final CountDownLatch latch = new CountDownLatch(1);
48          final AtomicReference message = new AtomicReference();
49  
50          EventCallback callback = new EventCallback()
51          {
52              public synchronized void eventReceived(UMOEventContext context, Object component)
53              {
54                  try
55                  {
56                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
57                      // without this we may have problems with the many repeats
58                      if (1 == latch.getCount())
59                      {
60                          message.set(ftc.getSummary());
61                          assertEquals(RESULT, message.get());
62                          latch.countDown();
63                      }
64                  }
65                  catch (Exception e)
66                  {
67                      logger.error(e.getMessage(), e);
68                  }
69              }
70          };
71  
72          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("streaming-model");
73          FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) model.getComponent(
74              "testComponent").getInstance();
75          assertNotNull(ftc);
76          ftc.setEventCallback(callback, TEST_MESSAGE.length());
77  
78          MuleClient client = new MuleClient();
79          client.dispatch("tcp://localhost:65432", TEST_MESSAGE, new HashMap());
80  
81          latch.await(10, TimeUnit.SECONDS);
82          assertEquals(RESULT, message.get());
83      }
84  }