You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTrackerTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.core.issue.tracking;
  21. import java.util.function.Function;
  22. import org.junit.Test;
  23. import org.sonar.api.rule.RuleKey;
  24. import org.sonar.core.issue.tracking.AbstractTracker.LineAndLineHashAndMessage;
  25. import org.sonar.core.issue.tracking.AbstractTracker.LineAndLineHashKey;
  26. import org.sonar.core.issue.tracking.AbstractTracker.LineAndMessageKey;
  27. import org.sonar.core.issue.tracking.AbstractTracker.LineHashAndMessageKey;
  28. import org.sonar.core.issue.tracking.AbstractTracker.LineHashKey;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. public class AbstractTrackerTest {
  33. private final Trackable base = trackable(RuleKey.of("r1", "r1"), 0, "m1", "hash1");
  34. private final Trackable same = trackable(RuleKey.of("r1", "r1"), 0, "m1", "hash1");
  35. private final Trackable diffRule = trackable(RuleKey.of("r1", "r2"), 0, "m1", "hash1");
  36. private final Trackable diffMessage = trackable(RuleKey.of("r1", "r1"), 0, null, "hash1");
  37. private final Trackable diffLineHash = trackable(RuleKey.of("r1", "r1"), 0, "m1", null);
  38. private final Trackable diffLine = trackable(RuleKey.of("r1", "r1"), null, "m1", "hash1");
  39. @Test
  40. public void lineAndLineHashKey() {
  41. Comparator comparator = new Comparator(LineAndLineHashKey::new);
  42. comparator.sameEqualsAndHashcode(base, same);
  43. comparator.sameEqualsAndHashcode(base, diffMessage);
  44. comparator.differentEquals(base, diffRule);
  45. comparator.differentEquals(base, diffLineHash);
  46. comparator.differentEquals(base, diffLine);
  47. }
  48. @Test
  49. public void lineAndLineHashAndMessage() {
  50. Comparator comparator = new Comparator(LineAndLineHashAndMessage::new);
  51. comparator.sameEqualsAndHashcode(base, same);
  52. comparator.differentEquals(base, diffMessage);
  53. comparator.differentEquals(base, diffRule);
  54. comparator.differentEquals(base, diffLineHash);
  55. comparator.differentEquals(base, diffLine);
  56. }
  57. @Test
  58. public void lineHashAndMessageKey() {
  59. Comparator comparator = new Comparator(LineHashAndMessageKey::new);
  60. comparator.sameEqualsAndHashcode(base, same);
  61. comparator.sameEqualsAndHashcode(base, diffLine);
  62. comparator.differentEquals(base, diffMessage);
  63. comparator.differentEquals(base, diffRule);
  64. comparator.differentEquals(base, diffLineHash);
  65. }
  66. @Test
  67. public void lineAndMessageKey() {
  68. Comparator comparator = new Comparator(LineAndMessageKey::new);
  69. comparator.sameEqualsAndHashcode(base, same);
  70. comparator.sameEqualsAndHashcode(base, diffLineHash);
  71. comparator.differentEquals(base, diffMessage);
  72. comparator.differentEquals(base, diffRule);
  73. comparator.differentEquals(base, diffLine);
  74. }
  75. @Test
  76. public void lineHashKey() {
  77. Comparator comparator = new Comparator(LineHashKey::new);
  78. comparator.sameEqualsAndHashcode(base, same);
  79. comparator.sameEqualsAndHashcode(base, diffLine);
  80. comparator.sameEqualsAndHashcode(base, diffMessage);
  81. comparator.differentEquals(base, diffRule);
  82. comparator.differentEquals(base, diffLineHash);
  83. }
  84. private static Trackable trackable(RuleKey ruleKey, Integer line, String message, String lineHash) {
  85. Trackable trackable = mock(Trackable.class);
  86. when(trackable.getRuleKey()).thenReturn(ruleKey);
  87. when(trackable.getLine()).thenReturn(line);
  88. when(trackable.getMessage()).thenReturn(message);
  89. when(trackable.getLineHash()).thenReturn(lineHash);
  90. return trackable;
  91. }
  92. private static class Comparator {
  93. private final Function<Trackable, AbstractTracker.SearchKey> searchKeyFactory;
  94. private Comparator(Function<Trackable, AbstractTracker.SearchKey> searchKeyFactory) {
  95. this.searchKeyFactory = searchKeyFactory;
  96. }
  97. private void sameEqualsAndHashcode(Trackable t1, Trackable t2) {
  98. AbstractTracker.SearchKey k1 = searchKeyFactory.apply(t1);
  99. AbstractTracker.SearchKey k2 = searchKeyFactory.apply(t2);
  100. assertThat(k1).isEqualTo(k1);
  101. assertThat(k1).isEqualTo(k2);
  102. assertThat(k2).isEqualTo(k1);
  103. assertThat(k1).hasSameHashCodeAs(k1);
  104. assertThat(k1).hasSameHashCodeAs(k2);
  105. assertThat(k2).hasSameHashCodeAs(k1);
  106. }
  107. private void differentEquals(Trackable t1, Trackable t2) {
  108. AbstractTracker.SearchKey k1 = searchKeyFactory.apply(t1);
  109. AbstractTracker.SearchKey k2 = searchKeyFactory.apply(t2);
  110. assertThat(k1).isNotEqualTo(k2);
  111. assertThat(k2).isNotEqualTo(k1);
  112. assertThat(k1).isNotEqualTo(new Object());
  113. assertThat(k1).isNotEqualTo(null);
  114. }
  115. }
  116. }