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.

ChangesetTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 ChangesetTest {
  25. @Test
  26. public void create_changeset() {
  27. Changeset underTest = Changeset.newChangesetBuilder()
  28. .setAuthor("john")
  29. .setDate(123456789L)
  30. .setRevision("rev-1")
  31. .build();
  32. assertThat(underTest.getAuthor()).isEqualTo("john");
  33. assertThat(underTest.getDate()).isEqualTo(123456789L);
  34. assertThat(underTest.getRevision()).isEqualTo("rev-1");
  35. }
  36. @Test
  37. public void create_changeset_with_minimum_fields() {
  38. Changeset underTest = Changeset.newChangesetBuilder()
  39. .setDate(123456789L)
  40. .build();
  41. assertThat(underTest.getAuthor()).isNull();
  42. assertThat(underTest.getDate()).isEqualTo(123456789L);
  43. assertThat(underTest.getRevision()).isNull();
  44. }
  45. @Test
  46. public void fail_with_NPE_when_setting_null_date() {
  47. assertThatThrownBy(() -> Changeset.newChangesetBuilder().setDate(null))
  48. .isInstanceOf(NullPointerException.class)
  49. .hasMessage("Date cannot be null");
  50. }
  51. @Test
  52. public void fail_with_NPE_when_building_without_date() {
  53. assertThatThrownBy(() -> {
  54. Changeset.newChangesetBuilder()
  55. .setAuthor("john")
  56. .setRevision("rev-1")
  57. .build();
  58. })
  59. .isInstanceOf(NullPointerException.class)
  60. .hasMessage("Date cannot be null");
  61. }
  62. @Test
  63. public void test_to_string() {
  64. Changeset underTest = Changeset.newChangesetBuilder()
  65. .setAuthor("john")
  66. .setDate(123456789L)
  67. .setRevision("rev-1")
  68. .build();
  69. assertThat(underTest).hasToString("Changeset{revision='rev-1', author='john', date=123456789}");
  70. }
  71. @Test
  72. public void equals_and_hashcode_are_based_on_all_fields() {
  73. Changeset.Builder changesetBuilder = Changeset.newChangesetBuilder()
  74. .setAuthor("john")
  75. .setDate(123456789L)
  76. .setRevision("rev-1");
  77. Changeset changeset = changesetBuilder.build();
  78. Changeset sameChangeset = changesetBuilder.build();
  79. Changeset anotherChangesetWithSameRevision = Changeset.newChangesetBuilder()
  80. .setAuthor("henry")
  81. .setDate(1234567810L)
  82. .setRevision("rev-1")
  83. .build();
  84. Changeset anotherChangeset = Changeset.newChangesetBuilder()
  85. .setAuthor("henry")
  86. .setDate(996L)
  87. .setRevision("rev-2")
  88. .build();
  89. assertThat(changeset)
  90. .isEqualTo(changeset)
  91. .isEqualTo(sameChangeset)
  92. .isNotEqualTo(anotherChangesetWithSameRevision)
  93. .isNotEqualTo(anotherChangeset)
  94. .hasSameHashCodeAs(changeset)
  95. .hasSameHashCodeAs(sameChangeset);
  96. assertThat(changeset.hashCode())
  97. .isNotEqualTo(anotherChangesetWithSameRevision.hashCode())
  98. .isNotEqualTo(anotherChangeset.hashCode());
  99. }
  100. }