JAVA-2599: Handle Windows specific exception in the ExistenceUnitTest

This commit is contained in:
Krzysiek 2020-09-16 10:11:58 +02:00
parent 4f14873967
commit 989acd5ed7
1 changed files with 12 additions and 5 deletions

View File

@ -4,10 +4,7 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -47,8 +44,18 @@ public class ExistenceUnitTest {
public void givenSymbolicLink_whenTargetDoesNotExists_thenFollowOrNotBasedOnTheOptions() throws IOException { public void givenSymbolicLink_whenTargetDoesNotExists_thenFollowOrNotBasedOnTheOptions() throws IOException {
Path target = Files.createTempFile("baeldung", "target"); Path target = Files.createTempFile("baeldung", "target");
Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt()); Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt());
Path symbolicLink = null;
try {
symbolicLink = Files.createSymbolicLink(symbol, target);
} catch (FileSystemException ex) {
System.out.println("Your OS security policy prevents the current user from creating symbolic links.\n" +
"Most probably you're running Windows with UAC.\n" +
"If this is the case, please see - https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links\n" +
"You must change your security settings to run this test under Windows.");
return;
}
Path symbolicLink = Files.createSymbolicLink(symbol, target);
assertTrue(Files.exists(symbolicLink)); assertTrue(Files.exists(symbolicLink));
assertTrue(Files.isSymbolicLink(symbolicLink)); assertTrue(Files.isSymbolicLink(symbolicLink));
assertFalse(Files.isSymbolicLink(target)); assertFalse(Files.isSymbolicLink(target));