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.tck.junit4.FunctionalTestCase;
10  import org.mule.tck.junit4.rule.DynamicPort;
11  import org.mule.transport.ftp.server.FTPTestClient;
12  import org.mule.transport.ftp.server.MuleFtplet;
13  import org.mule.transport.ftp.server.Server;
14  import org.mule.util.FileUtils;
15  
16  import java.io.File;
17  import java.io.FileWriter;
18  import java.io.IOException;
19  import java.io.Writer;
20  
21  import org.apache.ftpserver.ftplet.Ftplet;
22  import org.junit.Rule;
23  
24  import static org.junit.Assert.assertFalse;
25  
26  /**
27   * Abstract FTP test class. Sets up the ftp server and starts/stops it during the
28   * test lifecycle.
29   */
30  public abstract class AbstractFtpServerTestCase extends FunctionalTestCase implements MuleFtplet.Callback
31  {
32  	public static final String TEST_MESSAGE = "Test FTP message";
33      
34      private static final String DEFAULT_FTP_HOST = "localhost";
35      private static int DEFAULT_TIMEOUT = 10000;
36      public static final String FTP_SERVER_BASE_DIR = "target/ftpserver";
37          
38      /**
39       * We only test against an embedded server, but you can test against remote
40       * servers by changing this value. Some tests may start/stop the ftp server so
41       * you will have to implement remote start/stop as well.
42       */
43      private String ftpHost;
44      
45      private int ftpPort;
46      private String ftpUser = "anonymous";
47      private String ftpPassword = "password";
48      private int timeout;
49      private Server server = null;
50      private FTPTestClient ftpClient = null;
51      
52      /**
53       * Subclasses can overwrite Ftplet that will be registered when creating the server.
54       */
55      protected Ftplet ftplet = new MuleFtplet(this);
56  
57      @Rule
58      public DynamicPort dynamicPort = new DynamicPort("port1");
59      
60      public AbstractFtpServerTestCase(String ftpHost, int timeout)
61      {        
62      	super();
63          this.ftpHost = ftpHost;
64          this.timeout = timeout;
65      }
66      
67      public AbstractFtpServerTestCase(int timeout)
68      {
69          this(DEFAULT_FTP_HOST, timeout);
70      }
71  
72      public AbstractFtpServerTestCase()
73      {
74          this(DEFAULT_TIMEOUT);
75      }
76  
77      protected void startServer() throws Exception
78      {
79          server = new Server(ftpPort, ftplet);
80          // this is really ugly, but the above doesn't get to waiting.
81          // need to improve this as part of ftp server work
82          synchronized(this)
83          {
84              wait(500);
85          }
86      }
87  
88      protected void stopServer() throws Exception
89      {
90          // stop the server
91          if (null != server)
92          {
93              server.stop();
94          }
95      }
96  
97      @Override
98      protected void doSetUp() throws Exception
99      {
100         super.doSetUp();
101         this.ftpPort = dynamicPort.getNumber();
102         ftpClient = new FTPTestClient(this.ftpHost, this.ftpPort, this.ftpUser, this.ftpPassword);
103         // make sure we start out with a clean ftp server base
104         createFtpServerBaseDir();
105 
106         startServer();
107         if (!ftpClient.testConnection())
108         {
109             throw new IOException("could not connect to ftp server");
110         }
111     }
112 
113     @Override
114     protected void doTearDown() throws Exception
115     {
116         ftpClient.disconnect(); // we dont need the connection anymore for this test
117         stopServer();
118 
119         deleteFtpServerBaseDir();
120         
121         super.doTearDown();
122     }
123 
124     private void createFtpServerBaseDir()
125     {
126         deleteFtpServerBaseDir();
127         File ftpBaseDir = new File(FTP_SERVER_BASE_DIR);
128         ftpBaseDir.mkdirs();
129     }
130 
131     private void deleteFtpServerBaseDir()
132     {
133         File ftpServerBase = new File(FTP_SERVER_BASE_DIR);
134         FileUtils.deleteTree(ftpServerBase);
135     }
136 
137     protected int getTimeout()
138     {
139         return timeout;
140     }
141 
142     public FTPTestClient getFtpClient()
143     {
144         return ftpClient;
145     }
146     
147     /**
148      * Return the endpoint denoted by the ftp configuration
149      */
150     public String getMuleFtpEndpoint()
151     {
152         return "ftp://" + ftpUser + ":" + ftpPassword + "@" + ftpHost + ":" + ftpPort;       
153     }
154 
155     protected void createFileOnFtpServer(String fileName) throws IOException
156     {        
157         File outFile = new File(FTP_SERVER_BASE_DIR, fileName);
158         assertFalse(outFile.exists());
159         
160         Writer outWriter = new FileWriter(outFile);
161         outWriter.write(TEST_MESSAGE);
162         outWriter.close();
163     }
164 
165     //
166     // callback methods from MuleFtplet
167     // 
168     public void fileUploadCompleted()
169     {
170         // subclasses can override this method
171     }
172 
173     public void fileMoveCompleted()
174     {
175         // subclasses can override this method
176     }
177 }