1   /*
2    * $Id: FtpStreamingTestCase.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;
12  
13  import org.mule.MuleManager;
14  import org.mule.extras.client.MuleClient;
15  import org.mule.providers.ftp.server.NamedPayload;
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  
28  /**
29   * We don't have an integrated ftp server (yet), and synchronous return doesn't work
30   * with streaming, as far as i can tell, so the best we can do here is dispatch
31   * a through a streaming bridge to the test server, then pull it back again (again,
32   * through the streaming model).
33   */
34  public class FtpStreamingTestCase extends AbstractFtpServerTestCase
35  {
36  
37      private static int PORT = 60197;
38  
39      public FtpStreamingTestCase()
40      {
41          super(PORT);
42      }
43  
44      protected String getConfigResources()
45      {
46          return "ftp-streaming-test.xml";
47      }
48  
49      public void testSendAndReceive() throws Exception
50      {
51          final CountDownLatch latch = new CountDownLatch(1);
52          final AtomicReference message = new AtomicReference();
53          final AtomicInteger loopCount = new AtomicInteger(0);
54  
55          EventCallback callback = new EventCallback()
56          {
57              public synchronized void eventReceived(UMOEventContext context, Object component)
58              {
59                  try
60                  {
61                      logger.info("called " + loopCount.incrementAndGet() + " times");
62                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
63                      // without this we may have problems with the many repeats
64                      if (1 == latch.getCount())
65                      {
66                          message.set(ftc.getSummary());
67                          latch.countDown();
68                      }
69                  }
70                  catch (Exception e)
71                  {
72                      logger.error(e.getMessage(), e);
73                  }
74              }
75          };
76  
77          MuleClient client = new MuleClient();
78  
79          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("main");
80          FunctionalStreamingTestComponent ftc =
81                  (FunctionalStreamingTestComponent) model.getComponent("testComponent").getInstance();
82  //        assertNotNull(ftc);
83  //        assertEquals(1, ftc.getNumber());
84  
85          ftc.setEventCallback(callback, TEST_MESSAGE.length());
86  
87          // send out to FTP server via streaming model
88          client.dispatch("tcp://localhost:60196", TEST_MESSAGE, new HashMap());
89          NamedPayload payload = awaitUpload();
90          assertNotNull(payload);
91          logger.info("received message: " + payload);
92          assertEquals(TEST_MESSAGE, new String(payload.getPayload()));
93  
94          // poll and pull back through test component
95          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
96          assertEquals("Received stream; length: 16; 'Test...sage'", message.get());
97      }
98  
99  }