View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.ftp;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
24  import org.apache.ftpserver.ftplet.FileSystemFactory;
25  import org.apache.ftpserver.ftplet.FileSystemView;
26  import org.apache.ftpserver.ftplet.FtpException;
27  import org.apache.ftpserver.ftplet.User;
28  
29  import junit.framework.Test;
30  
31  /**
32   * Tests for FTP file systems (with homeDirIsRoot=true).
33   */
34  public class FtpProviderUserDirTestCase extends FtpProviderTestCase {
35      /**
36       * Creates the test suite for the ftp file system.
37       */
38      public static Test suite() throws Exception {
39          return suite(new FtpProviderUserDirTestCase());
40      }
41  
42      /**
43       * Gets option file system factory for local FTP server.
44       */
45      @Override
46      protected FileSystemFactory getFtpFileSystem() throws IOException {
47          // simulate a non-root home directory by copying test directory to it
48          final File testDir = new File(getTestDirectory());
49          final File rootDir = new File(testDir, "homeDirIsRoot");
50          final File homesDir = new File(rootDir, "home");
51          final File initialDir = new File(homesDir, "test");
52          FileUtils.deleteDirectory(rootDir);
53          // noinspection ResultOfMethodCallIgnored
54          rootDir.mkdir();
55          FileUtils.copyDirectory(testDir, initialDir, pathname -> !pathname.getPath().contains(rootDir.getName()));
56  
57          return new NativeFileSystemFactory() {
58              @Override
59              public FileSystemView createFileSystemView(final User user) throws FtpException {
60                  final FileSystemView fsView = super.createFileSystemView(user);
61                  fsView.changeWorkingDirectory("home/test");
62                  return fsView;
63              }
64          };
65      }
66  
67      /**
68       * Gets the root of the local FTP Server file system.
69       */
70      @Override
71      protected String getFtpRootDir() {
72          return new File(getTestDirectory(), "homeDirIsRoot").getPath();
73      }
74  
75      /**
76       * Prepares the file system manager.
77       */
78      @Override
79      protected boolean getUserDirIsRoot() {
80          return true;
81      }
82  
83  }