import org.sonar.plugins.java.Java;
import org.sonar.squidbridge.annotations.RuleTemplate;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
repository.done();
}
- @VisibleForTesting
- protected void newRule(Class<?> ruleClass, NewRepository repository) {
+ protected void newRule(Class<?> ruleClass, NewRepository repository) {
- org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
- if (ruleAnnotation == null) {
- throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
- }
- String ruleKey = ruleAnnotation.key();
- if (Strings.isNullOrEmpty(ruleKey)) {
- throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
- }
- NewRule rule = repository.rule(ruleKey);
- if (rule == null) {
- throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
- }
-
- // Check whether it is a Rule Template.
- rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
- ruleMetadata(ruleClass, rule);
- }
+ org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
+ if (ruleAnnotation == null) {
+ throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
+ }
+ String ruleKey = ruleAnnotation.key();
+ if (Strings.isNullOrEmpty(ruleKey)) {
+ throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
+ }
+ NewRule rule = repository.rule(ruleKey);
+ if (rule == null) {
+ throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
+ }
- private void ruleMetadata(Class<?> ruleClass, NewRule rule) {
- String metadataKey = rule.key();
- addHtmlDescription(rule, metadataKey);
- addMetadata(rule, metadataKey);
+ // Check whether it is a Rule Template.
+ rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
+ ruleMetadata(ruleClass, rule);
+ }
- }
+ private void ruleMetadata(Class<?> ruleClass, NewRule rule) {
+ String metadataKey = rule.key();
+ addHtmlDescription(rule, metadataKey);
+ addMetadata(rule, metadataKey);
+ }
private void addMetadata(NewRule rule, String metadataKey) {
URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json");
}
private static class Remediation {
- String func;
- String constantCost;
- String linearDesc;
- String linearOffset;
- String linearFactor;
+ private String func;
+ private String constantCost;
+ private String linearDesc;
+ private String linearOffset;
+ private String linearFactor;
public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
if(func.startsWith("Constant")) {
--- /dev/null
+package de.example.plugins.custom.java;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+import org.sonar.api.server.rule.RulesDefinition;
+import org.sonar.check.Rule;
+import org.sonar.plugins.java.Java;
+import org.sonar.plugins.java.api.JavaCheck;
+
+import de.example.custom.java.checks.CheckList;
+
+public class CustomRulesDefinitionTest {
+
+ @Test
+ public void whenCreatingCustomJavaRulesDefinitionThenGenerateRulesDefinition() {
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ CustomRulesDefinition rulesDefinition = new CustomRulesDefinition();
+
+ rulesDefinition.define(context);
+
+ RulesDefinition.Repository repository = context.repository(CheckList.REPOSITORY_KEY);
+ assertThat(repository.name(), is(CheckList.REPOSITORY_NAME));
+ assertThat(repository.language(), is(Java.KEY));
+ assertThat(repository.rules().size(), is(CheckList.getChecks().size()));
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void shouldThrowIllegalArgumentExceptionWhenNoRuleAnnotationIsFound() {
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME);
+ CustomRulesDefinition rulesDefinition = new CustomRulesDefinition();
+
+ rulesDefinition.newRule(CheckWithNoAnnotation.class, newRepository);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void shouldThrowIllegalArgumentExceptionWhenNoKeyIsFoundInRuleAnnotation() {
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME);
+ CustomRulesDefinition rulesDefinition = new CustomRulesDefinition();
+
+ rulesDefinition.newRule(EmptyRuleKey.class, newRepository);
+ }
+
+ @Test(expected=IllegalStateException.class)
+ public void shouldThrowIllegalStateExceptionWhenNoKeyIsFoundInRuleAnnotation() {
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME);
+ newRepository.createRule("myCardinality");
+ newRepository.createRule("correctRule");
+ CustomRulesDefinition rulesDefinition = new CustomRulesDefinition();
+
+ rulesDefinition.newRule(UnregisteredRule.class, newRepository);
+ }
+
+ private class CheckWithNoAnnotation implements JavaCheck {
+ }
+
+ @Rule(key = "")
+ private class EmptyRuleKey implements JavaCheck {
+ }
+
+ @Rule(key = "myKey")
+ private class UnregisteredRule implements JavaCheck {
+ }
+
+ @Rule(key = "correctRule")
+ private class CorrectRule implements JavaCheck {
+ }
+}