Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-grecourt committed Nov 13, 2024
1 parent 8497c10 commit 0b48ba1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 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.testing.junit5;

import java.util.Map;

import io.helidon.config.mp.MpConfigSources;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;

/**
* Bootstrap configuration.
*/
class BootstrapConfig {

private static final ThreadLocal<Config> THREAD_LOCAL = new ThreadLocal<>();
private static final Config BOOTSTRAP_CONFIG = ConfigProviderResolver.instance().getBuilder()
.withSources(MpConfigSources.create(Map.of(
"mp.initializer.allow", "true",
"mp.initializer.no-warn", "true")))
.build();

private BootstrapConfig() {
// cannot be instantiated
}

/**
* Register the bootstrap configuration.
*/
static void register() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ConfigProviderResolver resolver = ConfigProviderResolver.instance();
Config config = resolver.getConfig(cl);
THREAD_LOCAL.set(config);
resolver.releaseConfig(config);
resolver.registerConfig(BOOTSTRAP_CONFIG, cl);
}

/**
* Restore the original configuration.
*/
static void restore() {
ConfigProviderResolver resolver = ConfigProviderResolver.instance();
resolver.releaseConfig(BOOTSTRAP_CONFIG);
Config config = THREAD_LOCAL.get();
if (config != null) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
resolver.registerConfig(config, cl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
*/
package io.helidon.microprofile.testing.junit5;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import io.helidon.config.mp.MpConfigSources;

import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import jakarta.enterprise.inject.spi.BeanManager;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;

import static io.helidon.microprofile.testing.junit5.ReflectionHelper.requirePublic;

Expand All @@ -33,15 +28,6 @@
*/
final class HelidonTestContainer {

/**
* Bootstrap configuration.
*/
static final Config BOOTSTRAP_CONFIG = ConfigProviderResolver.instance().getBuilder()
.withSources(MpConfigSources.create(Map.of(
"mp.initializer.allow", "true",
"mp.initializer.no-warn", "true")))
.build();

private final HelidonTestInfo testInfo;
private final AtomicBoolean stopped = new AtomicBoolean(false);
private final AtomicBoolean started = new AtomicBoolean(false);
Expand Down Expand Up @@ -114,8 +100,7 @@ private SeContainer container() {

@SuppressWarnings("unchecked")
private void start() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ConfigProviderResolver.instance().registerConfig(BOOTSTRAP_CONFIG, cl);
BootstrapConfig.register();
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
if (testInfo.disableDiscovery()) {
initializer.disableDiscovery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ final class HelidonTestExtension implements Extension {
Configuration.class);

private final HelidonTestInfo testInfo;
private final List<AddBean> addBeans = new ArrayList<>();
private final List<Socket> sockets = new ArrayList<>();
private final Set<AddBean> addBeans = new HashSet<>();
private final Set<Socket> sockets = new HashSet<>();
private final List<Method> afterStop = new ArrayList<>();
private final Map<String, String> configMap = new HashMap<>();
private final List<ConfigSource> configSources = new ArrayList<>();
Expand Down Expand Up @@ -304,18 +304,20 @@ private void afterStop(@Observes
}

private void registerConfig() {
ConfigProviderResolver provider = ConfigProviderResolver.instance();
provider.releaseConfig(HelidonTestContainer.BOOTSTRAP_CONFIG);
if (!useExisting) {
ConfigBuilder builder = provider.getBuilder()
if (useExisting) {
BootstrapConfig.restore();
} else {
ConfigProviderResolver resolver = ConfigProviderResolver.instance();
ConfigBuilder builder = resolver
.getBuilder()
.addDefaultSources()
.addDiscoveredSources()
.addDiscoveredConverters();
configSources.forEach(builder::withSources);
Config config = builder.build();
provider.registerConfig(config, Thread.currentThread().getContextClassLoader());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
resolver.registerConfig(config, cl);
}
// TODO else restore pre-bootstrap config
}

private void configuration(Configuration annotation) {
Expand Down

0 comments on commit 0b48ba1

Please sign in to comment.