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.

PurgeConfigurationTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.db.purge;
  21. import java.util.Date;
  22. import java.util.Optional;
  23. import org.junit.Test;
  24. import org.sonar.api.config.PropertyDefinitions;
  25. import org.sonar.api.impl.config.MapSettings;
  26. import org.sonar.api.resources.Scopes;
  27. import org.sonar.api.utils.DateUtils;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.core.config.PurgeConstants;
  30. import org.sonar.core.config.PurgeProperties;
  31. import static java.util.Collections.emptySet;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. public class PurgeConfigurationTest {
  34. @Test
  35. public void should_delete_all_closed_issues() {
  36. PurgeConfiguration conf = new PurgeConfiguration("root", "project", emptySet(), 0, Optional.empty(), System2.INSTANCE, emptySet());
  37. assertThat(conf.maxLiveDateOfClosedIssues()).isNull();
  38. conf = new PurgeConfiguration("root", "project", emptySet(), -1, Optional.empty(), System2.INSTANCE, emptySet());
  39. assertThat(conf.maxLiveDateOfClosedIssues()).isNull();
  40. }
  41. @Test
  42. public void should_delete_only_old_closed_issues() {
  43. Date now = DateUtils.parseDate("2013-05-18");
  44. PurgeConfiguration conf = new PurgeConfiguration("root", "project", emptySet(), 30, Optional.empty(), System2.INSTANCE, emptySet());
  45. Date toDate = conf.maxLiveDateOfClosedIssues(now);
  46. assertThat(toDate.getYear()).isEqualTo(113);// =2013
  47. assertThat(toDate.getMonth()).isEqualTo(3); // means April
  48. assertThat(toDate.getDate()).isEqualTo(18);
  49. }
  50. @Test
  51. public void should_have_empty_branch_purge_date() {
  52. PurgeConfiguration conf = new PurgeConfiguration("root", "project", emptySet(), 30, Optional.of(10), System2.INSTANCE, emptySet());
  53. assertThat(conf.maxLiveDateOfInactiveShortLivingBranches()).isNotEmpty();
  54. long tenDaysAgo = DateUtils.addDays(new Date(System2.INSTANCE.now()), -10).getTime();
  55. assertThat(conf.maxLiveDateOfInactiveShortLivingBranches().get().getTime()).isBetween(tenDaysAgo - 5000, tenDaysAgo + 5000);
  56. }
  57. @Test
  58. public void should_calculate_branch_purge_date() {
  59. PurgeConfiguration conf = new PurgeConfiguration("root", "project", emptySet(), 30, Optional.empty(), System2.INSTANCE, emptySet());
  60. assertThat(conf.maxLiveDateOfInactiveShortLivingBranches()).isEmpty();
  61. }
  62. @Test
  63. public void delete_files_and_directories() {
  64. MapSettings settings = new MapSettings(new PropertyDefinitions(PurgeProperties.all()));
  65. settings.setProperty(PurgeConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES, 5);
  66. Date now = new Date();
  67. PurgeConfiguration underTest = PurgeConfiguration.newDefaultPurgeConfiguration(settings.asConfig(), "root", "project", emptySet());
  68. assertThat(underTest.getScopesWithoutHistoricalData())
  69. .containsExactlyInAnyOrder(Scopes.DIRECTORY, Scopes.FILE);
  70. assertThat(underTest.maxLiveDateOfClosedIssues(now)).isEqualTo(DateUtils.addDays(now, -5));
  71. }
  72. }