Skip to content

Commit

Permalink
Add test to ensure all Rules are registered
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed May 30, 2024
1 parent 716fe9b commit ea027b0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*/
@SonarLintSide
public class JavaCheckRegistrar implements CheckRegistrar {
private static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
ArrayCopyCheck.class,
IncrementCheck.class,
AvoidUsageOfStaticCollections.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
*/
package fr.greencodeinitiative.java;

import java.util.Set;

import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.CheckRegistrar;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -30,10 +34,14 @@ void checkNumberRules() {

final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);

assertThat(context.checkClasses()).hasSize(15);
assertThat(context.checkClasses())
.describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
.containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
assertThat(context.testCheckClasses()).isEmpty();

}

static Set<Class<?>> getDefinedRules() {
Reflections r = new Reflections(JavaCheckRegistrar.class.getPackageName() + ".checks");
return r.getTypesAnnotatedWith(Rule.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.api.utils.Version;

import static fr.greencodeinitiative.java.JavaCheckRegistrar.ANNOTATED_RULE_CLASSES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -36,8 +37,6 @@ class JavaRulesDefinitionTest {

private RulesDefinition.Repository repository;

private int rulesSize;

@BeforeEach
void init() {
final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
Expand All @@ -46,7 +45,6 @@ void init() {
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
rulesSize = 15;
}

@Test
Expand All @@ -55,12 +53,11 @@ 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);
assertThat(repository.rules()).hasSize(ANNOTATED_RULE_CLASSES.size());
}

@Test
Expand Down

0 comments on commit ea027b0

Please sign in to comment.