From bc5f94813ac5859510f019b602dbff37eb661339 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 9 Dec 2014 15:11:09 +0100 Subject: [PATCH] Fix quality flaws --- .../issue/CountUnresolvedIssuesDecorator.java | 33 +++---------------- .../CountUnresolvedIssuesDecoratorTest.java | 24 +++----------- .../org/sonar/server/rule/RuleOperations.java | 24 +++++--------- .../sonar/server/rule/RuleOperationsTest.java | 3 +- 4 files changed, 18 insertions(+), 66 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java index dfb05e23d9a..d775ba8ec4c 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecorator.java @@ -20,45 +20,24 @@ 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; } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java index 600f455ca21..10513e64918 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/CountUnresolvedIssuesDecoratorTest.java @@ -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); diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java index 7f0a6555aff..b4741613aee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java @@ -20,14 +20,12 @@ 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) diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java index b11dcc0f29d..f7b8b8070ac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java @@ -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 -- 2.39.5