SonarQube custom Java plugin, unit tests
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 22 Aug 2016 20:42:59 +0000 (22:42 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 22 Aug 2016 20:42:59 +0000 (22:42 +0200)
Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java
Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java
Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java
Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesDefinitionTest.java [new file with mode: 0644]

index bcb4f95..c67a437 100644 (file)
@@ -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/CustomRulesDefinitionTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesDefinitionTest.java
new file mode 100644 (file)
index 0000000..de09ebe
--- /dev/null
@@ -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 {
+       }
+}