diff --git a/ecocode-rules-specifications/src/main/assembly/java.xml b/ecocode-rules-specifications/src/main/assembly/java.xml index 14dec5680..904308053 100644 --- a/ecocode-rules-specifications/src/main/assembly/java.xml +++ b/ecocode-rules-specifications/src/main/assembly/java.xml @@ -10,6 +10,7 @@ ${project.build.outputDirectory} + io/ecocode/rules/*.class io/ecocode/rules/java/*.class diff --git a/ecocode-rules-specifications/src/main/assembly/php.xml b/ecocode-rules-specifications/src/main/assembly/php.xml index 05030977d..a9a1750c2 100644 --- a/ecocode-rules-specifications/src/main/assembly/php.xml +++ b/ecocode-rules-specifications/src/main/assembly/php.xml @@ -10,6 +10,7 @@ ${project.build.outputDirectory} + io/ecocode/rules/*.class io/ecocode/rules/php/*.class diff --git a/ecocode-rules-specifications/src/main/assembly/python.xml b/ecocode-rules-specifications/src/main/assembly/python.xml index c4c7ff09c..6153d537c 100644 --- a/ecocode-rules-specifications/src/main/assembly/python.xml +++ b/ecocode-rules-specifications/src/main/assembly/python.xml @@ -10,6 +10,7 @@ ${project.build.outputDirectory} + io/ecocode/rules/*.class io/ecocode/rules/python/*.class diff --git a/ecocode-rules-specifications/src/main/java/io/ecocode/rules/Common.java b/ecocode-rules-specifications/src/main/java/io/ecocode/rules/Common.java new file mode 100644 index 000000000..a2c20dbc4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/java/io/ecocode/rules/Common.java @@ -0,0 +1,43 @@ +package io.ecocode.rules; + +import org.sonar.api.SonarEdition; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarRuntime; +import org.sonar.api.utils.Version; + +/** + * This class is used to reduce duplication code and to pass SonarCloud Quality Gate + * //TODO: Remove this class when repository will be split + */ +public final class Common { + private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8); + + /** + * Base compatibility version of plugins + */ + public static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() { + @Override + public Version getApiVersion() { + return SONARQUBE_RUNTIME_VERSION; + } + + @Override + public SonarProduct getProduct() { + return SonarProduct.SONARQUBE; + } + + @Override + public SonarQubeSide getSonarQubeSide() { + return SonarQubeSide.SCANNER; + } + + @Override + public SonarEdition getEdition() { + return SonarEdition.COMMUNITY; + } + }; + + private Common() { + } +} diff --git a/ecocode-rules-specifications/src/test/java/io/ecocode/rules/CommonTest.java b/ecocode-rules-specifications/src/test/java/io/ecocode/rules/CommonTest.java new file mode 100644 index 000000000..f9b384f2f --- /dev/null +++ b/ecocode-rules-specifications/src/test/java/io/ecocode/rules/CommonTest.java @@ -0,0 +1,33 @@ +package io.ecocode.rules; + +import org.junit.jupiter.api.Test; +import org.sonar.api.SonarEdition; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarRuntime; +import org.sonar.api.utils.Version; + +import static io.ecocode.rules.Common.SONARQUBE_RUNTIME; +import static org.assertj.core.api.Assertions.assertThat; + +class CommonTest { + private static final Version MINIMAL_SONARQUBE_VERSION_COMPATIBILITY = Version.create(9, 8); + + @Test + void testPluginCompatibility() { + final SonarRuntime sonarRuntime = SONARQUBE_RUNTIME; + + assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion())) + .describedAs("Plugin must be compatible with SonarQube 9.8") + .isTrue(); + assertThat(sonarRuntime.getProduct()) + .describedAs("Plugin should applied to SonarQube") + .isEqualTo(SonarProduct.SONARQUBE); + assertThat(sonarRuntime.getEdition()) + .describedAs("Plugin should be compatible with Community Edition") + .isEqualTo(SonarEdition.COMMUNITY); + assertThat(sonarRuntime.getSonarQubeSide()) + .describedAs("Plugin should be executed by scanner") + .isEqualTo(SonarQubeSide.SCANNER); + } +} diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java index fe073263a..414ee548a 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java @@ -38,12 +38,7 @@ import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions; import fr.greencodeinitiative.java.checks.UnnecessarilyAssignValuesToVariables; import fr.greencodeinitiative.java.checks.UseCorrectForLoop; -import org.sonar.api.SonarEdition; -import org.sonar.api.SonarProduct; -import org.sonar.api.SonarQubeSide; -import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.utils.Version; import org.sonar.plugins.java.api.JavaCheck; import org.sonarsource.analyzer.commons.RuleMetadataLoader; @@ -51,6 +46,7 @@ import java.util.Collections; import java.util.List; +import static io.ecocode.rules.Common.SONARQUBE_RUNTIME; import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.LANGUAGE; import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.NAME; import static io.ecocode.rules.java.JavaRulesSpecificationsRepository.REPOSITORY_KEY; @@ -84,28 +80,6 @@ public class JavaRulesDefinition implements RulesDefinition { ); static final List> ANNOTATED_RULE_TEST_CLASSES = Collections.emptyList(); - private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8); - static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() { - @Override - public Version getApiVersion() { - return SONARQUBE_RUNTIME_VERSION; - } - - @Override - public SonarProduct getProduct() { - return SonarProduct.SONARQUBE; - } - - @Override - public SonarQubeSide getSonarQubeSide() { - return SonarQubeSide.SCANNER; - } - - @Override - public SonarEdition getEdition() { - return SonarEdition.COMMUNITY; - } - }; @Override public void define(Context context) { diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java index 65b3836a5..8d4fd0fb2 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java @@ -38,7 +38,6 @@ import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_CLASSES; import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_TEST_CLASSES; -import static fr.greencodeinitiative.java.JavaRulesDefinition.SONARQUBE_RUNTIME; import static org.assertj.core.api.Assertions.assertThat; class JavaRulesDefinitionTest { @@ -91,21 +90,4 @@ private static void assertAllRuleParametersHaveDescription(Repository repository } } } - - @Test - void testPluginCompatibility() { - SonarRuntime sonarRuntime = SONARQUBE_RUNTIME; - assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion())) - .describedAs("Plugin must be compatible with SonarQube 9.8") - .isTrue(); - assertThat(sonarRuntime.getProduct()) - .describedAs("Plugin should applied to SonarQube") - .isEqualTo(SonarProduct.SONARQUBE); - assertThat(sonarRuntime.getEdition()) - .describedAs("Plugin should be compatible with Community Edition") - .isEqualTo(SonarEdition.COMMUNITY); - assertThat(sonarRuntime.getSonarQubeSide()) - .describedAs("Plugin should be executed by scanner") - .isEqualTo(SonarQubeSide.SCANNER); - } } diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java index c0fb62e15..796ad177e 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java @@ -34,35 +34,13 @@ import java.util.List; +import static io.ecocode.rules.Common.SONARQUBE_RUNTIME; import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.LANGUAGE; import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.NAME; import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.REPOSITORY_KEY; import static io.ecocode.rules.php.PhpRulesSpecificationsRepository.RESOURCE_BASE_PATH; public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { - private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8); - static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() { - @Override - public Version getApiVersion() { - return SONARQUBE_RUNTIME_VERSION; - } - - @Override - public SonarProduct getProduct() { - return SonarProduct.SONARQUBE; - } - - @Override - public SonarQubeSide getSonarQubeSide() { - return SonarQubeSide.SCANNER; - } - - @Override - public SonarEdition getEdition() { - return SonarEdition.COMMUNITY; - } - }; - @Override public void define(Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java index 86a13f53e..ec8fda90c 100644 --- a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java @@ -22,14 +22,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.sonar.api.Plugin; -import org.sonar.api.SonarEdition; -import org.sonar.api.SonarProduct; -import org.sonar.api.SonarQubeSide; -import org.sonar.api.SonarRuntime; import org.sonar.api.internal.PluginContextImpl; import org.sonar.api.utils.Version; -import static fr.greencodeinitiative.php.PhpRuleRepository.SONARQUBE_RUNTIME; +import static io.ecocode.rules.Common.SONARQUBE_RUNTIME; import static org.assertj.core.api.Assertions.assertThat; class PhpPluginTest { @@ -47,21 +43,4 @@ void test() { new PHPPlugin().define(context); assertThat(context.getExtensions()).hasSize(1); } - - @Test - void testPluginCompatibility() { - SonarRuntime sonarRuntime = context.getRuntime(); - assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion())) - .describedAs("Plugin must be compatible with SonarQube 9.8") - .isTrue(); - assertThat(sonarRuntime.getProduct()) - .describedAs("Plugin should applied to SonarQube") - .isEqualTo(SonarProduct.SONARQUBE); - assertThat(sonarRuntime.getEdition()) - .describedAs("Plugin should be compatible with Community Edition") - .isEqualTo(SonarEdition.COMMUNITY); - assertThat(sonarRuntime.getSonarQubeSide()) - .describedAs("Plugin should be executed by scanner") - .isEqualTo(SonarQubeSide.SCANNER); - } } diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java index 7f599083e..4e84cdf87 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java @@ -29,46 +29,20 @@ import fr.greencodeinitiative.python.checks.AvoidUnoptimizedVectorImagesCheck; import fr.greencodeinitiative.python.checks.DetectUnoptimizedImageFormat; import fr.greencodeinitiative.python.checks.NoFunctionCallWhenDeclaringForLoop; -import org.sonar.api.SonarEdition; -import org.sonar.api.SonarProduct; -import org.sonar.api.SonarQubeSide; -import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.utils.Version; import org.sonar.plugins.python.api.PythonCustomRuleRepository; import org.sonarsource.analyzer.commons.RuleMetadataLoader; import java.util.Arrays; import java.util.List; +import static io.ecocode.rules.Common.SONARQUBE_RUNTIME; import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.LANGUAGE; import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.NAME; import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.REPOSITORY_KEY; import static io.ecocode.rules.python.PythonRulesSpecificationsRepository.RESOURCE_BASE_PATH; public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRepository { - private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8); - static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() { - @Override - public Version getApiVersion() { - return SONARQUBE_RUNTIME_VERSION; - } - - @Override - public SonarProduct getProduct() { - return SonarProduct.SONARQUBE; - } - - @Override - public SonarQubeSide getSonarQubeSide() { - return SonarQubeSide.SCANNER; - } - - @Override - public SonarEdition getEdition() { - return SonarEdition.COMMUNITY; - } - }; @Override public void define(Context context) { diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java index 35c8f9eca..6f0d988df 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java @@ -19,23 +19,15 @@ */ package fr.greencodeinitiative.python; -import static fr.greencodeinitiative.python.PythonRuleRepository.SONARQUBE_RUNTIME; -import static org.assertj.core.api.Assertions.assertThat; - import io.ecocode.rules.python.PythonRulesSpecificationsRepository; import org.assertj.core.api.SoftAssertions; import org.junit.Before; import org.junit.Test; -import org.sonar.api.SonarEdition; -import org.sonar.api.SonarProduct; -import org.sonar.api.SonarQubeSide; -import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.utils.Version; -public class PythonRuleRepositoryTest { - private static final Version MINIMAL_SONARQUBE_VERSION_COMPATIBILITY = Version.create(9, 8); +import static org.assertj.core.api.Assertions.assertThat; +public class PythonRuleRepositoryTest { private PythonRuleRepository pythonRuleRepository; private RulesDefinition.Context context; private RulesDefinition.Repository repository; @@ -80,23 +72,5 @@ public void testAllRuleParametersHaveDescription() { .flatMap(repository -> repository.rules().stream()) .flatMap(rule -> rule.params().stream()) .forEach(param -> assertThat(param.description()).as("description for " + param.key()).isNotEmpty()); - ; - } - - @Test - public void testPluginCompatibility() { - SonarRuntime sonarRuntime = SONARQUBE_RUNTIME; - assertThat(MINIMAL_SONARQUBE_VERSION_COMPATIBILITY.isGreaterThanOrEqual(sonarRuntime.getApiVersion())) - .describedAs("Plugin must be compatible with SonarQube 9.8") - .isTrue(); - assertThat(sonarRuntime.getProduct()) - .describedAs("Plugin should applied to SonarQube") - .isEqualTo(SonarProduct.SONARQUBE); - assertThat(sonarRuntime.getEdition()) - .describedAs("Plugin should be compatible with Community Edition") - .isEqualTo(SonarEdition.COMMUNITY); - assertThat(sonarRuntime.getSonarQubeSide()) - .describedAs("Plugin should be executed by scanner") - .isEqualTo(SonarQubeSide.SCANNER); } }