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    *      https://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  
18  package org.apache.commons.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotSame;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.apache.commons.lang3.SystemUtils;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.condition.EnabledOnOs;
28  import org.junit.jupiter.api.condition.OS;
29  
30  /**
31   * Tests {@link FileSystem}.
32   */
33  class FileSystemTest {
34  
35      @Test
36      void testGetBlockSize() {
37          assertTrue(FileSystem.getCurrent().getBlockSize() >= 0);
38      }
39  
40      @Test
41      void testGetCurrent() {
42          if (SystemUtils.IS_OS_WINDOWS) {
43              assertEquals(FileSystem.WINDOWS, FileSystem.getCurrent());
44          }
45          if (SystemUtils.IS_OS_LINUX) {
46              assertEquals(FileSystem.LINUX, FileSystem.getCurrent());
47          }
48          if (SystemUtils.IS_OS_MAC_OSX) {
49              assertEquals(FileSystem.MAC_OSX, FileSystem.getCurrent());
50          }
51      }
52  
53      @Test
54      void testGetIllegalFileNameChars() {
55          final FileSystem current = FileSystem.getCurrent();
56          assertNotSame(current.getIllegalFileNameChars(), current.getIllegalFileNameChars());
57      }
58  
59      @Test
60      void testIsLegalName() {
61          for (final FileSystem fs : FileSystem.values()) {
62              assertFalse(fs.isLegalFileName(""), fs.name()); // Empty is always illegal
63              assertFalse(fs.isLegalFileName(null), fs.name()); // null is always illegal
64              assertFalse(fs.isLegalFileName("\0"), fs.name()); // Assume NUL is always illegal
65              assertTrue(fs.isLegalFileName("0"), fs.name()); // Assume simple name always legal
66              for (final String candidate : fs.getReservedFileNames()) {
67                  // Reserved file names are not legal
68                  assertFalse(fs.isLegalFileName(candidate), candidate);
69              }
70          }
71      }
72  
73      @Test
74      void testIsReservedFileName() {
75          for (final FileSystem fs : FileSystem.values()) {
76              for (final String candidate : fs.getReservedFileNames()) {
77                  assertTrue(fs.isReservedFileName(candidate));
78              }
79          }
80      }
81  
82      @Test
83      @EnabledOnOs(OS.WINDOWS)
84      void testIsReservedFileNameOnWindows() {
85          final FileSystem fs = FileSystem.WINDOWS;
86          for (final String candidate : fs.getReservedFileNames()) {
87              // System.out.printf("Reserved %s exists: %s%n", candidate, Files.exists(Paths.get(candidate)));
88              assertTrue(fs.isReservedFileName(candidate));
89              assertTrue(fs.isReservedFileName(candidate + ".txt"), candidate);
90          }
91  
92  // This can hang when trying to create files for some reserved names, but it is interesting to keep
93  //
94  //        for (final String candidate : fs.getReservedFileNames()) {
95  //            System.out.printf("Testing %s%n", candidate);
96  //            assertTrue(fs.isReservedFileName(candidate));
97  //            final Path path = Paths.get(candidate);
98  //            final boolean exists = Files.exists(path);
99  //            try {
100 //                PathUtils.writeString(path, "Hello World!", StandardCharsets.UTF_8);
101 //            } catch (IOException ignored) {
102 //                // Asking to create a reserved file either:
103 //                // - Throws an exception, for example "AUX"
104 //                // - Is a NOOP, for example "COM3"
105 //            }
106 //            assertEquals(exists, Files.exists(path), path.toString());
107 //        }
108     }
109 
110     @Test
111     void testReplacementWithNUL() {
112         for (final FileSystem fs : FileSystem.values()) {
113             try {
114                 fs.toLegalFileName("Test", '\0'); // Assume NUL is always illegal
115             } catch (final IllegalArgumentException iae) {
116                 assertTrue(iae.getMessage().startsWith("The replacement character '\\0'"), iae.getMessage());
117             }
118         }
119     }
120 
121     @Test
122     void testSorted() {
123         for (final FileSystem fs : FileSystem.values()) {
124             final char[] chars = fs.getIllegalFileNameChars();
125             for (int i = 0; i < chars.length - 1; i++) {
126                 assertTrue(chars[i] < chars[i + 1], fs.name());
127             }
128         }
129     }
130 
131     @Test
132     void testSupportsDriveLetter() {
133         assertTrue(FileSystem.WINDOWS.supportsDriveLetter());
134         assertFalse(FileSystem.GENERIC.supportsDriveLetter());
135         assertFalse(FileSystem.LINUX.supportsDriveLetter());
136         assertFalse(FileSystem.MAC_OSX.supportsDriveLetter());
137     }
138 
139     @Test
140     void testToLegalFileNameWindows() {
141         final FileSystem fs = FileSystem.WINDOWS;
142         final char replacement = '-';
143         for (char i = 0; i < 32; i++) {
144             assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
145         }
146         final char[] illegal = { '<', '>', ':', '"', '/', '\\', '|', '?', '*' };
147         for (char i = 0; i < illegal.length; i++) {
148             assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
149         }
150         for (char i = 'a'; i < 'z'; i++) {
151             assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
152         }
153         for (char i = 'A'; i < 'Z'; i++) {
154             assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
155         }
156         for (char i = '0'; i < '9'; i++) {
157             assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
158         }
159     }
160 }