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 static org.apache.commons.vfs2.VfsTestUtils.assertSameMessage;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.net.URLConnection;
24  
25  import org.junit.Test;
26  
27  /**
28   * URL test cases for providers.
29   */
30  public class UrlTests extends AbstractProviderTestCase {
31  
32      /**
33       * Returns the capabilities required by the tests of this test case. The tests are not run if the provider being
34       * tested does not support all the required capabilities. Return null or an empty array to always run the tests.
35       */
36      @Override
37      protected Capability[] getRequiredCapabilities() {
38          return new Capability[] {Capability.URI};
39      }
40  
41      @Test
42      public void testReservedCharacterSpace() throws FileSystemException {
43          try (FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
44              final URL url = fileObject.getURL();
45              final String string = url.toString();
46              assertTrue(string, string.contains("file%20with%20spaces.txt"));
47          }
48          try (FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
49              final URL url = fileObject.getURL();
50              final String string = url.toString();
51              assertTrue(string, string.contains("file%20with%20spaces.txt"));
52          }
53      }
54  
55      /**
56       * Tests that unknown files have no content.
57       */
58      @Test
59      public void testUnknownURL() throws Exception {
60          // Try getting the content of an unknown file
61          final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
62          assertFalse(unknownFile.exists());
63  
64          final URLConnection connection = unknownFile.getURL().openConnection();
65          try {
66              connection.getInputStream();
67              fail();
68          } catch (final IOException e) {
69              assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
70          }
71          assertEquals(-1, connection.getContentLength());
72      }
73  
74      /**
75       * Tests url.
76       */
77      @Test
78      public void testURL() throws Exception {
79          final FileObject file = getReadFolder().resolveFile("some-dir/");
80          final URL url = file.getURL();
81  
82          assertEquals(file.getName().getURI(), url.toExternalForm());
83  
84          final URL parentURL = new URL(url, "..");
85          assertEquals(file.getParent().getURL(), parentURL);
86  
87          final URL rootURL = new URL(url, "/");
88          assertEquals(file.getFileSystem().getRoot().getURL(), rootURL);
89      }
90  
91      /**
92       * Tests content.
93       */
94      @Test
95      public void testURLContent() throws Exception {
96          testURLContent(getReadFolder());
97      }
98  
99      private void testURLContent(final FileObject readFolder) throws FileSystemException, IOException, Exception {
100         // Test non-empty file
101         FileObject file = readFolder.resolveFile("file1.txt");
102         assertTrue(file.toString(), file.exists());
103 
104         URLConnection urlCon = file.getURL().openConnection();
105         assertSameURLContent(FILE1_CONTENT, urlCon);
106 
107         // Test empty file
108         file = readFolder.resolveFile("empty.txt");
109         assertTrue(file.exists());
110 
111         urlCon = file.getURL().openConnection();
112         assertSameURLContent("", urlCon);
113     }
114 
115     /**
116      * Tests content.
117      */
118     @Test
119     public void testURLContentProvider() throws Exception {
120         // Test non-empty file
121         final FileObject file = getReadFolder().resolveFile("file1.txt");
122         assertTrue(file.exists());
123 
124         final String uri = file.getURL().toExternalForm();
125         final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
126 
127         final FileObject f1 = getManager().resolveFile(uri, options);
128         final FileObject f2 = getManager().resolveFile(uri, options);
129 
130         assertEquals("Two files resolved by URI must be equals on " + uri, f1, f2);
131         assertSame("Resolving two times should not produce new filesystem on " + uri, f1.getFileSystem(),
132             f2.getFileSystem());
133     }
134 
135 }