Skip to content

Commit

Permalink
eclipse#431 improve configsource api
Browse files Browse the repository at this point in the history
Signed-off-by: Emily Jiang <emijiang6@googlemail.com>
  • Loading branch information
Emily-Jiang committed Mar 6, 2020
1 parent 1ff7d91 commit 994b4c4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
******************************************************************************
* Copyright (c) 2009-2018 Contributors to the Eclipse Foundation
* Copyright (c) 2009-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -28,6 +28,7 @@
*******************************************************************************/
package org.eclipse.microprofile.config.spi;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -108,10 +109,16 @@ public interface ConfigSource {
*
* @return a map containing properties of this configuration source
*/
Map<String, String> getProperties();
default Map<String, String> getProperties() {
Map<String, String> props = new HashMap<>();
getPropertyNames().stream().forEach((prop) -> props.put(prop, getValue(prop)));
return props;
}



/**
* Gets all property names known to this configuration source, without evaluating the values.
* Gets all property names known to this configuration source, potentially without evaluating the values.
* <p>
* For backwards compatibility, there is a default implementation that just returns the keys of {@code getProperties()}.
* Implementations should consider replacing this with a more performant implementation.
Expand All @@ -120,9 +127,7 @@ public interface ConfigSource {
*
* @return a set of property names that are known to this configuration source
*/
default Set<String> getPropertyNames() {
return getProperties().keySet();
}
Set<String> getPropertyNames();

/**
* Return the ordinal priority value of this configuration source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
* @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
*
*/
@org.osgi.annotation.versioning.Version("1.4")
@org.osgi.annotation.versioning.Version("2.0")
package org.eclipse.microprofile.config.spi;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.CDI;
Expand All @@ -47,14 +48,15 @@
import org.testng.annotations.Test;

/**
* Test cases for CDI-based API that test retrieving values from the configuration.
* The tests depend only on CDI 1.2.
* Test cases for CDI-based API that test retrieving values from the
* configuration. The tests depend only on CDI 1.2.
*
* @author Ondrej Mihalyi
*/
public class CDIPlainInjectionTest extends Arquillian {

private static final String DEFAULT_PROPERTY_BEAN_KEY =
"org.eclipse.microprofile.config.tck.CDIPlainInjectionTest.DefaultPropertyBean.configProperty";
private static final String DEFAULT_PROPERTY_BEAN_KEY =
"org.eclipse.microprofile.config.tck.CDIPlainInjectionTest.DefaultPropertyBean.configProperty";

@Deployment
public static Archive deployment() {
Expand All @@ -77,17 +79,17 @@ public void canInjectSimpleValuesWhenDefined() {

assertThat(bean.stringProperty, is(equalTo("text")));
assertThat(bean.booleanProperty, is(true));
assertThat(bean.byteProperty, is(equalTo((byte)127)));
assertThat(bean.shortProperty, is(equalTo((short)32767)));
assertThat(bean.byteProperty, is(equalTo((byte) 127)));
assertThat(bean.shortProperty, is(equalTo((short) 32767)));
assertThat(bean.intProperty, is(equalTo(5)));
assertThat(bean.longProperty, is(equalTo(10L)));
assertThat(bean.floatProperty, is(floatCloseTo(10.5f, 0.1f)));
assertThat(bean.doubleProperty, is(closeTo(11.5, 0.1)));
assertThat(bean.charProperty, is(equalTo('c')));

assertThat(bean.booleanObjProperty, is(true));
assertThat(bean.byteObjProperty, is(equalTo(Byte.valueOf((byte)127))));
assertThat(bean.shortObjProperty, is(equalTo(Short.valueOf((short)32767))));
assertThat(bean.byteObjProperty, is(equalTo(Byte.valueOf((byte) 127))));
assertThat(bean.shortObjProperty, is(equalTo(Short.valueOf((short) 32767))));
assertThat(bean.integerProperty, is(equalTo(5)));
assertThat(bean.longObjProperty, is(equalTo(10L)));
assertThat(bean.floatObjProperty, is(floatCloseTo(10.5f, 0.1f)));
Expand All @@ -104,36 +106,34 @@ public void canInjectSimpleValuesWhenDefined() {
public void injectedValuesAreEqualToProgrammaticValues() {
SimpleValuesBean bean = getBeanOfType(SimpleValuesBean.class);

assertThat(bean.stringProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.string.property", String.class))));
assertThat(bean.booleanProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.boolean.property", Boolean.class))));
assertThat(bean.byteProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.byte.property", Byte.class))));
assertThat(bean.shortProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.short.property", Short.class))));
assertThat(bean.intProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.int.property", Integer.class))));
assertThat(bean.longProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.long.property", Long.class))));
assertThat(bean.floatProperty, is(floatCloseTo(
ConfigProvider.getConfig().getValue("my.float.property", Float.class), 0.1f)));
assertThat(bean.doubleProperty, is(closeTo(
ConfigProvider.getConfig().getValue("my.double.property", Double.class), 0.1)));
assertThat(bean.charProperty, is(equalTo(
ConfigProvider.getConfig().getValue("my.char.property", Character.class))));

assertThat(bean.doublePropertyWithDefaultValue, is(closeTo(
ConfigProvider.getConfig().getOptionalValue("my.not.configured.double.property", Double.class)
.orElse(3.1415), 0.1)));
assertThat(bean.stringProperty,
is(equalTo(ConfigProvider.getConfig().getValue("my.string.property", String.class))));
assertThat(bean.booleanProperty,
is(equalTo(ConfigProvider.getConfig().getValue("my.boolean.property", Boolean.class))));
assertThat(bean.byteProperty, is(equalTo(ConfigProvider.getConfig().getValue("my.byte.property", Byte.class))));
assertThat(bean.shortProperty,
is(equalTo(ConfigProvider.getConfig().getValue("my.short.property", Short.class))));
assertThat(bean.intProperty,
is(equalTo(ConfigProvider.getConfig().getValue("my.int.property", Integer.class))));
assertThat(bean.longProperty, is(equalTo(ConfigProvider.getConfig().getValue("my.long.property", Long.class))));
assertThat(bean.floatProperty,
is(floatCloseTo(ConfigProvider.getConfig().getValue("my.float.property", Float.class), 0.1f)));
assertThat(bean.doubleProperty,
is(closeTo(ConfigProvider.getConfig().getValue("my.double.property", Double.class), 0.1)));
assertThat(bean.charProperty,
is(equalTo(ConfigProvider.getConfig().getValue("my.char.property", Character.class))));

assertThat(bean.doublePropertyWithDefaultValue, is(closeTo(ConfigProvider.getConfig()
.getOptionalValue("my.not.configured.double.property", Double.class).orElse(3.1415), 0.1)));
}

@Test
public void canInjectDynamicValuesViaCdiProvider() {

DynamicValuesBean bean = getBeanOfType(DynamicValuesBean.class);

//X TODO clarify how Provider<T> should behave for missing values assertThat(bean.getIntProperty(), is(nullValue()));
// X TODO clarify how Provider<T> should behave for missing values
// assertThat(bean.getIntProperty(), is(nullValue()));

assertThat(bean.getIntProperty(), is(equalTo(5)));
}
Expand Down Expand Up @@ -179,77 +179,77 @@ private <T> T getBeanOfType(Class<T> beanClass) {
public static class SimpleValuesBean {

@Inject
@ConfigProperty(name="my.string.property")
@ConfigProperty(name = "my.string.property")
private String stringProperty;

@Inject
@ConfigProperty(name="my.boolean.property")
@ConfigProperty(name = "my.boolean.property")
private Boolean booleanObjProperty;

@Inject
@ConfigProperty(name="my.boolean.property")
@ConfigProperty(name = "my.boolean.property")
private boolean booleanProperty;

@Inject
@ConfigProperty(name="my.byte.property")
@ConfigProperty(name = "my.byte.property")
private Byte byteObjProperty;

@Inject
@ConfigProperty(name="my.byte.property")
@ConfigProperty(name = "my.byte.property")
private byte byteProperty;

@Inject
@ConfigProperty(name="my.short.property")
@ConfigProperty(name = "my.short.property")
private Short shortObjProperty;

@Inject
@ConfigProperty(name="my.short.property")
@ConfigProperty(name = "my.short.property")
private short shortProperty;

@Inject
@ConfigProperty(name="my.int.property")
@ConfigProperty(name = "my.int.property")
private Integer integerProperty;

@Inject
@ConfigProperty(name="my.int.property")
@ConfigProperty(name = "my.int.property")
private int intProperty;

@Inject
@ConfigProperty(name="my.long.property")
@ConfigProperty(name = "my.long.property")
private Long longObjProperty;

@Inject
@ConfigProperty(name="my.long.property")
@ConfigProperty(name = "my.long.property")
private long longProperty;

@Inject
@ConfigProperty(name="my.float.property")
@ConfigProperty(name = "my.float.property")
private Float floatObjProperty;

@Inject
@ConfigProperty(name="my.float.property")
@ConfigProperty(name = "my.float.property")
private float floatProperty;

@Inject
@ConfigProperty(name="my.double.property")
@ConfigProperty(name = "my.double.property")
private Double doubleObjProperty;

@Inject
@ConfigProperty(name="my.double.property")
@ConfigProperty(name = "my.double.property")
private double doubleProperty;

@Inject
@ConfigProperty(name="my.char.property")
@ConfigProperty(name = "my.char.property")
private Character characterProperty;

@Inject
@ConfigProperty(name="my.char.property")
@ConfigProperty(name = "my.char.property")
private char charProperty;

// the property is not configured in any ConfigSource but its defaultValue will
// be used to set the field.
@Inject
@ConfigProperty(name="my.not.configured.double.property", defaultValue = "3.1415")
@ConfigProperty(name = "my.not.configured.double.property", defaultValue = "3.1415")
private Double doublePropertyWithDefaultValue;

}
Expand All @@ -258,7 +258,7 @@ public static class SimpleValuesBean {
public static class DynamicValuesBean {

@Inject
@ConfigProperty(name="my.int.property")
@ConfigProperty(name = "my.int.property")
private Provider<Integer> intPropertyProvider;

public Integer getIntProperty() {
Expand Down Expand Up @@ -296,10 +296,6 @@ public TestConfigSource() {
properties.put(DEFAULT_PROPERTY_BEAN_KEY, "pathConfigValue");
}

@Override
public Map<String, String> getProperties() {
return properties;
}

@Override
public String getValue(String propertyName) {
Expand All @@ -310,5 +306,10 @@ public String getValue(String propertyName) {
public String getName() {
return this.getClass().getName();
}

@Override
public Set<String> getPropertyNames() {
return properties.keySet();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

Expand All @@ -41,12 +42,6 @@ public CustomDbConfigSource() {
public int getOrdinal() {
return 112;
}

@Override
public Map<String, String> getProperties() {
return readPropertiesFromDb();
}

@Override
public String getValue(String key) {
return readPropertyFromDb(key);
Expand All @@ -57,13 +52,15 @@ public String getName() {
return "customDbConfig";
}

private Map<String, String> readPropertiesFromDb() {
return configValues;
}

private String readPropertyFromDb(String key) {
return configValues.get(key);
}

@Override
public Set<String> getPropertyNames() {

return configValues.keySet();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

Expand All @@ -40,11 +41,6 @@ public int getOrdinal() {
return 110;
}

@Override
public Map<String, String> getProperties() {
return config;
}

@Override
public String getValue(String key) {
return config.get(key);
Expand All @@ -55,4 +51,10 @@ public String getName() {
return null;
}

@Override
public Set<String> getPropertyNames() {

return config.keySet();
}

}

0 comments on commit 994b4c4

Please sign in to comment.