From 1a469dd45e8d43ee64b550ae37207a4131c2473d Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Thu, 10 Nov 2022 01:29:04 -0800 Subject: [PATCH 1/2] Add null check to MP Server.Builder.config() --- .../java/io/helidon/config/mp/MpConfigSources.java | 4 ++-- .../io/helidon/config/mp/MpConfigSourcesTest.java | 10 ++++++++++ .../io/helidon/microprofile/server/Server.java | 5 +++-- .../microprofile/server/ServerImplTest.java | 14 +++++++++++++- 4 files changed, 28 insertions(+), 5 deletions(-) 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..684f34a4767 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; @@ -99,6 +100,15 @@ void testHelidonLazy() { assertThat("exists called exactly once", lazy.exists.get(), is(1)); } + @Test + void testMpConfigSourcesNullConfig() { + try { + MpConfigSources.create((Config) null); + } catch(NullPointerException npe) { + 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..97f5053185b 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,13 @@ 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.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; @@ -109,6 +111,16 @@ void testTwoApps() { } } + @Test + void testServerNullConfig() { + try { + Server.builder() + .config((Config) null).build(); + } catch(NullPointerException npe) { + assertThat(npe.getMessage(), is("Config cannot be null")); + } + } + private final class TestApplication1 extends Application { @Override public Set getSingletons() { From dc657f6a6bc1d9b4961e21cbde72c68c72dcddf1 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Thu, 10 Nov 2022 08:45:20 -0800 Subject: [PATCH 2/2] Improve the NullPointerException assertion test --- .../java/io/helidon/config/mp/MpConfigSourcesTest.java | 8 +++----- .../io/helidon/microprofile/server/ServerImplTest.java | 9 +++------ 2 files changed, 6 insertions(+), 11 deletions(-) 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 684f34a4767..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 @@ -37,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; @@ -102,11 +103,8 @@ void testHelidonLazy() { @Test void testMpConfigSourcesNullConfig() { - try { - MpConfigSources.create((Config) null); - } catch(NullPointerException npe) { - assertThat(npe.getMessage(), is("Config cannot be null")); - } + 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 { 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 97f5053185b..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 @@ -32,6 +32,7 @@ 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; @@ -113,12 +114,8 @@ void testTwoApps() { @Test void testServerNullConfig() { - try { - Server.builder() - .config((Config) null).build(); - } catch(NullPointerException npe) { - assertThat(npe.getMessage(), is("Config cannot be null")); - } + 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 {