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.

TextBlockTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Random;
  26. import org.junit.Test;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. public class TextBlockTest {
  30. @Test
  31. public void constructor_throws_IAE_if_start_is_0() {
  32. assertThatThrownBy(() -> new TextBlock(0, 2))
  33. .isInstanceOf(IllegalArgumentException.class)
  34. .hasMessage("First line index must be >= 1");
  35. }
  36. @Test
  37. public void constructor_throws_IAE_if_end_is_less_than_start() {
  38. assertThatThrownBy(() -> new TextBlock(1, 0))
  39. .isInstanceOf(IllegalArgumentException.class)
  40. .hasMessage("Last line index must be >= first line index");
  41. }
  42. @Test
  43. public void getStart_returns_constructor_argument() {
  44. TextBlock textBlock = new TextBlock(15, 300);
  45. assertThat(textBlock.getStart()).isEqualTo(15);
  46. }
  47. @Test
  48. public void getEnd_returns_constructor_argument() {
  49. TextBlock textBlock = new TextBlock(15, 300);
  50. assertThat(textBlock.getEnd()).isEqualTo(300);
  51. }
  52. @Test
  53. public void equals_compares_on_start_and_end() {
  54. assertThat(new TextBlock(15, 15)).isEqualTo(new TextBlock(15, 15));
  55. assertThat(new TextBlock(15, 300)).isEqualTo(new TextBlock(15, 300));
  56. assertThat(new TextBlock(15, 300)).isNotEqualTo(new TextBlock(15, 15));
  57. }
  58. @Test
  59. public void hashcode_is_based__on_start_and_end() {
  60. assertThat(new TextBlock(15, 15).hashCode()).isEqualTo(new TextBlock(15, 15).hashCode());
  61. assertThat(new TextBlock(15, 300).hashCode()).isEqualTo(new TextBlock(15, 300).hashCode());
  62. assertThat(new TextBlock(15, 300).hashCode()).isNotEqualTo(new TextBlock(15, 15).hashCode());
  63. }
  64. @Test
  65. public void TextBlock_defines_natural_order_by_start_then_end() {
  66. TextBlock textBlock1 = new TextBlock(1, 1);
  67. TextBlock textBlock2 = new TextBlock(1, 2);
  68. TextBlock textBlock3 = new TextBlock(2, 3);
  69. TextBlock textBlock4 = new TextBlock(2, 4);
  70. TextBlock textBlock5 = new TextBlock(5, 5);
  71. List<TextBlock> shuffledList = new ArrayList<>(Arrays.asList(textBlock1, textBlock2, textBlock3, textBlock4, textBlock5));
  72. Collections.shuffle(shuffledList, new Random());
  73. Collections.sort(shuffledList);
  74. assertThat(shuffledList).containsExactly(textBlock1, textBlock2, textBlock3, textBlock4, textBlock5);
  75. }
  76. @Test
  77. public void verify_toString() {
  78. assertThat(new TextBlock(13, 400).toString()).isEqualTo("TextBlock{start=13, end=400}");
  79. }
  80. }