*/
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;
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);
@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));
*/
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;
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, "");
+ }
}
*/
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));
*/
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;
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();
}
}
+ @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, "");
+ }
}
// 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);