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 7c807c01f90..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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 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. @@ -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 31e147089cc..14caaab7b29 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 @@ -23,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import io.helidon.config.Config; import io.helidon.config.ConfigException; import io.helidon.config.ConfigSources; import io.helidon.config.PropertiesConfigParser; @@ -36,6 +37,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; @@ -99,6 +101,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 303b9c9cf6f..b9abeae3459 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020 Oracle and/or its affiliates. + * Copyright (c) 2018, 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. @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.function.Supplier; import java.util.logging.Logger; @@ -320,7 +321,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/ServerImplTest.java b/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerImplTest.java index d59ef897a62..d7ded9f14e2 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerImplTest.java +++ b/microprofile/server/src/test/java/io/helidon/microprofile/server/ServerImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2021 Oracle and/or its affiliates. + * Copyright (c) 2018, 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. @@ -27,11 +27,14 @@ import javax.ws.rs.core.Application; import io.helidon.common.configurable.ThreadPoolSupplier; +import io.helidon.config.Config; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; 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.startsWith; import static org.hamcrest.MatcherAssert.assertThat; @@ -109,6 +112,12 @@ void testTwoApps() { } } + @Test + void testServerNullConfig() { + NullPointerException npe = assertThrows(NullPointerException.class, () -> Server.builder().config((Config) null).build()); + assertThat(npe.getMessage(), is("Config cannot be null")); + } + private final class TestApplication1 extends Application { @Override public Set getSingletons() {