From: Simon Brandhof Date: Sun, 24 Sep 2017 21:22:59 +0000 (+0200) Subject: SONAR-9802 fix NPE when app node is starting X-Git-Tag: 6.6-RC1~205 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=710eb58b26299acdc3595646371cb23e3d7b7dd5;p=sonarqube.git SONAR-9802 fix NPE when app node is starting --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProvider.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProvider.java index a2f106b5a4f..6cba07a7be2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProvider.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProvider.java @@ -55,7 +55,9 @@ public class ProcessInfoProvider implements Startable { public static ProtobufSystemInfo.SystemInfo provide() { ProtobufSystemInfo.SystemInfo.Builder protobuf = ProtobufSystemInfo.SystemInfo.newBuilder(); - instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf())); + if (instance != null) { + instance.sections.forEach(section -> protobuf.addSections(section.toProtobuf())); + } return protobuf.build(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProviderTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProviderTest.java index afc2ebcc4cb..d418ad254d6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProviderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/cluster/ProcessInfoProviderTest.java @@ -33,19 +33,6 @@ public class ProcessInfoProviderTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - @Test - public void set_self_as_statically_shared_instance() { - ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[0]); - - underTest.start(); - assertThat(ProcessInfoProvider.provide()).isNotNull(); - - underTest.stop(); - - expectedException.expect(NullPointerException.class); - ProcessInfoProvider.provide(); - } - @Test public void remove_global_sections_from_results() { ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{ @@ -72,4 +59,19 @@ public class ProcessInfoProviderTest { underTest.stop(); } + + @Test + public void empty_result_is_returned_if_not_started_yet() { + ProcessInfoProvider underTest = new ProcessInfoProvider(new SystemInfoSection[]{ + new TestSystemInfoSection("foo"), + new TestSystemInfoSection("bar")}); + + assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0); + + underTest.start(); + assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(2); + + underTest.stop(); + assertThat(ProcessInfoProvider.provide().getSectionsCount()).isEqualTo(0); + } }