de.hilling.junit.cdi.microprofile.TestScopedConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdi-test-microprofile Show documentation
Show all versions of cdi-test-microprofile Show documentation
Support Microprofile Config in cdi-test
package de.hilling.junit.cdi.microprofile;
import de.hilling.junit.cdi.annotations.GlobalTestImplementation;
import de.hilling.junit.cdi.scope.TestScoped;
import io.smallrye.config.PropertiesConfigSourceProvider;
import io.smallrye.config.SmallRyeConfigBuilder;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
@Priority(10000)
@TestScoped
public class TestScopedConfiguration implements ConfigSource {
public static final String TEST_CONFIGURATION_NAME = "TEST_CASE_MICROPROFILE_CONFIGURATION";
private static final String META_INF_MICROPROFILE_CONFIG_PROPERTIES = "META-INF/microprofile-config.properties";
private static final String WEB_INF_MICROPROFILE_CONFIG_PROPERTIES = "WEB-INF/classes/META-INF/microprofile-config.properties";
@GlobalTestImplementation
@Dependent
@Produces
private Config testConfig;
@Inject
private TestPropertiesHolder properties;
private Map testProperties;
@PostConstruct
protected void create() {
testProperties = properties.getProperties();
final ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
ArrayList defaultSources = new ArrayList<>();
defaultSources.addAll(new PropertiesConfigSourceProvider(META_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader, true).getConfigSources(
classLoader));
defaultSources.addAll(new PropertiesConfigSourceProvider(WEB_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader, true).getConfigSources(
classLoader));
defaultSources.add(this);
testConfig = new SmallRyeConfigBuilder().addDefaultSources()
.withSources(defaultSources.toArray(new ConfigSource[0]))
.addDiscoveredConverters()
.build();
}
@Override
public Map getProperties() {
return testProperties;
}
@Override
public Set getPropertyNames() {
return testProperties.keySet();
}
@Override
public String getValue(String propertyName) {
return testProperties.get(propertyName);
}
@Override
public String getName() {
return TEST_CONFIGURATION_NAME;
}
@Override
public int getOrdinal() {
return 10000;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy