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