]> source.dussan.org Git - sonarqube.git/commitdiff
Improve test coverage
authorDavid Gageot <david@gageot.net>
Fri, 25 May 2012 14:00:12 +0000 (16:00 +0200)
committerDavid Gageot <david@gageot.net>
Fri, 25 May 2012 14:29:25 +0000 (16:29 +0200)
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdTemplate.java
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationToRuleViolation.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java

index 83c6123e6d917051ba02351c836cbde907174d9a..ff16b039b71c2d49ab8d85cbd03bc1a04a84467b 100644 (file)
@@ -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();
-  }
 }
index f1d40dbb42053eb3818b65e455adadaa6ad40d7c..995038f96cf69a2233a55ea36d55823644ccea95 100644 (file)
@@ -74,9 +74,4 @@ public class PmdViolationToRuleViolation implements BatchExtension {
     }
     return ruleFinder.findByKey(PmdConstants.TEST_REPOSITORY_KEY, ruleKey);
   }
-
-  @Override
-  public String toString() {
-    return getClass().getSimpleName();
-  }
 }
index 016fa3f02a65fb74aefab93b0be1cc6ea31b4ac5..a00dbd851f18a84e6e83cb754b0ded6fe6698286 100644 (file)
  */
 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 (file)
index 0000000..0a2d19e
--- /dev/null
@@ -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();
+  }
+}
index e29f4d23393083c51f6411ab90547aee80884f10..602e5c99b54d4e6a410c533b476c1a791b0b09a3 100644 (file)
  */
 package org.sonar.api.web;
 
-import org.junit.rules.ExpectedException;
-
 import org.junit.Rule;
-
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.fest.assertions.Assertions.assertThat;
 
 public class FilterTest {
   @Rule
   public ExpectedException exception = ExpectedException.none();
 
+  @Test
+  public void should_create_filter() {
+    Filter filter = Filter.create()
+        .setDisplayAs("list")
+        .setFavouritesOnly(true)
+        .setPageSize(100);
+
+    assertThat(filter.getDisplayAs()).isEqualTo("list");
+    assertThat(filter.isFavouritesOnly()).isTrue();
+    assertThat(filter.getPageSize()).isEqualTo(100);
+  }
+
+  @Test
+  public void should_add_criteria() {
+    Criterion criterion1 = Criterion.createForQualifier("A");
+    Criterion criterion2 = Criterion.createForQualifier("A");
+    Filter filter = Filter.create()
+        .add(criterion1)
+        .add(criterion2);
+
+    assertThat(filter.getCriteria()).containsExactly(criterion1, criterion2);
+  }
+
+  @Test
+  public void should_add_columns() {
+    FilterColumn column1 = FilterColumn.create("", "", "ASC", false);
+    FilterColumn column2 = FilterColumn.create("", "", "DESC", false);
+    Filter filter = Filter.create()
+        .add(column1)
+        .add(column2);
+
+    assertThat(filter.getColumns()).containsExactly(column1, column2);
+  }
+
   @Test
   public void should_accept_valid_periods() {
     Filter.create().setDisplayAs("list");
@@ -42,4 +76,26 @@ public class FilterTest {
 
     Filter.create().setDisplayAs("<invalid>");
   }
+
+  @Test
+  public void should_accept_valid_pageSize() {
+    Filter.create().setPageSize(20);
+    Filter.create().setPageSize(200);
+  }
+
+  @Test
+  public void should_fail_on_pageSize_too_small() {
+    exception.expect(IllegalArgumentException.class);
+    exception.expectMessage("page size should be between 20 and 200");
+
+    Filter.create().setPageSize(19);
+  }
+
+  @Test
+  public void should_fail_on_pageSize_too_high() {
+    exception.expect(IllegalArgumentException.class);
+    exception.expectMessage("page size should be between 20 and 200");
+
+    Filter.create().setPageSize(201);
+  }
 }