Skip to content

Commit

Permalink
Modify type as string
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Apr 19, 2024
1 parent fb92215 commit b6d2527
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,11 @@

package io.helidon.microprofile.testing.junit5;

import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
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;
import java.util.Objects;
import java.util.Properties;

import io.helidon.config.mp.MpConfigSources;
import io.helidon.config.yaml.mp.YamlMpConfigSource;

import org.eclipse.microprofile.config.spi.ConfigSource;

/**
* Defines the configuration as a String in {@link #value()} for the
Expand All @@ -44,11 +34,11 @@
/**
* Specifies the format type of the {@link #value()}.
*
* It defaults to {@link Type#PROPERTIES}.
* It defaults to 'properties'.
*
* @return the supported type
*/
Type type() default Type.PROPERTIES;
String type() default "properties";

/**
* Configuration value.
Expand All @@ -57,44 +47,4 @@
*/
String value();

/**
* Different formats of the configuration.
*/
enum Type {

/**
* Properties format.
*/
PROPERTIES {
@Override
protected ConfigSource configSource(String value) {
Objects.requireNonNull(value);
Properties p = new Properties();
try {
p.load(new StringReader(value));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return MpConfigSources.create("PropertiesAddConfigBlock", p);
}
},
/**
* YAML format.
*/
YAML {
@Override
protected ConfigSource configSource(String value) {
Objects.requireNonNull(value);
return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(value));
}
};

/**
* Create the ConfigSource given the value.
*
* @param value
* @return the configuration
*/
protected abstract ConfigSource configSource(String value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,13 +33,13 @@
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;
import io.helidon.config.yaml.mp.YamlMpConfigSource;
import io.helidon.microprofile.server.JaxRsCdiExtension;
import io.helidon.microprofile.server.ServerCdiExtension;
import io.helidon.microprofile.testing.junit5.AddConfigBlock.Type;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand Down Expand Up @@ -323,8 +326,8 @@ private void configure(ConfigMeta configMeta) {
builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0]));
}
});
if (configMeta.type != null) {
builder.withSources(configMeta.type.configSource(configMeta.block));
if (configMeta.type != null && configMeta.block != null) {
builder.withSources(configSource(configMeta.type, configMeta.block));
}
config = builder
.withSources(MpConfigSources.create(configMeta.additionalKeys))
Expand All @@ -336,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 @@ -606,7 +626,7 @@ 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 Type type;
private String type;
private String block;
private boolean useExisting;
private String profile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,11 @@

package io.helidon.microprofile.testing.testng;

import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
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;
import java.util.Objects;
import java.util.Properties;

import io.helidon.config.mp.MpConfigSources;
import io.helidon.config.yaml.mp.YamlMpConfigSource;

import org.eclipse.microprofile.config.spi.ConfigSource;

/**
* Defines the configuration as a String in {@link #value()} for the
Expand All @@ -44,11 +34,11 @@
/**
* Specifies the format type of the {@link #value()}.
*
* It defaults to {@link Type#PROPERTIES}.
* It defaults to 'properties'.
*
* @return the supported type
*/
Type type() default Type.PROPERTIES;
String type() default "properties";

/**
* Configuration value.
Expand All @@ -57,44 +47,4 @@
*/
String value();

/**
* Different formats of the configuration.
*/
enum Type {

/**
* Properties format.
*/
PROPERTIES {
@Override
protected ConfigSource configSource(String value) {
Objects.requireNonNull(value);
Properties p = new Properties();
try {
p.load(new StringReader(value));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return MpConfigSources.create("PropertiesAddConfigBlock", p);
}
},
/**
* YAML format.
*/
YAML {
@Override
protected ConfigSource configSource(String value) {
Objects.requireNonNull(value);
return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(value));
}
};

/**
* Create the ConfigSource given the value.
*
* @param value
* @return the configuration
*/
protected abstract ConfigSource configSource(String value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,13 +32,13 @@
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;
import io.helidon.config.yaml.mp.YamlMpConfigSource;
import io.helidon.microprofile.server.JaxRsCdiExtension;
import io.helidon.microprofile.server.ServerCdiExtension;
import io.helidon.microprofile.testing.testng.AddConfigBlock.Type;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand Down Expand Up @@ -336,8 +339,8 @@ private void configure(ConfigMeta configMeta) {
builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0]));
}
});
if (configMeta.type != null) {
builder.withSources(configMeta.type.configSource(configMeta.block));
if (configMeta.type != null && configMeta.block != null) {
builder.withSources(configSource(configMeta.type, configMeta.block));
}
config = builder
.withSources(MpConfigSources.create(configMeta.additionalKeys))
Expand All @@ -349,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 @@ -491,7 +511,7 @@ 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 Type type;
private String type;
private String block;
private boolean useExisting;
private String profile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import jakarta.inject.Inject;

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

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

@HelidonTest
@AddConfigBlock(type = Type.YAML, value = """
@AddConfigBlock(type = "Yaml", value = """
another1:
key: "another1.value"
another2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import jakarta.inject.Inject;

import io.helidon.microprofile.testing.testng.AddConfigBlock;
import io.helidon.microprofile.testing.testng.AddConfigBlock.Type;
import io.helidon.microprofile.testing.testng.HelidonTest;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.testng.annotations.Test;

@HelidonTest
@AddConfigBlock(type = Type.YAML, value = """
@AddConfigBlock(type = "Yaml", value = """
another1:
key: "another1.value"
another2:
Expand Down

0 comments on commit b6d2527

Please sign in to comment.