From d91a7d94b623089b82205d8758d1e1ec482dcd9c Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Mon, 29 Jan 2018 16:06:16 +0100 Subject: [PATCH] SONAR-10313 Fix incorrect equlity test on RuleDefinitionDto --- .../org/sonar/db/rule/RuleDefinitionDto.java | 4 +- .../sonar/db/rule/RuleDefinitionDtoTest.java | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java 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 0321e74d430..b33dfbce2d9 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 @@ -318,13 +318,13 @@ public class RuleDefinitionDto { @Override public boolean equals(Object obj) { - if (!(obj instanceof RuleDto)) { + if (!(obj instanceof RuleDefinitionDto)) { return false; } if (this == obj) { return true; } - RuleDto other = (RuleDto) obj; + RuleDefinitionDto other = (RuleDefinitionDto) obj; return new EqualsBuilder() .append(repositoryKey, other.getRepositoryKey()) .append(ruleKey, other.getRuleKey()) 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 new file mode 100644 index 00000000000..7a76e138215 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java @@ -0,0 +1,79 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.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; + +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)) + ); + + // Comparison is done only with repository key and repository key + assertThat(underTest).isNotEqualTo(new RuleDefinitionDto() + .setRepositoryKey(randomAlphanumeric(9)) + .setRuleKey(ruleKey) + ); + } +} -- 2.39.5