diff --git a/CHANGELOG.md b/CHANGELOG.md index 855c493..79b9ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [#59](https://github.com/green-code-initiative/ecoCode-java/pull/59) Add builtin profile `ecoCode way` to aggregate all implemented ecoCode rules by this plugin + ### Changed - [#49](https://github.com/green-code-initiative/ecoCode-java/pull/49) Add test to ensure all Rules are registered diff --git a/src/main/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfile.java b/src/main/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfile.java new file mode 100644 index 0000000..d24fc82 --- /dev/null +++ b/src/main/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfile.java @@ -0,0 +1,40 @@ +/* + * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package fr.greencodeinitiative.java; + +import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; +import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader; + +import static fr.greencodeinitiative.java.JavaRulesDefinition.LANGUAGE; +import static fr.greencodeinitiative.java.JavaRulesDefinition.REPOSITORY_KEY; + +public final class JavaEcoCodeWayProfile implements BuiltInQualityProfilesDefinition { + static final String PROFILE_NAME = "ecoCode way"; + static final String PROFILE_PATH = JavaEcoCodeWayProfile.class.getPackageName().replace('.', '/') + "/ecoCode_way_profile.json"; + + @Override + public void define(Context context) { + NewBuiltInQualityProfile ecoCodeProfile = context.createBuiltInQualityProfile(PROFILE_NAME, LANGUAGE); + loadProfile(ecoCodeProfile); + ecoCodeProfile.done(); + } + + private void loadProfile(NewBuiltInQualityProfile profile) { + BuiltInQualityProfileJsonLoader.load(profile, REPOSITORY_KEY, PROFILE_PATH); + } +} diff --git a/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java b/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java index 958edc8..f9e03e3 100644 --- a/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java +++ b/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java @@ -31,7 +31,7 @@ public class JavaRulesDefinition implements RulesDefinition { private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/java"; private static final String NAME = "ecoCode"; - private static final String LANGUAGE = "java"; + static final String LANGUAGE = "java"; static final String REPOSITORY_KEY = "ecocode-java"; private final SonarRuntime sonarRuntime; diff --git a/src/main/resources/fr/greencodeinitiative/java/ecoCode_way_profile.json b/src/main/resources/fr/greencodeinitiative/java/ecoCode_way_profile.json new file mode 100644 index 0000000..88e381a --- /dev/null +++ b/src/main/resources/fr/greencodeinitiative/java/ecoCode_way_profile.json @@ -0,0 +1,21 @@ +{ + "name": "ecoCode way", + "language": "java", + "ruleKeys": [ + "EC1", + "EC2", + "EC3", + "EC5", + "EC27", + "EC28", + "EC32", + "EC67", + "EC69", + "EC72", + "EC74", + "EC76", + "EC77", + "EC78", + "EC79" + ] +} diff --git a/src/test/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfileTest.java b/src/test/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfileTest.java new file mode 100644 index 0000000..0d75016 --- /dev/null +++ b/src/test/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfileTest.java @@ -0,0 +1,51 @@ +/* + * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package fr.greencodeinitiative.java; + +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; +import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; +import org.sonar.check.Rule; + +import static fr.greencodeinitiative.java.JavaCheckRegistrarTest.getDefinedRules; +import static fr.greencodeinitiative.java.JavaEcoCodeWayProfile.PROFILE_NAME; +import static fr.greencodeinitiative.java.JavaEcoCodeWayProfile.PROFILE_PATH; +import static fr.greencodeinitiative.java.JavaRulesDefinition.LANGUAGE; +import static org.assertj.core.api.Assertions.assertThat; + +class JavaEcoCodeWayProfileTest { + @Test + void should_create_ecocode_profile() { + BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); + + JavaEcoCodeWayProfile definition = new JavaEcoCodeWayProfile(); + definition.define(context); + + BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile(LANGUAGE, PROFILE_NAME); + + assertThat(profile.language()).isEqualTo(LANGUAGE); + assertThat(profile.name()).isEqualTo(PROFILE_NAME); + List definedRuleIds = getDefinedRules().stream().map(c -> c.getAnnotation(Rule.class).key()).collect(Collectors.toList()); + assertThat(profile.rules()) + .describedAs("All implemented rules must be declared in '%s' profile file: %s", PROFILE_NAME, PROFILE_PATH) + .map(BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey) + .containsExactlyInAnyOrderElementsOf(definedRuleIds); + } +}