From d69998fa24fce30f6ea048e2b7d37805fd2e8b6e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 3 Aug 2018 11:38:58 +0200 Subject: [PATCH] SONAR-11077 display nb of analyzed components in CE logs --- .../projectanalysis/component/TreeRootHolder.java | 6 ++++++ .../component/TreeRootHolderImpl.java | 7 +++++++ .../step/BuildComponentTreeStep.java | 3 +++ .../component/TreeRootHolderImplTest.java | 13 +++++++++++++ .../component/TreeRootHolderRule.java | 5 +++++ 5 files changed, 34 insertions(+) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolder.java index f9ae8404eff..b8166c86daf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolder.java @@ -39,4 +39,10 @@ public interface TreeRootHolder { */ Component getComponentByRef(int ref); + /** + * Number of components, including root. + * + * @throws IllegalStateException if the holder is empty (ie. there is no root yet) + */ + int getSize(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImpl.java index ea12670158b..ab5b2cec0ac 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImpl.java @@ -59,6 +59,13 @@ public class TreeRootHolderImpl implements MutableTreeRootHolder { return component; } + @Override + public int getSize() { + checkInitialized(); + ensureComponentByRefIsPopulated(); + return componentsByRef.size(); + } + private void ensureComponentByRefIsPopulated() { if (componentsByRef != null) { return; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java index fffb6cb5db0..cddc65268af 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java @@ -21,6 +21,7 @@ package org.sonar.server.computation.task.projectanalysis.step; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import org.sonar.api.utils.log.Loggers; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.SnapshotDto; @@ -86,6 +87,8 @@ public class BuildComponentTreeStep implements ComputationStep { treeRootHolder.setRoot(project); analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis)); + + Loggers.get(getClass()).debug("components={}", treeRootHolder.getSize()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImplTest.java index f9d16b0e157..89c26966d83 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderImplTest.java @@ -125,6 +125,19 @@ public class TreeRootHolderImplTest { assertThat(underTest.getRoot()).isSameAs(DUMB_PROJECT); } + @Test + public void getSize_throws_ISE_if_not_initialized() { + expectNotInitialized_ISE(); + + underTest.getSize(); + } + + @Test + public void getSize_counts_number_of_components() { + underTest.setRoot(SOME_REPORT_COMPONENT_TREE); + assertThat(underTest.getSize()).isEqualTo(4); + } + private void expectNotInitialized_ISE() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Holder has not been initialized yet"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderRule.java index ea4ed8a6098..cf8c0510cc8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/TreeRootHolderRule.java @@ -44,4 +44,9 @@ public class TreeRootHolderRule extends ExternalResource implements TreeRootHold public Component getComponentByRef(int ref) { return delegate.getComponentByRef(ref); } + + @Override + public int getSize() { + return delegate.getSize(); + } } -- 2.39.5