From a92d064fc3083a83f0f26fdf58b6cece98360129 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 25 May 2012 16:00:12 +0200 Subject: [PATCH] Improve test coverage --- .../org/sonar/plugins/pmd/PmdTemplate.java | 16 ++-- .../pmd/PmdViolationToRuleViolation.java | 5 -- .../sonar/plugins/pmd/PmdTemplateTest.java | 46 ++++++++++ .../pmd/PmdViolationToRuleViolationTest.java | 88 +++++++++++++++++++ .../java/org/sonar/api/web/FilterTest.java | 62 ++++++++++++- 5 files changed, 199 insertions(+), 18 deletions(-) create mode 100644 plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationToRuleViolationTest.java 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,16 +19,52 @@ */ 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(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java index e29f4d23393..602e5c99b54 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/FilterTest.java @@ -19,16 +19,50 @@ */ 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(""); } + + @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); + } } -- 2.39.5