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.vfs.test;
18  
19  import org.apache.commons.vfs.Capability;
20  import org.apache.commons.vfs.FileObject;
21  
22  import java.io.IOException;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.net.URLConnection;
26  
27  /***
28   * URL test cases for providers.
29   *
30   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
32   */
33  public class UrlTests
34      extends AbstractProviderTestCase
35  {
36      /***
37       * Returns the capabilities required by the tests of this test case.  The
38       * tests are not run if the provider being tested does not support all
39       * the required capabilities.  Return null or an empty array to always
40       * run the tests.
41       * <p/>
42       * <p>This implementation returns null.
43       */
44      protected Capability[] getRequiredCaps()
45      {
46          return new Capability[]{Capability.URI};
47      }
48  
49      /***
50       * Tests url.
51       */
52      public void testURL() throws Exception
53      {
54          final FileObject file = getReadFolder().resolveFile("some-dir/");
55          final URL url = file.getURL();
56  
57          assertEquals(file.getName().getURI(), url.toExternalForm());
58  
59          final URL parentURL;
60          try
61          {
62              parentURL = new URL(url, "..");
63          }
64          catch (MalformedURLException e)
65          {
66              throw e;
67          }
68          assertEquals(file.getParent().getURL(), parentURL);
69  
70          final URL rootURL = new URL(url, "/");
71          assertEquals(file.getFileSystem().getRoot().getURL(), rootURL);
72      }
73  
74      /***
75       * Tests content.
76       */
77      public void testURLContent() throws Exception
78      {
79          // Test non-empty file
80          FileObject file = getReadFolder().resolveFile("file1.txt");
81          assertTrue(file.exists());
82  
83          URLConnection urlCon = file.getURL().openConnection();
84          assertSameURLContent(FILE1_CONTENT, urlCon);
85  
86          // Test empty file
87          file = getReadFolder().resolveFile("empty.txt");
88          assertTrue(file.exists());
89  
90          urlCon = file.getURL().openConnection();
91          assertSameURLContent("", urlCon);
92      }
93  
94      /***
95       * Tests that unknown files have no content.
96       */
97      public void testUnknownURL() throws Exception
98      {
99          // Try getting the content of an unknown file
100         final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
101         assertFalse(unknownFile.exists());
102 
103         final URLConnection connection = unknownFile.getURL().openConnection();
104         try
105         {
106             connection.getInputStream();
107             fail();
108         }
109         catch (final IOException e)
110         {
111             assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
112         }
113         assertEquals(-1, connection.getContentLength());
114     }
115 
116 }