]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3724 Validate rule repositories in unit tests
authorDavid Gageot <david@gageot.net>
Fri, 5 Oct 2012 08:09:39 +0000 (10:09 +0200)
committerDavid Gageot <david@gageot.net>
Fri, 5 Oct 2012 08:09:39 +0000 (10:09 +0200)
plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java
plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java
sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java

index d28bf2ccced7cabbf491911cc201664c725f3afe..ad0ae36e89c15c4eb70c5b91b07fa53743a09fd5 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.checkstyle;
 
+import com.google.common.collect.Lists;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.resources.Java;
 import org.sonar.api.rules.Rule;
@@ -26,14 +27,11 @@ import org.sonar.api.rules.RuleRepository;
 import org.sonar.api.rules.XMLRuleParser;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.List;
 
 public final class CheckstyleRuleRepository extends RuleRepository {
-
-  // for user extensions
-  private ServerFileSystem fileSystem;
-  private XMLRuleParser xmlRuleParser;
+  private final ServerFileSystem fileSystem;
+  private final XMLRuleParser xmlRuleParser;
 
   public CheckstyleRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) {
     super(CheckstyleConstants.REPOSITORY_KEY, Java.KEY);
@@ -44,7 +42,7 @@ public final class CheckstyleRuleRepository extends RuleRepository {
 
   @Override
   public List<Rule> createRules() {
-    List<Rule> rules = new ArrayList<Rule>();
+    List<Rule> rules = Lists.newArrayList();
     rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/checkstyle/rules.xml")));
     for (File userExtensionXml : fileSystem.getExtensions(CheckstyleConstants.REPOSITORY_KEY, "xml")) {
       rules.addAll(xmlRuleParser.parse(userExtensionXml));
index b42b99357d8bb2761184848b4525a47d3f9f7f5e..9c6daac1e045287117400f915c2074f4e5304181 100644 (file)
  */
 package org.sonar.plugins.checkstyle;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.XMLRuleParser;
+import org.sonar.test.i18n.RuleRepositoryTestHelper;
 
 import java.util.List;
 
@@ -30,13 +32,26 @@ import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
 public class CheckstyleRuleRepositoryTest {
+  CheckstyleRuleRepository repository;
+
+  @Before
+  public void setUpRuleRepository() {
+    repository = new CheckstyleRuleRepository(mock(ServerFileSystem.class), new XMLRuleParser());
+  }
 
   @Test
   public void loadRepositoryFromXml() {
-    ServerFileSystem fileSystem = mock(ServerFileSystem.class);
-    CheckstyleRuleRepository repository = new CheckstyleRuleRepository(fileSystem, new XMLRuleParser());
     List<Rule> rules = repository.createRules();
+
+    assertThat(repository.getKey()).isEqualTo("checkstyle");
     assertThat(rules.size()).isEqualTo(129);
   }
 
+  @Test
+  public void should_provide_a_name_and_description_for_each_rule() {
+    List<Rule> rules = RuleRepositoryTestHelper.createRulesWithNameAndDescription("checkstyle", repository);
+
+    assertThat(rules).onProperty("name").excludes(null, "");
+    assertThat(rules).onProperty("description").excludes(null, "");
+  }
 }
index 64a7c12ff34b7ba6378e706c1c52c340cf546496..db74c684352a2cc8c89d9a131eda4863a3e39c9f 100644 (file)
  */
 package org.sonar.plugins.findbugs;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
+import com.google.common.collect.Lists;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.resources.Java;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleRepository;
 import org.sonar.api.rules.XMLRuleParser;
 
-public final class FindbugsRuleRepository extends RuleRepository {
-
-  private XMLRuleParser xmlRuleParser;
+import java.io.File;
+import java.util.List;
 
-  private ServerFileSystem fileSystem;
+public final class FindbugsRuleRepository extends RuleRepository {
+  private final ServerFileSystem fileSystem;
+  private final XMLRuleParser xmlRuleParser;
 
   public FindbugsRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) {
     super(FindbugsConstants.REPOSITORY_KEY, Java.KEY);
     setName(FindbugsConstants.REPOSITORY_NAME);
-    this.xmlRuleParser = xmlRuleParser;
     this.fileSystem = fileSystem;
+    this.xmlRuleParser = xmlRuleParser;
   }
 
   @Override
   public List<Rule> createRules() {
-    List<Rule> rules = new ArrayList<Rule>();
+    List<Rule> rules = Lists.newArrayList();
     rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml")));
     for (File userExtensionXml : fileSystem.getExtensions(FindbugsConstants.REPOSITORY_KEY, "xml")) {
       rules.addAll(xmlRuleParser.parse(userExtensionXml));
index b95dbf58f48743d8aead6159ef74ac16d06022ff..4e8ddbf93cfd0cf97b3bc2ee53af7361bab70d96 100644 (file)
  */
 package org.sonar.plugins.findbugs;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.XMLRuleParser;
+import org.sonar.test.i18n.RuleRepositoryTestHelper;
 
 import java.util.List;
 
@@ -30,12 +32,17 @@ import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 
 public class FindbugsRuleRepositoryTest {
+  FindbugsRuleRepository repository;
+
+  @Before
+  public void setUpRuleRepository() {
+    repository = new FindbugsRuleRepository(mock(ServerFileSystem.class), new XMLRuleParser());
+  }
 
   @Test
   public void testLoadRepositoryFromXml() {
-    ServerFileSystem fileSystem = mock(ServerFileSystem.class);
-    FindbugsRuleRepository repository = new FindbugsRuleRepository(fileSystem, new XMLRuleParser());
     List<Rule> rules = repository.createRules();
+
     assertThat(rules.size()).isGreaterThan(300);
     for (Rule rule : rules) {
       assertThat(rule.getKey()).isNotNull();
@@ -44,4 +51,11 @@ public class FindbugsRuleRepositoryTest {
     }
   }
 
+  @Test
+  public void should_provide_a_name_and_description_for_each_rule() {
+    List<Rule> rules = RuleRepositoryTestHelper.createRulesWithNameAndDescription("findbugs", repository);
+
+    assertThat(rules).onProperty("name").excludes(null, "");
+    assertThat(rules).onProperty("description").excludes(null, "");
+  }
 }
index 79e763dd1ae38343f0ae58c18a58de0a260c9f8f..8315eb27282f8ea7d7f9646c823b8730122e9980 100644 (file)
@@ -35,13 +35,13 @@ public final class RuleRepositoryTestHelper {
     // Static utility class
   }
 
-  public static List<Rule> createRulesWithNameAndDescription(String plugin, RuleRepository repository) {
-    Properties props = loadProperties(String.format("/org/sonar/l10n/%s.properties", plugin));
+  public static List<Rule> createRulesWithNameAndDescription(String pluginKey, RuleRepository repository) {
+    Properties props = loadProperties(String.format("/org/sonar/l10n/%s.properties", pluginKey));
 
     List<Rule> rules = repository.createRules();
     for (Rule rule : rules) {
       String name = props.getProperty(String.format("rule.%s.%s.name", repository.getKey(), rule.getKey()));
-      String description = TestUtils.getResourceContent(String.format("/org/sonar/l10n/%s/rules/%s/%s.html", plugin, repository.getKey(), rule.getKey()));
+      String description = TestUtils.getResourceContent(String.format("/org/sonar/l10n/%s/rules/%s/%s.html", pluginKey, repository.getKey(), rule.getKey()));
 
       rule.setName(name);
       rule.setDescription(description);