Skip to content

Commit

Permalink
4.x: Add text block support (helidon-io#8655)
Browse files Browse the repository at this point in the history
Add text block support

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos authored May 2, 2024
1 parent a5c30ee commit 85f95d6
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Defines the configuration as a String in {@link #value()} for the
* given type.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Inherited
public @interface AddConfigBlock {

/**
* Specifies the format type of the {@link #value()}.
*
* It defaults to 'properties'.
*
* @return the supported type
*/
String type() default "properties";

/**
* Configuration value.
*
* @return String with value.
*/
String value();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 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.
Expand All @@ -16,7 +16,10 @@

package io.helidon.microprofile.testing.junit5;

import java.io.IOException;
import java.io.Serial;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
Expand All @@ -30,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import io.helidon.config.mp.MpConfigSources;
Expand Down Expand Up @@ -117,6 +121,7 @@ public void beforeAll(ExtensionContext context) {
AddConfig[] configs = getAnnotations(testClass, AddConfig.class);
classLevelConfigMeta.addConfig(configs);
classLevelConfigMeta.configuration(testClass.getAnnotation(Configuration.class));
classLevelConfigMeta.addConfigBlock(testClass.getAnnotation(AddConfigBlock.class));
configProviderResolver = ConfigProviderResolver.instance();

AddExtension[] extensions = getAnnotations(testClass, AddExtension.class);
Expand Down Expand Up @@ -193,6 +198,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
ConfigMeta methodLevelConfigMeta = classLevelConfigMeta.nextMethod();
methodLevelConfigMeta.addConfig(configs);
methodLevelConfigMeta.configuration(method.getAnnotation(Configuration.class));
methodLevelConfigMeta.addConfigBlock(method.getAnnotation(AddConfigBlock.class));

configure(methodLevelConfigMeta);

Expand Down Expand Up @@ -320,6 +326,9 @@ private void configure(ConfigMeta configMeta) {
builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0]));
}
});
if (configMeta.type != null && configMeta.block != null) {
builder.withSources(configSource(configMeta.type, configMeta.block));
}
config = builder
.withSources(MpConfigSources.create(configMeta.additionalKeys))
.addDefaultSources()
Expand All @@ -330,6 +339,23 @@ private void configure(ConfigMeta configMeta) {
}
}

private ConfigSource configSource(String type, String block) {
String lowerCase = type.toLowerCase();
if ("properties".equals(lowerCase)) {
Properties p = new Properties();
try {
p.load(new StringReader(block));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return MpConfigSources.create("PropertiesAddConfigBlock", p);
} else if ("yaml".equals(lowerCase)) {
return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(block));
} else {
throw new IllegalArgumentException(type + " is not supported");
}
}

private void releaseConfig() {
if (configProviderResolver != null && config != null) {
configProviderResolver.releaseConfig(config);
Expand Down Expand Up @@ -600,6 +626,8 @@ private boolean hasBda(Class<?> value) {
private static final class ConfigMeta {
private final Map<String, String> additionalKeys = new HashMap<>();
private final List<String> additionalSources = new ArrayList<>();
private String type;
private String block;
private boolean useExisting;
private String profile;

Expand Down Expand Up @@ -632,6 +660,14 @@ private void configuration(Configuration config) {
additionalKeys.put("mp.config.profile", profile);
}

private void addConfigBlock(AddConfigBlock config) {
if (config == null) {
return;
}
this.type = config.type();
this.block = config.value();
}

ConfigMeta nextMethod() {
ConfigMeta methodMeta = new ConfigMeta();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.testng;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Defines the configuration as a String in {@link #value()} for the
* given type.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Inherited
public @interface AddConfigBlock {

/**
* Specifies the format type of the {@link #value()}.
*
* It defaults to 'properties'.
*
* @return the supported type
*/
String type() default "properties";

/**
* Configuration value.
*
* @return String with value.
*/
String value();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 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.
Expand All @@ -16,7 +16,10 @@

package io.helidon.microprofile.testing.testng;

import java.io.IOException;
import java.io.Serial;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
Expand All @@ -29,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import io.helidon.config.mp.MpConfigSources;
Expand Down Expand Up @@ -105,6 +109,7 @@ public void onBeforeClass(ITestClass iTestClass) {
AddConfig[] configs = getAnnotations(testClass, AddConfig.class);
classLevelConfigMeta.addConfig(configs);
classLevelConfigMeta.configuration(testClass.getAnnotation(Configuration.class));
classLevelConfigMeta.addConfigBlock(testClass.getAnnotation(AddConfigBlock.class));
configProviderResolver = ConfigProviderResolver.instance();

AddExtension[] extensions = getAnnotations(testClass, AddExtension.class);
Expand Down Expand Up @@ -171,6 +176,7 @@ public void onTestStart(ITestResult result) {
ConfigMeta methodLevelConfigMeta = classLevelConfigMeta.nextMethod();
methodLevelConfigMeta.addConfig(configs);
methodLevelConfigMeta.configuration(method.getAnnotation(Configuration.class));
methodLevelConfigMeta.addConfigBlock(method.getAnnotation(AddConfigBlock.class));

configure(methodLevelConfigMeta);

Expand Down Expand Up @@ -333,6 +339,9 @@ private void configure(ConfigMeta configMeta) {
builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0]));
}
});
if (configMeta.type != null && configMeta.block != null) {
builder.withSources(configSource(configMeta.type, configMeta.block));
}
config = builder
.withSources(MpConfigSources.create(configMeta.additionalKeys))
.addDefaultSources()
Expand All @@ -343,6 +352,23 @@ private void configure(ConfigMeta configMeta) {
}
}

private ConfigSource configSource(String type, String block) {
String lowerCase = type.toLowerCase();
if ("properties".equals(lowerCase)) {
Properties p = new Properties();
try {
p.load(new StringReader(block));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return MpConfigSources.create("PropertiesAddConfigBlock", p);
} else if ("yaml".equals(lowerCase)) {
return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(block));
} else {
throw new IllegalArgumentException(type + " is not supported");
}
}

private void releaseConfig() {
if (configProviderResolver != null && config != null) {
configProviderResolver.releaseConfig(config);
Expand Down Expand Up @@ -485,6 +511,8 @@ private boolean hasBda(Class<?> value) {
private static final class ConfigMeta {
private final Map<String, String> additionalKeys = new HashMap<>();
private final List<String> additionalSources = new ArrayList<>();
private String type;
private String block;
private boolean useExisting;
private String profile;

Expand Down Expand Up @@ -517,6 +545,14 @@ private void configuration(Configuration config) {
additionalKeys.put("mp.config.profile", profile);
}

private void addConfigBlock(AddConfigBlock config) {
if (config == null) {
return;
}
this.type = config.type();
this.block = config.value();
}

ConfigMeta nextMethod() {
ConfigMeta methodMeta = new ConfigMeta();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.tests.testing.junit5;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import jakarta.inject.Inject;

import io.helidon.microprofile.testing.junit5.AddConfigBlock;
import io.helidon.microprofile.testing.junit5.HelidonTest;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Test;

@HelidonTest
@AddConfigBlock("""
some.key1=some.value1
some.key2=some.value2
""")
class TestAddConfigBlockProperties {

@Inject
@ConfigProperty(name = "some.key1")
private String value1;

@Inject
@ConfigProperty(name = "some.key2")
private String value2;

@Test
void testValue() {
assertThat(value1, is("some.value1"));
assertThat(value2, is("some.value2"));
}
}
Loading

0 comments on commit 85f95d6

Please sign in to comment.