JarFilesPathConverter implements Converter<String[]> { @Override public boolean getAllPossibleValues(List<Completion> completions, Class<?> targetType, String existingData, String optionContext, MethodTarget target) { int comma = existingData.lastIndexOf(',') + 1; String otherJars = existingData.substring(0, comma); existingData = existingData.substring(comma); List<Completion> allCompletions = new ArrayList<>(); delegate.getAllPossibleValues(allCompletions, targetType, existingData, optionContext, target); completions.addAll(allCompletions.stream() .map(Completion::getValue) .filter(JarFilesPathConverter::isDirOrJar) .map(s -> new Completion(otherJars + s)) .collect(Collectors.toList())); return allAreJars(completions); } JarFilesPathConverter(); void setDelegate(FilePathStringConverter delegate); @Override boolean supports(Class<?> type, String optionContext); @Override String[] convertFromText(String value, Class<?> targetType, String optionContext); @Override boolean getAllPossibleValues(List<Completion> completions, Class<?> targetType,
      String existingData, String optionContext, MethodTarget target);  }
@Test public void itFindsJarDirs() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testOne", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(testDir, "empty", true); createFileOrDir(libDir, "jar_one.jar", false); createFileOrDir(libDir, "jar_two.jar", false); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, testDir.getPath(), null, null); assertThat(completions.size()).isEqualTo(1); assertThat(completions.get(0).getValue()).endsWith("lib"); }
@Test public void itFindsJarFiles() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testOne", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(testDir, "empty", true); createFileOrDir(libDir, "jar_one.jar", false); createFileOrDir(libDir, "jar_two.jar", false); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, libDir.getPath(), null, null); assertThat(completions.size()).isEqualTo(2); assertThat(completions.get(0).getValue()).endsWith(".jar"); }
@Test public void itFindsDirsWithSubdirs() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testTwo", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(libDir, "file.txt", false); File nonEmptyDir = createFileOrDir(testDir, "nonEmpty", true); createFileOrDir(nonEmptyDir, "subDirOne", true); createFileOrDir(nonEmptyDir, "subDirTwo", true); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, "garbage," + testDir.getPath(), null, null); assertThat(completions.size()).isEqualTo(1); assertThat(completions.get(0).getValue()).endsWith("nonEmpty"); assertThat(completions.get(0).getValue()).startsWith("garbage,"); }
@Test public void itFindsDirsWithSubdirsAndJars() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testTwo", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(libDir, "file.jar", false); File nonEmptyDir = createFileOrDir(testDir, "nonEmpty", true); createFileOrDir(nonEmptyDir, "subDirOne", true); createFileOrDir(nonEmptyDir, "subDirTwo", true); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, testDir.getPath(), null, null); assertThat(completions).hasSize(2); assertThat(completions).containsExactlyInAnyOrder(new Completion(nonEmptyDir.getPath()), new Completion(libDir.getPath())); }
@Test public void itFindsNothingWithBadSearch() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testTwo", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(libDir, "file.txt", false); File nonEmptyDir = createFileOrDir(testDir, "nonEmpty", true); createFileOrDir(nonEmptyDir, "subDirOne", true); createFileOrDir(nonEmptyDir, "subDirTwo", true); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, "garbage", null, null); assertThat(completions).isEmpty(); }
@Test public void itFindsNothing() throws Exception { File testDir = createFileOrDir(tmpDir.getRoot(), "testTwo", true); File libDir = createFileOrDir(testDir, "lib", true); createFileOrDir(libDir, "file.txt", false); createFileOrDir(testDir, "empty", true); JarFilesPathConverter jarFilesPathConverter = new JarFilesPathConverter(); List<Completion> completions = new ArrayList<>(); jarFilesPathConverter.getAllPossibleValues(completions, null, testDir.getPath(), null, null); assertThat(completions.size()).isEqualTo(0); }