View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.utils;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  
25  import java.io.File;
26  import java.nio.file.Path;
27  import java.nio.file.Paths;
28  
29  import org.apache.commons.lang3.SystemUtils;
30  import org.junit.jupiter.api.Test;
31  
32  class FileNameUtilsTest {
33  
34      @Test
35      void testGetBaseNamePathBaseCases() {
36          assertEquals("bar", FileNameUtils.getBaseName(Paths.get("a/b/c/bar.foo")));
37          assertEquals("foo", FileNameUtils.getBaseName(Paths.get("foo")));
38          assertEquals("", FileNameUtils.getBaseName(Paths.get("")));
39          assertEquals("", FileNameUtils.getBaseName(Paths.get(".")));
40          for (final File f : File.listRoots()) {
41              assertNull(FileNameUtils.getBaseName(f.toPath()));
42          }
43          if (SystemUtils.IS_OS_WINDOWS) {
44              assertNull(FileNameUtils.getBaseName(Paths.get("C:\\")));
45          }
46      }
47  
48      @Test
49      void testGetBaseNamePathCornerCases() {
50          assertNull(FileNameUtils.getBaseName((Path) null));
51          assertEquals("foo", FileNameUtils.getBaseName(Paths.get("foo.")));
52          assertEquals("", FileNameUtils.getBaseName(Paths.get("bar/.foo")));
53      }
54  
55      @Test
56      void testGetBaseNameStringBaseCases() {
57          assertEquals("bar", FileNameUtils.getBaseName("a/b/c/bar.foo"));
58          assertEquals("foo", FileNameUtils.getBaseName("foo"));
59      }
60  
61      @Test
62      void testGetBaseNameStringCornerCases() {
63          assertNull(FileNameUtils.getBaseName((String) null));
64          assertEquals("foo", FileNameUtils.getBaseName("foo."));
65          assertEquals("", FileNameUtils.getBaseName("bar/.foo"));
66      }
67  
68      @Test
69      void testGetExtensionPathBaseCases() {
70          assertEquals("foo", FileNameUtils.getExtension(Paths.get("a/b/c/bar.foo")));
71          assertEquals("", FileNameUtils.getExtension(Paths.get("foo")));
72          assertEquals("", FileNameUtils.getExtension(Paths.get("")));
73          assertEquals("", FileNameUtils.getExtension(Paths.get(".")));
74          for (final File f : File.listRoots()) {
75              assertNull(FileNameUtils.getExtension(f.toPath()));
76          }
77          if (SystemUtils.IS_OS_WINDOWS) {
78              assertNull(FileNameUtils.getExtension(Paths.get("C:\\")));
79          }
80      }
81  
82      @Test
83      void testGetExtensionPathCornerCases() {
84          assertNull(FileNameUtils.getExtension((String) null));
85          assertEquals("", FileNameUtils.getExtension(Paths.get("foo.")));
86          assertEquals("foo", FileNameUtils.getExtension(Paths.get("bar/.foo")));
87      }
88  
89      @Test
90      void testGetExtensionStringBaseCases() {
91          assertEquals("foo", FileNameUtils.getExtension("a/b/c/bar.foo"));
92          assertEquals("", FileNameUtils.getExtension("foo"));
93      }
94  
95      @Test
96      void testGetExtensionStringCornerCases() {
97          assertNull(FileNameUtils.getExtension((String) null));
98          assertEquals("", FileNameUtils.getExtension("foo."));
99          assertEquals("foo", FileNameUtils.getExtension("bar/.foo"));
100     }
101 }