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