ScannotationComponentScanner implements ComponentScanner { public Collection<String> scan(ClasspathResolver resolver) { final URL webInfClasses = resolver.findWebInfClassesLocation(); final List<String> basePackages = resolver.findBasePackages(); HashSet<String> results = new HashSet<String>(); Map<String, Set<String>> webInfClassesAnnotationMap = scanWebInfClasses(webInfClasses); Map<String, Set<String>> basePackagesAnnotationMap = scanBasePackages(basePackages, resolver); Set<String> stereotypeNames = findStereotypes(webInfClassesAnnotationMap, basePackagesAnnotationMap, basePackages); findComponentsFromWebInfClasses(webInfClassesAnnotationMap, stereotypeNames, results); findComponentsFromBasePackages(basePackagesAnnotationMap, basePackages, results); return results; }  Collection<String> scan(ClasspathResolver resolver);  }
@Test public void shouldScanWEBINFClasses() { when(classPathResolver.findBasePackages()).thenReturn(Arrays.asList("")); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); when(classPathResolver.getClassLoader()).thenReturn(classLoader); when(classPathResolver.findWebInfClassesLocation()).thenReturn(classLoader.getResource("br/com/caelum/vraptor/ioc/")); ScannotationComponentScanner scanner = new ScannotationComponentScanner(); assertThat(scanner.scan(classPathResolver), hasItem(ResourceInTheClasspath.class.getName())); }
@Test public void shouldScanBasePackages() { ClassLoader classLoader = new URLClassLoader(new URL[] {ScannotationComponentScannerTest.class.getResource("/test-fixture.jar")}); when(classPathResolver.findBasePackages()).thenReturn(Arrays.asList("br.com.caelum.vraptor.ioc")); when(classPathResolver.getClassLoader()).thenReturn(classLoader); when(classPathResolver.findWebInfClassesLocation()).thenReturn(classLoader.getResource("br/com/caelum/vraptor/test/")); ScannotationComponentScanner scanner = new ScannotationComponentScanner(); Collection<String> classes = scanner.scan(classPathResolver); assertThat(classes, hasItem(ResourceInTheClasspath.class.getName())); }
@Test public void shouldReturnEmptyCollectionIfWebinfClassesNotFound() throws Exception { URL webinfLocation = new URL("file:/a/url/that/not-found"); when(classPathResolver.findWebInfClassesLocation()).thenReturn(webinfLocation); ScannotationComponentScanner scanner = new ScannotationComponentScanner(); assertThat(scanner.scan(classPathResolver), hasSize(0)); }
@Test(expected=ScannerException.class) public void shouldThrowScannerExceptionIfHasExceptionWhenProcessWebinfClasses() throws Exception { URL webinfLocation = new URL("file: when(classPathResolver.findWebInfClassesLocation()).thenReturn(webinfLocation); ScannotationComponentScanner scanner = new ScannotationComponentScanner(); assertThat(scanner.scan(classPathResolver), hasSize(0)); }