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