TestBase.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 de.jplag.exceptions.ExitException;
  8. import de.jplag.java.Language;
  9. import de.jplag.options.JPlagOptions;
  10. import de.jplag.options.Verbosity;
  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. JPlagOptions options = new JPlagOptions(LanguageLoader.getLanguage(Language.IDENTIFIER).orElseThrow(), newPaths, oldPaths);
  41. options = customization.apply(options);
  42. options = options.withVerbosity(Verbosity.LONG);
  43. JPlag jplag = new JPlag(options);
  44. return jplag.run();
  45. }
  46. }