DefaultClassScanner implements ClassScanner { @Override public Map<String, Class<?>> scan(ClassLoader loader) { Enumeration<URL> urls; URL url; String tmp; Map<String, Class<?>> parentClasses; File file; URI uri; synchronized (monitor) { if (classes == null) { classes = new TreeMap<String, Class<?>>(); if (parent != null) { if ((parentClasses = parent.scan(loader)) != null) { classes.putAll(parentClasses); } } try { urls = loader.getResources("META-INF"); while (urls.hasMoreElements()) { url = urls.nextElement(); tmp = url.toExternalForm(); tmp = tmp.substring(0, tmp.length() - 8); LOGGER.debug("listClasses - source={}", tmp); try { if (tmp.startsWith("file:")) { uri = new URI(tmp); if (sourceDirFilter == null || sourceDirFilter != null && sourceDirFilter.filter(uri)) { file = new File(uri); traverseFile(file.getAbsolutePath(), file, loader, classes); } } else if (tmp.startsWith("jar:")) { uri = new URI(tmp.substring(4, tmp.length() - 2)); if (sourceDirFilter == null || sourceDirFilter != null && sourceDirFilter.filter(uri)) { file = new File(uri); traverseJar(file.getAbsolutePath(), file.toURI().toURL(), loader, classes); } } } catch (URISyntaxException exception) { LOGGER.debug("error while generating URI", exception); } } } catch (IOException exception) { LOGGER.debug("error while traversing jar", exception); } } } return classes; } DefaultClassScanner();  DefaultClassScanner(ClassScanner parent);  DefaultClassScanner(GenericFilter<URI> sourceDirFilter, GenericFilter<String> classNameFilter);  DefaultClassScanner(ClassScanner parent, GenericFilter<URI> sourceDirFilter, GenericFilter<String> classNameFilter); @Override Map<String, Class<?>> scan(ClassLoader loader);  }
@Test public void scanWithoutParentAndFilter() { Map<String, Class<?>> classes = defaultClassScanner.scan(Thread.currentThread().getContextClassLoader()); verifyZeroInteractions(parent); verifyZeroInteractions(classNameFilter); verifyZeroInteractions(sourceDirFilter); assertTrue(classes.containsKey(Test.class.getName())); }
@Test public void scanWithoutFilter() { defaultClassScanner = new DefaultClassScanner(parent); Map<String, Class<?>> classes = defaultClassScanner.scan(Thread.currentThread().getContextClassLoader()); verifyZeroInteractions(classNameFilter); verifyZeroInteractions(sourceDirFilter); assertTrue(classes.containsKey(Test.class.getName())); verify(parent).scan(Thread.currentThread().getContextClassLoader()); }
@Test public void scanWithoutParent() throws Exception { boolean scannedJUnit = false; boolean scannedTest = false; defaultClassScanner = new DefaultClassScanner(sourceDirFilter, classNameFilter); Map<String, Class<?>> classes = defaultClassScanner.scan(Thread.currentThread().getContextClassLoader()); verifyZeroInteractions(parent); assertTrue(classes.containsKey(Test.class.getName())); verify(sourceDirFilter, atLeast(1)).filter(sourceDirFilterParameter.capture()); verify(classNameFilter, atLeast(1)).filter(classNameFilterParameter.capture()); for (URI uri : sourceDirFilterParameter.getAllValues()) { if (uri.toURL().toExternalForm().matches(".*junit-.*\\.jar")) { scannedJUnit = true; break; } } if (!scannedJUnit) { fail(); } for (String name : classNameFilterParameter.getAllValues()) { if (name.equals(Test.class.getName())) { scannedTest = true; break; } } if (!scannedTest) { fail(); } }
@Test public void scanWithParentAndFilter() throws Exception { boolean scannedJUnit = false; boolean scannedTest = false; defaultClassScanner = new DefaultClassScanner(parent, sourceDirFilter, classNameFilter); Map<String, Class<?>> classes = defaultClassScanner.scan(Thread.currentThread().getContextClassLoader()); assertTrue(classes.containsKey(Test.class.getName())); verify(parent).scan(Thread.currentThread().getContextClassLoader()); verify(sourceDirFilter, atLeast(1)).filter(sourceDirFilterParameter.capture()); verify(classNameFilter, atLeast(1)).filter(classNameFilterParameter.capture()); for (URI uri : sourceDirFilterParameter.getAllValues()) { if (uri.toURL().toExternalForm().matches(".*junit-.*\\.jar")) { scannedJUnit = true; break; } } if (!scannedJUnit) { fail(); } for (String name : classNameFilterParameter.getAllValues()) { if (name.equals(Test.class.getName())) { scannedTest = true; break; } } if (!scannedTest) { fail(); } }