]> source.dussan.org Git - sonarqube.git/commitdiff
Improve Unit Test coverage on PMD Plugin
authorDavid Gageot <david@gageot.net>
Sat, 5 May 2012 08:54:25 +0000 (10:54 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 7 May 2012 05:29:49 +0000 (07:29 +0200)
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java

index c0dd7de8a1eb24278dd0e1b3c51905a869589e01..f38f4ae6af002326b076ee348648d17e6d39d8b2 100644 (file)
@@ -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);
     }
   }
 
index c24d1fa21ede6d6aee4ec681eac1197544cfc00d..83c6123e6d917051ba02351c836cbde907174d9a 100644 (file)
@@ -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) {
index 7da11466987883a49d2694fe0894fca7fa311177..9f0ee2eed7c1259db715d4ed07a2dacb7151e89c 100644 (file)
@@ -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);
   }
 
index 50e7f46106af727eba85fe4c9ddb9fb51774b9c0..ac617358b2bd40ab9466a7324c2e457a972aca7c 100644 (file)
  */
 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);
   }