Procházet zdrojové kódy

Add global basecode option (#255)

* Get options instance from the class itself.
* Give access to the root path of a submission.
* Change -bc option to global path with fallback.

- Submission set builder tries to interpret the -bc option as a global
  path first, and falls back to an entry in a submission root directory.
- Afterwards a regular submission that is also used as the base code is
  dropped as regular submission.

- JPlagOptions stores baseCodeSubmissionName as Optional<String>.

* Adapt and extend tests.

- Don't work relative to root directory if considering the base
  option as a path.

- Java doesn't support relative paths so prefix basepath option
  with the full resource path.
- Java doesn't support dots in names, making a test with it impossible.
Alberth289346 před 4 roky
rodič
revize
96d20b65be

+ 16 - 2
jplag/src/main/java/de/jplag/Submission.java

@@ -13,7 +13,9 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collection;
 import java.util.List;
 import java.util.List;
 
 
+import de.jplag.exceptions.ExitException;
 import de.jplag.exceptions.ReportGenerationException;
 import de.jplag.exceptions.ReportGenerationException;
+import de.jplag.exceptions.SubmissionException;
 import de.jplag.options.JPlagOptions;
 import de.jplag.options.JPlagOptions;
 
 
 /**
 /**
@@ -88,6 +90,18 @@ public class Submission implements Comparable<Submission> {
         return name;
         return name;
     }
     }
 
 
+    public File getRoot() {
+        return submissionRoot;
+    }
+
+    public File getCanonicalRoot() throws ExitException {
+        try {
+            return submissionRoot.getCanonicalFile();
+        } catch (IOException e) {
+            throw new SubmissionException(String.format("Cannot compute canonical file path of \"%s\".", submissionRoot.toString()));
+        }
+    }
+
     /**
     /**
      * @return Number of tokens in the parse result.
      * @return Number of tokens in the parse result.
      */
      */
@@ -300,11 +314,11 @@ public class Submission implements Comparable<Submission> {
             }
             }
         }
         }
     }
     }
