View Javadoc

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