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.

CrossProjectDuplicateTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.ce.task.projectanalysis.duplication;
  21. import org.junit.Test;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  24. public class CrossProjectDuplicateTest {
  25. private static final String FILE_KEY_1 = "file key 1";
  26. private static final String FILE_KEY_2 = "file key 2";
  27. @Test
  28. public void constructors_throws_NPE_if_fileKey_is_null() {
  29. assertThatThrownBy(() -> new CrossProjectDuplicate(null, new TextBlock(1, 1)))
  30. .isInstanceOf(NullPointerException.class)
  31. .hasMessage("fileKey can not be null");
  32. }
  33. @Test
  34. public void constructors_throws_NPE_if_textBlock_is_null() {
  35. assertThatThrownBy(() -> new CrossProjectDuplicate(FILE_KEY_1, null))
  36. .isInstanceOf(NullPointerException.class)
  37. .hasMessage("textBlock of duplicate can not be null");
  38. }
  39. @Test
  40. public void getTextBlock_returns_TextBlock_constructor_argument() {
  41. TextBlock textBlock = new TextBlock(2, 3);
  42. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock).getTextBlock()).isSameAs(textBlock);
  43. }
  44. @Test
  45. public void getFileKey_returns_constructor_argument() {
  46. assertThat(new CrossProjectDuplicate(FILE_KEY_1, new TextBlock(2, 3)).getFileKey()).isEqualTo(FILE_KEY_1);
  47. }
  48. @Test
  49. public void equals_compares_on_file_and_TextBlock() {
  50. TextBlock textBlock1 = new TextBlock(1, 2);
  51. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock1)).isEqualTo(new CrossProjectDuplicate(FILE_KEY_1, new TextBlock(1, 2)));
  52. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock1)).isNotEqualTo(new CrossProjectDuplicate(FILE_KEY_1, new TextBlock(1, 1)));
  53. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock1)).isNotEqualTo(new CrossProjectDuplicate(FILE_KEY_2, textBlock1));
  54. }
  55. @Test
  56. public void hashcode_depends_on_file_and_TextBlock() {
  57. TextBlock textBlock = new TextBlock(1, 2);
  58. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock).hashCode()).isEqualTo(new CrossProjectDuplicate(FILE_KEY_1, textBlock).hashCode());
  59. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock).hashCode()).isNotEqualTo(new CrossProjectDuplicate(FILE_KEY_2, textBlock).hashCode());
  60. assertThat(new CrossProjectDuplicate(FILE_KEY_1, textBlock).hashCode()).isNotEqualTo(new CrossProjectDuplicate(FILE_KEY_2, new TextBlock(1, 1)).hashCode());
  61. }
  62. @Test
  63. public void verify_toString() {
  64. assertThat(new CrossProjectDuplicate(FILE_KEY_1, new TextBlock(1, 2)).toString()).isEqualTo("CrossProjectDuplicate{fileKey='file key 1', textBlock=TextBlock{start=1, end=2}}");
  65. }
  66. }