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.

InProjectDuplicateTest.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 org.sonar.ce.task.projectanalysis.component.Component;
  23. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  26. public class InProjectDuplicateTest {
  27. private static final Component FILE_1 = ReportComponent.builder(Component.Type.FILE, 1).build();
  28. private static final Component FILE_2 = ReportComponent.builder(Component.Type.FILE, 2).build();
  29. @Test
  30. public void constructors_throws_NPE_if_file_is_null() {
  31. assertThatThrownBy(() -> new InProjectDuplicate(null, new TextBlock(1, 1)))
  32. .isInstanceOf(NullPointerException.class)
  33. .hasMessage("file can not be null");
  34. }
  35. @Test
  36. public void constructors_throws_NPE_if_textBlock_is_null() {
  37. assertThatThrownBy(() -> new InProjectDuplicate(FILE_1, null))
  38. .isInstanceOf(NullPointerException.class)
  39. .hasMessage("textBlock of duplicate can not be null");
  40. }
  41. @Test
  42. public void constructors_throws_IAE_if_type_of_file_argument_is_not_FILE() {
  43. assertThatThrownBy(() -> new InProjectDuplicate(ReportComponent.builder(Component.Type.PROJECT, 1).build(), new TextBlock(1, 1)))
  44. .isInstanceOf(IllegalArgumentException.class)
  45. .hasMessage("file must be of type FILE");
  46. }
  47. @Test
  48. public void getTextBlock_returns_TextBlock_constructor_argument() {
  49. TextBlock textBlock = new TextBlock(2, 3);
  50. assertThat(new InProjectDuplicate(FILE_1, textBlock).getTextBlock()).isSameAs(textBlock);
  51. }
  52. @Test
  53. public void getFile_returns_Component_constructor_argument() {
  54. assertThat(new InProjectDuplicate(FILE_1, new TextBlock(2, 3)).getFile()).isSameAs(FILE_1);
  55. }
  56. @Test
  57. public void equals_compares_on_file_and_TextBlock() {
  58. TextBlock textBlock1 = new TextBlock(1, 2);
  59. assertThat(new InProjectDuplicate(FILE_1, textBlock1)).isEqualTo(new InProjectDuplicate(FILE_1, new TextBlock(1, 2)));
  60. assertThat(new InProjectDuplicate(FILE_1, textBlock1)).isNotEqualTo(new InProjectDuplicate(FILE_1, new TextBlock(1, 1)));
  61. assertThat(new InProjectDuplicate(FILE_1, textBlock1)).isNotEqualTo(new InProjectDuplicate(FILE_2, textBlock1));
  62. }
  63. @Test
  64. public void hashcode_depends_on_file_and_TextBlock() {
  65. TextBlock textBlock = new TextBlock(1, 2);
  66. assertThat(new InProjectDuplicate(FILE_1, textBlock).hashCode()).isEqualTo(new InProjectDuplicate(FILE_1, textBlock).hashCode());
  67. assertThat(new InProjectDuplicate(FILE_1, textBlock).hashCode()).isNotEqualTo(new InProjectDuplicate(FILE_2, textBlock).hashCode());
  68. assertThat(new InProjectDuplicate(FILE_1, textBlock).hashCode()).isNotEqualTo(new InProjectDuplicate(FILE_2, new TextBlock(1, 1)).hashCode());
  69. }
  70. @Test
  71. public void verify_toString() {
  72. assertThat(new InProjectDuplicate(FILE_1, new TextBlock(1, 2)).toString())
  73. .isEqualTo("InProjectDuplicate{file=ReportComponent{ref=1, key='key_1', type=FILE}, textBlock=TextBlock{start=1, end=2}}");
  74. }
  75. }