View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.ftp;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.functional.EventCallback;
12  import org.mule.tck.functional.FunctionalTestComponent;
13  
14  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
15  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
16  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  
24  public class FtpFunctionalTestCase extends AbstractFtpServerTestCase
25  {
26      protected String getConfigResources()
27      {
28          return "ftp-functional-test.xml";
29      }
30     
31      @Test
32      public void testSendAndRequest() throws Exception
33      {
34          CountDownLatch latch = new CountDownLatch(1);
35          AtomicReference message = new AtomicReference();
36  
37          Object component = getComponent("testComponent");
38          assertNotNull(component);
39          assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
40          FunctionalTestComponent ftc = (FunctionalTestComponent) component;
41          ftc.setEventCallback(new FunctionalEventCallback(latch, message));
42  
43          MuleClient client = new MuleClient(muleContext);
44          client.dispatch(getMuleFtpEndpoint(), TEST_MESSAGE, null);
45          
46          // TODO DZ: need a reliable way to check the file once it's been written to
47          // the ftp server. Currently, once mule processes the ftp'd file, it
48          // auto-deletes it, so we can't check it
49          //assertTrue(getFtpClient().expectFileCount("/", 1, 10000));
50          
51          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
52          assertEquals(TEST_MESSAGE, message.get());                
53          
54          // give Mule some time to disconnect from the FTP server
55          Thread.sleep(500);
56      }
57  
58      protected static class FunctionalEventCallback implements EventCallback
59      {
60          private CountDownLatch latch;
61          private AtomicReference message;
62  
63          public FunctionalEventCallback(CountDownLatch latch, AtomicReference message)
64          {
65              super();
66              this.latch = latch;
67              this.message = message;
68          }
69          
70          public synchronized void eventReceived(MuleEventContext context, Object component)
71          {
72              try
73              {
74                  FunctionalTestComponent ftc = (FunctionalTestComponent) component;
75                  
76                  // without this we may have problems with the many repeats
77                  if (1 == latch.getCount())
78                  {
79                      String o = new String((byte[])ftc.getLastReceivedMessage());
80                      message.set(o);
81                      latch.countDown();
82                  }
83              }
84              catch (Exception e)
85              {
86                  throw new IllegalStateException(e);
87              }
88          }
89      }
90  }