From 1bd03f2eebdfa1b56c1ce410081223b2b646754f Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 5 Oct 2012 10:09:39 +0200 Subject: [PATCH] SONAR-3724 Validate rule repositories in unit tests --- .../checkstyle/CheckstyleRuleRepository.java | 10 ++++------ .../CheckstyleRuleRepositoryTest.java | 19 +++++++++++++++++-- .../findbugs/FindbugsRuleRepository.java | 18 ++++++++---------- .../findbugs/FindbugsRuleRepositoryTest.java | 18 ++++++++++++++++-- .../test/i18n/RuleRepositoryTestHelper.java | 6 +++--- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java index d28bf2ccced..ad0ae36e89c 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java @@ -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 createRules() { - List rules = new ArrayList(); + List 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)); diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java index b42b99357d8..9c6daac1e04 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java @@ -19,10 +19,12 @@ */ 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 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 rules = RuleRepositoryTestHelper.createRulesWithNameAndDescription("checkstyle", repository); + + assertThat(rules).onProperty("name").excludes(null, ""); + assertThat(rules).onProperty("description").excludes(null, ""); + } } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java index 64a7c12ff34..db74c684352 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java @@ -19,32 +19,30 @@ */ 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 createRules() { - List rules = new ArrayList(); + List 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)); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java index b95dbf58f48..4e8ddbf93cf 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java @@ -19,10 +19,12 @@ */ 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 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 rules = RuleRepositoryTestHelper.createRulesWithNameAndDescription("findbugs", repository); + + assertThat(rules).onProperty("name").excludes(null, ""); + assertThat(rules).onProperty("description").excludes(null, ""); + } } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java index 79e763dd1ae..8315eb27282 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/RuleRepositoryTestHelper.java @@ -35,13 +35,13 @@ public final class RuleRepositoryTestHelper { // Static utility class } - public static List createRulesWithNameAndDescription(String plugin, RuleRepository repository) { - Properties props = loadProperties(String.format("/org/sonar/l10n/%s.properties", plugin)); + public static List createRulesWithNameAndDescription(String pluginKey, RuleRepository repository) { + Properties props = loadProperties(String.format("/org/sonar/l10n/%s.properties", pluginKey)); List 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); -- 2.39.5