From 4c4b10d28a9d947eb295e7164d6ff05f54defcb6 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Thu, 22 Aug 2024 15:43:27 +0200 Subject: [PATCH 1/3] Added missing CLI tests --- cli/src/test/java/de/jplag/cli/DebugTest.java | 27 ++++++++ .../java/de/jplag/cli/ExcludeFileTest.java | 29 ++++++++ .../test/java/de/jplag/cli/LogLevelTest.java | 28 ++++++++ .../java/de/jplag/cli/ResultFileTest.java | 68 +++++++++++++++++++ .../java/de/jplag/cli/SubdirectoryTest.java | 29 ++++++++ .../test/java/de/jplag/cli/SuffixesTest.java | 30 ++++++++ .../java/de/jplag/cli/test/CliArgument.java | 6 ++ .../java/de/jplag/cli/test/CliResult.java | 4 +- .../test/java/de/jplag/cli/test/CliTest.java | 27 +++++++- 9 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 cli/src/test/java/de/jplag/cli/DebugTest.java create mode 100644 cli/src/test/java/de/jplag/cli/ExcludeFileTest.java create mode 100644 cli/src/test/java/de/jplag/cli/LogLevelTest.java create mode 100644 cli/src/test/java/de/jplag/cli/ResultFileTest.java create mode 100644 cli/src/test/java/de/jplag/cli/SubdirectoryTest.java create mode 100644 cli/src/test/java/de/jplag/cli/SuffixesTest.java diff --git a/cli/src/test/java/de/jplag/cli/DebugTest.java b/cli/src/test/java/de/jplag/cli/DebugTest.java new file mode 100644 index 000000000..36b1ea19a --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/DebugTest.java @@ -0,0 +1,27 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; +import de.jplag.options.JPlagOptions; + +public class DebugTest extends CliTest { + @Test + void testDefaultDebug() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(); + assertFalse(options.debugParser()); + } + + @Test + void testSetDebug() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.DEBUG, true)); + assertTrue(options.debugParser()); + } +} diff --git a/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java new file mode 100644 index 000000000..41733582f --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java @@ -0,0 +1,29 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; +import de.jplag.options.JPlagOptions; + +public class ExcludeFileTest extends CliTest { + private static final String excludeFileName = "exclusions"; + + @Test + void testNoDefaultExcludeFile() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(); + assertNull(options.exclusionFileName()); + } + + @Test + void testSetExcludeFile() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.EXCLUDE_FILES, excludeFileName)); + assertEquals(excludeFileName, options.exclusionFileName()); + } +} diff --git a/cli/src/test/java/de/jplag/cli/LogLevelTest.java b/cli/src/test/java/de/jplag/cli/LogLevelTest.java new file mode 100644 index 000000000..5cde8e940 --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/LogLevelTest.java @@ -0,0 +1,28 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; +import org.slf4j.event.Level; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; + +public class LogLevelTest extends CliTest { + private static final Level DEFAULT_LOG_LEVEL = Level.INFO; + + @Test + void testDefaultLogLevel() throws IOException, ExitException { + Level level = runCliForLogLevel(); + assertEquals(DEFAULT_LOG_LEVEL, level); + } + + @Test + void testSetLogLevel() throws IOException, ExitException { + Level level = runCliForLogLevel(args -> args.with(CliArgument.LOG_LEVEL, Level.ERROR.name())); + assertEquals(Level.ERROR, level); + } +} diff --git a/cli/src/test/java/de/jplag/cli/ResultFileTest.java b/cli/src/test/java/de/jplag/cli/ResultFileTest.java new file mode 100644 index 000000000..a96117f23 --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/ResultFileTest.java @@ -0,0 +1,68 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.jupiter.api.Test; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; + +public class ResultFileTest extends CliTest { + private static final String DEFAULT_RESULT_FILE = "results.zip"; + private static final String TEST_RESULT_FILE = "customResults.zip"; + private static final String TEST_RESULT_FILE_WITH_AVOIDANCE = "customResults(1).zip"; + private static final String TEST_RESULT_FILE_WITHOUT_ZIP = "customResults"; + + @Test + void testDefaultResultFolder() throws IOException, ExitException { + String targetPath = runCliForTargetPath(); + assertEquals(DEFAULT_RESULT_FILE, targetPath); + } + + @Test + void testSetResultFolder() throws IOException, ExitException { + String targetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, TEST_RESULT_FILE)); + assertEquals(TEST_RESULT_FILE, targetPath); + } + + @Test + void testSetResultFolderWithoutZip() throws IOException, ExitException { + String targetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, TEST_RESULT_FILE_WITHOUT_ZIP)); + assertEquals(TEST_RESULT_FILE, targetPath); + } + + @Test + void testResultFileOverrideAvoidance() throws IOException, ExitException { + File testDir = Files.createTempDirectory("JPlagResultFileTest").toFile(); + File targetFile = new File(testDir, TEST_RESULT_FILE); + File expectedTargetFile = new File(testDir, TEST_RESULT_FILE_WITH_AVOIDANCE); + targetFile.createNewFile(); + + String actualTargetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, targetFile.getAbsolutePath())); + + targetFile.delete(); + testDir.delete(); + + assertEquals(expectedTargetFile.getAbsolutePath(), actualTargetPath); + } + + @Test + void testResultFileOverwrite() throws IOException, ExitException { + File testDir = Files.createTempDirectory("JPlagResultFileTest").toFile(); + File targetFile = new File(testDir, TEST_RESULT_FILE); + targetFile.createNewFile(); + + String actualTargetPath = runCliForTargetPath( + args -> args.with(CliArgument.RESULT_FILE, targetFile.getAbsolutePath()).with(CliArgument.OVERWRITE_RESULT_FILE, true)); + + targetFile.delete(); + testDir.delete(); + + assertEquals(targetFile.getAbsolutePath(), actualTargetPath); + } +} diff --git a/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java b/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java new file mode 100644 index 000000000..a19f5434f --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java @@ -0,0 +1,29 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; +import de.jplag.options.JPlagOptions; + +public class SubdirectoryTest extends CliTest { + private static final String TEST_SUBDIRECTORY = "dir"; + + @Test + void testDefaultSubdirectory() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(); + assertNull(options.subdirectoryName()); + } + + @Test + void testSetSubdirectory() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SUBDIRECTORY, TEST_SUBDIRECTORY)); + assertEquals(TEST_SUBDIRECTORY, options.subdirectoryName()); + } +} diff --git a/cli/src/test/java/de/jplag/cli/SuffixesTest.java b/cli/src/test/java/de/jplag/cli/SuffixesTest.java new file mode 100644 index 000000000..aede36a95 --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/SuffixesTest.java @@ -0,0 +1,30 @@ +package de.jplag.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import de.jplag.cli.test.CliArgument; +import de.jplag.cli.test.CliTest; +import de.jplag.exceptions.ExitException; +import de.jplag.options.JPlagOptions; + +public class SuffixesTest extends CliTest { + private static final List JAVA_SUFFIXES = List.of(".java", ".JAVA"); + private static final List CUSTOM_SUFFIXES = List.of(".j", ".jva"); + + @Test + void testDefaultSuffixes() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(); + assertEquals(JAVA_SUFFIXES, options.fileSuffixes()); + } + + @Test + void testSetSuffixes() throws IOException, ExitException { + JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SUFFIXES, CUSTOM_SUFFIXES.toArray(new String[0]))); + assertEquals(CUSTOM_SUFFIXES, options.fileSuffixes()); + } +} diff --git a/cli/src/test/java/de/jplag/cli/test/CliArgument.java b/cli/src/test/java/de/jplag/cli/test/CliArgument.java index 6ac22a524..3b90977ab 100644 --- a/cli/src/test/java/de/jplag/cli/test/CliArgument.java +++ b/cli/src/test/java/de/jplag/cli/test/CliArgument.java @@ -28,4 +28,10 @@ public record CliArgument(String name, boolean isPositional) { public static CliArgument RESULT_FILE = new CliArgument<>("r", false); public static CliArgument OVERWRITE_RESULT_FILE = new CliArgument<>("overwrite", false); + + public static CliArgument LOG_LEVEL = new CliArgument<>("log-level", false); + public static CliArgument DEBUG = new CliArgument<>("d", false); + + public static CliArgument SUBDIRECTORY = new CliArgument<>("subdirectory", false); + public static CliArgument EXCLUDE_FILES = new CliArgument<>("x", false); } diff --git a/cli/src/test/java/de/jplag/cli/test/CliResult.java b/cli/src/test/java/de/jplag/cli/test/CliResult.java index 4978a5195..7051a6061 100644 --- a/cli/src/test/java/de/jplag/cli/test/CliResult.java +++ b/cli/src/test/java/de/jplag/cli/test/CliResult.java @@ -1,6 +1,8 @@ package de.jplag.cli.test; +import org.slf4j.event.Level; + import de.jplag.options.JPlagOptions; -public record CliResult(JPlagOptions jPlagOptions, String targetPath) { +public record CliResult(JPlagOptions jPlagOptions, String targetPath, Level logLevel) { } diff --git a/cli/src/test/java/de/jplag/cli/test/CliTest.java b/cli/src/test/java/de/jplag/cli/test/CliTest.java index d26a1fb47..bff7528a6 100644 --- a/cli/src/test/java/de/jplag/cli/test/CliTest.java +++ b/cli/src/test/java/de/jplag/cli/test/CliTest.java @@ -11,9 +11,11 @@ import org.junit.jupiter.api.BeforeEach; import org.mockito.MockedStatic; import org.mockito.Mockito; +import org.slf4j.event.Level; import de.jplag.JPlagResult; import de.jplag.cli.*; +import de.jplag.cli.logger.CollectedLogger; import de.jplag.cli.picocli.CliInputHandler; import de.jplag.exceptions.ExitException; import de.jplag.options.JPlagOptions; @@ -105,6 +107,29 @@ protected String runCliForTargetPath(Consumer additionalOpti return runCli(additionalOptionsBuilder).targetPath(); } + /** + * Runs the cli + * @return The log level set by the cli + * @throws ExitException If JPlag throws an exception + * @throws IOException If JPlag throws an exception + * @see #runCli() + */ + protected Level runCliForLogLevel() throws IOException, ExitException { + return runCli().logLevel(); + } + + /** + * Runs the cli using custom options + * @param additionalOptionsBuilder May modify the {@link CliArgumentBuilder} object to set custom options for this run. + * @return The log level set by the cli + * @throws ExitException If JPlag throws an exception + * @throws IOException If JPlag throws an exception + * @see #runCli() + */ + protected Level runCliForLogLevel(Consumer additionalOptionsBuilder) throws IOException, ExitException { + return runCli(additionalOptionsBuilder).logLevel(); + } + /** * Runs the cli * @return The options returned by the cli @@ -129,7 +154,7 @@ protected CliResult runCli(Consumer additionalOptionsBuilder String targetPath = (String) getWritableFileMethod.invoke(cli); - return new CliResult(optionsBuilder.buildOptions(), targetPath); + return new CliResult(optionsBuilder.buildOptions(), targetPath, CollectedLogger.getLogLevel()); } catch (IllegalAccessException | InvocationTargetException e) { Assumptions.abort("Could not access private field in CLI for test."); return null; // will not be executed From 9fe198efffbde2d68977c5e8de61f26b917aa909 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Mon, 26 Aug 2024 17:16:25 +0200 Subject: [PATCH 2/3] Added new version of ANTLR grammar and adapted the listener. --- cli/src/test/java/de/jplag/cli/ExcludeFileTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java index 41733582f..3f9b0230a 100644 --- a/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java +++ b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java @@ -13,7 +13,7 @@ import de.jplag.options.JPlagOptions; public class ExcludeFileTest extends CliTest { - private static final String excludeFileName = "exclusions"; + private static final String EXCLUDE_FILE_NAME = "exclusions"; @Test void testNoDefaultExcludeFile() throws IOException, ExitException { @@ -23,7 +23,7 @@ void testNoDefaultExcludeFile() throws IOException, ExitException { @Test void testSetExcludeFile() throws IOException, ExitException { - JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.EXCLUDE_FILES, excludeFileName)); - assertEquals(excludeFileName, options.exclusionFileName()); + JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.EXCLUDE_FILES, EXCLUDE_FILE_NAME)); + assertEquals(EXCLUDE_FILE_NAME, options.exclusionFileName()); } } From e9c9f2da908f0cba1c7a2189eb76dbcf5e8f5a7b Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Thu, 29 Aug 2024 10:20:14 +0200 Subject: [PATCH 3/3] Removed redundant modifiers --- cli/src/test/java/de/jplag/cli/DebugTest.java | 2 +- cli/src/test/java/de/jplag/cli/ExcludeFileTest.java | 2 +- cli/src/test/java/de/jplag/cli/LogLevelTest.java | 2 +- cli/src/test/java/de/jplag/cli/ResultFileTest.java | 2 +- cli/src/test/java/de/jplag/cli/SubdirectoryTest.java | 2 +- cli/src/test/java/de/jplag/cli/SuffixesTest.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/src/test/java/de/jplag/cli/DebugTest.java b/cli/src/test/java/de/jplag/cli/DebugTest.java index 36b1ea19a..a6f22f74c 100644 --- a/cli/src/test/java/de/jplag/cli/DebugTest.java +++ b/cli/src/test/java/de/jplag/cli/DebugTest.java @@ -12,7 +12,7 @@ import de.jplag.exceptions.ExitException; import de.jplag.options.JPlagOptions; -public class DebugTest extends CliTest { +class DebugTest extends CliTest { @Test void testDefaultDebug() throws IOException, ExitException { JPlagOptions options = runCliForOptions(); diff --git a/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java index 3f9b0230a..e2cb11669 100644 --- a/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java +++ b/cli/src/test/java/de/jplag/cli/ExcludeFileTest.java @@ -12,7 +12,7 @@ import de.jplag.exceptions.ExitException; import de.jplag.options.JPlagOptions; -public class ExcludeFileTest extends CliTest { +class ExcludeFileTest extends CliTest { private static final String EXCLUDE_FILE_NAME = "exclusions"; @Test diff --git a/cli/src/test/java/de/jplag/cli/LogLevelTest.java b/cli/src/test/java/de/jplag/cli/LogLevelTest.java index 5cde8e940..9994de5fa 100644 --- a/cli/src/test/java/de/jplag/cli/LogLevelTest.java +++ b/cli/src/test/java/de/jplag/cli/LogLevelTest.java @@ -11,7 +11,7 @@ import de.jplag.cli.test.CliTest; import de.jplag.exceptions.ExitException; -public class LogLevelTest extends CliTest { +class LogLevelTest extends CliTest { private static final Level DEFAULT_LOG_LEVEL = Level.INFO; @Test diff --git a/cli/src/test/java/de/jplag/cli/ResultFileTest.java b/cli/src/test/java/de/jplag/cli/ResultFileTest.java index a96117f23..888d166fd 100644 --- a/cli/src/test/java/de/jplag/cli/ResultFileTest.java +++ b/cli/src/test/java/de/jplag/cli/ResultFileTest.java @@ -12,7 +12,7 @@ import de.jplag.cli.test.CliTest; import de.jplag.exceptions.ExitException; -public class ResultFileTest extends CliTest { +class ResultFileTest extends CliTest { private static final String DEFAULT_RESULT_FILE = "results.zip"; private static final String TEST_RESULT_FILE = "customResults.zip"; private static final String TEST_RESULT_FILE_WITH_AVOIDANCE = "customResults(1).zip"; diff --git a/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java b/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java index a19f5434f..e2d319551 100644 --- a/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java +++ b/cli/src/test/java/de/jplag/cli/SubdirectoryTest.java @@ -12,7 +12,7 @@ import de.jplag.exceptions.ExitException; import de.jplag.options.JPlagOptions; -public class SubdirectoryTest extends CliTest { +class SubdirectoryTest extends CliTest { private static final String TEST_SUBDIRECTORY = "dir"; @Test diff --git a/cli/src/test/java/de/jplag/cli/SuffixesTest.java b/cli/src/test/java/de/jplag/cli/SuffixesTest.java index aede36a95..ab095224b 100644 --- a/cli/src/test/java/de/jplag/cli/SuffixesTest.java +++ b/cli/src/test/java/de/jplag/cli/SuffixesTest.java @@ -12,7 +12,7 @@ import de.jplag.exceptions.ExitException; import de.jplag.options.JPlagOptions; -public class SuffixesTest extends CliTest { +class SuffixesTest extends CliTest { private static final List JAVA_SUFFIXES = List.of(".java", ".JAVA"); private static final List CUSTOM_SUFFIXES = List.of(".j", ".jva");