diff options
author | David Gageot <david@gageot.net> | 2012-05-05 10:54:25 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-05-07 07:29:49 +0200 |
commit | ff684ecc918063feff56ea7ecb4603d13cb3fbc2 (patch) | |
tree | dfad1a491cf9369354d3593c5813213c1dace13a /plugins/sonar-pmd-plugin | |
parent | 23ce7323fc6044a4d889a3914112b1f93a7470b6 (diff) | |
download | sonarqube-ff684ecc918063feff56ea7ecb4603d13cb3fbc2.tar.gz sonarqube-ff684ecc918063feff56ea7ecb4603d13cb3fbc2.zip |
Improve Unit Test coverage on PMD Plugin
Diffstat (limited to 'plugins/sonar-pmd-plugin')
4 files changed, 46 insertions, 9 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 c0dd7de8a1e..f38f4ae6af0 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 @@ -93,7 +93,7 @@ public class PmdExecutor implements BatchExtension { RuleSets rulesets = createRulesets(repositoryKey); for (InputFile file : files) { - pmdFactory.process(file.getFile(), encoding, rulesets, ruleContext); + pmdFactory.process(file, encoding, rulesets, ruleContext); } } 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 c24d1fa21ed..83c6123e6d9 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 @@ -30,10 +30,10 @@ import net.sourceforge.pmd.RuleSets; import net.sourceforge.pmd.SourceType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonar.api.resources.InputFile; import org.sonar.api.utils.SonarException; import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; import java.nio.charset.Charset; import java.util.Map; @@ -54,12 +54,13 @@ public class PmdTemplate { setJavaVersion(pmd, javaVersion); } - public void process(File file, Charset encoding, RuleSets rulesets, RuleContext ruleContext) { + public void process(InputFile inputFile, Charset encoding, RuleSets rulesets, RuleContext ruleContext) { + File file = inputFile.getFile(); ruleContext.setSourceCodeFilename(file.getAbsolutePath()); InputStream inputStream = null; try { - inputStream = new FileInputStream(file); + inputStream = inputFile.getInputStream(); pmd.processFile(inputStream, encoding.displayName(), rulesets, ruleContext); } catch (PMDException e) { diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java index 7da11466987..9f0ee2eed7c 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java @@ -75,8 +75,8 @@ public class PmdExecutorTest { Report report = pmdExecutor.execute(); - verify(pmdTemplate).process(eq(new File("src/Class.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); - verify(pmdTemplate).process(eq(new File("test/ClassTest.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); + verify(pmdTemplate).process(eq(srcFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); + verify(pmdTemplate).process(eq(tstFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); assertThat(report).isNotNull(); } @@ -116,7 +116,7 @@ public class PmdExecutorTest { pmdExecutor.execute(); - verify(pmdTemplate).process(eq(new File("src/Class.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); + verify(pmdTemplate).process(eq(srcFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class)); verifyNoMoreInteractions(pmdTemplate); } diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java index 50e7f46106a..ac617358b2b 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java @@ -19,6 +19,12 @@ */ package org.sonar.plugins.pmd; +import org.junit.rules.ExpectedException; + +import org.junit.Rule; + +import org.sonar.api.utils.XmlParserException; + import com.google.common.collect.Iterators; import net.sourceforge.pmd.IRuleViolation; import net.sourceforge.pmd.Report; @@ -47,20 +53,34 @@ public class PmdSensorTest { SensorContext sensorContext = mock(SensorContext.class); Violation violation = mock(Violation.class); + @Rule + public ExpectedException exception = ExpectedException.none(); + @Before public void setUpPmdSensor() { pmdSensor = new PmdSensor(profile, executor, pmdViolationToRuleViolation); } @Test - public void should_execute_on_project_with_main_files_and_active_rules() { + public void should_execute_on_project_without_main_files() { + when(project.getFileSystem().mainFiles(Java.KEY).isEmpty()).thenReturn(true); + + boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project); + + assertThat(shouldExecute).isTrue(); + } + + @Test + public void should_execute_on_project_without_test_files() { + when(project.getFileSystem().testFiles(Java.KEY).isEmpty()).thenReturn(true); + boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project); assertThat(shouldExecute).isTrue(); } @Test - public void should_not_execute_on_project_without_files() { + public void should_not_execute_on_project_without_any_files() { when(project.getFileSystem().mainFiles(Java.KEY).isEmpty()).thenReturn(true); when(project.getFileSystem().testFiles(Java.KEY).isEmpty()).thenReturn(true); @@ -114,6 +134,22 @@ public class PmdSensorTest { verifyZeroInteractions(sensorContext); } + @Test + public void should_report_analyse_failure() { + when(executor.execute()).thenThrow(new RuntimeException()); + + exception.expect(XmlParserException.class); + + pmdSensor.analyse(project, sensorContext); + } + + @Test + public void should_to_string() { + String toString = pmdSensor.toString(); + + assertThat(toString).isEqualTo("PmdSensor"); + } + static IRuleViolation violation() { return mock(IRuleViolation.class); } |