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  
18  package org.apache.commons.io.file;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.file.FileSystems;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  
29  import org.junit.jupiter.api.AfterEach;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.io.TempDir;
32  
33  /**
34   * Provides services for test subclasses.
35   */
36  public abstract class AbstractTempDirTest {
37  
38      protected static final String SUB_DIR = "subdir";
39      protected static final String SYMLINKED_DIR = "symlinked-dir";
40  
41      public static void assertCreateNewFile(final File file) throws IOException {
42          assertTrue(file.createNewFile(), file::toString);
43      }
44  
45      public static void assertDelete(final boolean expected, final File file) {
46          assertEquals(expected, file.delete(), file::toString);
47      }
48  
49      public static void assertMkdir(final boolean expected, final File subDir) {
50          assertEquals(expected, subDir.mkdir(), subDir::toString);
51      }
52  
53      /**
54       * Creates directory test fixtures in the given directory {@code rootDir}.
55       * <ol>
56       * <li>{@code rootDir/subdir}</li>
57       * <li>{@code rootDir/symlinked-dir} -> {@code rootDir/subdir}</li>
58       * </ol>
59       * @param rootDir Root for directory entries.
60       * @return Path for {@code tempDirPath/subdir}.
61       * @throws IOException if an I/O error occurs or the parent directory does not exist.
62       */
63      protected static Path createTempSymbolicLinkedRelativeDir(final Path rootDir) throws IOException {
64          final Path targetDir = rootDir.resolve(SUB_DIR);
65          final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
66          Files.createDirectory(targetDir);
67          return Files.createSymbolicLink(symlinkDir, targetDir);
68      }
69  
70      /**
71       * A temporary directory managed by JUnit.
72       */
73      @TempDir
74      public Path managedTempDirPath;
75  
76      /**
77       * A File version of this test's Path object.
78       */
79      public File tempDirFile;
80  
81      /**
82       * A temporary directory managed by each test so we can optionally fiddle with its permissions independently.
83       */
84      public Path tempDirPath;
85  
86      @AfterEach
87      void afterEachDeleteTempDir() throws IOException {
88          // For @TempDir, symbolic and other types of links, such as junctions on Windows, are not followed.
89          if (Files.exists(tempDirPath)) {
90              PathUtils.deleteDirectory(tempDirPath);
91          }
92      }
93  
94      @BeforeEach
95      public void beforeEachCreateTempDirs() throws IOException {
96          tempDirPath = Files.createDirectory(managedTempDirPath.resolve(getClass().getSimpleName()));
97          tempDirFile = tempDirPath.toFile();
98      }
99  
100     @SuppressWarnings("resource") // no FileSystem allocation
101     protected final boolean isPosixFilePermissionsSupported() {
102         return FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
103     }
104 }