diff --git a/languages/multi-language/pom.xml b/languages/multi-language/pom.xml index ba0908c50..ca87dfa7e 100644 --- a/languages/multi-language/pom.xml +++ b/languages/multi-language/pom.xml @@ -8,6 +8,19 @@ multi-language - + + + de.jplag + java + ${revision} + test + + + de.jplag + cpp + ${revision} + test + + diff --git a/languages/multi-language/src/main/java/de/jplag/multilang/MultiLanguageOptions.java b/languages/multi-language/src/main/java/de/jplag/multilang/MultiLanguageOptions.java index 0f3f4f645..c94bf0406 100644 --- a/languages/multi-language/src/main/java/de/jplag/multilang/MultiLanguageOptions.java +++ b/languages/multi-language/src/main/java/de/jplag/multilang/MultiLanguageOptions.java @@ -11,20 +11,24 @@ public class MultiLanguageOptions extends LanguageOptions { private static final String ERROR_LANGUAGE_NOT_FOUND = "The selected language %s could not be found"; - private static final String ERROR_NOT_ENOUGH_LANGUAGES = "To use multi language specify at least 2 languages"; + private static final String ERROR_NOT_ENOUGH_LANGUAGES = "To use multi language specify at least 1 language"; - public LanguageOption languageNames = createOption(OptionType.string(), "langs", + public LanguageOption languageNames = createOption(OptionType.string(), "languages", "The languages that should be used. This is a ',' separated list"); private List languages = null; public List getLanguages() { if (this.languages == null) { + if (languageNames.getValue() == null) { + throw new IllegalArgumentException(ERROR_NOT_ENOUGH_LANGUAGES); + } + List languages = Arrays.stream(languageNames.getValue().split(",")) .map(name -> LanguageLoader.getLanguage(name) .orElseThrow(() -> new IllegalArgumentException(String.format(ERROR_LANGUAGE_NOT_FOUND, name)))) .filter(language -> !language.getClass().equals(MultiLanguage.class)).toList(); - if (languages.size() <= 1) { + if (languages.isEmpty()) { throw new IllegalArgumentException(ERROR_NOT_ENOUGH_LANGUAGES); } diff --git a/languages/multi-language/src/test/java/de/java/multilang/MultilangTest.java b/languages/multi-language/src/test/java/de/java/multilang/MultilangTest.java new file mode 100644 index 000000000..670b98dc8 --- /dev/null +++ b/languages/multi-language/src/test/java/de/java/multilang/MultilangTest.java @@ -0,0 +1,79 @@ +package de.java.multilang; + +import static de.jplag.SharedTokenType.FILE_END; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import de.jplag.ParsingException; +import de.jplag.Token; +import de.jplag.TokenType; +import de.jplag.cpp.CPPTokenType; +import de.jplag.java.JavaTokenType; +import de.jplag.multilang.MultiLanguage; +import de.jplag.multilang.MultiLanguageOptions; + +public class MultilangTest { + private static File testDataDirectory; + private static File javaCode; + private static File cppCode; + + private static List expectedTokens = List.of(CPPTokenType.FUNCTION_BEGIN, CPPTokenType.RETURN, CPPTokenType.FUNCTION_END, FILE_END, + JavaTokenType.J_CLASS_BEGIN, JavaTokenType.J_CLASS_END, FILE_END); + + @BeforeAll + static void setUp() throws IOException { + testDataDirectory = Files.createTempDirectory("multiLanguageTestData").toFile(); + cppCode = new File(testDataDirectory, "CppCode.cpp"); + javaCode = new File(testDataDirectory, "JavaCode.java"); + + MultilangTest.class.getResourceAsStream("/de/jplag/multilang/testDataSet/CppCode.cpp").transferTo(new FileOutputStream(cppCode)); + MultilangTest.class.getResourceAsStream("/de/jplag/multilang/testDataSet/JavaCode.java").transferTo(new FileOutputStream(javaCode)); + } + + @Test + void testMultiLanguageParsing() throws ParsingException { + MultiLanguage languageModule = new MultiLanguage(); + ((MultiLanguageOptions) languageModule.getOptions()).languageNames.setValue("java,cpp"); + + Set sources = new TreeSet<>(List.of(javaCode, cppCode)); // Using TreeSet to ensure order of entries + List tokens = languageModule.parse(sources, false); + + Assertions.assertEquals(expectedTokens, tokens.stream().map(Token::getType).toList()); + } + + @Test + void testNoLanguagesConfigured() { + MultiLanguage languageModule = new MultiLanguage(); + Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> { + languageModule.parse(Set.of(javaCode, cppCode), false); + }); + } + + @Test + void testInvalidLanguage() { + MultiLanguage languageModule = new MultiLanguage(); + ((MultiLanguageOptions) languageModule.getOptions()).languageNames.setValue("thisIsNotALanguage"); + + Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> { + languageModule.parse(Set.of(javaCode, cppCode), false); + }); + } + + @AfterAll + static void cleanUp() { + javaCode.delete(); + cppCode.delete(); + testDataDirectory.delete(); + } +} diff --git a/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/CppCode.cpp b/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/CppCode.cpp new file mode 100644 index 000000000..e9cdae165 --- /dev/null +++ b/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/CppCode.cpp @@ -0,0 +1,3 @@ +int main() { + return 0; +} \ No newline at end of file diff --git a/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/JavaCode.java b/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/JavaCode.java new file mode 100644 index 000000000..32aacd210 --- /dev/null +++ b/languages/multi-language/src/test/resources/de/jplag/multilang/testDataSet/JavaCode.java @@ -0,0 +1,3 @@ +public class JavaCode { + +} \ No newline at end of file