Skip to content

Commit

Permalink
Add null check to MP Server.Builder.config() (#5363) (#5374)
Browse files Browse the repository at this point in the history
* Add null check to MP Server.Builder.config()

* Improve the NullPointerException assertion test
  • Loading branch information
klustria authored Nov 11, 2022
1 parent fe81804 commit 6180534
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 6180534

Please sign in to comment.