1   /*
2    * $Id: MultiStreamMule1696TestCase.java 7963 2007-08-21 08:53:15Z 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.ftp.issues;
12  
13  import org.mule.MuleManager;
14  import org.mule.extras.client.MuleClient;
15  import org.mule.providers.ftp.AbstractFtpServerTestCase;
16  import org.mule.providers.ftp.server.NamedPayload;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalStreamingTestComponent;
19  import org.mule.umo.UMOEventContext;
20  import org.mule.umo.model.UMOModel;
21  
22  import java.util.HashMap;
23  
24  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
25  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
26  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
27  
28  public class MultiStreamMule1696TestCase extends AbstractFtpServerTestCase
29  {
30  
31      public static final String TEST_MESSAGE_2 = "Another test message";
32      private static int PORT = 60197;
33  
34      public MultiStreamMule1696TestCase()
35      {
36          super(PORT);
37      }
38  
39      protected String getConfigResources()
40      {
41          return "ftp-streaming-test.xml";
42      }
43  
44      private EventCallback newCallback(final CountDownLatch latch, final AtomicReference message)
45      {
46          return new EventCallback()
47          {
48              public synchronized void eventReceived(UMOEventContext context, Object component)
49              {
50                  try
51                  {
52                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
53                      logger.debug("Callback called: " + ftc.getSummary());
54                      message.set(ftc.getSummary());
55                      latch.countDown();
56                  }
57                  catch (Exception e)
58                  {
59                      logger.error(e.getMessage(), e);
60                  }
61              }
62          };
63      }
64  
65      public void testSendAndReceive() throws Exception
66      {
67          MuleClient client = new MuleClient();
68  
69          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("main");
70          FunctionalStreamingTestComponent ftc =
71                  (FunctionalStreamingTestComponent) model.getComponent("testComponent").getInstance();
72  //        assertNotNull(ftc);
73  //        assertEquals(1, ftc.getNumber());
74  
75          CountDownLatch latch = new CountDownLatch(1);
76          AtomicReference message = new AtomicReference();
77          EventCallback callback = newCallback(latch, message);
78          ftc.setEventCallback(callback, TEST_MESSAGE.length());
79  
80          // send out to FTP server via streaming model
81          client.dispatch("tcp://localhost:60196", TEST_MESSAGE, new HashMap());
82          NamedPayload payload = awaitUpload();
83          assertNotNull(payload);
84          logger.info("received message: " + payload);
85          assertEquals(TEST_MESSAGE, new String(payload.getPayload()));
86  
87          // poll and pull back through test component
88          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
89          assertEquals("Received stream; length: 16; 'Test...sage'", message.get());
90  
91          // repeat, but restart server due to simple state, connection limitations
92          stopServer();
93          synchronized(this)
94          {
95              wait(2000); // TCP socket timeout
96          }
97          startServer();
98  
99          CountDownLatch latch2 = new CountDownLatch(1);
100         AtomicReference message2 = new AtomicReference();
101         EventCallback callback2 = newCallback(latch2, message2);
102         ftc.setEventCallback(callback2, TEST_MESSAGE_2.length());
103 
104         client.dispatch("tcp://localhost:60196", TEST_MESSAGE_2, new HashMap());
105         NamedPayload payload2 = awaitUpload();
106         assertNotNull(payload2);
107         logger.info("received message: " + payload2);
108         assertEquals(TEST_MESSAGE_2, new String(payload2.getPayload()));
109 
110         latch2.await(getTimeout(), TimeUnit.MILLISECONDS);
111         assertEquals("Received stream; length: 20; 'Anot...sage'", message2.get());
112     }
113 
114 }