Skip to content

Commit cf2e612

Browse files
authored
Merge pull request #59 from green-code-initiative/feature/ecocode-builtin-profil
Add ecoCode builtin profile for ecocode-java plugin
2 parents 2329fd8 + 3a58257 commit cf2e612

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [#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
13+
1214
### Changed
1315

1416
- [#49](https://github.com/green-code-initiative/ecoCode-java/pull/49) Add test to ensure all Rules are registered
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java;
19+
20+
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
21+
import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader;
22+
23+
import static fr.greencodeinitiative.java.JavaRulesDefinition.LANGUAGE;
24+
import static fr.greencodeinitiative.java.JavaRulesDefinition.REPOSITORY_KEY;
25+
26+
public final class JavaEcoCodeWayProfile implements BuiltInQualityProfilesDefinition {
27+
static final String PROFILE_NAME = "ecoCode way";
28+
static final String PROFILE_PATH = JavaEcoCodeWayProfile.class.getPackageName().replace('.', '/') + "/ecoCode_way_profile.json";
29+
30+
@Override
31+
public void define(Context context) {
32+
NewBuiltInQualityProfile ecoCodeProfile = context.createBuiltInQualityProfile(PROFILE_NAME, LANGUAGE);
33+
loadProfile(ecoCodeProfile);
34+
ecoCodeProfile.done();
35+
}
36+
37+
private void loadProfile(NewBuiltInQualityProfile profile) {
38+
BuiltInQualityProfileJsonLoader.load(profile, REPOSITORY_KEY, PROFILE_PATH);
39+
}
40+
}

src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JavaRulesDefinition implements RulesDefinition {
3131
private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/java";
3232

3333
private static final String NAME = "ecoCode";
34-
private static final String LANGUAGE = "java";
34+
static final String LANGUAGE = "java";
3535
static final String REPOSITORY_KEY = "ecocode-java";
3636

3737
private final SonarRuntime sonarRuntime;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "ecoCode way",
3+
"language": "java",
4+
"ruleKeys": [
5+
"EC1",
6+
"EC2",
7+
"EC3",
8+
"EC5",
9+
"EC27",
10+
"EC28",
11+
"EC32",
12+
"EC67",
13+
"EC69",
14+
"EC72",
15+
"EC74",
16+
"EC76",
17+
"EC77",
18+
"EC78",
19+
"EC79"
20+
]
21+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java;
19+
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
25+
import org.sonar.check.Rule;
26+
27+
import static fr.greencodeinitiative.java.JavaCheckRegistrarTest.getDefinedRules;
28+
import static fr.greencodeinitiative.java.JavaEcoCodeWayProfile.PROFILE_NAME;
29+
import static fr.greencodeinitiative.java.JavaEcoCodeWayProfile.PROFILE_PATH;
30+
import static fr.greencodeinitiative.java.JavaRulesDefinition.LANGUAGE;
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
class JavaEcoCodeWayProfileTest {
34+
@Test
35+
void should_create_ecocode_profile() {
36+
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
37+
38+
JavaEcoCodeWayProfile definition = new JavaEcoCodeWayProfile();
39+
definition.define(context);
40+
41+
BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile(LANGUAGE, PROFILE_NAME);
42+
43+
assertThat(profile.language()).isEqualTo(LANGUAGE);
44+
assertThat(profile.name()).isEqualTo(PROFILE_NAME);
45+
List<String> definedRuleIds = getDefinedRules().stream().map(c -> c.getAnnotation(Rule.class).key()).collect(Collectors.toList());
46+
assertThat(profile.rules())
47+
.describedAs("All implemented rules must be declared in '%s' profile file: %s", PROFILE_NAME, PROFILE_PATH)
48+
.map(BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey)
49+
.containsExactlyInAnyOrderElementsOf(definedRuleIds);
50+
}
51+
}

0 commit comments

Comments
 (0)