From 3e1757f1ee08bfac62fd2f387c7786b468017701 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Thu, 10 Nov 2022 13:29:19 -0800 Subject: [PATCH] Add null check to MP Server.Builder.config() (#5363) * Add null check to MP Server.Builder.config() * Improve the NullPointerException assertion test --- .../io/helidon/config/mp/MpConfigSources.java | 2 +- .../config/mp/MpConfigSourcesTest.java | 8 ++++ .../helidon/microprofile/server/Server.java | 3 +- .../microprofile/server/ServerTest.java | 37 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 microprofile/server/src/test/java/io/helidon/microprofile/server/ServerTest.java diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigSources.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigSources.java index 987bb392bbb..274c3c9b0b2 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigSources.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigSources.java @@ -352,7 +352,7 @@ public static ConfigSource create(io.helidon.config.spi.ConfigSource helidonConf * @return a new MicroProfile Config config source */ public static ConfigSource create(Config config) { - return new MpHelidonConfigSource(config); + return new MpHelidonConfigSource(Objects.requireNonNull(config, "Config cannot be null")); } /** diff --git a/config/config-mp/src/test/java/io/helidon/config/mp/MpConfigSourcesTest.java b/config/config-mp/src/test/java/io/helidon/config/mp/MpConfigSourcesTest.java index a2027ff0dfc..e426e36cfd6 100644 --- a/config/config-mp/src/test/java/io/helidon/config/mp/MpConfigSourcesTest.java +++ b/config/config-mp/src/test/java/io/helidon/config/mp/MpConfigSourcesTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger; import io.helidon.common.media.type.MediaType; +import io.helidon.config.Config; import io.helidon.config.ConfigException; import io.helidon.config.ConfigSources; import io.helidon.config.PropertiesConfigParser; @@ -37,6 +38,7 @@ import io.helidon.config.spi.ParsableSource; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; @@ -100,6 +102,12 @@ void testHelidonLazy() { assertThat("exists called exactly once", lazy.exists.get(), is(1)); } + @Test + void testMpConfigSourcesNullConfig() { + NullPointerException npe = assertThrows(NullPointerException.class, () -> MpConfigSources.create((Config) null)); + assertThat(npe.getMessage(), is("Config cannot be null")); + } + private static final class NodeImpl implements ConfigSource, NodeConfigSource { private static final String DESCRIPTION = "node-unit-test"; private static final String KEY = "key"; diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java index dde2f5b1465..3bb34ecaa80 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.logging.Logger; import io.helidon.common.context.Contexts; @@ -292,7 +293,7 @@ public Builder port(int port) { public Builder config(io.helidon.config.Config config) { this.config = ConfigProviderResolver.instance() .getBuilder() - .withSources(MpConfigSources.create(config)) + .withSources(MpConfigSources.create(Objects.requireNonNull(config, "Config cannot be null"))) .build(); return this; diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerTest.java b/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerTest.java new file mode 100644 index 00000000000..f6e33f76b5a --- /dev/null +++ b/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerTest.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019, 2022 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.server; + +import io.helidon.config.Config; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Unit test for {@link Server}. + */ +class ServerTest { + + @Test + void testServerNullConfig() { + NullPointerException npe = assertThrows(NullPointerException.class, () -> Server.builder().config((Config) null).build()); + assertThat(npe.getMessage(), is("Config cannot be null")); + } +}