use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.
the class ClassPathTest method doTestExistsThrowsSecurityException.
private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
File file = null;
// In Java 9, Logger may read the TZ database. Only disallow reading the class path URLs.
final PermissionCollection readClassPathFiles = new FilePermission("", "read").newPermissionCollection();
for (URL url : ClassPath.parseJavaClassPath()) {
if (url.getProtocol().equalsIgnoreCase("file")) {
file = new File(url.toURI());
readClassPathFiles.add(new FilePermission(file.getAbsolutePath(), "read"));
}
}
assertThat(file).isNotNull();
SecurityManager disallowFilesSecurityManager = new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (readClassPathFiles.implies(p)) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
file.exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(getClass().getClassLoader());
// ClassPath may contain resources from the boot class loader; just not from the class path.
for (ResourceInfo resource : classPath.getResources()) {
assertThat(resource.getResourceName()).doesNotContain("com/google/common/reflect/");
}
}
use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.
the class ClassPathTest method testScanDirectory_symlinkToRootCycle.
// Path (for symlink creation)
@AndroidIncompatible
public void testScanDirectory_symlinkToRootCycle() throws IOException {
ClassLoader loader = ClassPathTest.class.getClassLoader();
// directory with a cycle,
// /root
// /child
// /[grandchild -> root]
java.nio.file.Path root = createTempDirectory("ClassPathTest");
try {
createFile(root.resolve("some.txt"));
java.nio.file.Path child = createDirectory(root.resolve("child"));
createSymbolicLink(child.resolve("grandchild"), root);
assertEquals(ImmutableSet.of(new ResourceInfo(FILE, "some.txt", loader)), new ClassPath.LocationInfo(root.toFile(), loader).scanResources());
} finally {
deleteRecursivelyOrLog(root);
}
}
use of com.google.common.reflect.ClassPath.ResourceInfo in project guava by google.
the class ClassPathTest method testScanDirectory_symlinkCycle.
// Path (for symlink creation)
@AndroidIncompatible
public void testScanDirectory_symlinkCycle() throws IOException {
ClassLoader loader = ClassPathTest.class.getClassLoader();
// directory with a cycle,
// /root
// /left
// /[sibling -> right]
// /right
// /[sibling -> left]
java.nio.file.Path root = createTempDirectory("ClassPathTest");
try {
java.nio.file.Path left = createDirectory(root.resolve("left"));
createFile(left.resolve("some.txt"));
java.nio.file.Path right = createDirectory(root.resolve("right"));
createFile(right.resolve("another.txt"));
createSymbolicLink(left.resolve("sibling"), right);
createSymbolicLink(right.resolve("sibling"), left);
assertEquals(ImmutableSet.of(new ResourceInfo(FILE, "left/some.txt", loader), new ResourceInfo(FILE, "left/sibling/another.txt", loader), new ResourceInfo(FILE, "right/another.txt", loader), new ResourceInfo(FILE, "right/sibling/some.txt", loader)), new ClassPath.LocationInfo(root.toFile(), loader).scanResources());
} finally {
deleteRecursivelyOrLog(root);
}
}
Aggregations