*
* @throws IllegalStateException if the holder is empty (ie. there is no root yet)
* @deprecated This method was introduced as a quick fix for SONAR-10781. Ultimately one should never manipulate component
- * ref that doesn't exists in the scanner report
+ * ref that doesn't exist in the scanner report
*/
@Deprecated
Optional<Component> getOptionalComponentByRef(int ref);
+ /**
+ * Number of components, including root.
+ *
+ * @throws IllegalStateException if the holder is empty (ie. there is no root yet)
+ */
+ int getSize();
}
return Optional.ofNullable(componentsByRef.get(ref));
}
+ @Override
+ public int getSize() {
+ checkInitialized();
+ ensureComponentByRefIsPopulated();
+ return componentsByRef.size();
+ }
+
private void ensureComponentByRefIsPopulated() {
if (componentsByRef != null) {
return;
treeRootHolder.setRoot(project);
analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
+
+ context.getStatistics().add("components", treeRootHolder.getSize());
}
}
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");
public Optional<Component> getOptionalComponentByRef(int ref) {
return delegate.getOptionalComponentByRef(ref);
}
+
+ @Override
+ public int getSize() {
+ return delegate.getSize();
+ }
+
+
}
reportReader.putComponent(component(DIR_REF_2, DIRECTORY, FILE_3_REF));
reportReader.putComponent(component(FILE_3_REF, FILE));
- underTest.execute(new TestComputationStepContext());
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
Component root = treeRootHolder.getRoot();
assertThat(root).isNotNull();
Component dir2 = module.getChildren().get(1);
verifyComponent(dir2, Component.Type.DIRECTORY, DIR_REF_2, 1);
verifyComponent(dir2.getChildren().iterator().next(), Component.Type.FILE, FILE_3_REF, 0);
+
+ context.getStatistics().assertValue("components", 7);
}
@Test