diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-03-29 10:54:40 +0600 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-03-29 11:01:42 +0600 |
commit | 2a93969813f040f9072fe261ac94f1c9b65738bb (patch) | |
tree | 480c59ed8da563af0a2292ae94f6fab53f5f0a52 /plugins/sonar-pmd-plugin | |
parent | 9f7cdba5e642faa17cb556f2b7882d7dd5adab0c (diff) | |
download | sonarqube-2a93969813f040f9072fe261ac94f1c9b65738bb.tar.gz sonarqube-2a93969813f040f9072fe261ac94f1c9b65738bb.zip |
Fix some quality flaws
Diffstat (limited to 'plugins/sonar-pmd-plugin')
5 files changed, 47 insertions, 23 deletions
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index 5beb5a20f7d..1dbc6a4901e 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -118,7 +118,7 @@ public class PmdExecutor implements BatchExtension { if (found) { return new FileInputStream(file); } - InputStream stream = getClass().getResourceAsStream(rulesetPath); + InputStream stream = PmdExecutor.class.getResourceAsStream(rulesetPath); if (stream == null) { throw new SonarException("The PMD ruleset can not be found: " + rulesetPath); } diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java index e907efe72f0..8c4ddb45286 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java @@ -19,9 +19,6 @@ */ package org.sonar.plugins.pmd; -import java.io.Reader; -import java.util.List; - import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; @@ -40,6 +37,11 @@ import org.sonar.plugins.pmd.xml.PmdProperty; import org.sonar.plugins.pmd.xml.PmdRule; import org.sonar.plugins.pmd.xml.PmdRuleset; +import javax.annotation.Nullable; + +import java.io.Reader; +import java.util.List; + public class PmdProfileImporter extends ProfileImporter { private final RuleFinder ruleFinder; @@ -113,7 +115,7 @@ public class PmdProfileImporter extends ProfileImporter { } } - private List<Element> getChildren(Element parent, String childName, Namespace namespace) { + private List<Element> getChildren(Element parent, String childName, @Nullable Namespace namespace) { if (namespace == null) { return (List<Element>) parent.getChildren(childName); } else { @@ -121,7 +123,7 @@ public class PmdProfileImporter extends ProfileImporter { } } - private void parsePmdProperties(Element eltRule, PmdRule pmdRule, Namespace namespace) { + private void parsePmdProperties(Element eltRule, PmdRule pmdRule, @Nullable Namespace namespace) { for (Element eltProperties : getChildren(eltRule, "properties", namespace)) { for (Element eltProperty : getChildren(eltProperties, "property", namespace)) { pmdRule.addProperty(new PmdProperty(eltProperty.getAttributeValue("name"), eltProperty.getAttributeValue("value"))); @@ -129,7 +131,7 @@ public class PmdProfileImporter extends ProfileImporter { } } - private void parsePmdPriority(Element eltRule, PmdRule pmdRule, Namespace namespace) { + private void parsePmdPriority(Element eltRule, PmdRule pmdRule, @Nullable Namespace namespace) { for (Element eltPriority : getChildren(eltRule, "priority", namespace)) { pmdRule.setPriority(eltPriority.getValue()); } diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java index 2bf9109f803..c18ec4d5162 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java @@ -34,7 +34,7 @@ public class PmdRule { private List<PmdProperty> properties = new ArrayList<PmdProperty>(); - private String clazz;// NOSONAR unused private field + private String clazz; public PmdRule(String ref) { this(ref, null); @@ -89,6 +89,10 @@ public class PmdRule { this.name = name; } + public String getName() { + return name; + } + public void setMessage(String message) { this.message = message; } @@ -97,6 +101,10 @@ public class PmdRule { return message; } + public void setClazz(String clazz) { + this.clazz = clazz; + } + public String getClazz() { return clazz; } @@ -110,15 +118,7 @@ public class PmdRule { properties.remove(prop); } - public void setClazz(String clazz) { - this.clazz = clazz; - } - - public String getName() { - return name; - } - public boolean hasProperties() { return properties != null && !properties.isEmpty(); } -}
\ No newline at end of file +} diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java index 5e098334438..64ad90b8bc4 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java @@ -19,17 +19,22 @@ */ package org.sonar.plugins.pmd; -import static org.hamcrest.Matchers.greaterThan; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -import java.util.List; - +import org.apache.commons.io.FileUtils; import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.rules.Rule; import org.sonar.api.rules.XMLRuleParser; +import java.io.File; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class PmdRuleRepositoryTest { @Test @@ -40,4 +45,15 @@ public class PmdRuleRepositoryTest { assertThat(rules.size(), greaterThan(100)); } + @Test + public void shouldLoadExtensions() { + ServerFileSystem fileSystem = mock(ServerFileSystem.class); + File file = FileUtils.toFile(getClass().getResource("/org/sonar/plugins/pmd/rules-extension.xml")); + when(fileSystem.getExtensions("pmd", "xml")).thenReturn(Collections.singletonList(file)); + PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser()); + List<Rule> rules = repository.createRules(); + assertThat(rules.size(), greaterThan(100)); + assertThat(rules.get(rules.size() - 1).getKey(), is("Extension")); + } + } diff --git a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/rules-extension.xml b/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/rules-extension.xml new file mode 100644 index 00000000000..5b560d6bce4 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/rules-extension.xml @@ -0,0 +1,6 @@ +<rules> + <rule key="Extension"> + <priority>MAJOR</priority> + <configKey>rulesets.xml/extension</configKey> + </rule> +</rules> |