ZuulFiltersModule extends AbstractModule { @VisibleForTesting String[] findClassNames(AbstractConfiguration config) { String filterClassNamesStr = config.getString("zuul.filters.classes", "").trim(); Stream<String> classNameStream = Pattern.compile(",") .splitAsStream(filterClassNamesStr) .map(String::trim) .filter(blank.negate()); String packageNamesStr = config.getString("zuul.filters.packages", "").trim(); ClassPath cp; try { cp = ClassPath.from(this.getClass().getClassLoader()); } catch (IOException e) { throw new RuntimeException("Error attempting to read classpath to find filters!", e); } Stream<String> packageStream = Pattern.compile(",") .splitAsStream(packageNamesStr) .map(String::trim) .filter(blank.negate()) .flatMap( packageName -> cp.getTopLevelClasses(packageName).stream()) .map(ClassPath.ClassInfo::load) .filter(ZuulFilter.class::isAssignableFrom) .map(Class::getCanonicalName); String[] filterClassNames = Stream.concat(classNameStream, packageStream).toArray(String[]::new); if (filterClassNames.length != 0) { LOG.info("Using filter classnames: "); for (String location : filterClassNames) { LOG.info(" " + location); } } return filterClassNames; }    }
@Test public void testEmptyClassNames() { when(configuration.getString(eq("zuul.filters.classes"), anyString())).thenReturn(" "); when(configuration.getString(eq("zuul.filters.packages"), anyString())).thenReturn(" "); String[] classNames = module.findClassNames(configuration); assertThat(classNames.length, equalTo(0)); }
@Test public void testClassNamesOnly() { Class expectedClass = TestZuulFilter.class; when(configuration.getString(eq("zuul.filters.classes"), anyString())).thenReturn("com.netflix.zuul.init.TestZuulFilter"); when(configuration.getString(eq("zuul.filters.packages"), anyString())).thenReturn(""); String[] classNames = module.findClassNames(configuration); assertThat(classNames.length, equalTo(1)); assertThat(classNames[0], equalTo(expectedClass.getCanonicalName())); }
@Test public void testClassNamesPackagesOnly() { Class expectedClass = TestZuulFilter.class; when(configuration.getString(eq("zuul.filters.classes"), anyString())).thenReturn(""); when(configuration.getString(eq("zuul.filters.packages"), anyString())).thenReturn("com.netflix.zuul.init"); String[] classNames = module.findClassNames(configuration); assertThat(classNames.length, equalTo(1)); assertThat(classNames[0], equalTo(expectedClass.getCanonicalName())); }