TestBase.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package de.jplag;
  2. import java.io.File;
  3. import java.nio.file.Path;
  4. import java.util.List;
  5. import java.util.StringJoiner;
  6. import java.util.function.Function;
  7. import java.util.stream.Collectors;
  8. import de.jplag.exceptions.ExitException;
  9. import de.jplag.java.Language;
  10. import de.jplag.options.JPlagOptions;
  11. public abstract class TestBase {
  12. protected static final String BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "samples").toString();
  13. protected static final double DELTA = 0.001;
  14. protected String getBasePath() {
  15. return BASE_PATH;
  16. }
  17. protected String getBasePath(String... subs) {
  18. StringJoiner joiner = new StringJoiner(File.separator);
  19. joiner.add(BASE_PATH);
  20. for (String sub : subs) {
  21. joiner.add(sub);
  22. }
  23. return joiner.toString();
  24. }
  25. protected JPlagResult runJPlagWithExclusionFile(String testSampleName, String exclusionFileName) throws ExitException {
  26. String blackList = Path.of(BASE_PATH, testSampleName, exclusionFileName).toString();
  27. return runJPlag(testSampleName, options -> options.withExclusionFileName(blackList));
  28. }
  29. protected JPlagResult runJPlagWithDefaultOptions(String testSampleName) throws ExitException {
  30. return runJPlag(testSampleName, options -> options);
  31. }
  32. protected JPlagResult runJPlag(String testSampleName, Function<JPlagOptions, JPlagOptions> customization) throws ExitException {
  33. return runJPlag(List.of(getBasePath(testSampleName)), List.of(), customization);
  34. }
  35. protected JPlagResult runJPlag(List<String> newPaths, Function<JPlagOptions, JPlagOptions> customization) throws ExitException {
  36. return runJPlag(newPaths, List.of(), customization);
  37. }
  38. protected JPlagResult runJPlag(List<String> newPaths, List<String> oldPaths, Function<JPlagOptions, JPlagOptions> customization)
  39. throws ExitException {
  40. var newFiles = newPaths.stream().map(path -> new File(path)).collect(Collectors.toSet());
  41. var oldFiles = oldPaths.stream().map(path -> new File(path)).collect(Collectors.toSet());
  42. JPlagOptions options = new JPlagOptions(LanguageLoader.getLanguage(Language.IDENTIFIER).orElseThrow(), newFiles, oldFiles);
  43. options = customization.apply(options);
  44. JPlag jplag = new JPlag(options);
  45. return jplag.run();
  46. }
  47. }