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 18 package org.apache.commons.io.file; 19 20 import java.io.File; 21 import java.io.IOException; 22 import java.nio.file.FileSystems; 23 import java.nio.file.Files; 24 import java.nio.file.Path; 25 26 import org.junit.jupiter.api.BeforeEach; 27 import org.junit.jupiter.api.io.TempDir; 28 29 /** 30 * Provides services for test subclasses. 31 */ 32 public abstract class AbstractTempDirTest { 33 34 protected static final String SUB_DIR = "subdir"; 35 protected static final String SYMLINKED_DIR = "symlinked-dir"; 36 37 /** 38 * Creates directory test fixtures in the given directory {@code rootDir}. 39 * <ol> 40 * <li>{@code rootDir/subdir}</li> 41 * <li>{@code rootDir/symlinked-dir} -> {@code rootDir/subdir}</li> 42 * </ol> 43 * @param rootDir Root for directory entries. 44 * @return Path for {@code tempDirPath/subdir}. 45 * @throws IOException if an I/O error occurs or the parent directory does not exist. 46 */ 47 protected static Path createTempSymlinkedRelativeDir(final Path rootDir) throws IOException { 48 final Path targetDir = rootDir.resolve(SUB_DIR); 49 final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR); 50 Files.createDirectory(targetDir); 51 return Files.createSymbolicLink(symlinkDir, targetDir); 52 } 53 54 /** 55 * A temporary directory managed by JUnit. 56 */ 57 @TempDir 58 public Path managedTempDirPath; 59 60 /** 61 * A File version of this test's Path object. 62 */ 63 public File tempDirFile; 64 65 /** 66 * A temporary directory managed by each test so we can optionally fiddle with its permissions independently. 67 */ 68 public Path tempDirPath; 69 70 @BeforeEach 71 public void beforeEachCreateTempDirs() throws IOException { 72 tempDirPath = Files.createTempDirectory(managedTempDirPath, getClass().getSimpleName()); 73 tempDirFile = tempDirPath.toFile(); 74 } 75 76 @SuppressWarnings("resource") // no FileSystem allocation 77 protected final boolean isPosixFilePermissionsSupported() { 78 return FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); 79 } 80 }