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.

CeTaskComponentTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. @RunWith(DataProviderRunner.class)
  30. public class CeTaskComponentTest {
  31. @Test
  32. @UseDataProvider("nullOrEmpty")
  33. public void constructor_fails_with_NPE_if_uuid_is_null_or_empty(String str) {
  34. assertThatThrownBy(() -> new CeTask.Component(str, "foo", "bar"))
  35. .isInstanceOf(NullPointerException.class)
  36. .hasMessage("uuid can't be null nor empty");
  37. }
  38. @Test
  39. @UseDataProvider("nullOrEmpty")
  40. public void constructor_considers_empty_as_null_and_accept_it_for_key(String str) {
  41. CeTask.Component underTest = new CeTask.Component("foo", str, "bar");
  42. assertThat(underTest.getKey()).isEmpty();
  43. }
  44. @Test
  45. @UseDataProvider("nullOrEmpty")
  46. public void constructor_considers_empty_as_null_and_accept_it_for_name(String str) {
  47. CeTask.Component underTest = new CeTask.Component("foo", "bar", str);
  48. assertThat(underTest.getName()).isEmpty();
  49. }
  50. @Test
  51. public void equals_is_based_on_all_fields() {
  52. String uuid = randomAlphabetic(2);
  53. String key = randomAlphabetic(3);
  54. String name = randomAlphabetic(4);
  55. String somethingElse = randomAlphabetic(5);
  56. CeTask.Component underTest = new CeTask.Component(uuid, key, name);
  57. assertThat(underTest)
  58. .isEqualTo(underTest)
  59. .isEqualTo(new CeTask.Component(uuid, key, name))
  60. .isNotNull()
  61. .isNotEqualTo(new Object())
  62. .isNotEqualTo(new CeTask.Component(somethingElse, key, name))
  63. .isNotEqualTo(new CeTask.Component(uuid, somethingElse, name))
  64. .isNotEqualTo(new CeTask.Component(uuid, key, somethingElse))
  65. .isNotEqualTo(new CeTask.Component(uuid, key, null));
  66. }
  67. @Test
  68. public void hashcode_is_based_on_all_fields() {
  69. String uuid = randomAlphabetic(2);
  70. String key = randomAlphabetic(3);
  71. String name = randomAlphabetic(4);
  72. String somethingElse = randomAlphabetic(5);
  73. CeTask.Component underTest = new CeTask.Component(uuid, key, name);
  74. assertThat(underTest)
  75. .hasSameHashCodeAs(underTest)
  76. .hasSameHashCodeAs(new CeTask.Component(uuid, key, name));
  77. assertThat(underTest.hashCode())
  78. .isNotEqualTo(new Object().hashCode())
  79. .isNotEqualTo(new CeTask.Component(somethingElse, key, name).hashCode())
  80. .isNotEqualTo(new CeTask.Component(uuid, somethingElse, name).hashCode())
  81. .isNotEqualTo(new CeTask.Component(uuid, key, somethingElse).hashCode())
  82. .isNotEqualTo(new CeTask.Component(uuid, key, null).hashCode());
  83. }
  84. @DataProvider
  85. public static Object[][] nullOrEmpty() {
  86. return new Object[][] {
  87. {null},
  88. {""},
  89. };
  90. }
  91. }