aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-05-25 16:00:12 +0200
committerDavid Gageot <david@gageot.net>2012-05-25 16:29:25 +0200
commita92d064fc3083a83f0f26fdf58b6cece98360129 (patch)
tree190f28836d0494b9eac896d24d0bef4de1a485bb /plugins
parent28ad4901ffc85a84d1b0a7458ae48e54a7b0a2b4 (diff)
downloadsonarqube-a92d064fc3083a83f0f26fdf58b6cece98360129.tar.gz
sonarqube-a92d064fc3083a83f0f26fdf58b6cece98360129.zip
Improve test coverage
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java16
-rw-r--r--plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java5
-rw-r--r--plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java46
-rw-r--r--plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java88
4 files changed, 140 insertions, 15 deletions
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java
index 83c6123e6d9..ff16b039b71 100644
--- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java
+++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java
@@ -50,10 +50,15 @@ public class PmdTemplate {
private final PMD pmd;
public PmdTemplate(String javaVersion) {
- pmd = new PMD();
+ this(new PMD());
setJavaVersion(pmd, javaVersion);
}
+ @VisibleForTesting
+ PmdTemplate(PMD pmd) {
+ this.pmd = pmd;
+ }
+
public void process(InputFile inputFile, Charset encoding, RuleSets rulesets, RuleContext ruleContext) {
File file = inputFile.getFile();
ruleContext.setSourceCodeFilename(file.getAbsolutePath());
@@ -75,10 +80,6 @@ public class PmdTemplate {
@VisibleForTesting
static void setJavaVersion(PMD pmd, String javaVersion) {
String version = normalize(javaVersion);
- if (version == null) {
- return; // Do nothing
- }
-
SourceType sourceType = SourceType.getSourceTypeForId("java " + version);
if (sourceType == null) {
throw new SonarException("Unsupported Java version for PMD: " + version);
@@ -91,9 +92,4 @@ public class PmdTemplate {
private static String normalize(String version) {
return Functions.forMap(JAVA_VERSIONS, version).apply(version);
}
-
- @Override
- public String toString() {
- return getClass().getSimpleName();
- }
}
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java
index f1d40dbb420..995038f96cf 100644
--- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java
+++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java
@@ -74,9 +74,4 @@ public class PmdViolationToRuleViolation implements BatchExtension {
}
return ruleFinder.findByKey(PmdConstants.TEST_REPOSITORY_KEY, ruleKey);
}
-
- @Override
- public String toString() {
- return getClass().getSimpleName();
- }
}
diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
index 016fa3f02a6..a00dbd851f1 100644
--- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
+++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
@@ -19,17 +19,53 @@
*/
package org.sonar.plugins.pmd;
+import com.google.common.base.Charsets;
import net.sourceforge.pmd.PMD;
+import net.sourceforge.pmd.PMDException;
+import net.sourceforge.pmd.RuleContext;
+import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.SourceType;
import org.junit.Test;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.utils.SonarException;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class PmdTemplateTest {
+ InputFile inputFile = mock(InputFile.class);
+ RuleSets rulesets = mock(RuleSets.class);
+ RuleContext ruleContext = mock(RuleContext.class);
+ InputStream inputStream = mock(InputStream.class);
PMD pmd = mock(PMD.class);
@Test
+ public void should_process_input_file() throws PMDException, FileNotFoundException {
+ when(inputFile.getFile()).thenReturn(new File("source.java"));
+ when(inputFile.getInputStream()).thenReturn(inputStream);
+
+ new PmdTemplate(pmd).process(inputFile, Charsets.UTF_8, rulesets, ruleContext);
+
+ verify(ruleContext).setSourceCodeFilename(new File("source.java").getAbsolutePath());
+ verify(pmd).processFile(inputStream, Charsets.UTF_8.displayName(), rulesets, ruleContext);
+ }
+
+ @Test
+ public void should_ignore_PMD_error() throws PMDException, FileNotFoundException {
+ when(inputFile.getFile()).thenReturn(new File("source.java"));
+ when(inputFile.getInputStream()).thenReturn(inputStream);
+ doThrow(new PMDException("BUG")).when(pmd).processFile(inputStream, Charsets.UTF_8.displayName(), rulesets, ruleContext);
+
+ new PmdTemplate(pmd).process(inputFile, Charsets.UTF_8, rulesets, ruleContext);
+ }
+
+ @Test
public void should_set_java11_version() {
PmdTemplate.setJavaVersion(pmd, "1.1");
@@ -56,4 +92,14 @@ public class PmdTemplateTest {
verify(pmd).setJavaVersion(SourceType.JAVA_16);
}
+
+ @Test(expected = SonarException.class)
+ public void should_fail_on_invalid_java_version() {
+ new PmdTemplate("12.2");
+ }
+
+ @Test
+ public void shouldnt_fail_on_valid_java_version() {
+ new PmdTemplate("6");
+ }
}
diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java
new file mode 100644
index 00000000000..0a2d19eeac9
--- /dev/null
+++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java
@@ -0,0 +1,88 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.pmd;
+
+import net.sourceforge.pmd.IRuleViolation;
+import net.sourceforge.pmd.Rule;
+import org.junit.Test;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.Violation;
+
+import java.io.File;
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.test.MoreConditions.reflectionEqualTo;
+
+public class PmdViolationToRuleViolationTest {
+ private org.sonar.api.rules.Rule sonarRule = org.sonar.api.rules.Rule.create("pmd", "RULE");
+ private ProjectFileSystem projectFileSystem = mock(ProjectFileSystem.class);
+ private IRuleViolation pmdViolation = mock(IRuleViolation.class);
+ private SensorContext context = mock(SensorContext.class);
+ private RuleFinder ruleFinder = mock(RuleFinder.class);
+ private Rule rule = mock(Rule.class);
+
+ @Test
+ public void should_convert_pmd_violation_to_sonar_violation() {
+ when(projectFileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/src")));
+ when(pmdViolation.getFilename()).thenReturn("/src/source.java");
+ when(pmdViolation.getBeginLine()).thenReturn(42);
+ when(pmdViolation.getDescription()).thenReturn("Description");
+ when(pmdViolation.getRule()).thenReturn(rule);
+ when(rule.getName()).thenReturn("RULE");
+ when(context.getResource(new JavaFile("[default].source"))).thenReturn(new JavaFile("[default].source"));
+ when(ruleFinder.findByKey("pmd", "RULE")).thenReturn(sonarRule);
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).is(reflectionEqualTo(Violation.create(sonarRule, new JavaFile("[default].source")).setLineId(42).setMessage("Description")));
+ }
+
+ @Test
+ public void should_ignore_violation_on_unknown_resource() {
+ when(projectFileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/src")));
+ when(pmdViolation.getFilename()).thenReturn("/src/UNKNOWN.java");
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).isNull();
+ }
+
+ @Test
+ public void should_ignore_violation_on_unknown_rule() {
+ when(projectFileSystem.getTestDirs()).thenReturn(Arrays.asList(new File("/test")));
+ when(pmdViolation.getFilename()).thenReturn("/test/source.java");
+ when(pmdViolation.getRule()).thenReturn(rule);
+ when(rule.getName()).thenReturn("UNKNOWN");
+ when(context.getResource(new JavaFile("[default].source"))).thenReturn(new JavaFile("[default].source"));
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).isNull();
+ }
+}