]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3577 Add "Avoid commented-out lines of code" rule to Sonar Way
authorDavid Gageot <david@gageot.net>
Mon, 18 Jun 2012 12:15:02 +0000 (14:15 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 18 Jun 2012 12:30:14 +0000 (14:30 +0200)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/check/CommentedOutCodeLineCheck.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayProfile.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayWithFindbugsProfile.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidPlugin.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SonarWayProfileTest.java [new file with mode: 0644]
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidPluginTest.java

index 6d600c756fec7691023a149808c16572ab9cd6a3..13bae7d030444c764d12efc93683978734a14f26 100644 (file)
@@ -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 (file)
index 0000000..2a999df
--- /dev/null
@@ -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<Class> rules = ImmutableList.<Class> 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 (file)
index 0000000..7e47af0
--- /dev/null
@@ -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;
+  }
+}
+
index e67b02d531e36860b4d527d855a3730a5dd815fe..d0c69c613bc41301cb9fcc59a4f4b76f98b630b4 100644 (file)
@@ -67,14 +67,15 @@ public final class SquidPlugin extends SonarPlugin {
 
   public List<Class<? extends Extension>> 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 (file)
index 0000000..204a6fb
--- /dev/null
@@ -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<Rule>() {
+      public Rule answer(InvocationOnMock invocation) {
+        Object[] arguments = invocation.getArguments();
+        return Rule.create((String) arguments[0], (String) arguments[1], (String) arguments[1]);
+      }
+    }).getMock();
+  }
+}
index c42ac5ecb76d8c24aebd1c06baad7d8967b2aaf9..7e99769c1f3837d3017f1caabfc81947e963a187 100644 (file)
@@ -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);
   }
 }