VolumeTest.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package de.jplag.special;
  2. import static org.junit.jupiter.api.Assertions.assertEquals;
  3. import static org.junit.jupiter.api.Assertions.assertTrue;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.junit.jupiter.api.Disabled;
  12. import org.junit.jupiter.api.Test;
  13. import de.jplag.TestBase;
  14. import de.jplag.exceptions.ExitException;
  15. import de.jplag.options.JPlagOptions;
  16. /**
  17. * These tests are not intended to be used automatically but rather manually prior to releases. These tests shall be
  18. * used with large data sets (e.g., 100+ submissions) in order to validate the core functionality.
  19. */
  20. public class VolumeTest extends TestBase {
  21. /**
  22. * This path ensures that volume testing data is not in the git repository (with default configuration)
  23. */
  24. private static final String VOLUME_BASE_PATH = "../../volume";
  25. @Override
  26. protected String getBasePath() {
  27. return VOLUME_BASE_PATH;
  28. }
  29. /**
  30. * This test requires a folder "data" with submissions and a file named "matches_avg.csv" inside the volume folder.
  31. * Accepts a derivation of 0.1% in the matching percentage
  32. */
  33. @Test
  34. @Disabled
  35. public void volumeComparisonTest() throws ExitException, IOException {
  36. // Always succeed if not executed in an appropriate environment
  37. if (!new File(this.getBasePath()).exists()) {
  38. return;
  39. }
  40. var results = runJPlag("data", jPlagOptions -> jPlagOptions.setMaximumNumberOfComparisons(-1));
  41. var csv = readCSVResults(String.format("%s/%s", this.getBasePath(), "matches_avg.csv"));
  42. assertEquals(csv.size(), results.getAllComparisons().size());
  43. System.out.println("Volume test size: " + csv.size());
  44. results.getAllComparisons().forEach(result -> {
  45. var key = result.getFirstSubmission().getName() + result.getSecondSubmission().getName();
  46. assertTrue(csv.containsKey(key));
  47. assertEquals(csv.getOrDefault(key, -1f), result.similarity(), DELTA);
  48. });
  49. }
  50. private Map<String, Float> readCSVResults(String filePathAndName) throws IOException {
  51. List<String> lines = Files.readAllLines(Path.of(filePathAndName), JPlagOptions.CHARSET);
  52. var results = new HashMap<String, Float>();
  53. lines.forEach(line -> {
  54. var entries = line.split(";");
  55. if (entries.length != 4) {
  56. throw new IllegalArgumentException(String.format("Illegal line: '%s'", line));
  57. }
  58. results.put(entries[1] + entries[2], Float.parseFloat((entries[3])));
  59. });
  60. return results;
  61. }
  62. }