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