aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-10-05 10:09:39 +0200
committerDavid Gageot <david@gageot.net>2012-10-05 10:09:39 +0200
commit1bd03f2eebdfa1b56c1ce410081223b2b646754f (patch)
tree7402e98f1c61ef6466e0a6d6ad407f740d8ac06e /plugins
parent8e94477d8a7e41f5835e9554f71d007453461339 (diff)
downloadsonarqube-1bd03f2eebdfa1b56c1ce410081223b2b646754f.tar.gz
sonarqube-1bd03f2eebdfa1b56c1ce410081223b2b646754f.zip
SONAR-3724 Validate rule repositories in unit tests
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java10
-rw-r--r--plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java19
-rw-r--r--plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java18
-rw-r--r--plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java18
4 files changed, 45 insertions, 20 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<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));
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<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, "");
+ }
}
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<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));
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<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, "");
+ }
}