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.

ScmInfoImplTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.scm;
  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 ScmInfoImplTest {
  25. static final Changeset CHANGESET_1 = Changeset.newChangesetBuilder()
  26. .setAuthor("john")
  27. .setDate(123456789L)
  28. .setRevision("rev-1")
  29. .build();
  30. static final Changeset CHANGESET_2 = Changeset.newChangesetBuilder()
  31. .setAuthor("henry")
  32. .setDate(1234567810L)
  33. .setRevision("rev-2")
  34. .build();
  35. @Test
  36. public void get_all_changesets() {
  37. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  38. assertThat(scmInfo.getAllChangesets()).contains(CHANGESET_1, CHANGESET_2, CHANGESET_1, CHANGESET_1);
  39. }
  40. @Test
  41. public void get_latest_changeset() {
  42. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  43. assertThat(scmInfo.getLatestChangeset()).isEqualTo(CHANGESET_2);
  44. }
  45. @Test
  46. public void get_changeset_for_given_line() {
  47. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  48. assertThat(scmInfo.getChangesetForLine(1)).isEqualTo(CHANGESET_1);
  49. assertThat(scmInfo.getChangesetForLine(2)).isEqualTo(CHANGESET_2);
  50. assertThat(scmInfo.getChangesetForLine(3)).isEqualTo(CHANGESET_1);
  51. assertThat(scmInfo.getChangesetForLine(4)).isEqualTo(CHANGESET_1);
  52. }
  53. @Test
  54. public void exists_for_given_line() {
  55. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  56. assertThat(scmInfo.hasChangesetForLine(1)).isTrue();
  57. assertThat(scmInfo.hasChangesetForLine(5)).isFalse();
  58. }
  59. @Test
  60. public void fail_with_ISE_on_empty_changeset() {
  61. assertThatThrownBy(() -> new ScmInfoImpl(new Changeset[0]))
  62. .isInstanceOf(IllegalStateException.class)
  63. .hasMessage("ScmInfo cannot be empty");
  64. }
  65. @Test
  66. public void fail_with_IAE_when_line_is_smaller_than_one() {
  67. assertThatThrownBy(() -> {
  68. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  69. scmInfo.getChangesetForLine(0);
  70. })
  71. .isInstanceOf(IllegalArgumentException.class)
  72. .hasMessage("There's no changeset on line 0");
  73. }
  74. @Test
  75. public void fail_with_IAE_when_line_is_bigger_than_changetset_size() {
  76. assertThatThrownBy(() -> {
  77. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  78. scmInfo.getChangesetForLine(5);
  79. })
  80. .isInstanceOf(IllegalArgumentException.class)
  81. .hasMessage("There's no changeset on line 5");
  82. }
  83. @Test
  84. public void test_to_string() {
  85. ScmInfo scmInfo = createScmInfoWithTwoChangestOnFourLines();
  86. assertThat(scmInfo).hasToString("ScmInfoImpl{" +
  87. "latestChangeset=Changeset{revision='rev-2', author='henry', date=1234567810}, " +
  88. "lineChangesets={" +
  89. "1=Changeset{revision='rev-1', author='john', date=123456789}, " +
  90. "2=Changeset{revision='rev-2', author='henry', date=1234567810}, " +
  91. "3=Changeset{revision='rev-1', author='john', date=123456789}, " +
  92. "4=Changeset{revision='rev-1', author='john', date=123456789}" +
  93. "}}");
  94. }
  95. private static ScmInfo createScmInfoWithTwoChangestOnFourLines() {
  96. Changeset changeset1 = Changeset.newChangesetBuilder()
  97. .setAuthor("john")
  98. .setDate(123456789L)
  99. .setRevision("rev-1")
  100. .build();
  101. // Latest changeset
  102. Changeset changeset2 = Changeset.newChangesetBuilder()
  103. .setAuthor("henry")
  104. .setDate(1234567810L)
  105. .setRevision("rev-2")
  106. .build();
  107. return new ScmInfoImpl(new Changeset[] {changeset1, changeset2, changeset1, changeset1});
  108. }
  109. }