private Function<String, Stream<Reader>> classNameToSourceFileReader( final String fileName) { return className -> { if (className.contains(".")) { final File f = new File(className.replace(".", File.separator)); return locate(f.getParent() + File.separator + fileName); } else { return locate(fileName); } }; }
@Override public Optional<Reader> locate(final Collection<String> classes, final String fileName) { final Stream<Reader> matches = classes.stream().flatMap(classNameToSourceFileReader(fileName)); return matches.findFirst(); }
public SmartSourceLocator(final Collection<File> roots) { final Collection<File> childDirs = FCollection.flatMap(roots, collectChildren(0)); childDirs.addAll(roots); final Function<File, SourceLocator> fileToSourceLocator = a -> new DirectorySourceLocator(a); this.children = FCollection.map(childDirs, fileToSourceLocator); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); this.root = new File("."); this.testee = new DirectorySourceLocator(this.root, this.locator); when(this.locator.apply(any(File.class))) .thenReturn(Optional.<Reader> empty()); }
@Test public void shouldLocateSourceForClassesInNamedPacakges() { this.testee .locate(Collections.singletonList("com.example.Foo"), "Foo.java"); final File expected = new File(this.root + File.separator + "com" + File.separator + "example" + File.separator + "Foo.java"); verify(this.locator).apply(expected); } }
@Test public void shouldLocateSourceForClassesInDefaultPackage() { this.testee.locate(Collections.singletonList("Foo"), "Foo.java"); final File expected = new File(this.root + File.separator + "Foo.java"); verify(this.locator).apply(expected); }