-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathJavaRulesDefinitionTest.java
More file actions
94 lines (82 loc) · 3.46 KB
/
JavaRulesDefinitionTest.java
File metadata and controls
94 lines (82 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* 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.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.sonar.api.SonarRuntime;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction.Type;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.api.utils.Version;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
class JavaRulesDefinitionTest {
private RulesDefinition.Repository repository;
private int rulesSize;
@BeforeEach
void init() {
final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion();
JavaRulesDefinition rulesDefinition = new JavaRulesDefinition(sonarRuntime);
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
rulesSize = 16;
}
@Test
@DisplayName("Test repository metadata")
void testMetadata() {
assertThat(repository.name()).isEqualTo("ecoCode");
assertThat(repository.language()).isEqualTo("java");
assertThat(repository.key()).isEqualTo("ecocode-java");
assertThat(repository.rules()).hasSize(rulesSize);
}
@Test
void testRegistredRules() {
assertThat(repository.rules()).hasSize(rulesSize);
}
@Test
@DisplayName("All rule keys must be prefixed by 'EC'")
void testRuleKeyPrefix() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().forEach(
rule -> assertions.assertThat(rule.key()).startsWith("EC")
);
assertions.assertAll();
}
@Test
void assertRuleProperties() {
Rule rule = repository.rule("EC67");
assertThat(rule).isNotNull();
assertThat(rule.name()).isEqualTo("Use ++i instead of i++");
assertThat(rule.debtRemediationFunction().type()).isEqualTo(Type.CONSTANT_ISSUE);
assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
}
@Test
void testAllRuleParametersHaveDescription() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().stream()
.flatMap(rule -> rule.params().stream())
.forEach(param -> assertions.assertThat(param.description()).as("description for " + param.key()).isNotEmpty());
assertions.assertAll();
}
}