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.

ProjectInfoTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.scanner;
  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 java.time.Clock;
  25. import java.time.LocalDate;
  26. import java.time.OffsetDateTime;
  27. import java.time.ZoneId;
  28. import java.time.ZoneOffset;
  29. import java.util.Date;
  30. import javax.annotation.Nullable;
  31. import org.apache.commons.lang.RandomStringUtils;
  32. import org.junit.Rule;
  33. import org.junit.Test;
  34. import org.junit.rules.ExpectedException;
  35. import org.junit.runner.RunWith;
  36. import org.sonar.api.CoreProperties;
  37. import org.sonar.api.config.internal.MapSettings;
  38. import org.sonar.api.utils.MessageException;
  39. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.mockito.Mockito.mock;
  42. @RunWith(DataProviderRunner.class)
  43. public class ProjectInfoTest {
  44. @Rule
  45. public ExpectedException expectedException = ExpectedException.none();
  46. private MapSettings settings = new MapSettings();
  47. private Clock clock = mock(Clock.class);
  48. private ProjectInfo underTest = new ProjectInfo(settings.asConfig(), clock);
  49. @Test
  50. public void testSimpleDateTime() {
  51. OffsetDateTime date = OffsetDateTime.of(2017, 1, 1, 12, 13, 14, 0, ZoneOffset.ofHours(2));
  52. settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01T12:13:14+0200");
  53. settings.appendProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version");
  54. underTest.start();
  55. assertThat(underTest.getAnalysisDate()).isEqualTo(Date.from(date.toInstant()));
  56. assertThat(underTest.getProjectVersion()).contains("version");
  57. }
  58. @Test
  59. public void testSimpleDate() {
  60. LocalDate date = LocalDate.of(2017, 1, 1);
  61. settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  62. underTest.start();
  63. assertThat(underTest.getAnalysisDate())
  64. .isEqualTo(Date.from(date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  65. }
  66. @Test
  67. public void emptyDate() {
  68. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "");
  69. settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version");
  70. expectedException.expect(RuntimeException.class);
  71. underTest.start();
  72. }
  73. @Test
  74. public void fail_with_too_long_version() {
  75. String version = randomAlphabetic(101);
  76. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  77. settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, version);
  78. expectedException.expect(MessageException.class);
  79. expectedException.expectMessage("\"" + version + "\" is not a valid project version. " +
  80. "The maximum length is 100 characters.");
  81. underTest.start();
  82. }
  83. @Test
  84. public void fail_with_too_long_buildString() {
  85. String buildString = randomAlphabetic(101);
  86. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  87. settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, buildString);
  88. expectedException.expect(MessageException.class);
  89. expectedException.expectMessage("\"" + buildString + "\" is not a valid buildString. " +
  90. "The maximum length is 100 characters.");
  91. underTest.start();
  92. }
  93. @Test
  94. @UseDataProvider("emptyOrNullString")
  95. public void getProjectVersion_is_empty_if_property_is_empty_or_null(@Nullable String projectVersion) {
  96. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  97. settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, projectVersion);
  98. underTest.start();
  99. assertThat(underTest.getProjectVersion()).isEmpty();
  100. }
  101. @Test
  102. public void getProjectVersion_contains_value_of_property() {
  103. String value = RandomStringUtils.randomAlphabetic(10);
  104. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  105. settings.setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, value);
  106. underTest.start();
  107. assertThat(underTest.getProjectVersion()).contains(value);
  108. }
  109. @Test
  110. @UseDataProvider("emptyOrNullString")
  111. public void getBuildString_is_empty_if_property_is_empty_or_null(@Nullable String buildString) {
  112. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  113. settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, buildString);
  114. underTest.start();
  115. assertThat(underTest.getBuildString()).isEmpty();
  116. }
  117. @Test
  118. public void getBuildString_contains_value_of_property() {
  119. String value = RandomStringUtils.randomAlphabetic(10);
  120. settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01");
  121. settings.setProperty(CoreProperties.BUILD_STRING_PROPERTY, value);
  122. underTest.start();
  123. assertThat(underTest.getBuildString()).contains(value);
  124. }
  125. @DataProvider
  126. public static Object[][] emptyOrNullString() {
  127. return new Object[][] {
  128. {""},
  129. {null},
  130. };
  131. }
  132. }