ClasspathResource extends AbstractDataResource { public ClasspathResource(String absoluteClasspath) { super(); Objects.requireNonNull(absoluteClasspath, "absoluteClasspath"); if (absoluteClasspath.startsWith("/")) { assert false : "Classpath '" + absoluteClasspath + "' should NOT contain leading slash!"; this.path = absoluteClasspath.substring(1); } else { this.path = absoluteClasspath; } this.url = Thread.currentThread().getContextClassLoader().getResource(absoluteClasspath); } ClasspathResource(String absoluteClasspath);  ClasspathResource(Class<?> someClass, String nameOrSuffix, boolean append);  ClasspathResource(Package somePackage, String filename); @Override String getSchemePrefix(); @Override boolean isData(); @Override String getPath(); @Override URL getUrl(); @Override Date getLastModificationDate(); @Override String getUri(); @Override DataResource navigate(String resourcePath); static final String SCHEME_PREFIX; }
@Test public void testClasspathResource() throws Exception { ClasspathResource resource = new ClasspathResource(ClasspathResource.class, ".txt", true); verifyResource(resource); verifyResource(new ClasspathResource(ClasspathResource.class, ClasspathResource.class.getSimpleName() + ".txt", false)); verifyResource(new ClasspathResource(ClasspathResource.class.getPackage(), ClasspathResource.class.getSimpleName() + ".txt")); String name = ClasspathResource.class.getSimpleName() + ".txt"; String absoluteClasspath = ClasspathResource.class.getPackage().getName().replace('.', '/') + "/" + name; verifyResource(new ClasspathResource(absoluteClasspath)); assertThat(resource.getName()).isEqualTo(name); assertThat(resource.getPath()).isEqualTo(absoluteClasspath); assertThat(resource.getUri()).isEqualTo(ClasspathResource.SCHEME_PREFIX + absoluteClasspath); }