]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 9 Dec 2014 14:11:09 +0000 (15:11 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 9 Dec 2014 14:26:40 +0000 (15:26 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java

index dfb05e23d9a5670d2e781c49e60ace8fefc46cc3..d775ba8ec4c1a5a8937d45216c79c92d85e07f62 100644 (file)
 package org.sonar.plugins.core.issue;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.HashMultiset;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Multiset;
-import com.google.common.collect.Sets;
+import com.google.common.collect.*;
 import org.apache.commons.lang.time.DateUtils;
-import org.sonar.api.batch.Decorator;
-import org.sonar.api.batch.DecoratorBarriers;
-import org.sonar.api.batch.DecoratorContext;
-import org.sonar.api.batch.DependedUpon;
-import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.batch.*;
 import org.sonar.api.component.ResourcePerspectives;
 import org.sonar.api.issue.Issuable;
 import org.sonar.api.issue.Issue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasureUtils;
-import org.sonar.api.measures.MeasuresFilters;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.RuleMeasure;
+import org.sonar.api.measures.*;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.ResourceUtils;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.batch.components.Period;
 import org.sonar.batch.components.TimeMachineConfiguration;
 
 import javax.annotation.Nullable;
 
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Computes metrics related to number of issues.
@@ -69,12 +48,10 @@ import java.util.Set;
 public class CountUnresolvedIssuesDecorator implements Decorator {
 
   private final ResourcePerspectives perspectives;
-  private final RuleFinder rulefinder;
   private final TimeMachineConfiguration timeMachineConfiguration;
 
-  public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, RuleFinder rulefinder, TimeMachineConfiguration timeMachineConfiguration) {
+  public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, TimeMachineConfiguration timeMachineConfiguration) {
     this.perspectives = perspectives;
-    this.rulefinder = rulefinder;
     this.timeMachineConfiguration = timeMachineConfiguration;
   }
 
index 600f455ca21dd4f504a5aed457aa727105132aa0..10513e64918c40b606de77508227b9b63f4f2249 100644 (file)
@@ -31,18 +31,13 @@ import org.sonar.api.component.ResourcePerspectives;
 import org.sonar.api.issue.Issuable;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.internal.DefaultIssue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasuresFilter;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.RuleMeasure;
+import org.sonar.api.measures.*;
 import org.sonar.api.resources.Project;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RulePriority;
 import org.sonar.api.test.IsRuleMeasure;
 import org.sonar.batch.components.Period;
@@ -59,18 +54,12 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyDouble;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 public class CountUnresolvedIssuesDecoratorTest {
 
   CountUnresolvedIssuesDecorator decorator;
   TimeMachineConfiguration timeMachineConfiguration;
-  RuleFinder ruleFinder;
   Issuable issuable;
   DecoratorContext context;
   Resource resource;
@@ -91,11 +80,6 @@ public class CountUnresolvedIssuesDecoratorTest {
     ruleA2 = Rule.create().setRepositoryKey("ruleA2").setKey("ruleA2").setName("nameA2");
     ruleB1 = Rule.create().setRepositoryKey("ruleB1").setKey("ruleB1").setName("nameB1");
 
-    ruleFinder = mock(RuleFinder.class);
-    when(ruleFinder.findByKey(ruleA1.ruleKey())).thenReturn(ruleA1);
-    when(ruleFinder.findByKey(ruleA2.ruleKey())).thenReturn(ruleA2);
-    when(ruleFinder.findByKey(ruleB1.ruleKey())).thenReturn(ruleB1);
-
     rightNow = new Date();
     tenDaysAgo = DateUtils.addDays(rightNow, -10);
     afterTenDaysAgo = DateUtils.addDays(tenDaysAgo, 1);
@@ -116,7 +100,7 @@ public class CountUnresolvedIssuesDecoratorTest {
     issuable = mock(Issuable.class);
     ResourcePerspectives perspectives = mock(ResourcePerspectives.class);
     when(perspectives.as(Issuable.class, resource)).thenReturn(issuable);
-    decorator = new CountUnresolvedIssuesDecorator(perspectives, ruleFinder, timeMachineConfiguration);
+    decorator = new CountUnresolvedIssuesDecorator(perspectives, timeMachineConfiguration);
   }
 
   @Test
@@ -139,7 +123,7 @@ public class CountUnresolvedIssuesDecoratorTest {
   public void should_do_nothing_when_issuable_is_null() {
     ResourcePerspectives perspectives = mock(ResourcePerspectives.class);
     when(perspectives.as(Issuable.class, resource)).thenReturn(null);
-    CountUnresolvedIssuesDecorator decorator = new CountUnresolvedIssuesDecorator(perspectives, ruleFinder, timeMachineConfiguration);
+    CountUnresolvedIssuesDecorator decorator = new CountUnresolvedIssuesDecorator(perspectives, timeMachineConfiguration);
 
     decorator.decorate(resource, context);
 
index 7f0a6555aff1d279181b548cdb93feddc73598d4..b4741613aeeb9bd28c2672c79d280bb2a7da6840 100644 (file)
 
 package org.sonar.server.rule;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Strings;
 import org.apache.commons.lang.builder.EqualsBuilder;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
-import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.MyBatis;
@@ -48,19 +46,11 @@ import javax.annotation.Nullable;
 public class RuleOperations implements ServerComponent {
 
   private final DbClient dbClient;
-  private final System2 system;
 
   public RuleOperations(DbClient dbClient) {
-    this(dbClient, System2.INSTANCE);
-  }
-
-  @VisibleForTesting
-  RuleOperations(DbClient dbClient, System2 system) {
     this.dbClient = dbClient;
-    this.system = system;
   }
 
-
   public void updateRule(RuleChange ruleChange, UserSession userSession) {
     checkPermission(userSession);
     DbSession session = dbClient.openSession(false);
@@ -80,7 +70,8 @@ public class RuleOperations implements ServerComponent {
         }
       }
 
-      boolean needUpdate = updateRule(ruleDto, subCharacteristic, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset(),
+      boolean needUpdate = updateRule(ruleDto, subCharacteristic, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(),
+        ruleChange.debtRemediationOffset(),
         session);
       if (needUpdate) {
         session.commit();
@@ -93,7 +84,7 @@ public class RuleOperations implements ServerComponent {
   }
 
   public boolean updateRule(RuleDto ruleDto, @Nullable CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
-                            @Nullable String newCoefficient, @Nullable String newOffset, DbSession session) {
+    @Nullable String newCoefficient, @Nullable String newOffset, DbSession session) {
     boolean needUpdate = false;
 
     // A sub-characteristic and a remediation function is given -> update rule debt
@@ -121,7 +112,8 @@ public class RuleOperations implements ServerComponent {
     } else {
       // Rule characteristic is not already disabled -> update it
       if (ruleDto.getSubCharacteristicId() == null || !RuleDto.DISABLED_CHARACTERISTIC_ID.equals(ruleDto.getSubCharacteristicId())) {
-        // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin update
+        // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin
+        // update
         ruleDto.setSubCharacteristicId(ruleDto.getDefaultSubCharacteristicId() != null ? RuleDto.DISABLED_CHARACTERISTIC_ID : null);
         ruleDto.setRemediationFunction(null);
         ruleDto.setRemediationCoefficient(null);
@@ -137,20 +129,20 @@ public class RuleOperations implements ServerComponent {
   }
 
   private static boolean isRuleDebtSameAsDefaultValues(RuleDto ruleDto, CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
-                                                       @Nullable String newCoefficient, @Nullable String newOffset) {
+    @Nullable String newCoefficient, @Nullable String newOffset) {
     return newSubCharacteristic.getId().equals(ruleDto.getDefaultSubCharacteristicId()) &&
       isSameRemediationFunction(newFunction, newCoefficient, newOffset, ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationCoefficient(),
         ruleDto.getDefaultRemediationOffset());
   }
 
   private static boolean isRuleDebtSameAsOverriddenValues(RuleDto ruleDto, CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
-                                                          @Nullable String newCoefficient, @Nullable String newOffset) {
+    @Nullable String newCoefficient, @Nullable String newOffset) {
     return newSubCharacteristic.getId().equals(ruleDto.getSubCharacteristicId())
       && isSameRemediationFunction(newFunction, newCoefficient, newOffset, ruleDto.getRemediationFunction(), ruleDto.getRemediationCoefficient(), ruleDto.getRemediationOffset());
   }
 
   private static boolean isSameRemediationFunction(@Nullable String newFunction, @Nullable String newCoefficient, @Nullable String newOffset,
-                                                   String oldFunction, @Nullable String oldCoefficient, @Nullable String oldOffset) {
+    String oldFunction, @Nullable String oldCoefficient, @Nullable String oldOffset) {
     return new EqualsBuilder()
       .append(oldFunction, newFunction)
       .append(oldCoefficient, newCoefficient)
index b11dcc0f29d12764f5d6cef49a260b9e36b7b7f0..f7b8b8070ac58b69caa76eb8eb47fd0c7e361f5a 100644 (file)
@@ -28,7 +28,6 @@ import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.rule.RuleDto;
@@ -74,7 +73,7 @@ public class RuleOperationsTest {
     when(dbClient.openSession(false)).thenReturn(session);
     when(dbClient.ruleDao()).thenReturn(ruleDao);
     when(dbClient.debtCharacteristicDao()).thenReturn(characteristicDao);
-    operations = new RuleOperations(dbClient, System2.INSTANCE);
+    operations = new RuleOperations(dbClient);
   }
 
   @Test