From: David Gageot Date: Mon, 18 Jun 2012 12:15:02 +0000 (+0200) Subject: SONAR-3577 Add "Avoid commented-out lines of code" rule to Sonar Way X-Git-Tag: 3.2~312 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4dc6769693d35d1716541ef58dcb391abac840de;p=sonarqube.git SONAR-3577 Add "Avoid commented-out lines of code" rule to Sonar Way --- diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java index 6d600c756fe..13bae7d0304 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java @@ -19,6 +19,8 @@ */ package org.sonar.java.ast.check; +import org.sonar.check.BelongsToProfile; + import java.util.Arrays; import java.util.List; import java.util.Set; @@ -42,6 +44,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; * @since 2.13 */ @Rule(key = "CommentedOutCodeLine", priority = Priority.MAJOR) +@BelongsToProfile(title = "Sonar way", priority = Priority.MAJOR) public class CommentedOutCodeLineCheck extends JavaAstVisitor { private static final double THRESHOLD = 0.9; diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayProfile.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayProfile.java new file mode 100644 index 00000000000..2a999df7b23 --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayProfile.java @@ -0,0 +1,46 @@ +/* + * 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.squid; + +import org.sonar.api.resources.Java; + +import com.google.common.collect.ImmutableList; +import org.sonar.api.profiles.AnnotationProfileParser; +import org.sonar.api.profiles.ProfileDefinition; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.java.ast.check.CommentedOutCodeLineCheck; + +import java.util.Collection; + +public class SonarWayProfile extends ProfileDefinition { + private final AnnotationProfileParser annotationProfileParser; + + public SonarWayProfile(AnnotationProfileParser annotationProfileParser) { + this.annotationProfileParser = annotationProfileParser; + } + + @Override + public RulesProfile createProfile(ValidationMessages messages) { + Collection rules = ImmutableList. of(CommentedOutCodeLineCheck.class); + + return annotationProfileParser.parse(SquidConstants.REPOSITORY_KEY, RulesProfile.SONAR_WAY_NAME, Java.KEY, rules, messages); + } +} \ No newline at end of file diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayWithFindbugsProfile.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayWithFindbugsProfile.java new file mode 100644 index 00000000000..7e47af0cdae --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayWithFindbugsProfile.java @@ -0,0 +1,40 @@ +/* + * 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.squid; + +import org.sonar.api.profiles.ProfileDefinition; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.utils.ValidationMessages; + +public class SonarWayWithFindbugsProfile extends ProfileDefinition { + private final SonarWayProfile sonarWay; + + public SonarWayWithFindbugsProfile(SonarWayProfile sonarWay) { + this.sonarWay = sonarWay; + } + + @Override + public RulesProfile createProfile(ValidationMessages validationMessages) { + RulesProfile profile = sonarWay.createProfile(validationMessages); + profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME); + return profile; + } +} + diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java index e67b02d531e..d0c69c613bc 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java @@ -67,14 +67,15 @@ public final class SquidPlugin extends SonarPlugin { public List> getExtensions() { return ImmutableList.of( - SquidSensor.class, - SquidRuleRepository.class, - JavaSourceImporter.class, + ChidamberKemererDistributionBuilder.class, + ClassesDecorator.class, FileComplexityDistributionDecorator.class, FunctionComplexityDistributionBuilder.class, - ClassesDecorator.class, - ChidamberKemererDistributionBuilder.class, - FunctionsDecorator.class); + FunctionsDecorator.class, + JavaSourceImporter.class, + SonarWayProfile.class, + SonarWayWithFindbugsProfile.class, + SquidRuleRepository.class, + SquidSensor.class); } - } diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SonarWayProfileTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SonarWayProfileTest.java new file mode 100644 index 00000000000..204a6fb66db --- /dev/null +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SonarWayProfileTest.java @@ -0,0 +1,69 @@ +/* + * 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.squid; + +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.profiles.AnnotationProfileParser; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.utils.ValidationMessages; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SonarWayProfileTest { + @Test + public void should_create_sonar_way_profile() { + ValidationMessages validation = ValidationMessages.create(); + + SonarWayProfile definition = new SonarWayProfile(new AnnotationProfileParser(ruleFinder())); + RulesProfile profile = definition.createProfile(validation); + + assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(1); + assertThat(profile.getName()).isEqualTo(RulesProfile.SONAR_WAY_NAME); + assertThat(validation.hasErrors()).isFalse(); + } + + @Test + public void should_create_sonar_way_with_findbugs_profile() { + ValidationMessages validation = ValidationMessages.create(); + + SonarWayWithFindbugsProfile definition = new SonarWayWithFindbugsProfile(new SonarWayProfile(new AnnotationProfileParser(ruleFinder()))); + RulesProfile profile = definition.createProfile(validation); + + assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(1); + assertThat(profile.getName()).isEqualTo(RulesProfile.SONAR_WAY_FINDBUGS_NAME); + assertThat(validation.hasErrors()).isFalse(); + } + + static RuleFinder ruleFinder() { + return when(mock(RuleFinder.class).findByKey(anyString(), anyString())).thenAnswer(new Answer() { + public Rule answer(InvocationOnMock invocation) { + Object[] arguments = invocation.getArguments(); + return Rule.create((String) arguments[0], (String) arguments[1], (String) arguments[1]); + } + }).getMock(); + } +} diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java index c42ac5ecb76..7e99769c1f3 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java @@ -21,13 +21,11 @@ package org.sonar.plugins.squid; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.fest.assertions.Assertions.assertThat; public class SquidPluginTest { - @Test public void coverageForFun() { - assertThat(new SquidPlugin().getExtensions().size(), is(8)); + assertThat(new SquidPlugin().getExtensions()).hasSize(10); } }