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  package org.apache.commons.io;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.params.ParameterizedTest;
24  import org.junit.jupiter.params.provider.MethodSource;
25  
26  /**
27   * Tests {@link FileSystemUtils}.
28   */
29  @SuppressWarnings("deprecation") // testing deprecated class
30  class FileSystemUtilsTest {
31  
32      private static final String NON_EMPTY_DIR = "src/test/resources/dir-equals-tests";
33  
34      static char[] getIllegalFileNameChars() {
35          return FileSystem.getCurrent().getIllegalFileNameChars();
36      }
37  
38      @ParameterizedTest
39      @MethodSource("getIllegalFileNameChars")
40      void testGetFreeSpace_IllegalFileName(final char illegalFileNameChar) throws Exception {
41          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("\\ \"" + illegalFileNameChar));
42      }
43  
44      @Test
45      void testGetFreeSpace_IllegalFileNames() throws Exception {
46          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("\\ \""));
47      }
48  
49      @Test
50      void testGetFreeSpace_String() throws Exception {
51          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpace(null));
52          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("this directory does not exist, at all."));
53          // "" means current dir.
54          assertTrue(FileSystemUtils.freeSpace("") > 0);
55          assertTrue(FileSystemUtils.freeSpace(NON_EMPTY_DIR) > 0);
56          // files worked as well in previous versions.
57          assertTrue(FileSystemUtils.freeSpace("pom.xml") > 0);
58      }
59  
60      @Test
61      void testGetFreeSpaceKb() throws Exception {
62          assertTrue(FileSystemUtils.freeSpaceKb() > 0);
63      }
64  
65      @Test
66      void testGetFreeSpaceKb_long() throws Exception {
67          assertTrue(FileSystemUtils.freeSpaceKb(0) > 0);
68      }
69  
70      @Test
71      void testGetFreeSpaceKb_String() throws Exception {
72          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpaceKb(null));
73          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpaceKb("this directory does not exist, at all."));
74          // "" means current dir.
75          assertTrue(FileSystemUtils.freeSpaceKb("") > 0);
76          assertTrue(FileSystemUtils.freeSpaceKb(NON_EMPTY_DIR) > 0);
77          // files worked as well in previous versions.
78          assertTrue(FileSystemUtils.freeSpaceKb("pom.xml") > 0);
79      }
80  
81      @Test
82      void testGetFreeSpaceKb_String_long() throws Exception {
83          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpaceKb(null, 0));
84          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpaceKb("this directory does not exist, at all.", 0));
85          // "" means current dir.
86          assertTrue(FileSystemUtils.freeSpaceKb("", 0) > 0);
87          assertTrue(FileSystemUtils.freeSpaceKb(NON_EMPTY_DIR, 0) > 0);
88          // files worked as well in previous versions.
89          assertTrue(FileSystemUtils.freeSpaceKb("pom.xml", 0) > 0);
90      }
91  
92  }