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.apache.commons.lang3.ObjectUtils;
22  import org.junit.Test;
23  
24  /**
25   * URI test cases for providers.
26   */
27  public class UriTests 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 tested does not support all the required
31       * 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          // 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          // assert properties
48          assertEquals(readFolder.exists(), file.exists());
49          assertEquals(readFolder.getName(), file.getName());
50          // Needs JRE file providers
51          // assertEquals(readFolder.getPath(), file.getPath());
52          assertEquals(readFolder.getPublicURIString(), file.getPublicURIString());
53          assertEquals(readFolder.getType(), file.getType());
54          assertEquals(readFolder.getURI(), file.getURI());
55          assertEquals(readFolder.getURL(), file.getURL());
56          assertEquals(readFolder.isAttached(), file.isAttached());
57          assertEquals(readFolder.isContentOpen(), file.isContentOpen());
58          assertEquals(readFolder.isExecutable(), file.isExecutable());
59          assertEquals(readFolder.isFile(), file.isFile());
60          assertEquals(readFolder.isFolder(), file.isFolder());
61          assertEquals(readFolder.isHidden(), file.isHidden());
62          assertEquals(readFolder.isReadable(), file.isReadable());
63          assertEquals(readFolder.isSymbolicLink(), file.isSymbolicLink());
64          assertEquals(readFolder.isWriteable(), file.isWriteable());
65          assertEquals(readFolder.toString(), file.toString());
66          assertEquals(String.format("file object %s %s, %s %s", readFolder.getClass(), ObjectUtils.identityHashCodeHex(readFolder), file.getClass(),
67                  ObjectUtils.identityHashCodeHex(file)), readFolder.toString(), file.toString());
68          // Try fetching the filesystem root by its URI
69          final String rootUri = readFolder.getName().getRootURI();
70          file = getManager().resolveFile(rootUri, readFolder.getFileSystem().getFileSystemOptions());
71          assertEquals(readFolder.getFileSystem().getRoot().toString(), file.toString());
72          assertEquals(rootUri, file.getName().getRootURI());
73          assertEquals(rootUri, file.getName().getURI());
74          assertEquals(FileName.ROOT_PATH, file.getName().getPath());
75      }
76  
77      @Test
78      public void testGetURI() throws Exception {
79          final FileObject fileObject = getReadFolder().resolveFile("some-dir/");
80          final URI uri = fileObject.getURI();
81  
82          // FileName#getURI() returns a String, not a URI.
83          assertEquals(fileObject.getName().getURI(), uri.toString());
84          assertEquals(URI.create(fileObject.getName().getURI()), uri);
85  
86          assertEquals(fileObject.getURL().toString(), fileObject.getURI().toString());
87          assertEquals(fileObject.getURL().toURI(), fileObject.getURI());
88      }
89  
90      @Test
91      public void testReservedCharacterSpace() throws FileSystemException {
92          try (FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
93              final URI url = fileObject.getURI();
94              final String string = url.toString();
95              assertTrue(string, string.contains("file%20with%20spaces.txt"));
96          }
97          try (FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
98              final URI url = fileObject.getURI();
99              final String string = url.toString();
100             assertTrue(string, string.contains("file%20with%20spaces.txt"));
101         }
102     }
103 
104     /**
105      * Tests content.
106      */
107     @Test
108     public void testURIContentProvider() throws Exception {
109         // Test non-empty file
110         final FileObject fileObject = getReadFolder().resolveFile("file1.txt");
111         assertTrue(fileObject.exists());
112 
113         final URI uri = fileObject.getURI();
114         final String uriStr = uri.toString();
115         final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
116 
117         final FileObject f1 = getManager().resolveFile(uriStr, options);
118         final FileObject f2 = getManager().resolveFile(uriStr, options);
119 
120         assertEquals("Two files resolved by URI must be equals on " + uriStr, f1, f2);
121         assertSame("Resolving two times should not produce new filesystem on " + uriStr, f1.getFileSystem(), f2.getFileSystem());
122     }
123 
124 }