-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
junit5: add property to allow SecurityManager override in tests (#217)
- Loading branch information
Showing
4 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
java/test/com/github/bazel_contrib/contrib_rules_jvm/junit5/SecurityManagerSettingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.bazel_contrib.contrib_rules_jvm.junit5; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.security.Permission; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_THROWABLE") | ||
public class SecurityManagerSettingTest { | ||
|
||
private static final String ALLOW_SETTING_SECURITY_MANAGER_PROPERTY = | ||
"bazel.junit5runner.allowSettingSecurityManager"; | ||
|
||
@Test | ||
void testCanSetSecurityManagerWhenPropertyIsTrue() { | ||
System.setProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY, "true"); | ||
SecurityManager originalSecurityManager = System.getSecurityManager(); | ||
SecurityManager testSecurityManager = | ||
new SecurityManager() { | ||
@Override | ||
public void checkPermission(Permission perm) {} | ||
}; | ||
|
||
try { | ||
System.setSecurityManager(testSecurityManager); | ||
assertEquals(testSecurityManager, System.getSecurityManager()); | ||
} finally { | ||
System.setSecurityManager(originalSecurityManager); | ||
System.clearProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY); | ||
} | ||
} | ||
|
||
@Test | ||
void testCannotSetSecurityManagerWhenPropertyIsNotSet() { | ||
assertNull(System.getProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY)); | ||
assertThrows(SecurityException.class, () -> System.setSecurityManager(new SecurityManager())); | ||
} | ||
} |