]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2333 Add new rule to report about cycles between packages
authorEvgeny Mandrikov <mandrikov@gmail.com>
Thu, 5 Jul 2012 12:38:29 +0000 (18:38 +0600)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Thu, 5 Jul 2012 13:06:30 +0000 (19:06 +0600)
* Add rule to the "Sonar way" profile
* Compute cost of fix of violation

plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/squid/check/CycleBetweenPackagesCheck.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarWayProfile.java
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/DesignBridge.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SonarWayProfileTest.java

index dfa13c23536cec17b4e86cac629b9953c55089e6..2abc22ca0e27204b394cfc20217e9eb45fac9234 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.java.squid.check;
 
 import org.sonar.api.checks.CheckFactory;
 import org.sonar.api.rules.ActiveRule;
+import org.sonar.check.BelongsToProfile;
 import org.sonar.check.Priority;
 import org.sonar.check.Rule;
 
@@ -28,8 +29,11 @@ import javax.annotation.CheckForNull;
 
 /**
  * Companion of {@link org.sonar.plugins.squid.bridges.DesignBridge} which actually does the job on finding cycles and creation of violations.
+ *
+ * @since 3.2
  */
 @Rule(key = "CycleBetweenPackages", priority = Priority.MAJOR)
+@BelongsToProfile(title = "Sonar way", priority = Priority.MAJOR)
 public class CycleBetweenPackagesCheck extends SquidCheck {
 
   /**
index 2a999df7b23caf89c918f5f74181f9fad0212e5f..5c0cdb89b17467275dbc500e076730c2f95d2fda 100644 (file)
  */
 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.resources.Java;
 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;
@@ -39,8 +34,6 @@ public class SonarWayProfile extends ProfileDefinition {
 
   @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);
+    return annotationProfileParser.parse(SquidConstants.REPOSITORY_KEY, RulesProfile.SONAR_WAY_NAME, Java.KEY, SquidRuleRepository.getCheckClasses(), messages);
   }
-}
\ No newline at end of file
+}
index 6e279b03220902b3bed6a9c5f6a1ba908b3ac6e4..19685cd04f9fc1043991a08de610b38b6ab7be29 100644 (file)
@@ -165,7 +165,8 @@ public class DesignBridge extends Bridge {
         // If resource cannot be obtained, then silently ignore, because anyway warning will be printed by method saveEdge
         if ((fromFile != null) && (toFile != null)) {
           Violation violation = Violation.create(rule, fromFile)
-              .setMessage("Remove the dependency on the source file \"" + toFile.getLongName() + "\" to break a package cycle.");
+              .setMessage("Remove the dependency on the source file \"" + toFile.getLongName() + "\" to break a package cycle.")
+              .setCost((double) subEdge.getWeight());
           context.saveViolation(violation);
         }
       }
index 204a6fb66db0eaf650351d6bf429252c652b2368..b72b2ffc10e8c9c3b69f51e33e8c03b37336abab 100644 (file)
@@ -41,7 +41,7 @@ public class SonarWayProfileTest {
     SonarWayProfile definition = new SonarWayProfile(new AnnotationProfileParser(ruleFinder()));
     RulesProfile profile = definition.createProfile(validation);
 
-    assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(1);
+    assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(2);
     assertThat(profile.getName()).isEqualTo(RulesProfile.SONAR_WAY_NAME);
     assertThat(validation.hasErrors()).isFalse();
   }
@@ -53,7 +53,7 @@ public class SonarWayProfileTest {
     SonarWayWithFindbugsProfile definition = new SonarWayWithFindbugsProfile(new SonarWayProfile(new AnnotationProfileParser(ruleFinder())));
     RulesProfile profile = definition.createProfile(validation);
 
-    assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(1);
+    assertThat(profile.getActiveRulesByRepository(SquidConstants.REPOSITORY_KEY)).hasSize(2);
     assertThat(profile.getName()).isEqualTo(RulesProfile.SONAR_WAY_FINDBUGS_NAME);
     assertThat(validation.hasErrors()).isFalse();
   }