Skip to content

Commit

Permalink
Add null check to MP Server.Builder.config() (helidon-io#5363)
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 committed Nov 10, 2022
1 parent 430ab8b commit d6d7674
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
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.concurrent.ExecutorService;
import java.util.function.Supplier;
import java.util.logging.Logger;
Expand Down Expand Up @@ -326,7 +327,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
@@ -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.
Expand All @@ -20,6 +20,7 @@
import java.util.concurrent.ExecutorService;

import io.helidon.common.configurable.ThreadPoolSupplier;
import io.helidon.config.Config;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
Expand All @@ -30,7 +31,9 @@
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;

Expand Down Expand Up @@ -108,6 +111,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<Object> getSingletons() {
Expand Down

0 comments on commit d6d7674

Please sign in to comment.