Browse Source

Adapt base code test cases for new expected behavior and enforce system-independent paths in tests.

Timur Sağlam 5 years ago
parent
commit
cd20bb1e5a

+ 8 - 19
jplag/src/test/java/de/jplag/BaseCodeTest.java

@@ -10,21 +10,20 @@ public class BaseCodeTest extends TestBase {
 
 
     @Test
     @Test
     public void testBasecodeComparison() throws ExitException {
     public void testBasecodeComparison() throws ExitException {
-        checkResult(runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base")));
+        JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base"));
+        verifyResults(result);
     }
     }
-    
+
     @Test
     @Test
-    public void testWeirdPath() throws ExitException {
-        String weirdPath = "." + File.separator + ".." + File.separator + "basecode" + File.separator + "base";
-        checkResult(runJPlag("basecode", it -> it.setBaseCodeSubmissionName(weirdPath)));
+    public void testAutoTrimFileSeparators() throws ExitException {
+        JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName(File.separator + "base" + File.separator));
+        verifyResults(result);
     }
     }
 
 
-
-    private void checkResult(JPlagResult result) {
+    private void verifyResults(JPlagResult result) {
         assertEquals(2, result.getNumberOfSubmissions());
         assertEquals(2, result.getNumberOfSubmissions());
         assertEquals(1, result.getComparisons().size());
         assertEquals(1, result.getComparisons().size());
         assertEquals(1, result.getComparisons().get(0).getMatches().size());
         assertEquals(1, result.getComparisons().get(0).getMatches().size());
-
         assertEquals(1, result.getSimilarityDistribution()[8]);
         assertEquals(1, result.getSimilarityDistribution()[8]);
         assertEquals(85f, result.getComparisons().get(0).similarity(), DELTA);
         assertEquals(85f, result.getComparisons().get(0).similarity(), DELTA);
     }
     }
@@ -40,17 +39,7 @@ public class BaseCodeTest extends TestBase {
     }
     }
 
 
     @Test(expected = ExitException.class)
     @Test(expected = ExitException.class)
-    public void testInvalidBasecode2() throws ExitException {
+    public void testBasecodeWithDots() throws ExitException {
         runJPlag("basecode", it -> it.setBaseCodeSubmissionName("." + File.separator + "base"));
         runJPlag("basecode", it -> it.setBaseCodeSubmissionName("." + File.separator + "base"));
     }
     }
-
-    @Test(expected = ExitException.class)
-    public void testInvalidBasecode3() throws ExitException {
-        runJPlag("basecode", it -> it.setBaseCodeSubmissionName(".." + File.separator + "base"));
-    }
-
-    @Test(expected = ExitException.class)
-    public void testInvalidBasecode4() throws ExitException {
-        runJPlag("basecode", it -> it.setBaseCodeSubmissionName(File.separator + "base" + File.separator));
-    }
 }
 }

+ 7 - 5
jplag/src/test/java/de/jplag/TestBase.java

@@ -1,5 +1,6 @@
 package de.jplag;
 package de.jplag;
 
 
+import java.nio.file.Path;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
 import de.jplag.options.JPlagOptions;
 import de.jplag.options.JPlagOptions;
@@ -8,24 +9,25 @@ import de.jplag.options.Verbosity;
 
 
 public abstract class TestBase {
 public abstract class TestBase {
 
 
-    private static final String BASE_PATH = "src/test/resources/de/jplag/samples";
+    private static final String BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "samples").toString();
     protected static final float DELTA = 0.1f;
     protected static final float DELTA = 0.1f;
-    
+
     protected String getBasePath() {
     protected String getBasePath() {
         return BASE_PATH;
         return BASE_PATH;
     }
     }
 
 
     protected JPlagResult runJPlagWithExclusionFile(String testSampleName, String exclusionFileName) throws ExitException {
     protected JPlagResult runJPlagWithExclusionFile(String testSampleName, String exclusionFileName) throws ExitException {
-        String blackList = String.format(getBasePath() + "/%s/%s", testSampleName, exclusionFileName);
+        String blackList = Path.of(BASE_PATH, testSampleName, exclusionFileName).toString();
         return runJPlag(testSampleName, options -> options.setExclusionFileName(blackList));
         return runJPlag(testSampleName, options -> options.setExclusionFileName(blackList));
     }
     }
 
 
     protected JPlagResult runJPlagWithDefaultOptions(String testSampleName) throws ExitException {
     protected JPlagResult runJPlagWithDefaultOptions(String testSampleName) throws ExitException {
-        return runJPlag(testSampleName, options -> {});
+        return runJPlag(testSampleName, options -> {
+        });
     }
     }
 
 
     protected JPlagResult runJPlag(String testSampleName, Consumer<JPlagOptions> customization) throws ExitException {
     protected JPlagResult runJPlag(String testSampleName, Consumer<JPlagOptions> customization) throws ExitException {
-        JPlagOptions options = new JPlagOptions(String.format(getBasePath() + "/%s", testSampleName), LanguageOption.JAVA_1_9);
+        JPlagOptions options = new JPlagOptions(Path.of(BASE_PATH, testSampleName).toString(), LanguageOption.JAVA_1_9);
         options.setVerbosity(Verbosity.LONG);
         options.setVerbosity(Verbosity.LONG);
         customization.accept(options);
         customization.accept(options);
         JPlag jplag = new JPlag(options);
         JPlag jplag = new JPlag(options);