Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ecoCode builtin profile for ecocode-java plugin #59

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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"
]
}
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);
}
}
Loading