]> source.dussan.org Git - sonarqube.git/commitdiff
fix missing coverage on Module class
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 2 Jun 2015 14:28:55 +0000 (16:28 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 2 Jun 2015 16:01:17 +0000 (18:01 +0200)
sonar-core/src/main/java/org/sonar/core/component/Module.java
sonar-core/src/test/java/org/sonar/core/component/ModuleTest.java [new file with mode: 0644]

index 25a015e55e6d95130e1d0c8aa8e2db2e8bc6b1b4..3ccaff79709f28668e07abfb9b4c734ba69a6f13 100644 (file)
@@ -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 (file)
index 0000000..5a2f061
--- /dev/null
@@ -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();
+  }
+}