From: Sébastien Lesaint Date: Tue, 2 Jun 2015 14:28:55 +0000 (+0200) Subject: fix missing coverage on Module class X-Git-Tag: 5.2-RC1~1688 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f0a705fc931f245d4f434d079e23d19d4cd71d2;p=sonarqube.git fix missing coverage on Module class --- diff --git a/sonar-core/src/main/java/org/sonar/core/component/Module.java b/sonar-core/src/main/java/org/sonar/core/component/Module.java index 25a015e55e6..3ccaff79709 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/Module.java +++ b/sonar-core/src/main/java/org/sonar/core/component/Module.java @@ -19,6 +19,7 @@ */ package org.sonar.core.component; +import javax.annotation.Nullable; import org.sonar.core.platform.ComponentContainer; import static com.google.common.base.Preconditions.checkNotNull; @@ -40,7 +41,11 @@ public abstract class Module { return container.getComponentByType(tClass); } - protected void add(Object... objects) { + protected void add(@Nullable Object... objects) { + if (objects == null) { + return; + } + for (Object object : objects) { if (object != null) { container.addComponent(object, true); diff --git a/sonar-core/src/test/java/org/sonar/core/component/ModuleTest.java b/sonar-core/src/test/java/org/sonar/core/component/ModuleTest.java new file mode 100644 index 00000000000..5a2f061a7c8 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/component/ModuleTest.java @@ -0,0 +1,80 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.component; + +import org.junit.Test; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ModuleTest { + ComponentContainer container = new ComponentContainer(); + int initialSize = sizeOf(container); + + @Test(expected = NullPointerException.class) + public void configure_throws_NPE_if_container_is_empty() { + new Module() { + @Override + protected void configureModule() { + // empty + } + }.configure(null); + } + + @Test + public void module_with_empty_configureModule_method_adds_no_component() { + new Module() { + @Override + protected void configureModule() { + // empty + } + }.configure(container); + + assertThat(sizeOf(container)).isSameAs(initialSize); + } + + @Test + public void add_method_supports_null_and_adds_nothing_to_container() { + new Module() { + @Override + protected void configureModule() { + add(null); + } + }.configure(container); + + assertThat(sizeOf(container)).isEqualTo(initialSize); + } + + @Test + public void add_method_filters_out_null_inside_vararg_parameter() { + new Module() { + @Override + protected void configureModule() { + add(new Object(), null, ""); + } + }.configure(container); + + assertThat(sizeOf(container)).isEqualTo(initialSize + 2); + } + + private static int sizeOf(ComponentContainer container) { + return container.getPicoContainer().getComponentAdapters().size(); + } +}