ResourceLocator { public static URL findNamedResource(final String name) { URI nameUri = URI.create(name); if (isClasspath(nameUri)) { Iterable<URL> resources = urlClassPathResource(nameUri); return firstElementOrNull(resources); } try { if (nameUri.isAbsolute()) { return nameUri.toURL(); } return new URL("file:" + name); } catch (MalformedURLException e) { throw new IllegalArgumentException(e); } } private  ResourceLocator(); static Iterable<URL> findNamedResources(String name); static URL findNamedResource(final String name);  }
@Test public void classPathSingleResourceExample() { URL url = ResourceLocator.findNamedResource("classpath:dummy-file.txt"); assertThat(url, notNullValue()); }
@Test public void absolutePathExample() throws IOException { Path modulePath = createDirectoryWithAFile("some-module", "module.yaml").resolve("module.yaml"); URL url = ResourceLocator.findNamedResource(modulePath.toUri().toString()); assertThat(url, is(url(modulePath))); }
@Test public void nonAbosultePath() throws MalformedURLException { URL url = ResourceLocator.findNamedResource("/tmp/a.txt"); assertThat(url, is(url("file:/tmp/a.txt"))); }