FileLocator { @CheckForNull InputFile getInputFile(String filePath) { String sanitizedPath = PathUtils.sanitize(filePath); if (sanitizedPath == null) { return null; } String[] pathElements = sanitizedPath.split("/"); return tree.getFileWithSuffix(pathElements); } FileLocator(Iterable<InputFile> inputFiles);   }
@Test public void should_normalize_paths() { InputFile inputFile = new TestInputFileBuilder("module1", "src/main/java/org/sonar/test/File.java").build(); FileLocator locator = new FileLocator(singletonList(inputFile)); assertThat(locator.getInputFile("./org/sonar/test/File.java")).isEqualTo(inputFile); assertThat(locator.getInputFile("./org assertThat(locator.getInputFile("./org assertThat(locator.getInputFile(" }
@Test public void should_match_suffix() { InputFile inputFile = new TestInputFileBuilder("module1", "src/main/java/org/sonar/test/File.java").build(); FileLocator locator = new FileLocator(Collections.singleton(inputFile)); assertThat(locator.getInputFile("org/sonar/test/File.java")).isEqualTo(inputFile); }
@Test public void should_not_match() { InputFile inputFile = new TestInputFileBuilder("module1", "src/main/java/org/sonar/test/File.java").build(); FileLocator locator = new FileLocator(Collections.singleton(inputFile)); assertThat(locator.getInputFile("org/sonar/test/File2.java")).isNull(); assertThat(locator.getInputFile("org/sonar/test2/File.java")).isNull(); assertThat(locator.getInputFile("not/found")).isNull(); }
@Test public void should_match_first_with_many_options() { InputFile inputFile1 = new TestInputFileBuilder("module1", "src/main/java/org/sonar/test/File.java").build(); InputFile inputFile2 = new TestInputFileBuilder("module1", "src/test/java/org/sonar/test/File.java").build(); FileLocator locator = new FileLocator(Arrays.asList(inputFile1, inputFile2)); assertThat(locator.getInputFile("org/sonar/test/File.java")).isEqualTo(inputFile1); }