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   *   http://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  package org.apache.rat.utils;
20  
21  import org.junit.jupiter.api.Test;
22  import org.junit.jupiter.api.extension.ExtendWith;
23  import org.mockito.Mock;
24  import org.mockito.MockedStatic;
25  import org.mockito.junit.jupiter.MockitoExtension;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.util.List;
32  
33  import static org.apache.rat.utils.FileUtils.writeFile;
34  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
35  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  import static org.mockito.ArgumentMatchers.any;
38  import static org.mockito.Mockito.mockStatic;
39  import static org.mockito.Mockito.when;
40  
41  @ExtendWith(MockitoExtension.class)
42  class FileUtilsTest {
43      public static final List<String> JUST_A_TEST = List.of("just a test");
44  
45      @Mock
46      private File mockedFile;
47  
48      @Test
49      void deleteSwallowsExceptions() {
50          when(mockedFile.exists()).thenReturn(true);
51          when(mockedFile.isDirectory()).thenReturn(false);
52  
53          try (MockedStatic<Files> files = mockStatic(Files.class)) {
54              files.when(() -> Files.delete(any(Path.class)))
55                      .thenThrow(new IOException("Mocked exception"));
56  
57              assertDoesNotThrow(() -> FileUtils.delete(mockedFile));
58          }
59      }
60  
61      @Test
62      void writeFileHandlesNullFile() {
63          assertThrows(IllegalArgumentException.class, () ->
64                  writeFile(null, "just a Test", JUST_A_TEST));
65      }
66  
67      @Test
68      void shouldWrapIOException() throws IOException {
69          Path tempDir = Files.createTempDirectory("shouldWrapIOExceptionTest");
70          File file = Files.createFile(tempDir.resolve("existing")).toFile();
71  
72          RuntimeException exception = assertThrows(
73                  RuntimeException.class, () -> writeFile(file, "test.txt", JUST_A_TEST)
74          );
75  
76          assertInstanceOf(IOException.class, exception.getCause());
77      }
78  }