Created
April 15, 2020 09:41
-
-
Save vietj/ef0bd3e56896a8dfbfbacab23036f752 to your computer and use it in GitHub Desktop.
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
@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