Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new framework for Cli-Tests #1744

Merged
merged 10 commits into from
Jun 20, 2024
23 changes: 0 additions & 23 deletions cli/src/test/java/de/jplag/cli/AdvancedGroupTest.java

This file was deleted.

21 changes: 16 additions & 5 deletions cli/src/test/java/de/jplag/cli/BaseCodeOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
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;

class BaseCodeOptionTest extends CommandLineInterfaceTest {
import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliResult;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

class BaseCodeOptionTest extends CliTest {

private static final String NAME = "BaseCodeName";

@Test
void testDefaultValue() throws CliException {
buildOptionsFromCLI(defaultArguments());
void testDefaultValue() throws ExitException, IOException {
JPlagOptions options = runCli().jPlagOptions();

assertNull(options.baseCodeSubmissionDirectory());
}

@Test
void testCustomName() throws CliException {
buildOptionsFromCLI(defaultArguments().baseCode(NAME));
void testCustomName() throws ExitException, IOException {
CliResult result = runCli(options -> options.with(CliArgument.BASE_CODE, NAME));
JPlagOptions options = result.jPlagOptions();

assertEquals(NAME, options.baseCodeSubmissionDirectory().getName());
}
}
48 changes: 10 additions & 38 deletions cli/src/test/java/de/jplag/cli/CheckResultFileWritableTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
package de.jplag.cli;

import static de.jplag.cli.test.CliArgument.OVERWRITE_RESULT_FILE;
import static de.jplag.cli.test.CliArgument.RESULT_FILE;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import de.jplag.cli.picocli.CliInputHandler;

public class CheckResultFileWritableTest extends CommandLineInterfaceTest {
private static Field inputHandlerField;
private static Method getWritableFileMethod;

@BeforeAll
public static void setup() throws NoSuchFieldException, NoSuchMethodException {
Class<CLI> cliClass = CLI.class;
inputHandlerField = cliClass.getDeclaredField("inputHandler");
getWritableFileMethod = cliClass.getDeclaredMethod("getWritableFileName");

inputHandlerField.setAccessible(true);
getWritableFileMethod.setAccessible(true);
}
import de.jplag.cli.test.CliTest;

class CheckResultFileWritableTest extends CliTest {
@Test
void testNonExistingWritableFile() throws Throwable {
File directory = Files.createTempDirectory("JPlagTest").toFile();
File targetFile = new File(directory, "results.zip");

String path = runResultFileCheck(defaultArguments().resultFile(targetFile.getAbsolutePath()));
String path = runCliForTargetPath(args -> args.with(RESULT_FILE, targetFile.getAbsolutePath()));
Assertions.assertEquals(targetFile.getAbsolutePath(), path);
}

Expand All @@ -44,7 +30,7 @@ void testNonExistingNotWritableFile() throws IOException {
File targetFile = new File(directory, "results.zip");

Assertions.assertThrows(CliException.class, () -> {
runResultFileCheck(defaultArguments().resultFile(targetFile.getAbsolutePath()));
runCli(args -> args.with(RESULT_FILE, targetFile.getAbsolutePath()));
});
}

Expand All @@ -54,7 +40,7 @@ void testExistingFile() throws Throwable {
File targetFile = new File(directory, "results.zip");
Assumptions.assumeTrue(targetFile.createNewFile());

String path = runResultFileCheck(defaultArguments().resultFile(targetFile.getAbsolutePath()));
String path = runCliForTargetPath(args -> args.with(RESULT_FILE, targetFile.getAbsolutePath()));
Assertions.assertEquals(new File(directory, "results(1).zip").getAbsolutePath(), path);
}

Expand All @@ -64,7 +50,7 @@ void testExistingFileOverwrite() throws Throwable {
File targetFile = new File(directory, "results.zip");
Assumptions.assumeTrue(targetFile.createNewFile());

String path = runResultFileCheck(defaultArguments().resultFile(targetFile.getAbsolutePath()).overwrite());
String path = runCliForTargetPath(args -> args.with(RESULT_FILE, targetFile.getAbsolutePath()).with(OVERWRITE_RESULT_FILE));
Assertions.assertEquals(targetFile.getAbsolutePath(), path);
}

Expand All @@ -76,21 +62,7 @@ void testExistingNotWritableFile() throws IOException {
Assumptions.assumeTrue(targetFile.setWritable(false));

Assertions.assertThrows(CliException.class, () -> {
runResultFileCheck(defaultArguments().resultFile(targetFile.getAbsolutePath()).overwrite());
runCli(args -> args.with(OVERWRITE_RESULT_FILE).with(RESULT_FILE, targetFile.getAbsolutePath()));
});
}

private String runResultFileCheck(ArgumentBuilder builder) throws Throwable {
String[] args = builder.getArgumentsAsArray();
CLI cli = new CLI(args);

CliInputHandler inputHandler = (CliInputHandler) inputHandlerField.get(cli);
inputHandler.parse();

try {
return (String) getWritableFileMethod.invoke(cli);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
33 changes: 20 additions & 13 deletions cli/src/test/java/de/jplag/cli/ClusteringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,48 @@
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.clustering.Preprocessing;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

class ClusteringTest extends CommandLineInterfaceTest {
class ClusteringTest extends CliTest {
private static final double CLUSTERING_TEST_PERCENTILE = .5;

@Test
void parseSkipClustering() throws CliException {
buildOptionsFromCLI(defaultArguments().skipClustering());
void parseSkipClustering() throws ExitException, IOException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SKIP_CLUSTERING));
assertFalse(options.clusteringOptions().enabled());
}

@Test
void parseDefaultClustering() throws CliException {
buildOptionsFromCLI(defaultArguments());
void parseDefaultClustering() throws ExitException, IOException {
JPlagOptions options = runCliForOptions();
assertTrue(options.clusteringOptions().enabled());
}

@Test
void parsePercentilePreProcessor() throws CliException {
buildOptionsFromCLI(defaultArguments().clusterPpPercentile(.5));
void parsePercentilePreProcessor() throws ExitException, IOException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.CLUSTER_PP_PERCENTILE, CLUSTERING_TEST_PERCENTILE));

assertEquals(Preprocessing.PERCENTILE, options.clusteringOptions().preprocessor());
assertEquals(0.5, options.clusteringOptions().preprocessorPercentile());
assertEquals(CLUSTERING_TEST_PERCENTILE, options.clusteringOptions().preprocessorPercentile());
}

@Test
void parseCdfPreProcessor() throws CliException {
buildOptionsFromCLI(defaultArguments().clusterPpCdf());
void parseCdfPreProcessor() throws ExitException, IOException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.CLUSTER_PP_CDF));
assertEquals(Preprocessing.CUMULATIVE_DISTRIBUTION_FUNCTION, options.clusteringOptions().preprocessor());
}

@Test
void parseNoPreProcessor() throws CliException {
buildOptionsFromCLI(defaultArguments().clusterPpNone());
void parseNoPreProcessor() throws ExitException, IOException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.CLUSTER_PP_NONE));
assertEquals(Preprocessing.NONE, options.clusteringOptions().preprocessor());
}

}
42 changes: 0 additions & 42 deletions cli/src/test/java/de/jplag/cli/CommandLineInterfaceTest.java

This file was deleted.

38 changes: 25 additions & 13 deletions cli/src/test/java/de/jplag/cli/LanguageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import de.jplag.Language;
import de.jplag.cli.options.CliOptions;
import de.jplag.cli.options.LanguageLoader;
import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

class LanguageTest extends CommandLineInterfaceTest {
class LanguageTest extends CliTest {

@Test
void testDefaultLanguage() throws CliException {
buildOptionsFromCLI(defaultArguments());
void testDefaultLanguage() throws ExitException, IOException {
JPlagOptions options = runCliForOptions();
assertEquals(CliOptions.defaultLanguage.getIdentifier(), options.language().getIdentifier());
}

@Test
void testInvalidLanguage() {
Assertions.assertThrowsExactly(CliException.class, () -> buildOptionsFromCLI(defaultArguments().language("Piet")));
Assertions.assertThrowsExactly(CliException.class, () -> {
runCli(args -> args.with(CliArgument.LANGUAGE, "Piet"));
});
}

@Test
Expand All @@ -31,21 +41,23 @@ void testLoading() {
assertEquals(19, languages.size(), "Loaded Languages: " + languages.keySet());
}

@Test
void testValidLanguages() throws CliException {
for (Language language : LanguageLoader.getAllAvailableLanguages().values()) {
buildOptionsFromCLI(defaultArguments().language(language.getIdentifier()));
@ParameterizedTest
@MethodSource("getAllLanguages")
void testValidLanguages(Language language) throws ExitException, IOException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.LANGUAGE, language.getIdentifier()));

assertEquals(language.getIdentifier(), options.language().getIdentifier());
assertEquals(Arrays.asList(language.suffixes()), options.fileSuffixes());
}
assertEquals(language.getIdentifier(), options.language().getIdentifier());
assertEquals(Arrays.asList(language.suffixes()), options.fileSuffixes());
}

@Test
void testCustomSuffixes() throws CliException {
void testCustomSuffixes() throws ExitException, IOException {
String[] suffixes = {"x", "y", "z"};
buildOptionsFromCLI(defaultArguments().suffixes(suffixes));
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SUFFIXES, suffixes));
assertEquals(List.of(suffixes), options.fileSuffixes());
}

public static Collection<Language> getAllLanguages() {
return LanguageLoader.getAllAvailableLanguages().values();
}
}
12 changes: 9 additions & 3 deletions cli/src/test/java/de/jplag/cli/MergingOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.merging.MergingOptions;
import de.jplag.options.JPlagOptions;

/**
* Test cases for the options of the match merging mechanism.
*/
class MergingOptionsTest extends CommandLineInterfaceTest {
class MergingOptionsTest extends CliTest {

@Test
@DisplayName("Test if default values are used when creating merging options from CLI")
void testMergingDefault() throws CliException {
buildOptionsFromCLI(defaultArguments());
void testMergingDefault() throws ExitException, IOException {
JPlagOptions options = runCliForOptions();

assertNotNull(options.mergingOptions());
assertEquals(MergingOptions.DEFAULT_ENABLED, options.mergingOptions().enabled());
assertEquals(MergingOptions.DEFAULT_NEIGHBOR_LENGTH, options.mergingOptions().minimumNeighborLength());
Expand Down
Loading