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.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  public class FileSystemUtilsTest {
31  
32      static char[] getIllegalFileNameChars() {
33          return FileSystem.getCurrent().getIllegalFileNameChars();
34      }
35  
36      @ParameterizedTest
37      @MethodSource("getIllegalFileNameChars")
38      public void testGetFreeSpace_IllegalFileName(final char illegalFileNameChar) throws Exception {
39          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("\\ \"" + illegalFileNameChar));
40      }
41  
42      @Test
43      public void testGetFreeSpace_IllegalFileNames() throws Exception {
44          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("\\ \""));
45      }
46  
47      @Test
48      public void testGetFreeSpace_String() throws Exception {
49          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpace(null));
50          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpace("this directory does not exist, at all."));
51          // "" means current dir.
52          assertTrue(FileSystemUtils.freeSpace("") > 0);
53          assertTrue(FileSystemUtils.freeSpace("target") > 0);
54          // files worked as well in previous versions.
55          assertTrue(FileSystemUtils.freeSpace("pom.xml") > 0);
56      }
57  
58      @Test
59      public void testGetFreeSpaceKb() throws Exception {
60          assertTrue(FileSystemUtils.freeSpaceKb() > 0);
61      }
62  
63      @Test
64      public void testGetFreeSpaceKb_long() throws Exception {
65          assertTrue(FileSystemUtils.freeSpaceKb(0) > 0);
66      }
67  
68      @Test
69      public void testGetFreeSpaceKb_String() throws Exception {
70          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpaceKb(null));
71          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpaceKb("this directory does not exist, at all."));
72          // "" means current dir.
73          assertTrue(FileSystemUtils.freeSpaceKb("") > 0);
74          assertTrue(FileSystemUtils.freeSpaceKb("target") > 0);
75          // files worked as well in previous versions.
76          assertTrue(FileSystemUtils.freeSpaceKb("pom.xml") > 0);
77      }
78  
79      @Test
80      public void testGetFreeSpaceKb_String_long() throws Exception {
81          assertThrows(NullPointerException.class, () -> FileSystemUtils.freeSpaceKb(null, 0));
82          assertThrows(IllegalArgumentException.class, () -> FileSystemUtils.freeSpaceKb("this directory does not exist, at all.", 0));
83          // "" means current dir.
84          assertTrue(FileSystemUtils.freeSpaceKb("", 0) > 0);
85          assertTrue(FileSystemUtils.freeSpaceKb("target", 0) > 0);
86          // files worked as well in previous versions.
87          assertTrue(FileSystemUtils.freeSpaceKb("pom.xml", 0) > 0);
88      }
89  
90  }