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.provider.local;
18  
19  import org.apache.commons.vfs2.AbstractProviderTestCase;
20  import org.apache.commons.vfs2.FileName;
21  import org.apache.commons.vfs2.FileObject;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  /**
28   * Additional naming tests for local file system.
29   * <p>
30   * Only executed on Windows.
31   */
32  public class WindowsFileNameTests extends AbstractProviderTestCase {
33  
34      @Test
35      public void testWindowsFilenameParserError() throws Exception {
36          // check VFS-338 with 2+4 slashes we want a dedicated error
37          try {
38              final String FILE = "file://////";
39              final DefaultFileSystemManager manager = getManager();
40              Assert.assertNotNull("Unexpected null manager for test " + this, manager);
41              final FileObject fo = manager.resolveFile(FILE);
42              fail("Windows File Parser should not allow " + FILE + " " + fo);
43          } catch (FileSystemException ex) {
44              assertEquals("Exception code", "vfs.provider/invalid-absolute-uri.error", ex.getCode());
45              ex = (FileSystemException) ex.getCause();
46              assertEquals("Exception code", "vfs.provider.local/not-absolute-file-name.error", ex.getCode());
47          }
48      }
49  
50      @Test
51      public void testWindowsFilenameUNCStartError() throws Exception {
52          try {
53              final String FILE = "file://///";
54              final DefaultFileSystemManager manager = getManager();
55              Assert.assertNotNull("Unexpected null manager for test " + this, manager);
56              final FileObject fo = manager.resolveFile(FILE);
57              fail("Windows File Parser should not allow " + FILE + " " + fo);
58          } catch (FileSystemException ex) {
59              assertEquals("Exception code", "vfs.provider/invalid-absolute-uri.error", ex.getCode());
60              ex = (FileSystemException) ex.getCause();
61              assertEquals("Exception code", "vfs.provider.local/missing-share-name.error", ex.getCode());
62          }
63      }
64  
65      @Test
66      public void testWindowsRoots() throws Exception {
67          // valid URI forms of the filesystem root
68          final String[] tests = new String[] { "file:///C:/", "file://C:/", "file:/C:/", "file:C:/" };
69  
70          for (final String name : tests) {
71              final DefaultFileSystemManager manager = getManager();
72              Assert.assertNotNull("Unexpected null manager for test " + this, manager);
73              final FileName fn = manager.resolveFile(name).getName();
74  
75              // the following tests work for Windows file names only
76              assertSame(WindowsFileName.class, fn.getClass());
77  
78              // all should result in the same FileName
79              assertEquals("file:///C:/", fn.toString());
80              assertEquals("/", fn.getPath());
81              assertEquals("/", fn.getPathDecoded());
82              assertEquals("file:///C:/", fn.getRootURI());
83              assertEquals("file:///C:/", fn.getFriendlyURI());
84  
85              assertEquals("file:///C:/", fn.getRoot().toString());
86  
87              assertEquals("", fn.getExtension());
88              assertEquals("", fn.getBaseName());
89          }
90      }
91  
92      @Test
93      public void testWindowsWrongRoots() throws Exception {
94          final String[] tests = new String[] { "file:///C:", "file://C:", "file:/C:", "file:C:" };
95  
96          for (final String name : tests) {
97              try {
98                  final DefaultFileSystemManager manager = getManager();
99                  Assert.assertNotNull("Unexpected null manager for test " + this, manager);
100                 final FileName fn = manager.resolveFile(name).getName();
101                 fail("should not accept root " + name);
102             } catch (final FileSystemException ex) {
103                 assertEquals("vfs.provider/invalid-absolute-uri.error", ex.getCode());
104                 assertTrue(ex.toString().indexOf(name) >= 0);
105             }
106         }
107     }
108 }