From fefcbdcece48719cc98753629d58e79ec480e101 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 4 May 2023 13:41:55 -0500 Subject: [PATCH] SONAR-13362 Do not show a deprecation warning if cross-project duplication is overridden and disabled at project level --- .../IntegrateCrossProjectDuplications.java | 8 ++++---- .../IntegrateCrossProjectDuplicationsTest.java | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplications.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplications.java index 25ad3067ed7..e9b902642ea 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplications.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplications.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.function.Function; import java.util.function.Predicate; import javax.annotation.Nonnull; -import org.sonar.api.CoreProperties; import org.sonar.api.config.Configuration; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; @@ -56,12 +55,13 @@ public class IntegrateCrossProjectDuplications { private final Configuration config; private final DuplicationRepository duplicationRepository; - private Map numberOfUnitsByLanguage = new HashMap<>(); + private final Map numberOfUnitsByLanguage = new HashMap<>(); - public IntegrateCrossProjectDuplications(Configuration config, DuplicationRepository duplicationRepository, CeTaskMessages ceTaskMessages, System2 system) { + public IntegrateCrossProjectDuplications(CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder, Configuration config, + DuplicationRepository duplicationRepository, CeTaskMessages ceTaskMessages, System2 system) { this.config = config; this.duplicationRepository = duplicationRepository; - if (config.getBoolean(CoreProperties.CPD_CROSS_PROJECT).orElse(false)) { + if (crossProjectDuplicationStatusHolder.isEnabled()) { LOGGER.warn(DEPRECATED_WARNING); ceTaskMessages.add(new CeTaskMessages.Message(DEPRECATED_WARNING_DASHBOARD, system.now())); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplicationsTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplicationsTest.java index 9e0c7ed63e6..172e6a04e7e 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplicationsTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/IntegrateCrossProjectDuplicationsTest.java @@ -42,6 +42,7 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder; @@ -59,10 +60,12 @@ public class IntegrateCrossProjectDuplicationsTest { @Rule public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(); - private TestSystem2 system = new TestSystem2(); - private MapSettings settings = new MapSettings(); - private CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class); - private IntegrateCrossProjectDuplications underTest = new IntegrateCrossProjectDuplications(settings.asConfig(), duplicationRepository, ceTaskMessages, system); + private final TestSystem2 system = new TestSystem2(); + private final MapSettings settings = new MapSettings(); + private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class); + private final CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class); + private final IntegrateCrossProjectDuplications underTest = new IntegrateCrossProjectDuplications(crossProjectDuplicationStatusHolder, settings.asConfig(), + duplicationRepository, ceTaskMessages, system); @Test public void add_duplications_from_two_blocks() { @@ -336,10 +339,10 @@ public class IntegrateCrossProjectDuplicationsTest { @Test public void log_warning_if_this_deprecated_feature_is_enabled() { - settings.setProperty("sonar.cpd.cross_project", "true"); + when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true); system.setNow(1000L); - new IntegrateCrossProjectDuplications(settings.asConfig(), duplicationRepository, ceTaskMessages, system); + new IntegrateCrossProjectDuplications(crossProjectDuplicationStatusHolder, settings.asConfig(), duplicationRepository, ceTaskMessages, system); assertThat(logTester.logs()).containsExactly("This analysis uses the deprecated cross-project duplication feature."); verify(ceTaskMessages).add(new CeTaskMessages.Message("This project uses the deprecated cross-project duplication feature.", 1000L)); -- 2.39.5