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 static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import junit.framework.Test;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory;
28  import org.apache.ftpserver.ftplet.FileSystemFactory;
29  import org.apache.ftpserver.ftplet.FileSystemView;
30  import org.apache.ftpserver.ftplet.FtpException;
31  import org.apache.ftpserver.ftplet.User;
32  
33  /**
34   * Tests for FTP file systems (with homeDirIsRoot=true).
35   */
36  public class FtpProviderUserDirTestCase extends FtpProviderTestCase {
37  
38      /**
39       * Creates the test suite for the ftp file system.
40       */
41      public static Test suite() throws Exception {
42          return suite(new FtpProviderUserDirTestCase());
43      }
44  
45      /**
46       * Gets option file system factory for local FTP server.
47       */
48      @Override
49      protected FileSystemFactory getFtpFileSystem() throws IOException {
50          // simulate a non-root home directory by copying test directory to it
51          final File testDir = new File(getTestDirectory());
52          final File rootDir = new File(testDir, "homeDirIsRoot");
53          final File homesDir = new File(rootDir, "home");
54          final File initialDir = new File(homesDir, "test");
55          FileUtils.deleteDirectory(rootDir);
56          // noinspection ResultOfMethodCallIgnored
57          rootDir.mkdir();
58          FileUtils.copyDirectory(testDir, initialDir, pathName -> !pathName.getPath().contains(rootDir.getName()));
59  
60          return new NativeFileSystemFactory() {
61              @Override
62              public FileSystemView createFileSystemView(final User user) throws FtpException {
63                  final FileSystemView fsView = super.createFileSystemView(user);
64                  fsView.changeWorkingDirectory("home/test");
65                  return fsView;
66              }
67          };
68      }
69  
70      /**
71       * Gets the root of the local FTP Server file system.
72       */
73      @Override
74      protected String getFtpRootDir() {
75          return new File(getTestDirectory(), "homeDirIsRoot").getPath();
76      }
77  
78      /**
79       * Prepares the file system manager.
80       */
81      @Override
82      protected boolean getUserDirIsRoot() {
83          return true;
84      }
85  
86  }