-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This profile aggregate all implemented rules by this plugin. This profile makes it easier to test ecoCode rules
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/resources/fr/greencodeinitiative/java/ecoCode_way_profile.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/fr/greencodeinitiative/java/JavaEcoCodeWayProfileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
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<String> 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); | ||
} | ||
} |