View Javadoc

1   /*
2    * $Id: FTPServerClientTest.java 20320 2010-11-24 15:03:31Z 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.server;
12  
13  import org.mule.transport.ftp.AbstractFtpServerTestCase;
14  
15  import java.io.File;
16  import java.io.IOException;
17  
18  import junit.framework.TestCase;
19  
20  /**
21   * Various tests against the FTPClient/Server we use in Mule ftp transport tests.
22   * This is to make sure basic ftp functionality works with our current ftp
23   * client/server before we throw Mule into the mix.
24   */
25  public class FTPServerClientTest extends TestCase
26  {
27      Server ftpServer = null;
28      FTPTestClient ftpClient = null;
29      public static final int PORT = 60198;
30      private static final String adminUser = "admin";
31      private static final String adminPassword = "admin";
32      
33      /**
34       * Initialize the ftp server
35       */
36      public void setUp() throws Exception
37      {
38          new File(AbstractFtpServerTestCase.FTP_SERVER_BASE_DIR).mkdirs();
39          ftpServer = new Server(PORT);
40      }
41      
42      /**
43       * Create a directory and delete it
44       * @throws IOException
45       */
46      public void testCreateDeleteDir() throws IOException
47      {
48          ftpClient = new FTPTestClient("localhost", PORT, adminUser, adminPassword);
49          String dir = "/foo/";
50          assertTrue("unable to create directory: " + dir, ftpClient.makeDir(dir));
51          //verify directory was created
52          assertTrue("Directory '" + dir + "' does not exist", ftpClient.dirExists(dir));
53          assertTrue("unable to delete directory: " + dir, ftpClient.deleteDir(dir));
54          assertFalse("Directory '" + dir + "' still exists", ftpClient.dirExists(dir));
55      }
56      
57      /**
58       * Create a file and delete it
59       * @throws IOException
60       */    
61      public void testCreateDeleteFile() throws IOException
62      {
63          ftpClient = new FTPTestClient("localhost", PORT, adminUser, adminPassword);
64          File testFile = File.createTempFile("fake", "file");
65          ftpClient.putFile(testFile.getAbsolutePath(),"/");
66          assertTrue("Could not find file :" + testFile.getName(), ftpClient.fileExists(testFile.getName()));
67          ftpClient.deleteFile(testFile.getName());
68          assertFalse("file was not deleted :" + testFile.getName(), ftpClient.fileExists(testFile.getName()));
69      }
70  
71      /**
72       * Create a bunch of files/dirs then recursively delete them
73       * @throws IOException
74       */
75      public void testRecursiveDelete() throws IOException
76      {                        
77          ftpClient = new FTPTestClient("localhost", PORT, adminUser, adminPassword);
78          
79          assertTrue(ftpClient.makeDir("dir1/"));
80          ftpClient.dirExists("dir1/");
81          assertTrue(ftpClient.makeDir("/dir1/dir21/"));
82          ftpClient.dirExists("/dir1/dir21/");
83          assertTrue(ftpClient.makeDir("/dir1/dir22/"));
84          ftpClient.dirExists("/dir1/dir22/");
85          assertTrue(ftpClient.makeDir("/dir1/dir21/dir3/"));
86          ftpClient.dirExists("/dir1/dir21/dir3/");
87          
88          //TODO DZ: we should really be using files with data in them for more realistic testing  
89          File testFile0 = File.createTempFile("testFile0", "file");
90          File testFile1 = File.createTempFile("testFile1", "file");
91          File testFile2 = File.createTempFile("testFile2", "file");
92          File testFile3 = File.createTempFile("testFile3", "file");
93          File testFile4 = File.createTempFile("testFile4", "file");
94          File testFile5 = File.createTempFile("testFile5", "file");
95          File testFile6 = File.createTempFile("testFile6", "file");        
96          File testFile7 = File.createTempFile("testFile7", "file");
97          File testFile8 = File.createTempFile("testFile8", "file");
98          File testFile9 = File.createTempFile("testFile9", "file");
99                 
100         assertTrue(ftpClient.putFile(testFile0.getAbsolutePath(), "/"));
101         ftpClient.fileExists("/" + testFile0.getName());
102         assertTrue(ftpClient.putFile(testFile1.getAbsolutePath(), "/"));
103         ftpClient.fileExists("/" + testFile0.getName());
104         assertTrue(ftpClient.putFile(testFile2.getAbsolutePath(), "/dir1/"));
105         ftpClient.fileExists("/dir1/" + testFile0.getName());
106         assertTrue(ftpClient.putFile(testFile3.getAbsolutePath(), "/dir1/"));
107         ftpClient.fileExists("/dir1/" + testFile0.getName());
108         assertTrue(ftpClient.putFile(testFile4.getAbsolutePath(), "/dir1/dir21/"));
109         ftpClient.fileExists("/dir1/dir21/" + testFile0.getName());
110         assertTrue(ftpClient.putFile(testFile5.getAbsolutePath(), "/dir1/dir21/"));
111         ftpClient.fileExists("/dir1/dir21/" + testFile0.getName());
112         assertTrue(ftpClient.putFile(testFile6.getAbsolutePath(), "/dir1/dir22/"));
113         ftpClient.fileExists("/dir1/dir22/" + testFile0.getName());
114         assertTrue(ftpClient.putFile(testFile7.getAbsolutePath(), "/dir1/dir22/"));
115         ftpClient.fileExists("/dir1/dir22/" + testFile0.getName());
116         assertTrue(ftpClient.putFile(testFile8.getAbsolutePath(), "/dir1/dir21/dir3/"));
117         ftpClient.fileExists("/dir1/dir21/dir3/" + testFile0.getName());
118         assertTrue(ftpClient.putFile(testFile9.getAbsolutePath(), "/dir1/dir21/dir3/"));
119         ftpClient.fileExists("/dir1/dir21/dir3/" + testFile0.getName());
120                
121         ftpClient.recursiveDelete("/"); //there should be no files left over after this command
122         assertEquals("there are still files left over", 0, ftpClient.getFileList("/").length);
123     }
124         
125     /**
126      * Stop the ftp server and disconnect the client
127      */
128     public void tearDown()
129     {
130         if(ftpServer != null)
131         {
132             ftpServer.stop();
133         }                
134         
135         if(ftpClient != null && ftpClient.isConnected())
136         {
137             try
138             {
139                 ftpClient.disconnect();
140             }
141             catch (IOException e)
142             {
143                 e.printStackTrace();
144             }
145         }                
146     }
147 }