-    
+
     private File createSubdirectory(File parent, String... subdirectoryNames) {
     private File createSubdirectory(File parent, String... subdirectoryNames) {
         File subdirectory = parent;
         File subdirectory = parent;
         for (String name : subdirectoryNames) {
         for (String name : subdirectoryNames) {
-            subdirectory = new File(subdirectory, name); 
+            subdirectory = new File(subdirectory, name);
         }
         }
         if (!subdirectory.exists()) {
         if (!subdirectory.exists()) {
             subdirectory.mkdirs();
             subdirectory.mkdirs();

+ 129 - 39
jplag/src/main/java/de/jplag/SubmissionSetBuilder.java

@@ -5,9 +5,11 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
@@ -57,20 +59,35 @@ public class SubmissionSetBuilder {
         // Extract the basecode submission if necessary.
         // Extract the basecode submission if necessary.
         Optional<Submission> baseCodeSubmission = Optional.empty();
         Optional<Submission> baseCodeSubmission = Optional.empty();
         if (options.hasBaseCode()) {
         if (options.hasBaseCode()) {
-            verifyBaseCodeName();
-
-            String baseCodeName = options.getBaseCodeSubmissionName();
-            String baseCodePath = rootDirectory.toString() + File.separator + baseCodeName;
-
-            // Grab the basecode submission and erase it from the regular submissions.
-            Submission foundBaseCodeSubmission = foundSubmissions.get(baseCodeName);
-            if (foundBaseCodeSubmission == null) {
-                throw new BasecodeException("Basecode submission \"" + baseCodePath + "\" doesn't exist!");
+            Submission baseCode = tryLoadBaseCodeAsPath();
+            if (baseCode == null) {
+                baseCode = tryLoadBaseCodeAsRootSubDirectory(foundSubmissions);
+
+                String baseCodeName = options.getBaseCodeSubmissionName().get();
+                if (baseCode == null) {
+                    // No base code found at all, report an error.
+                    throw new BasecodeException(
+                            String.format("Basecode path \"%s\" relative to the working directory could not be found.", baseCodeName));
+                } else {
+                    // Found a base code as a submission, report about obsolete usage.
+                    System.out.printf("Deprecated use of the -bc option found, please specify the basecode as \"%s%s%s\" instead.\n",
+                            rootDirectory.toString(), File.separator, baseCodeName);
+                }
+            }
+            baseCodeSubmission = Optional.of(baseCode);
+            System.out.println(String.format("Basecode directory \"%s\" will be used.", baseCode.getRoot().toString()));
+
+            // Basecode may also be registered as a user submission. If so, remove the latter.
+            File baseCodeRoot = baseCode.getCanonicalRoot(); // Use canonical form for a more sane equality notion.
+            Iterator<Entry<String, Submission>> submissionIterator = foundSubmissions.entrySet().iterator();
+            while (submissionIterator.hasNext()) {
+                Entry<String, Submission> entry = submissionIterator.next();
+                if (baseCodeRoot.equals(entry.getValue().getCanonicalRoot())) {
+                    submissionIterator.remove();
+                    System.out.println(String.format("Skipping \"%s\" as user submission.", entry.getValue().getRoot().toString()));
+                    break;
+                }
             }
             }
-
-            foundSubmissions.remove(baseCodeName);
-            baseCodeSubmission = Optional.of(foundBaseCodeSubmission);
-            System.out.println("Basecode directory \"" + baseCodePath + "\" will be used");
         }
         }
 
 
         // Merge everything in a submission set.
         // Merge everything in a submission set.
@@ -92,13 +109,59 @@ public class SubmissionSetBuilder {
     }
     }
 
 
     /**
     /**
-     * Verify the basecode name.
+     * Try to load the basecode under the assumption of being a path.
+     * @return Base code submission if the option value can be interpreted as global path, else {@code null}.
+     * @throws ExitException when the option value is a path with errors.
      */
      */
-    private void verifyBaseCodeName() throws ExitException {
-        String name = options.getBaseCodeSubmissionName();
+    private Submission tryLoadBaseCodeAsPath() throws ExitException {
+        String baseCodeName = options.getBaseCodeSubmissionName().get();
+        File basecodeSubmission = new File(baseCodeName);
+        if (!basecodeSubmission.exists()) {
+            return null;
+        }
+
+        String errorMessage = isExcludedEntry(basecodeSubmission);
+        if (errorMessage != null) {
+            throw new BasecodeException(errorMessage); // Stating an excluded path as basecode isn't very useful.
+        }
+
+        try {
+            return processDirEntry(basecodeSubmission);
+
+        } catch (SubmissionException ex) {
+            throw new BasecodeException(ex.getMessage()); // Change thrown exception to basecode exception.
+
+        } catch (ExitException ex) {
+            throw ex;
+        }
+    }
+
+    /**
+     * Try to load the basecode under the assumption of being a sub-directory in the user submissions directory.
+     * @return Base code submission if the option value can be interpreted as a sub-directory, else {@code null}.
+     * @throws ExitException when the option value is a sub-directory with errors.
+     */
+    private Submission tryLoadBaseCodeAsRootSubDirectory(Map<String, Submission> foundSubmissions) throws ExitException {
+        String baseCodeName = options.getBaseCodeSubmissionName().get();
+
+        // Is the option value a single name after trimming spurious separators?
+        String name = baseCodeName;
+        while (name.startsWith(File.separator)) {
+            name = name.substring(1);
+        }
+        while (name.endsWith(File.separator)) {
+            name = name.substring(0, name.length() - 1);
+        }
+
+        // If it is not a name of a single sub-directory, bail out.
+        if (name.isEmpty() || name.contains(File.separator)) return null;
+
         if (name.contains(".")) {
         if (name.contains(".")) {
             throw new BasecodeException("The basecode directory name \"" + name + "\" cannot contain dots!");
             throw new BasecodeException("The basecode directory name \"" + name + "\" cannot contain dots!");
         }
         }
+
+        // Grab the basecode submission from the regular submissions.
+        return foundSubmissions.get(name);
     }
     }
 
 
     /**
     /**
@@ -125,6 +188,51 @@ public class SubmissionSetBuilder {
         return fileNames;
         return fileNames;
     }
     }
 
 
+    /**
+     * Check that the given submission entry is not invalid due to exclusion names or bad suffix.
+     * @param submissionEntry Entry to check.
+     * @return Error message if the entry should be ignored.
+     */
+    private String isExcludedEntry(File submissionEntry) {
+        if (isFileExcluded(submissionEntry)) {
+            return "Exclude submission: " + submissionEntry.getName();
+        }
+
+        if (submissionEntry.isFile() && !hasValidSuffix(submissionEntry)) {
+            return "Ignore submission with invalid suffix: " + submissionEntry.getName();
+        }
+        return null;
+    }
+
+    /**
+     * Process the given directory entry as a submission root, the path MUST not be excluded.
+     * @param submissionEntry Entry to process.
+     * @return The entry converted to a submission.
+     * @throws ExitException when an error has been found with the entry.
+     */
+    private Submission processDirEntry(File submissionEntry) throws ExitException {
+        if (isExcludedEntry(submissionEntry) != null) {
+            throw new AssertionError("Pre-condition of non-exclusion is violated.");
+        }
+
+        String fileName = submissionEntry.getName();
+        if (submissionEntry.isDirectory() && options.getSubdirectoryName() != null) {
+            // Use subdirectory instead
+            submissionEntry = new File(submissionEntry, options.getSubdirectoryName());
+
+            if (!submissionEntry.exists()) {
+                throw new SubmissionException(
+                        String.format("Submission %s does not contain the given subdirectory '%s'", fileName, options.getSubdirectoryName()));
+            }
+
+            if (!submissionEntry.isDirectory()) {
+                throw new SubmissionException(String.format("The given subdirectory '%s' is not a directory!", options.getSubdirectoryName()));
+            }
+        }
+
+        return new Submission(fileName, submissionEntry, parseFilesRecursively(submissionEntry), language, errorCollector);
+    }
+
     /**
     /**
      * Process entries in the root directory to check whether they qualify as submissions.
      * Process entries in the root directory to check whether they qualify as submissions.
      * @param rootDirectory Root directory being examined.
      * @param rootDirectory Root directory being examined.
@@ -137,33 +245,15 @@ public class SubmissionSetBuilder {
         for (String fileName : fileNames) {
         for (String fileName : fileNames) {
             File submissionFile = new File(rootDirectory, fileName);
             File submissionFile = new File(rootDirectory, fileName);
 
 
-            if (isFileExcluded(submissionFile)) {
-                System.out.println("Exclude submission: " + submissionFile.getName());
+            String errorMessage = isExcludedEntry(submissionFile);
+            if (errorMessage != null) {
+                System.out.println(errorMessage);
                 continue;
                 continue;
             }
             }
 
 
-            if (submissionFile.isFile() && !hasValidSuffix(submissionFile)) {
-                System.out.println("Ignore submission with invalid suffix: " + submissionFile.getName());
-                continue;
-            }
-
-            if (submissionFile.isDirectory() && options.getSubdirectoryName() != null) {
-                // Use subdirectory instead
-                submissionFile = new File(submissionFile, options.getSubdirectoryName());
-
-                if (!submissionFile.exists()) {
-                    throw new SubmissionException(
-                            String.format("Submission %s does not contain the given subdirectory '%s'", fileName, options.getSubdirectoryName()));
-                }
-
-                if (!submissionFile.isDirectory()) {
-                    throw new SubmissionException(String.format("The given subdirectory '%s' is not a directory!", options.getSubdirectoryName()));
-                }
-            }
-
-            Submission submission = new Submission(fileName, submissionFile, parseFilesRecursively(submissionFile), language, errorCollector);
-            foundSubmissions.put(fileName, submission);
+            foundSubmissions.put(fileName, processDirEntry(submissionFile));
         }
         }
+
         return foundSubmissions;
         return foundSubmissions;
     }
     }
 
 

+ 21 - 7
jplag/src/main/java/de/jplag/options/JPlagOptions.java

@@ -6,6 +6,7 @@ import java.io.File;
 import java.nio.charset.Charset;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.Collections;
+import java.util.Optional;
 import java.util.Set;
 import java.util.Set;
 
 
 import de.jplag.Language;
 import de.jplag.Language;
@@ -70,7 +71,7 @@ public class JPlagOptions {
      * Name of the file that contains the names of files to exclude from comparison.
      * Name of the file that contains the names of files to exclude from comparison.
      */
      */
     private String exclusionFileName;
     private String exclusionFileName;
-    
+
     /**
     /**
      * Names of the excluded files.
      * Names of the excluded files.
      */
      */
@@ -82,9 +83,19 @@ public class JPlagOptions {
     private String rootDirectoryName;
     private String rootDirectoryName;
 
 
     /**
     /**
-     * Name of the directory which contains the base code.
+     * Path name of the directory containing the base code.
+     * <p>
+     * For backwards compatibility it may also be a directory name inside the root directory.
+     * Condition for the latter is
+     * <ul><li>Specified path does not exist.</li>
+     *     <li>Name has not have a separator character after trimming them from both ends (leaving at least a one-character name).</li>
+     *     <li>A submission with the specified name exists in the root directory.</li>
+     * </ul>
+     * It's an error if a string has been provided but it is neither an existing path nor does it fulfill all the
+     * conditions of the compatibility fallback listed above.
+     * </p>
      */
      */
-    private String baseCodeSubmissionName;
+    private Optional<String> baseCodeSubmissionName = Optional.empty();
 
 
     /**
     /**
      * Example: If the subdirectoryName is 'src', only the code inside submissionDir/src of each submission will be used for
      * Example: If the subdirectoryName is 'src', only the code inside submissionDir/src of each submission will be used for
@@ -110,7 +121,7 @@ public class JPlagOptions {
         this.languageOption = languageOption;
         this.languageOption = languageOption;
     }
     }
 
 
-    public String getBaseCodeSubmissionName() {
+    public Optional<String> getBaseCodeSubmissionName() {
         return baseCodeSubmissionName;
         return baseCodeSubmissionName;
     }
     }
 
 
@@ -167,7 +178,7 @@ public class JPlagOptions {
     }
     }
 
 
     public boolean hasBaseCode() {
     public boolean hasBaseCode() {
-        return this.baseCodeSubmissionName != null;
+        return this.baseCodeSubmissionName.isPresent();
     }
     }
 
 
     public boolean isDebugParser() {
     public boolean isDebugParser() {
@@ -175,8 +186,11 @@ public class JPlagOptions {
     }
     }
 
 
     public void setBaseCodeSubmissionName(String baseCodeSubmissionName) {
     public void setBaseCodeSubmissionName(String baseCodeSubmissionName) {
-        // Trim problematic file separators.
-        this.baseCodeSubmissionName = (baseCodeSubmissionName == null) ? null : baseCodeSubmissionName.replace(File.separator, "");
+        if (baseCodeSubmissionName == null || baseCodeSubmissionName.isEmpty()) {
+            this.baseCodeSubmissionName = Optional.empty();
+        } else {
+            this.baseCodeSubmissionName = Optional.of(baseCodeSubmissionName);
+        }
     }
     }
 
 
     public void setComparisonMode(ComparisonMode comparisonMode) {
     public void setComparisonMode(ComparisonMode comparisonMode) {

+ 19 - 19
jplag/src/main/java/de/jplag/reporting/Report.java

@@ -161,7 +161,7 @@ public class Report { // Mostly legacy code with some minor improvements.
                     + "',3)\" NAME=\"" + i + "\">");
                     + "',3)\" NAME=\"" + i + "\">");
             htmlFile.print(new String(startA.file.getBytes()));
             htmlFile.print(new String(startA.file.getBytes()));
 
 
-            if (result.getOptions().getLanguage().usesIndex()) {
+            if (options.getLanguage().usesIndex()) {
                 htmlFile.print("(" + startA.getIndex() + "-" + endA.getIndex() + ")");
                 htmlFile.print("(" + startA.getIndex() + "-" + endA.getIndex() + ")");
             } else {
             } else {
                 htmlFile.print("(" + startA.getLine() + "-" + endA.getLine() + ")");
                 htmlFile.print("(" + startA.getLine() + "-" + endA.getLine() + ")");
@@ -171,7 +171,7 @@ public class Report { // Mostly legacy code with some minor improvements.
                     + "',3)\" NAME=\"" + i + "\">");
                     + "',3)\" NAME=\"" + i + "\">");
             htmlFile.print(startB.file);
             htmlFile.print(startB.file);
 
 
-            if (result.getOptions().getLanguage().usesIndex()) {
+            if (options.getLanguage().usesIndex()) {
                 htmlFile.print("(" + startB.getIndex() + "-" + endB.getIndex());
                 htmlFile.print("(" + startB.getIndex() + "-" + endB.getIndex());
             } else {
             } else {
                 htmlFile.print("(" + startB.getLine() + "-" + endB.getLine());
                 htmlFile.print("(" + startB.getLine() + "-" + endB.getLine());
@@ -181,7 +181,7 @@ public class Report { // Mostly legacy code with some minor improvements.
                     ")</A><TD ALIGN=center>" + "<FONT COLOR=\"" + comparison.color(match.getLength()) + "\">" + match.getLength() + "</FONT>");
                     ")</A><TD ALIGN=center>" + "<FONT COLOR=\"" + comparison.color(match.getLength()) + "\">" + match.getLength() + "</FONT>");
         }
         }
 
 
-        if (result.getOptions().hasBaseCode()) {
+        if (options.hasBaseCode()) {
             htmlFile.print(
             htmlFile.print(
                     "<TR><TD BGCOLOR=\"#C0C0C0\"><TD>" + msg.getString("AllMatches.Basecode") + " " + comparison.basecodeSimilarityOfFirst() + "%");
                     "<TR><TD BGCOLOR=\"#C0C0C0\"><TD>" + msg.getString("AllMatches.Basecode") + " " + comparison.basecodeSimilarityOfFirst() + "%");
             htmlFile.println("<TD>" + msg.getString("AllMatches.Basecode") + " " + comparison.basecodeSimilarityOfSecond() + "%<TD>&nbsp;");
             htmlFile.println("<TD>" + msg.getString("AllMatches.Basecode") + " " + comparison.basecodeSimilarityOfSecond() + "%<TD>&nbsp;");
@@ -306,7 +306,7 @@ public class Report { // Mostly legacy code with some minor improvements.
             }
             }
         }
         }
 
 
-        if (result.getOptions().hasBaseCode() && comparison.getFirstBaseCodeMatches() != null && comparison.getSecondBaseCodeMatches() != null) {
+        if (options.hasBaseCode() && comparison.getFirstBaseCodeMatches() != null && comparison.getSecondBaseCodeMatches() != null) {
             JPlagComparison baseCodeComparison = comparison.getBaseCodeMatches(j == 0);
             JPlagComparison baseCodeComparison = comparison.getBaseCodeMatches(j == 0);
 
 
             for (Match match : baseCodeComparison.getMatches()) {
             for (Match match : baseCodeComparison.getMatches()) {
@@ -386,18 +386,18 @@ public class Report { // Mostly legacy code with some minor improvements.
             f.println("</center>");
             f.println("</center>");
             f.println("</h3>");
             f.println("</h3>");
             f.println("<HR>");
             f.println("<HR>");
-            if (result.getOptions().getLanguage().isPreformatted()) {
+            if (options.getLanguage().isPreformatted()) {
                 f.println("<PRE>");
                 f.println("<PRE>");
             }
             }
             for (int y = 0; y < text[x].length; y++) {
             for (int y = 0; y < text[x].length; y++) {
                 f.print(text[x][y]);
                 f.print(text[x][y]);
-                if (!result.getOptions().getLanguage().isPreformatted()) {
+                if (!options.getLanguage().isPreformatted()) {
                     f.println("<BR>");
                     f.println("<BR>");
                 } else {
                 } else {
                     f.println();
                     f.println();
                 }
                 }
             }
             }
-            if (result.getOptions().getLanguage().isPreformatted()) {
+            if (options.getLanguage().isPreformatted()) {
                 f.println("</PRE>");
                 f.println("</PRE>");
             }
             }
         }
         }
@@ -437,20 +437,20 @@ public class Report { // Mostly legacy code with some minor improvements.
         htmlFile.println("<TD><H1><BIG>" + title + "</BIG></H1></TD></TR>");
         htmlFile.println("<TD><H1><BIG>" + title + "</BIG></H1></TD></TR>");
 
 
         htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Language") + ":</TD><TD>"
         htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Language") + ":</TD><TD>"
-                + result.getOptions().getLanguageOption().name() + "</TD></TR>");
+                + options.getLanguageOption().name() + "</TD></TR>");
         htmlFile.print("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Submissions") + ":</TD><TD>" + result.getNumberOfSubmissions());
         htmlFile.print("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Submissions") + ":</TD><TD>" + result.getNumberOfSubmissions());
 
 
         htmlFile.println("</TD></TR>");
         htmlFile.println("</TD></TR>");
 
 
-        if (result.getOptions().hasBaseCode()) {
+        if (options.hasBaseCode()) {
             htmlFile.print("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Basecode_submission") + ":</TD>" + "<TD>"
             htmlFile.print("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Basecode_submission") + ":</TD>" + "<TD>"
-                    + result.getOptions().getBaseCodeSubmissionName() + "</TD></TR>");
+                    + options.getBaseCodeSubmissionName().get() + "</TD></TR>");
         }
         }
 
 
         if (options.getMaximumNumberOfComparisons() > 0) {
         if (options.getMaximumNumberOfComparisons() > 0) {
             htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Matches_displayed") + ":</TD>" + "<TD>");
             htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Matches_displayed") + ":</TD>" + "<TD>");
             htmlFile.println(options.getMaximumNumberOfComparisons() + " of " + result.getComparisons().size() + " ("
             htmlFile.println(options.getMaximumNumberOfComparisons() + " of " + result.getComparisons().size() + " ("
-                    + msg.getString("Report.Treshold") + ": " + result.getOptions().getSimilarityThreshold() + "%)<br>");
+                    + msg.getString("Report.Treshold") + ": " + options.getSimilarityThreshold() + "%)<br>");
 
 
             htmlFile.println("</TD></TR>");
             htmlFile.println("</TD></TR>");
         }
         }
@@ -460,10 +460,10 @@ public class Report { // Mostly legacy code with some minor improvements.
         htmlFile.println(
         htmlFile.println(
                 "<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Date") + ":</TD><TD>" + dateFormat.format(new Date()) + "</TD></TR>");
                 "<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Date") + ":</TD><TD>" + dateFormat.format(new Date()) + "</TD></TR>");
         htmlFile.println("<TR BGCOLOR=#aaaaff>" + "<TD><EM>" + msg.getString("Report.Minimum_Match_Length") + "</EM> ("
         htmlFile.println("<TR BGCOLOR=#aaaaff>" + "<TD><EM>" + msg.getString("Report.Minimum_Match_Length") + "</EM> ("
-                + msg.getString("Report.sensitivity") + "):</TD><TD>" + result.getOptions().getMinimumTokenMatch() + "</TD></TR>");
+                + msg.getString("Report.sensitivity") + "):</TD><TD>" + options.getMinimumTokenMatch() + "</TD></TR>");
         htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Suffixes") + ":</TD><TD>");
         htmlFile.println("<TR BGCOLOR=#aaaaff VALIGN=top><TD>" + msg.getString("Report.Suffixes") + ":</TD><TD>");
 
 
-        String[] fileSuffixes = result.getOptions().getFileSuffixes();
+        String[] fileSuffixes = options.getFileSuffixes();
 
 
         for (int i = 0; i < fileSuffixes.length; i++) {
         for (int i = 0; i < fileSuffixes.length; i++) {
             htmlFile.print(fileSuffixes[i] + (i < fileSuffixes.length - 1 ? ", " : "</TD></TR>\n"));
             htmlFile.print(fileSuffixes[i] + (i < fileSuffixes.length - 1 ? ", " : "</TD></TR>\n"));
@@ -619,10 +619,10 @@ public class Report { // Mostly legacy code with some minor improvements.
 
 
         htmlFile.println("  <div style=\"display: flex;\">");
         htmlFile.println("  <div style=\"display: flex;\">");
 
 
-        if (result.getOptions().getLanguage().usesIndex()) {
+        if (options.getLanguage().usesIndex()) {
             writeIndexedSubmission(htmlFile, i, comparison, 0);
             writeIndexedSubmission(htmlFile, i, comparison, 0);
             writeIndexedSubmission(htmlFile, i, comparison, 1);
             writeIndexedSubmission(htmlFile, i, comparison, 1);
-        } else if (result.getOptions().getLanguage().supportsColumns()) {
+        } else if (options.getLanguage().supportsColumns()) {
             writeImprovedSubmission(htmlFile, i, comparison, 0);
             writeImprovedSubmission(htmlFile, i, comparison, 0);
             writeImprovedSubmission(htmlFile, i, comparison, 1);
             writeImprovedSubmission(htmlFile, i, comparison, 1);
         } else {
         } else {
@@ -723,7 +723,7 @@ public class Report { // Mostly legacy code with some minor improvements.
             }
             }
         }
         }
 
 
-        if (result.getOptions().hasBaseCode() && comparison.getFirstBaseCodeMatches() != null && comparison.getSecondBaseCodeMatches() != null) {
+        if (options.hasBaseCode() && comparison.getFirstBaseCodeMatches() != null && comparison.getSecondBaseCodeMatches() != null) {
             JPlagComparison baseCodeComparison = comparison.getBaseCodeMatches(j == 0);
             JPlagComparison baseCodeComparison = comparison.getBaseCodeMatches(j == 0);
 
 
             for (Match currentMatch : baseCodeComparison.getMatches()) {
             for (Match currentMatch : baseCodeComparison.getMatches()) {
@@ -763,18 +763,18 @@ public class Report { // Mostly legacy code with some minor improvements.
             f.println("</center>");
             f.println("</center>");
             f.println("</h3>");
             f.println("</h3>");
             f.println("<HR>");
             f.println("<HR>");
-            if (result.getOptions().getLanguage().isPreformatted()) {
+            if (options.getLanguage().isPreformatted()) {
                 f.println("<PRE>");
                 f.println("<PRE>");
             }
             }
             for (int y = 0; y < text[x].length; y++) {
             for (int y = 0; y < text[x].length; y++) {
                 f.print(text[x][y]);
                 f.print(text[x][y]);
-                if (!result.getOptions().getLanguage().isPreformatted()) {
+                if (!options.getLanguage().isPreformatted()) {
                     f.println("<BR>");
                     f.println("<BR>");
                 } else {
                 } else {
                     f.println();
                     f.println();
                 }
                 }
             }
             }
-            if (result.getOptions().getLanguage().isPreformatted()) {
+            if (options.getLanguage().isPreformatted()) {
                 f.println("</PRE>");
                 f.println("</PRE>");
             }
             }
         }
         }

+ 1 - 1
jplag/src/main/resources/de/jplag/messages.properties

@@ -1,4 +1,4 @@
-CommandLineArgument.BaseCode=Name of the subdirectory of the root directory which contains the base code (common framework used in all submissions)
+CommandLineArgument.BaseCode=Path of the directory containing the base code (common framework used in all submissions)
 CommandLineArgument.ComparisonMode=Comparison mode used to compare the programs
 CommandLineArgument.ComparisonMode=Comparison mode used to compare the programs
 CommandLineArgument.Debug=Debug parser. Non-parsable files will be stored
 CommandLineArgument.Debug=Debug parser. Non-parsable files will be stored
 CommandLineArgument.Suffixes=comma-separated list of all filename suffixes that are included
 CommandLineArgument.Suffixes=comma-separated list of all filename suffixes that are included

+ 10 - 4
jplag/src/test/java/de/jplag/BaseCodeTest.java

@@ -13,11 +13,11 @@ import de.jplag.exceptions.RootDirectoryException;
 public class BaseCodeTest extends TestBase {
 public class BaseCodeTest extends TestBase {
 
 
     @Test
     @Test
-    public void testBasecodeComparison() throws ExitException {
+    public void testBasecodeUserSubmissionComparison() throws ExitException {
         JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base"));
         JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base"));
         verifyResults(result);
         verifyResults(result);
     }
     }
-    
+
     @Test(expected = BasecodeException.class)
     @Test(expected = BasecodeException.class)
     public void testTinyBasecode() throws ExitException {
     public void testTinyBasecode() throws ExitException {
         runJPlag("TinyBasecode", it -> it.setBaseCodeSubmissionName("base"));
         runJPlag("TinyBasecode", it -> it.setBaseCodeSubmissionName("base"));
@@ -43,6 +43,12 @@ public class BaseCodeTest extends TestBase {
         assertEquals(85f, result.getComparisons().get(0).similarity(), DELTA);
         assertEquals(85f, result.getComparisons().get(0).similarity(), DELTA);
     }
     }
 
 
+    @Test
+    public void testBasecodePathComparison() throws ExitException {
+        JPlagResult result = runJPlag("basecode", it -> it.setBaseCodeSubmissionName(getBasePath("basecode-base")));
+        assertEquals(3, result.getNumberOfSubmissions()); // "basecode/base" is now a user submission.
+    }
+
     @Test(expected = RootDirectoryException.class)
     @Test(expected = RootDirectoryException.class)
     public void testInvalidRoot() throws ExitException {
     public void testInvalidRoot() throws ExitException {
         runJPlag("basecode", it -> it.setRootDirectoryName("WrongRoot"));
         runJPlag("basecode", it -> it.setRootDirectoryName("WrongRoot"));
@@ -54,7 +60,7 @@ public class BaseCodeTest extends TestBase {
     }
     }
 
 
     @Test(expected = BasecodeException.class)
     @Test(expected = BasecodeException.class)
-    public void testBasecodeWithDots() throws ExitException {
-        runJPlag("basecode", it -> it.setBaseCodeSubmissionName("." + File.separator + "base"));
+    public void testBasecodeUserSubmissionWithDots() throws ExitException {
+        runJPlag("basecode", it -> it.setBaseCodeSubmissionName("base.ext"));
     }
     }
 }
 }

+ 9 - 0
jplag/src/test/java/de/jplag/TestBase.java

@@ -1,5 +1,6 @@
 package de.jplag;
 package de.jplag;
 
 
+import java.io.File;
 import java.nio.file.Path;
 import java.nio.file.Path;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
@@ -17,6 +18,14 @@ public abstract class TestBase {
         return BASE_PATH;
         return BASE_PATH;
     }
     }
 
 
+    protected String getBasePath(String... subs) {
+        String path = BASE_PATH;
+        for (String sub: subs) {
+            path += File.separator + sub;
+        }
+        return path;
+    }
+
     protected JPlagResult runJPlagWithExclusionFile(String testSampleName, String exclusionFileName) throws ExitException {
     protected JPlagResult runJPlagWithExclusionFile(String testSampleName, String exclusionFileName) throws ExitException {
         String blackList = Path.of(BASE_PATH, testSampleName, exclusionFileName).toString();
         String blackList = Path.of(BASE_PATH, testSampleName, exclusionFileName).toString();
         return runJPlag(testSampleName, options -> options.setExclusionFileName(blackList));
         return runJPlag(testSampleName, options -> options.setExclusionFileName(blackList));

+ 4 - 3
jplag/src/test/java/de/jplag/cli/BaseCodeOptionTest.java

@@ -1,7 +1,8 @@
 package de.jplag.cli;
 package de.jplag.cli;
 
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
+
+import java.util.Optional;
 
 
 import org.junit.Test;
 import org.junit.Test;
 
 
@@ -14,13 +15,13 @@ public class BaseCodeOptionTest extends CommandLineInterfaceTest {
     @Test
     @Test
     public void testDefaultValue() {
     public void testDefaultValue() {
         buildOptionsFromCLI(CURRENT_DIRECTORY);
         buildOptionsFromCLI(CURRENT_DIRECTORY);
-        assertNull(options.getBaseCodeSubmissionName());
+        assertEquals(Optional.empty(), options.getBaseCodeSubmissionName());
     }
     }
 
 
     @Test
     @Test
     public void testCustomName() {
     public void testCustomName() {
         String argument = buildArgument(CommandLineArgument.BASE_CODE, NAME);
         String argument = buildArgument(CommandLineArgument.BASE_CODE, NAME);
         buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
         buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
-        assertEquals(NAME, options.getBaseCodeSubmissionName());
+        assertEquals(NAME, options.getBaseCodeSubmissionName().get());
     }
     }
 }
 }

+ 12 - 0
jplag/src/test/resources/de/jplag/samples/basecode-base/TerrainType.java

@@ -0,0 +1,12 @@
+package carcassonne.model.terrain;
+
+/**
+ * Enumeration for the terrain type. Is used to specify the terrain of a tile on its different positions.
+ */
+public enum TerrainType {
+    CASTLE,
+    ROAD,
+    MONASTERY,
+    FIELDS,
+    OTHER;
+}