Browse Source

Add three CLI test classes for the number-based CLI arguments.

Timur Sağlam 5 years ago
parent
commit
9cc65680ce

+ 6 - 0
jplag/pom.xml

@@ -207,5 +207,11 @@
       <groupId>edu.kit.ipd.jplag</groupId>
       <artifactId>chars</artifactId>
     </dependency>
+    <dependency>
+    	<groupId>com.github.stefanbirkner</groupId>
+    	<artifactId>system-rules</artifactId>
+    	<scope>test</scope>
+    	<version>1.19.0</version>
+    </dependency>
   </dependencies>
 </project>

+ 30 - 30
jplag/src/test/java/jplag/cli/CommandLineInterfaceTest.java

@@ -27,22 +27,22 @@ public class CommandLineInterfaceTest {
     protected JPlagOptions options;
 
     /**
-     * Runs JPlag via the command line interface. Sets {@link CommandLineInterfaceTest#cli},
-     * {@link CommandLineInterfaceTest#namespace}, and {@link CommandLineInterfaceTest#options}.
-     * @param arguments arguments are the command line interface arguments.
-     * @return the result of the JPlag run.
+     * Creates a string for all arguments and their values that have been succesfully parsed.F
      */
-    protected JPlagResult runViaCLI(String... arguments) {
-        buildOptionsFromCLI(arguments);
-        try {
-            JPlag program = new JPlag(options);
-            return program.run();
-        } catch (ExitException e) {
-            e.printStackTrace();
-            fail(e.getMessage());
-            e.printStackTrace();
-        }
-        throw new IllegalStateException("Should never be reached!");
+    private String parsedKeys(String... arguments) {
+        var keys = namespace.getAttrs().keySet().stream()
+                .filter(key -> key.equals(ROOT_DIRECTORY.flag()) || Arrays.stream(arguments).anyMatch(arg -> arg.contains("-" + key)));
+        return keys.map(it -> it + "=" + namespace.get(it)).collect(toSet()).toString();
+    }
+
+    /**
+     * Builds a CLI string for a CLI argument and a value.
+     * @param argument is the CLI argument.
+     * @param value is the value for that argument.
+     * @return <code>"flag value"</code>
+     */
+    protected String buildArgument(CommandLineArgument argument, String value) {
+        return argument.flag() + "=" + value;
     }
 
     /**
@@ -58,22 +58,22 @@ public class CommandLineInterfaceTest {
     }
 
     /**
-     * Builds a CLI string for a CLI argument and a value.
-     * @param argument is the CLI argument.
-     * @param value is the value for that argument.
-     * @return <code>"flag value"</code>
-     */
-    protected String buildArgument(CommandLineArgument argument, String value) {
-        return argument.flag() + " " + value;
-    }
-
-    /**
-     * Creates a string for all arguments and their values that have been succesfully parsed.F
+     * Runs JPlag via the command line interface. Sets {@link CommandLineInterfaceTest#cli},
+     * {@link CommandLineInterfaceTest#namespace}, and {@link CommandLineInterfaceTest#options}.
+     * @param arguments arguments are the command line interface arguments.
+     * @return the result of the JPlag run.
      */
-    private String parsedKeys(String... arguments) {
-        var keys = namespace.getAttrs().keySet().stream()
-                .filter(key -> key.equals(ROOT_DIRECTORY.flag()) || Arrays.stream(arguments).anyMatch(arg -> arg.contains(key)));
-        return keys.map(it -> it + "=" + namespace.get(it)).collect(toSet()).toString();
+    protected JPlagResult runViaCLI(String... arguments) {
+        buildOptionsFromCLI(arguments);
+        try {
+            JPlag program = new JPlag(options);
+            return program.run();
+        } catch (ExitException e) {
+            e.printStackTrace();
+            fail(e.getMessage());
+            e.printStackTrace();
+        }
+        throw new IllegalStateException("Should never be reached!");
     }
 
 }

+ 47 - 0
jplag/src/test/java/jplag/cli/ComparisonModeTest.java

@@ -0,0 +1,47 @@
+package jplag.cli;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.ExpectedSystemExit;
+
+import jplag.CommandLineArgument;
+import jplag.options.JPlagOptions;
+import jplag.strategy.ComparisonMode;
+
+public class ComparisonModeTest extends CommandLineInterfaceTest {
+
+    @Rule
+    public final ExpectedSystemExit exit = ExpectedSystemExit.none();
+
+    @Test
+    public void testDefaultMode() {
+        buildOptionsFromCLI(CURRENT_DIRECTORY);
+        assertEquals(JPlagOptions.DEFAULT_COMPARISON_MODE, options.getComparisonMode());
+    }
+
+    @Test
+    public void testInvalidMode() {
+        exit.expectSystemExitWithStatus(1);
+        String argument = buildArgument(CommandLineArgument.COMPARISON_MODE, "Test'); DROP TABLE STUDENTS; --");
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+    }
+
+    @Test
+    public void testNormalMode() {
+        ComparisonMode mode = ComparisonMode.NORMAL;
+        String argument = buildArgument(CommandLineArgument.COMPARISON_MODE, mode.getName());
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(mode, options.getComparisonMode());
+    }
+
+    @Test
+    public void testParallelMode() {
+        ComparisonMode mode = ComparisonMode.PARALLEL;
+        String argument = buildArgument(CommandLineArgument.COMPARISON_MODE, mode.getName());
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(mode, options.getComparisonMode());
+    }
+
+}

+ 51 - 0
jplag/src/test/java/jplag/cli/SimiliarityThresholdTest.java

@@ -0,0 +1,51 @@
+package jplag.cli;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.ExpectedSystemExit;
+
+import jplag.CommandLineArgument;
+import jplag.options.JPlagOptions;
+
+public class SimiliarityThresholdTest extends CommandLineInterfaceTest {
+
+    @Rule
+    public final ExpectedSystemExit exit = ExpectedSystemExit.none();
+
+    @Test
+    public void testDefaultThreshold() {
+        buildOptionsFromCLI(CURRENT_DIRECTORY);
+        assertEquals(JPlagOptions.DEFAULT_SIMILARITY_THRESHOLD, options.getSimilarityThreshold(), DELTA);
+    }
+
+    @Test
+    public void testInvalidThreshold() {
+        exit.expectSystemExitWithStatus(1);
+        String argument = buildArgument(CommandLineArgument.SIMILARITY_THRESHOLD, "Not a float...");
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+    }
+
+    @Test
+    public void testLowerBound() {
+        String argument = buildArgument(CommandLineArgument.SIMILARITY_THRESHOLD, Float.toString(-1f));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(0f, options.getSimilarityThreshold(), DELTA);
+    }
+
+    @Test
+    public void testUpperBound() {
+        String argument = buildArgument(CommandLineArgument.SIMILARITY_THRESHOLD, Float.toString(101f));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(100f, options.getSimilarityThreshold(), DELTA);
+    }
+
+    @Test
+    public void testValidThreshold() {
+        float expectedValue = 50f;
+        String argument = buildArgument(CommandLineArgument.SIMILARITY_THRESHOLD, Float.toString(expectedValue));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(expectedValue, options.getSimilarityThreshold(), DELTA);
+    }
+}

+ 52 - 0
jplag/src/test/java/jplag/cli/StoredMatchesTest.java

@@ -0,0 +1,52 @@
+package jplag.cli;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.ExpectedSystemExit;
+
+import jplag.CommandLineArgument;
+import jplag.options.JPlagOptions;
+
+public class StoredMatchesTest extends CommandLineInterfaceTest {
+
+    @Rule
+    public final ExpectedSystemExit exit = ExpectedSystemExit.none();
+
+    @Test
+    public void testDefault() {
+        buildOptionsFromCLI(CURRENT_DIRECTORY);
+        assertEquals(JPlagOptions.DEFAULT_STORED_MATCHES, options.getMaxNumberOfMatches());
+    }
+
+    @Test
+    public void testValidThreshold() {
+        int expectedValue = 999;
+        String argument = buildArgument(CommandLineArgument.STORED_MATCHES, Integer.toString(expectedValue));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(expectedValue, options.getMaxNumberOfMatches());
+    }
+
+    @Test
+    public void testAll() {
+        int expectedValue = -1;
+        String argument = buildArgument(CommandLineArgument.STORED_MATCHES, Integer.toString(expectedValue));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(expectedValue, options.getMaxNumberOfMatches());
+    }
+
+    @Test
+    public void testLowerBound() {
+        String argument = buildArgument(CommandLineArgument.STORED_MATCHES, Integer.toString(-2));
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(-1, options.getMaxNumberOfMatches());
+    }
+
+    @Test
+    public void testInvalidThreshold() {
+        exit.expectSystemExitWithStatus(1);
+        String argument = buildArgument(CommandLineArgument.STORED_MATCHES, "Not an integer...");
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+    }
+}