From 38f6fc18ff2c25290662783fb10979a144a9d8fd Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 1 Jul 2014 18:48:35 +0200 Subject: [PATCH] Fix quality flaws --- .../sonar/server/issue/RulesAggregation.java | 7 +- .../server/issue/RulesAggregationTest.java | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java diff --git a/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java b/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java index 433cfba56f7..f6f0fb7fade 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java @@ -82,9 +82,6 @@ public class RulesAggregation { Rule rule = (Rule) o; - if (!name.equals(rule.name)) { - return false; - } if (!ruleKey.equals(rule.ruleKey)) { return false; } @@ -94,9 +91,7 @@ public class RulesAggregation { @Override public int hashCode() { - int result = ruleKey.hashCode(); - result = 31 * result + name.hashCode(); - return result; + return ruleKey.hashCode(); } } } diff --git a/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java b/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java new file mode 100644 index 00000000000..31cb3fbc9e9 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/issue/RulesAggregationTest.java @@ -0,0 +1,64 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.issue; + +import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.core.rule.RuleDto; +import org.sonar.server.rule.RuleTesting; + +import static org.fest.assertions.Assertions.assertThat; + +public class RulesAggregationTest { + + @Test + public void empty() throws Exception { + RulesAggregation rulesAggregation = new RulesAggregation(); + assertThat(rulesAggregation.rules()).isEmpty(); + } + + @Test + public void count_rules() throws Exception { + RulesAggregation rulesAggregation = new RulesAggregation(); + RuleKey ruleKey = RuleKey.of("xoo", "S001"); + RuleDto ruleDto = RuleTesting.newDto(ruleKey).setName("Rule name"); + rulesAggregation.add(ruleDto); + rulesAggregation.add(ruleDto); + + RulesAggregation.Rule rule = new RulesAggregation.Rule().setRuleKey(ruleKey).setName("Rule name"); + + assertThat(rulesAggregation.rules()).hasSize(1); + assertThat(rulesAggregation.rules().iterator().next().name()).isEqualTo("Rule name"); + assertThat(rulesAggregation.countRule(rule)).isEqualTo(2); + } + + @Test + public void count_rules_with_different_rules() throws Exception { + RulesAggregation rulesAggregation = new RulesAggregation(); + + RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("xoo", "S001")).setName("Rule name 1"); + rulesAggregation.add(ruleDto); + rulesAggregation.add(ruleDto); + rulesAggregation.add(RuleTesting.newDto(RuleKey.of("xoo", "S002")).setName("Rule name 2")); + + assertThat(rulesAggregation.rules()).hasSize(2); + } +} -- 2.39.5