private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
URL[] urls = myLoader.getURLs();
ImmutableList.Builder<File> filesBuilder = ImmutableList.builder();
for (URL url : urls) {
if (url.getProtocol().equalsIgnoreCase("file")) {
filesBuilder.add(new File(url.toURI()));
}
}
ImmutableList<File> files = filesBuilder.build();
assertThat(files).isNotEmpty();
SecurityManager disallowFilesSecurityManager = new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (p instanceof FilePermission) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
files.get(0).exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(myLoader);
assertThat(classPath.getResources()).isEmpty();
}
ClassPathTest.java 文件源码
java
阅读 25
收藏 0
点赞 0
评论 0
项目:guava-mock
作者:
评论列表
文章目录