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;
18  
19  import java.nio.file.Path;
20  import java.nio.file.Paths;
21  
22  import org.junit.Test;
23  
24  /**
25   * Path test cases for providers.
26   */
27  public class PathTests extends AbstractProviderTestCase {
28  
29      /**
30       * Returns the capabilities required by the tests of this test case. The tests are not run if the provider being
31       * tested does not support all the required capabilities. Return null or an empty array to always run the tests.
32       */
33      @Override
34      protected Capability[] getRequiredCapabilities() {
35          return new Capability[] {Capability.URI};
36      }
37  
38      /**
39       * Tests resolution of absolute URI.
40       */
41      @Test
42      public void testAbsoluteURI() throws Exception {
43          final FileObject readFolder = getReadFolder();
44  
45          // Try fetching base folder again by its Path
46          final String pathStr = readFolder.getPath().toString();
47          try (FileObject fileObject = getManager().resolveFile(pathStr,
48              readFolder.getFileSystem().getFileSystemOptions())) {
49              assertSame("file object", readFolder, fileObject);
50          }
51  
52          // Try fetching the filesystem root by its Path
53          final Path rootPath = Paths.get(readFolder.getName().getRootURI());
54          try (FileObject fileObject = getManager().resolveFile(rootPath.toString(),
55              readFolder.getFileSystem().getFileSystemOptions())) {
56              assertSame(readFolder.getFileSystem().getRoot(), fileObject);
57              assertEquals(rootPath, Paths.get(fileObject.getName().getRootURI()));
58              assertEquals(rootPath, fileObject.getName().getPath());
59              assertEquals(FileName.ROOT_PATH, fileObject.getName().getPath());
60          }
61      }
62  
63      @Test
64      public void testGetPath() throws Exception {
65          try (final FileObject fileObject = getReadFolder().resolveFile("some-dir/")) {
66              final Path path = fileObject.getPath();
67  
68              // FileName#getURI() returns a String, not a URI.
69              assertEquals(Paths.get(fileObject.getName().getURI()).toString(), path.toString());
70              assertEquals(Paths.get(fileObject.getName().getURI()), path);
71  
72              assertEquals(fileObject.getPath().toString(), fileObject.getURI().toString());
73          }
74      }
75  
76      @Test
77      public void testReservedCharacterSpace() throws FileSystemException {
78          try (final FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
79              final Path path = fileObject.getPath();
80              final String string = path.toString();
81              assertTrue(string, string.contains("file%20with%20spaces.txt"));
82          }
83          try (final FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
84              final Path path = fileObject.getPath();
85              final String string = path.toString();
86              assertTrue(string, string.contains("file%20with%20spaces.txt"));
87          }
88      }
89  
90      /**
91       * Tests content.
92       */
93      @Test
94      public void testURIContentProvider() throws Exception {
95          // Test non-empty file
96          try (final FileObject fileObject = getReadFolder().resolveFile("file1.txt")) {
97              assertTrue(fileObject.exists());
98  
99              final Path path = fileObject.getPath();
100             final String pathStr = path.toString();
101             final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
102 
103             try (final FileObject f1 = getManager().resolveFile(pathStr, options);
104                 final FileObject f2 = getManager().resolveFile(pathStr, options)) {
105 
106                 assertEquals("Two files resolved by URI must be equals on " + pathStr, f1, f2);
107                 assertSame("Resolving two times should not produce new filesystem on " + pathStr, f1.getFileSystem(),
108                     f2.getFileSystem());
109             }
110         }
111     }
112 }