Skip to content

Instantly share code, notes, and snippets.

@vietj
Created April 15, 2020 09:41
Show Gist options
  • Save vietj/ef0bd3e56896a8dfbfbacab23036f752 to your computer and use it in GitHub Desktop.
Save vietj/ef0bd3e56896a8dfbfbacab23036f752 to your computer and use it in GitHub Desktop.
@Test
public void testResolveAbsoluteFile() throws Exception {
File folder = File.createTempFile("test", ".txt");
assertTrue(folder.delete());
assertTrue(folder.mkdirs());
folder.deleteOnExit(); // Cleanup
File f = new File(folder, "child.txt");
Files.write(f.toPath(), "the_child".getBytes());
File notFound = new File(folder, "not_found");
Thread thread = Thread.currentThread();
ClassLoader prev = thread.getContextClassLoader();
ClassLoader next = new URLClassLoader(new URL[0], prev) {
@Override
public URL getResource(String name) {
File f = new File(name);
if (f.exists()) {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
fail(e);
}
}
return super.getResource(name);
}
};
thread.setContextClassLoader(next);
try {
File file = resolver.resolveFile(notFound.getAbsolutePath());
assertFalse(file.exists());
} finally {
thread.setContextClassLoader(prev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment