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.

CrossProjectDuplicationStatusHolderImplTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.duplication;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.log.LogTester;
  24. import org.sonar.api.utils.log.LoggerLevel;
  25. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  26. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. public class CrossProjectDuplicationStatusHolderImplTest {
  32. @Rule
  33. public LogTester logTester = new LogTester();
  34. @Rule
  35. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  36. private CrossProjectDuplicationStatusHolderImpl underTest = new CrossProjectDuplicationStatusHolderImpl(analysisMetadataHolder);
  37. @Test
  38. public void cross_project_duplication_is_enabled_when_enabled_in_report_and_no_branch() {
  39. analysisMetadataHolder
  40. .setCrossProjectDuplicationEnabled(true)
  41. .setBranch(newBranch(true));
  42. underTest.start();
  43. assertThat(underTest.isEnabled()).isTrue();
  44. assertThat(logTester.logs(LoggerLevel.DEBUG)).containsOnly("Cross project duplication is enabled");
  45. }
  46. @Test
  47. public void cross_project_duplication_is_disabled_when_not_enabled_in_report() {
  48. analysisMetadataHolder
  49. .setCrossProjectDuplicationEnabled(false)
  50. .setBranch(newBranch(true));
  51. underTest.start();
  52. assertThat(underTest.isEnabled()).isFalse();
  53. assertThat(logTester.logs(LoggerLevel.DEBUG)).containsOnly("Cross project duplication is disabled because it's disabled in the analysis report");
  54. }
  55. @Test
  56. public void cross_project_duplication_is_disabled_when_branch_is_used() {
  57. analysisMetadataHolder
  58. .setCrossProjectDuplicationEnabled(true)
  59. .setBranch(newBranch(false));
  60. underTest.start();
  61. assertThat(underTest.isEnabled()).isFalse();
  62. assertThat(logTester.logs(LoggerLevel.DEBUG)).containsOnly("Cross project duplication is disabled because of a branch is used");
  63. }
  64. @Test
  65. public void cross_project_duplication_is_disabled_when_not_enabled_in_report_and_when_branch_is_used() {
  66. analysisMetadataHolder
  67. .setCrossProjectDuplicationEnabled(false)
  68. .setBranch(newBranch(false));
  69. underTest.start();
  70. assertThat(underTest.isEnabled()).isFalse();
  71. assertThat(logTester.logs(LoggerLevel.DEBUG)).containsOnly("Cross project duplication is disabled because it's disabled in the analysis report");
  72. }
  73. @Test
  74. public void flag_is_build_in_start() {
  75. analysisMetadataHolder
  76. .setCrossProjectDuplicationEnabled(true)
  77. .setBranch(newBranch(true));
  78. underTest.start();
  79. assertThat(underTest.isEnabled()).isTrue();
  80. // Change the boolean from the report. This can never happen, it's only to validate that the flag is build in the start method
  81. analysisMetadataHolder.setCrossProjectDuplicationEnabled(false);
  82. assertThat(underTest.isEnabled()).isTrue();
  83. }
  84. @Test
  85. public void isEnabled_throws_ISE_when_start_have_not_been_called_before() {
  86. assertThatThrownBy(() -> underTest.isEnabled())
  87. .isInstanceOf(IllegalStateException.class)
  88. .hasMessageContaining("Flag hasn't been initialized, the start() should have been called before");
  89. }
  90. private static Branch newBranch(boolean supportsCrossProjectCpd) {
  91. Branch branch = mock(Branch.class);
  92. when(branch.supportsCrossProjectCpd()).thenReturn(supportsCrossProjectCpd);
  93. return branch;
  94. }
  95. }