aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-02 18:08:20 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-08 13:41:00 +0100
commitbcce6277e32d0b4852402dae8d03aeb198a8225a (patch)
treec33f40bddee392af47173822dcad8d4a9d196e30 /server
parentb2b9d1a5bf8d3adbf8a601bc47a583ce7048d0e5 (diff)
downloadsonarqube-bcce6277e32d0b4852402dae8d03aeb198a8225a.tar.gz
sonarqube-bcce6277e32d0b4852402dae8d03aeb198a8225a.zip
SONAR-10307 RuleDefinitionDto equals/hashcode based on id
instead of rule key and repository, because now, rule key and repository of a rule can change
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java11
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java69
2 files changed, 29 insertions, 51 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java
index b33dfbce2d9..942f8ca866c 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java
@@ -21,12 +21,12 @@ package org.sonar.db.rule;
import java.util.Arrays;
import java.util.HashSet;
+import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
@@ -325,22 +325,19 @@ public class RuleDefinitionDto {
return true;
}
RuleDefinitionDto other = (RuleDefinitionDto) obj;
- return new EqualsBuilder()
- .append(repositoryKey, other.getRepositoryKey())
- .append(ruleKey, other.getRuleKey())
- .isEquals();
+ return Objects.equals(id, other.id);
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
- .append(repositoryKey)
- .append(ruleKey)
+ .append(id)
.toHashCode();
}
public static RuleDto createFor(RuleKey key) {
return new RuleDto()
+ .setId(new HashCodeBuilder(17, 37).append(key.rule()).append(key.repository()).toHashCode())
.setRepositoryKey(key.repository())
.setRuleKey(key.rule());
}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java
index 7a76e138215..3f53992ff2f 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java
@@ -20,60 +20,41 @@
package org.sonar.db.rule;
-import com.google.common.collect.ImmutableSet;
import java.util.Random;
import org.junit.Test;
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.rule.RuleTesting.newRule;
public class RuleDefinitionDtoTest {
private static final Random RANDOM = new Random();
@Test
- public void test_equality() {
- String repositoryKey = randomAlphanumeric(10);
- String ruleKey = randomAlphanumeric(10);
- RuleDefinitionDto underTest = new RuleDefinitionDto().setRepositoryKey(repositoryKey).setRuleKey(ruleKey);
-
- // Comparison is done only with repository key and rule key
- assertThat(underTest).isEqualTo(new RuleDefinitionDto()
- .setRepositoryKey(repositoryKey)
- .setRuleKey(ruleKey));
-
- // All other fields are ignored
- assertThat(underTest).isEqualTo(new RuleDefinitionDto()
- .setRepositoryKey(repositoryKey)
- .setRuleKey(ruleKey)
- .setLanguage(randomAlphanumeric(5))
- .setUpdatedAt(RANDOM.nextInt())
- .setCreatedAt(RANDOM.nextInt())
- .setName(randomAlphanumeric(10))
- .setSeverity(RANDOM.nextInt())
- .setType(RANDOM.nextInt(3))
- .setSystemTags(ImmutableSet.of("test", "test2"))
- .setDescription(randomAlphanumeric(50))
- .setIsTemplate(RANDOM.nextBoolean())
- .setId(RANDOM.nextInt())
- .setTemplateId(RANDOM.nextInt())
- .setDescriptionFormat(RANDOM.nextBoolean() ? RuleDto.Format.HTML : RuleDto.Format.MARKDOWN)
- .setDefRemediationBaseEffort(randomAlphanumeric(10))
- .setDefRemediationFunction(randomAlphanumeric(10))
- .setDefRemediationGapMultiplier(randomAlphanumeric(10))
- .setGapDescription(randomAlphanumeric(50))
- );
-
- // Must not be equal to other rule with other rule key
- assertThat(underTest).isNotEqualTo(new RuleDefinitionDto()
- .setRepositoryKey(repositoryKey)
- .setRuleKey(randomAlphanumeric(9))
- );
+ public void equals_is_based_on_id() {
+ int id = RANDOM.nextInt(153151);
+ RuleDefinitionDto dto = newRule().setId(id);
+
+ assertThat(dto).isEqualTo(dto);
+ assertThat(dto).isEqualTo(newRule().setId(id));
+ assertThat(dto).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id));
+ assertThat(dto).isNotEqualTo(null);
+ assertThat(dto).isNotEqualTo(new Object());
+ assertThat(dto).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id - 1));
+ assertThat(dto).isNotEqualTo(newRule().setId(id + 1));
+ }
- // Comparison is done only with repository key and repository key
- assertThat(underTest).isNotEqualTo(new RuleDefinitionDto()
- .setRepositoryKey(randomAlphanumeric(9))
- .setRuleKey(ruleKey)
- );
+ @Test
+ public void hashcode_is_based_on_id() {
+ int id = RANDOM.nextInt(153151);
+ RuleDefinitionDto dto = newRule().setId(id);
+
+ assertThat(dto.hashCode()).isEqualTo(dto.hashCode());
+ assertThat(dto.hashCode()).isEqualTo(newRule().setId(id).hashCode());
+ assertThat(dto.hashCode()).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id).hashCode());
+ assertThat(dto.hashCode()).isNotEqualTo(null);
+ assertThat(dto.hashCode()).isNotEqualTo(new Object().hashCode());
+ assertThat(dto.hashCode()).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id - 1).hashCode());
+ assertThat(dto.hashCode()).isNotEqualTo(newRule().setId(id + 1).hashCode());
}
}