View Javadoc

1   /*
2    * $Id: FtpFunctionalTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.FunctionalTestComponent;
17  
18  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
19  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
21  
22  
23  public class FtpFunctionalTestCase extends AbstractFtpServerTestCase
24  {
25      private static int PORT = 60198;
26  
27      public FtpFunctionalTestCase()
28      {
29          super(PORT);
30      }
31  
32      protected String getConfigResources()
33      {
34          return "ftp-functional-test.xml";
35      }
36  
37      /**
38       * Used by subclasses (in EE)
39       */
40      public int getPort()
41      {
42          return PORT;
43      }
44      
45      public void testSendAndRequest() throws Exception
46      {
47          CountDownLatch latch = new CountDownLatch(1);
48          AtomicReference message = new AtomicReference();
49  
50          Object component = getComponent("testComponent");
51          assertNotNull(component);
52          assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
53          FunctionalTestComponent ftc = (FunctionalTestComponent) component;
54          ftc.setEventCallback(new FunctionalEventCallback(latch, message));
55  
56          MuleClient client = new MuleClient(muleContext);
57          client.dispatch(getMuleFtpEndpoint(), TEST_MESSAGE, null);
58          
59          // TODO DZ: need a reliable way to check the file once it's been written to
60          // the ftp server. Currently, once mule processes the ftp'd file, it
61          // auto-deletes it, so we can't check it
62          //assertTrue(getFtpClient().expectFileCount("/", 1, 10000));
63          
64          latch.await(getTimeout(), TimeUnit.MILLISECONDS);
65          assertEquals(TEST_MESSAGE, message.get());                
66          
67          // give Mule some time to disconnect from the FTP server
68          Thread.sleep(500);
69      }
70  
71      protected static class FunctionalEventCallback implements EventCallback
72      {
73          private CountDownLatch latch;
74          private AtomicReference message;
75  
76          public FunctionalEventCallback(CountDownLatch latch, AtomicReference message)
77          {
78              super();
79              this.latch = latch;
80              this.message = message;
81          }
82          
83          public synchronized void eventReceived(MuleEventContext context, Object component)
84          {
85              try
86              {
87                  FunctionalTestComponent ftc = (FunctionalTestComponent) component;
88                  
89                  // without this we may have problems with the many repeats
90                  if (1 == latch.getCount())
91                  {
92                      String o = new String((byte[])ftc.getLastReceivedMessage());
93                      message.set(o);
94                      latch.countDown();
95                  }
96              }
97              catch (Exception e)
98              {
99                  throw new IllegalStateException(e);
100             }
101         }
102     }
103 }