Skip to content

Commit

Permalink
Roll back previous change.
Browse files Browse the repository at this point in the history
  • Loading branch information
SwingGuy1024 committed Apr 19, 2023
1 parent 452aeac commit a273627
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,6 @@
* @see FileFilterUtils#fileFileFilter()
*/
public class SymbolicLinkFileFilter extends AbstractFileFilter implements Serializable {
/*
* Note to developers: The unit test needs to create symbolic links to files. However, on
* Windows, this can't be done without admin privileges. This class is designed to allow a
* unit test to works around this by doing two things: 1) This separates the class logic
* from the call to identify a symbolic link, and 2) It allows the unit test to override
* that symbolic link call on Windows only.
* This means we can write unit tests will run on all machines. On Windows, the unit test
* can't create a symbolic link without admin privileges, so the unit tests won't
* completely test all the necessary behavior on Windows, but they will still test the class
* logic. Be careful not to break this, but be aware of it when writing unit tests. You can
* still maintain this class and its unit tests on Windows. The one method that won't get
* tested on Windows is not likely to change, and will be tested properly when it gets run
* on Apache servers.
*/

/**
* Singleton instance of file filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,191 +17,21 @@

package org.apache.commons.io.filefilter;

import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.BiFunction;

import org.apache.commons.io.file.PathUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Tests {@link SymbolicLinkFileFilter}.
*/
public class SymbolicLinkFileFilterTest {

public static final String TARGET_SHORT_NAME = "SLFF_Target";
public static final String TARGET_EXT = ".txt";
public static final String TARGET_NAME = TARGET_SHORT_NAME + TARGET_EXT;
public static final String DIRECTORY_NAME = "SLFF_TargetDirectory";
public static final String DIRECTORY_LINK_NAME = "SLFF_LinkDirectory";
public static final String MISSING = "Missing";
private static File testTargetFile; // hard file
private static Path testTargetPath; // hard file Path
private static File parentDirectoryFile; // System Temp directory
private static File testLinkFile; // symbolic link to hard file
private static String linkName; // Name of link file
private static Path testLinkPath; // symbolic link to hard file Path
private static File targetDirFile; //
private static Path targetDirPath; // hard directory Path
private static Path linkDirPath; // symbolic link to hardDirectory
private static File linkDirFile;
private static SymbolicLinkFileFilter filter;
private static File missingFile; // non-existent file

private static Path createRealSymbolicLink(Path link, Path target) {
try {
if (Files.exists(link)) {
Files.delete(link);
}
return Files.createSymbolicLink(link, target);
} catch (IOException e) {
throw new IllegalStateException("Failure to create Symbolic Link", e);
}
}

private static Path createMockSymbolicLink(Path lnk, Path tgt) {
try {
return Files.createFile(lnk);
} catch (IOException e) {
throw new IllegalStateException("Failure to create Symbolic Link", e);
}
}

// Mock filter for testing on Windows.
private static SymbolicLinkFileFilter createMockFilter() {
return new SymbolicLinkFileFilter() {
@Override
boolean isSymbolicLink(final Path filePath) {
return filePath.toFile().exists() && filePath.toString().contains("Link"); // Mock test
}
};
}

/**
* <p>Unit test setup creates a hard file, a symbolic link to the hard file, a hard directory,
* and a symbolic link to that directory. All are created in the temp directory</p>
* <p>Unit test teardown deletes all four of these files.</p>
*
* @throws IOException If it fails to create the temporary files
*/
@BeforeAll
static void testSetup() throws IOException {
BiFunction<Path, Path, Path> symbolicLinkCreator;

// We can't create symbolic links on Windows without admin privileges,
// so iff that's our OS, we mock them.
String os = System.getProperty("os.name");
if (os.toLowerCase().contains("windows")) {
symbolicLinkCreator = SymbolicLinkFileFilterTest::createMockSymbolicLink;
filter = createMockFilter();
} else {
symbolicLinkCreator = SymbolicLinkFileFilterTest::createRealSymbolicLink;
filter = SymbolicLinkFileFilter.INSTANCE;
}

testTargetFile = File.createTempFile(TARGET_SHORT_NAME, TARGET_EXT);
testTargetPath = testTargetFile.toPath();
parentDirectoryFile = testTargetFile.getParentFile();
// parent directory
Path parentDirectoryPath = parentDirectoryFile.toPath();
linkName = "SLFF_LinkTo" + testTargetFile.getName();
testLinkPath = symbolicLinkCreator.apply(parentDirectoryPath.resolve(linkName), testTargetPath);
testLinkFile = testLinkPath.toFile();
targetDirPath = Files.createDirectories(parentDirectoryPath.resolve(DIRECTORY_NAME));
targetDirFile = targetDirPath.toFile();
linkDirPath = symbolicLinkCreator.apply(parentDirectoryPath.resolve(DIRECTORY_LINK_NAME), targetDirPath);
linkDirFile = linkDirPath.toFile();
missingFile = new File(parentDirectoryPath.toFile(), MISSING);
}

@AfterAll
static void tearDown() {
// Fortunately, delete() doesn't throw an exception if the file doesn't exist.
linkDirFile.delete();
targetDirFile.delete();
testLinkFile.delete();
testTargetFile.delete();
}

@Test
public void testFileFilter_HardFile() {
assertFalse(filter.accept(testTargetFile));
}

@Test
public void testFileFilter_Link() {
assertTrue(filter.accept(testLinkFile));
}

@Test
public void testFileFilter_HardDirectory() {
assertFalse(filter.accept(targetDirFile));
}

@Test
public void testFileFilter_PathLink() {
assertTrue(filter.accept(linkDirFile));
}

@Test
public void testFileFilter_missingFile() {
assertFalse(filter.accept(missingFile));
}

@Test
public void testFileNameFilter_HardFile() {
assertFalse(filter.accept(parentDirectoryFile, TARGET_NAME));
}

@Test
public void testFileNameFilter_Link() {
assertTrue(filter.accept(parentDirectoryFile, linkName));
}

@Test
public void testFileNameFilter_HardDirectory() {
assertFalse(filter.accept(parentDirectoryFile, DIRECTORY_NAME));
}

@Test
public void testFileNameFilter_PathLink() {
assertTrue(filter.accept(parentDirectoryFile, DIRECTORY_LINK_NAME));
}

@Test
public void testFileNameFilter_missingFile() {
assertFalse(filter.accept(parentDirectoryFile, MISSING));
}

@Test
public void testPathFilter_HardFile() {
assertEquals(FileVisitResult.TERMINATE, filter.accept(testTargetPath, null));
}

@Test
public void testPathFilter_Link() {
assertEquals(FileVisitResult.CONTINUE, filter.accept(testLinkPath, null));
}
public void testSymbolicLinkFileFilter() {
assertEquals(FileVisitResult.TERMINATE, SymbolicLinkFileFilter.INSTANCE.accept(PathUtils.current(), null));

@Test
public void testPathFilter_HardDirectory() {
assertEquals(FileVisitResult.TERMINATE, filter.accept(targetDirPath, null));
}

@Test
public void testPathFilter_PathLink() {
assertEquals(FileVisitResult.CONTINUE, filter.accept(linkDirPath, null));
}

@Test
public void testPathFilter_missingFile() {
assertEquals(FileVisitResult.TERMINATE, filter.accept(missingFile.toPath(), null));
}
}

0 comments on commit a273627

Please sign in to comment.