View Javadoc

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