Skip to content

Commit 1442467

Browse files
committed
JAVA-2599: Handle Windows specific exception in the ExistenceUnitTest
1 parent cfb549b commit 1442467

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

core-java-modules/core-java-io-3/src/test/java/com/baeldung/existence/ExistenceUnitTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
import java.io.File;
66
import java.io.IOException;
7-
import java.nio.file.Files;
8-
import java.nio.file.LinkOption;
9-
import java.nio.file.Path;
10-
import java.nio.file.Paths;
7+
import java.nio.file.*;
118
import java.util.concurrent.ThreadLocalRandom;
129

1310
import static org.junit.Assert.assertFalse;
@@ -47,8 +44,18 @@ public void givenFile_whenExists_thenFilesShouldReturnTrue() throws IOException
4744
public void givenSymbolicLink_whenTargetDoesNotExists_thenFollowOrNotBasedOnTheOptions() throws IOException {
4845
Path target = Files.createTempFile("baeldung", "target");
4946
Path symbol = Paths.get("test-link-" + ThreadLocalRandom.current().nextInt());
47+
Path symbolicLink = null;
48+
49+
try {
50+
symbolicLink = Files.createSymbolicLink(symbol, target);
51+
} catch (FileSystemException ex) {
52+
System.out.println("Your OS security policy prevents the current user from creating symbolic links.\n" +
53+
"Most probably you're running Windows with UAC.\n" +
54+
"If this is the case, please see - https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links\n" +
55+
"You must change your security settings to run this test under Windows.");
56+
return;
57+
}
5058

51-
Path symbolicLink = Files.createSymbolicLink(symbol, target);
5259
assertTrue(Files.exists(symbolicLink));
5360
assertTrue(Files.isSymbolicLink(symbolicLink));
5461
assertFalse(Files.isSymbolicLink(target));

0 commit comments

Comments
 (0)