aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-03-29 10:54:40 +0600
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-03-29 11:01:42 +0600
commit2a93969813f040f9072fe261ac94f1c9b65738bb (patch)
tree480c59ed8da563af0a2292ae94f6fab53f5f0a52
parent9f7cdba5e642faa17cb556f2b7882d7dd5adab0c (diff)
downloadsonarqube-2a93969813f040f9072fe261ac94f1c9b65738bb.tar.gz
sonarqube-2a93969813f040f9072fe261ac94f1c9b65738bb.zip
Fix some quality flaws
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSensor.java3
-rw-r--r--plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java3
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoMavenPluginHandlerTest.java16
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java1
-rw-r--r--plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java23
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java2
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java14
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java20
-rw-r--r--plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java28
-rw-r--r--plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/rules-extension.xml6
10 files changed, 70 insertions, 46 deletions
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSensor.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSensor.java
index 0c1e5cdc983..1c0db7f6fcb 100644
--- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSensor.java
+++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSensor.java
@@ -22,6 +22,7 @@ package org.sonar.plugins.checkstyle;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
public class CheckstyleSensor implements Sensor {
@@ -35,7 +36,7 @@ public class CheckstyleSensor implements Sensor {
}
public boolean shouldExecuteOnProject(Project project) {
- return project.getFileSystem().hasJavaSourceFiles() &&
+ return !project.getFileSystem().mainFiles(Java.KEY).isEmpty() &&
!profile.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY).isEmpty();
}
diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java
index 2af3cc8c9e5..1f0cabd8e06 100644
--- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java
+++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JacocoMavenInitializer.java
@@ -38,7 +38,8 @@ public class JacocoMavenInitializer extends Initializer implements CoverageExten
@Override
public boolean shouldExecuteOnProject(Project project) {
- return project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC) && project.getFileSystem().hasTestFiles(Java.INSTANCE);
+ return project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)
+ && !project.getFileSystem().testFiles(Java.KEY).isEmpty();
}
@Override
diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoMavenPluginHandlerTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoMavenPluginHandlerTest.java
index 8bb93a1a865..0dc3f3127a7 100644
--- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoMavenPluginHandlerTest.java
+++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoMavenPluginHandlerTest.java
@@ -19,16 +19,6 @@
*/
package org.sonar.plugins.jacoco;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.File;
-
-import org.apache.commons.configuration.BaseConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.maven.MavenPlugin;
@@ -38,6 +28,12 @@ import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.test.MavenTestUtils;
+import java.io.File;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.*;
+
/**
* @author Evgeny Mandrikov
*/
diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java
index fc776e86de4..2939ca6337b 100644
--- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java
+++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoConfigurationTest.java
@@ -19,7 +19,6 @@
*/
package org.sonar.plugins.jacoco;
-import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java
index 8a47ee7efc9..4f8c02216a8 100644
--- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java
+++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JacocoMavenInitializerTest.java
@@ -19,20 +19,21 @@
*/
package org.sonar.plugins.jacoco;
-import static org.hamcrest.Matchers.instanceOf;
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
+import java.util.Collections;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.*;
+
public class JacocoMavenInitializerTest {
private JaCoCoMavenPluginHandler mavenPluginHandler;
private JacocoMavenInitializer initializer;
@@ -54,7 +55,8 @@ public class JacocoMavenInitializerTest {
@Test
public void shouldExecuteMaven() {
Project project = mockProject();
- when(project.getFileSystem().hasTestFiles(argThat(is(Java.INSTANCE)))).thenReturn(true);
+ InputFile inputFile = mock(InputFile.class);
+ when(project.getFileSystem().testFiles(Java.KEY)).thenReturn(Collections.singletonList(inputFile));
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
assertThat(initializer.shouldExecuteOnProject(project), is(true));
@@ -64,7 +66,8 @@ public class JacocoMavenInitializerTest {
@Test
public void shouldNotExecuteMavenWhenReuseReports() {
Project project = mockProject();
- when(project.getFileSystem().hasTestFiles(argThat(is(Java.INSTANCE)))).thenReturn(true);
+ InputFile inputFile = mock(InputFile.class);
+ when(project.getFileSystem().testFiles(Java.KEY)).thenReturn(Collections.singletonList(inputFile));
when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
assertThat(initializer.shouldExecuteOnProject(project), is(false));
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>