Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup to some tests that register custom config #22921

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.util.Map;
import java.util.NoSuchElementException;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.smallrye.config.ExpressionConfigSourceInterceptor;
Expand All @@ -23,32 +19,11 @@
*/
public class ConfigExpanderTestCase {

static ClassLoader classLoader;
static ConfigProviderResolver cpr;
Config config;

@BeforeAll
public static void initConfig() {
classLoader = Thread.currentThread().getContextClassLoader();
cpr = ConfigProviderResolver.instance();
}

@AfterEach
public void doAfter() {
try {
cpr.releaseConfig(cpr.getConfig());
} catch (IllegalStateException ignored) {
// just means no config was installed, which is fine
}
}

private SmallRyeConfig buildConfig(Map<String, String> configMap) {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withInterceptors(new ExpressionConfigSourceInterceptor());
builder.withSources(new PropertiesConfigSource(configMap, "test input", 500));
final SmallRyeConfig config = (SmallRyeConfig) builder.build();
cpr.registerConfig(config, classLoader);
this.config = config;
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.util.Map;
import java.util.stream.StreamSupport;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.smallrye.config.ExpressionConfigSourceInterceptor;
Expand All @@ -24,24 +21,6 @@

public class ConfigProfileTestCase {

static ClassLoader classLoader;
static ConfigProviderResolver cpr;

@BeforeAll
public static void initConfig() {
classLoader = Thread.currentThread().getContextClassLoader();
cpr = ConfigProviderResolver.instance();
}

@AfterEach
public void doAfter() {
try {
cpr.releaseConfig(cpr.getConfig());
} catch (IllegalStateException ignored) {
// just means no config was installed, which is fine
}
}

private SmallRyeConfigBuilder configBuilder(String... keyValues) {
return new SmallRyeConfigBuilder()
.addDefaultInterceptors()
Expand All @@ -51,7 +30,6 @@ private SmallRyeConfigBuilder configBuilder(String... keyValues) {

private SmallRyeConfig buildConfig(String... keyValues) {
final SmallRyeConfig config = configBuilder(keyValues).build();
cpr.registerConfig(config, classLoader);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.smallrye.config.SmallRyeConfig;
Expand All @@ -28,24 +25,6 @@
*/
public class DotEnvTestCase {

static ClassLoader classLoader;
static ConfigProviderResolver cpr;

@BeforeAll
public static void initConfig() {
classLoader = Thread.currentThread().getContextClassLoader();
cpr = ConfigProviderResolver.instance();
}

@AfterEach
public void doAfter() {
try {
cpr.releaseConfig(cpr.getConfig());
} catch (IllegalStateException ignored) {
// just means no config was installed, which is fine
}
}

private SmallRyeConfig buildConfig(Map<String, String> configMap) throws IOException {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
final Path dotEnv = Files.createTempFile("test-", ".env");
Expand All @@ -62,10 +41,10 @@ private SmallRyeConfig buildConfig(Map<String, String> configMap) throws IOExcep
}
}
builder.withSources(
new ConfigUtils.BuildTimeDotEnvConfigSourceProvider(dotEnv.toUri().toString()).getConfigSources(classLoader));
new ConfigUtils.BuildTimeDotEnvConfigSourceProvider(dotEnv.toUri().toString())
.getConfigSources(Thread.currentThread().getContextClassLoader()));
final SmallRyeConfig config = builder.build();
Files.delete(dotEnv);
cpr.registerConfig(config, classLoader);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import java.util.UUID;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.quarkus.runtime.LaunchMode;
Expand All @@ -16,29 +13,10 @@

public class UUIDPropertyTest {

static ClassLoader classLoader;
static ConfigProviderResolver cpr;

@BeforeAll
public static void initConfig() {
classLoader = Thread.currentThread().getContextClassLoader();
cpr = ConfigProviderResolver.instance();
}

@AfterEach
public void doAfter() {
try {
cpr.releaseConfig(cpr.getConfig());
} catch (IllegalStateException ignored) {
// just means no config was installed, which is fine
}
}

private SmallRyeConfig buildConfig() {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withDefaultValue(ConfigUtils.UUID_KEY, UUID.randomUUID().toString());
final SmallRyeConfig config = builder.build();
cpr.registerConfig(config, classLoader);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@

import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Optional;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.smallrye.config.ConfigLogging;
import io.smallrye.config.PropertiesConfigSource;

public class RestClientConfigTest {

private static ClassLoader classLoader;
private static ConfigProviderResolver configProviderResolver;

@BeforeAll
public static void initConfig() {
classLoader = Thread.currentThread().getContextClassLoader();
configProviderResolver = ConfigProviderResolver.instance();
}

@AfterEach
public void releaseConfig() {
configProviderResolver.releaseConfig(configProviderResolver.getConfig());
}

@Test
public void testLoadRestClientConfig() throws IOException {
setupMPConfig();
Expand Down Expand Up @@ -62,27 +75,10 @@ private void verifyConfig(RestClientConfig config) {
}

private static void setupMPConfig() throws IOException {
ConfigProviderResolver resolver = ConfigProviderResolver.instance();
ConfigBuilder configBuilder = resolver.getBuilder();
ConfigBuilder configBuilder = configProviderResolver.getBuilder();
URL propertyFile = RestClientConfigTest.class.getClassLoader().getResource("application.properties");
assertThat(propertyFile).isNotNull();
configBuilder.withSources(new PropertiesConfigSource(propertyFile));
resolver.registerConfig(configBuilder.build(), getContextClassLoader());
}

static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
ClassLoader tccl = null;
try {
tccl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) {
ConfigLogging.log.failedToRetrieveClassloader(ex);
}
return tccl;
});
}
configProviderResolver.registerConfig(configBuilder.build(), classLoader);
}
}