From: Gustavo Martin Morcuende Date: Mon, 22 Aug 2016 20:42:59 +0000 (+0200) Subject: SonarQube custom Java plugin, unit tests X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=7d235b8e95945d64fb1552be8f7c7688edfafe22;p=JavaForFun SonarQube custom Java plugin, unit tests --- diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java index bcb4f95..c67a437 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java @@ -14,7 +14,6 @@ import org.sonar.api.utils.AnnotationUtils; 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; @@ -44,33 +43,31 @@ public class CustomRulesDefinition implements RulesDefinition { 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"); @@ -113,11 +110,11 @@ public class CustomRulesDefinition implements RulesDefinition { } 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")) { diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java index 296a57b..4573990 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java @@ -1,6 +1,6 @@ package de.example.plugins.custom.java; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; import org.junit.Test; diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java index 1c07e4b..d5b2f19 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java @@ -1,6 +1,6 @@ package de.example.plugins.custom.java; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.util.List; diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesDefinitionTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesDefinitionTest.java new file mode 100644 index 0000000..de09ebe --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesDefinitionTest.java @@ -0,0 +1,72 @@ +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 { + } +}