-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add builtin profile to aggregate all ecoCode rules implemented by thi…
…s plugin
- Loading branch information
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/fr/greencodeinitiative/java/JavaEcoCodeProfile.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,23 @@ | ||
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 JavaEcoCodeProfile implements BuiltInQualityProfilesDefinition { | ||
static final String PROFILE_NAME = "ecoCode"; | ||
private static final String PROFILE_PATH = JavaEcoCodeProfile.class.getPackageName().replace('.', '/') + "/ecocode_profile.json"; | ||
|
||
@Override | ||
public void define(Context context) { | ||
NewBuiltInQualityProfile ecoCodeProfile = context.createBuiltInQualityProfile(PROFILE_NAME, LANGUAGE); | ||
loadProfile(ecoCodeProfile); | ||
ecoCodeProfile.done(); | ||
} | ||
|
||
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
20 changes: 20 additions & 0 deletions
20
src/main/resources/fr/greencodeinitiative/java/ecocode_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,20 @@ | ||
{ | ||
"name": "ecoCode", | ||
"ruleKeys": [ | ||
"EC1", | ||
"EC2", | ||
"EC3", | ||
"EC5", | ||
"EC27", | ||
"EC28", | ||
"EC32", | ||
"EC67", | ||
"EC69", | ||
"EC72", | ||
"EC74", | ||
"EC76", | ||
"EC77", | ||
"EC78", | ||
"EC79" | ||
] | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/fr/greencodeinitiative/java/JavaEcoCodeProfileTest.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,32 @@ | ||
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.JavaEcoCodeProfile.PROFILE_NAME; | ||
import static fr.greencodeinitiative.java.JavaRulesDefinition.LANGUAGE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class JavaEcoCodeProfileTest { | ||
@Test | ||
void should_create_ecocode_profile() { | ||
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); | ||
|
||
JavaEcoCodeProfile definition = new JavaEcoCodeProfile(); | ||
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()) | ||
.map(BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey) | ||
.containsExactlyInAnyOrderElementsOf(definedRuleIds); | ||
} | ||
} |