ClasspathScanningResourcesDetector implements PipelineResourcesDetector { @Override public List<String> detect(ClassLoader classLoader) { List<File> classpathContents = classGraph.disableNestedJarScanning().addClassLoader(classLoader).getClasspathFiles(); return classpathContents.stream().map(File::getAbsolutePath).collect(Collectors.toList()); } ClasspathScanningResourcesDetector(ClassGraph classGraph); @Override List<String> detect(ClassLoader classLoader);  }
@Test public void shouldDetectDirectories() throws Exception { File folder = tmpFolder.newFolder("folder1"); ClassLoader classLoader = new URLClassLoader(new URL[] {folder.toURI().toURL()}); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> result = detector.detect(classLoader); assertThat(result, hasItem(containsString(folder.getAbsolutePath()))); }
@Test public void shouldDetectJarFiles() throws Exception { File jarFile = createTestTmpJarFile("test"); ClassLoader classLoader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()}); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> result = detector.detect(classLoader); assertThat(result, hasItem(containsString(jarFile.getAbsolutePath()))); }
@Test public void shouldDetectResourcesInOrderTheyAppearInURLClassLoader() throws Exception { File file1 = createTestTmpJarFile("test1"); File file2 = createTestTmpJarFile("test2"); ClassLoader classLoader = new URLClassLoader(new URL[] {file1.toURI().toURL(), file2.toURI().toURL()}); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> result = detector.detect(classLoader); assertThat( result, containsInRelativeOrder( containsString(file1.getAbsolutePath()), containsString(file2.getAbsolutePath()))); }
@Test public void shouldNotDetectOrdinaryFiles() throws Exception { File textFile = tmpFolder.newFile("ordinaryTextFile.txt"); ClassLoader classLoader = new URLClassLoader(new URL[] {textFile.toURI().toURL()}); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> result = detector.detect(classLoader); assertThat(result, not(hasItem(containsString(textFile.getAbsolutePath())))); }
@Test public void shouldDetectClassPathResourceFromJavaClassPathEnvVariable() throws IOException { String path = tmpFolder.newFolder("folder").getAbsolutePath(); System.setProperty("java.class.path", path); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> resources = detector.detect(null); assertThat(resources, hasItems(containsString(path))); }
@Test public void shouldNotDetectClassPathResourceThatIsNotAFile() throws Exception { String url = "http: ClassLoader classLoader = new URLClassLoader(new URL[] {new URL(url)}); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> result = detector.detect(classLoader); assertThat(result, not(hasItem(containsString(url)))); }
@Test public void shouldStillDetectResourcesEvenIfClassloaderIsUseless() { ClassLoader uselessClassLoader = Mockito.mock(ClassLoader.class); ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph()); List<String> detectedResources = detector.detect(uselessClassLoader); assertFalse(detectedResources.isEmpty()); }