Repository: incubator-tamaya Updated Branches: refs/heads/master 620ffe420 -> 1e04cd215
[TAMAYA-303] Implementation of EnvironmentPropertySource fixed. JavaDoc needs update. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/1e04cd21 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/1e04cd21 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/1e04cd21 Branch: refs/heads/master Commit: 1e04cd21563a9a8ebfd83a40143fec32624bda16 Parents: 620ffe4 Author: Oliver B. Fischer <[email protected]> Authored: Thu Sep 21 00:50:22 2017 +0200 Committer: Oliver B. Fischer <[email protected]> Committed: Thu Sep 21 00:50:22 2017 +0200 ---------------------------------------------------------------------- .../EnvironmentPropertySource.java | 123 ++++++++++++++----- .../EnvironmentPropertySourceTest.java | 117 +++++++++++++++++- 2 files changed, 211 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1e04cd21/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java index c204d26..85d1f28 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java +++ b/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java @@ -30,6 +30,9 @@ import java.util.*; * or {@code tamaya.defaults.disable}. */ public class EnvironmentPropertySource extends BasePropertySource { + private static final String TAMAYA_ENVPROPS_PREFIX = "tamaya.envprops.prefix"; + private static final String TAMAYA_ENVPROPS_DISABLE = "tamaya.envprops.disable"; + private static final String TAMAYA_DEFAULT_DISABLE = "tamaya.defaults.disable"; /** * Default ordinal for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource} @@ -48,6 +51,7 @@ public class EnvironmentPropertySource extends BasePropertySource { */ private boolean disabled = false; + private SystemPropertiesProvider propertiesProvider = new SystemPropertiesProvider(); /** * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties @@ -70,22 +74,22 @@ public class EnvironmentPropertySource extends BasePropertySource { * </pre> */ private void initFromSystemProperties() { - String value = System.getProperty("tamaya.envprops.prefix"); - if(value==null){ - prefix = System.getenv("tamaya.envprops.prefix"); - } - value = System.getProperty("tamaya.envprops.disable"); - if(value==null){ - value = System.getenv("tamaya.envprops.disable"); - } - if(value==null){ - value = System.getProperty("tamaya.defaults.disable"); + String temp = getPropertiesProvider().getEnvPropsPrefix(); + + if (temp != null) { + prefix = temp; } - if(value==null){ - value = System.getenv("tamaya.defaults.disable"); + + temp = getPropertiesProvider().getEnvPropsDisable(); + + if (temp != null) { + this.disabled = Boolean.parseBoolean(temp); } - if(value!=null && !value.isEmpty()) { - this.disabled = Boolean.parseBoolean(value); + + temp = getPropertiesProvider().getDefaultsDisable(); + + if (temp != null) { + disabled |= Boolean.parseBoolean(temp); } } @@ -122,7 +126,7 @@ public class EnvironmentPropertySource extends BasePropertySource { @Override public String getName() { - if(disabled){ + if (isDisabled()) { return "environment-properties(disabled)"; } return "environment-properties"; @@ -130,31 +134,48 @@ public class EnvironmentPropertySource extends BasePropertySource { @Override public PropertyValue get(String key) { - if(disabled){ + if (isDisabled()) { return null; } - String prefix = this.prefix; - if(prefix==null) { - return PropertyValue.of(key, System.getenv(key), getName()); - } - return PropertyValue.of(key, System.getenv(key.substring(prefix.length())), getName()); + + String effectiveKey = hasPrefix() ? getPrefix() + "." + key + : key; + + String value = getPropertiesProvider().getenv(effectiveKey); + + return PropertyValue.of(key, value, getName()); + } + + private boolean hasPrefix() { + return null != prefix; } @Override public Map<String, PropertyValue> getProperties() { - if(disabled){ + if (isDisabled()) { return Collections.emptyMap(); } - String prefix = this.prefix; - Map<String,String> envProps = System.getenv(); + + String effectivePrefix = getPrefix() + "."; + int effectivePrefixLength = hasPrefix() ? getPrefix().length() + 1 + : 0; + Map<String, String> envProps = getPropertiesProvider().getenv(); + Map<String, PropertyValue> values = new HashMap<>(); - for (Map.Entry<String,String> entry : envProps.entrySet()) { - if(prefix==null) { + + for (Map.Entry<String, String> entry : envProps.entrySet()) { + if (hasPrefix()) { + if (entry.getKey().startsWith(effectivePrefix)) { + + String choppedKey = entry.getKey().substring(effectivePrefixLength); + String value = entry.getValue(); + values.put(choppedKey, PropertyValue.of(choppedKey, value, getName())); + } + } else { values.put(entry.getKey(), PropertyValue.of(entry.getKey(), entry.getValue(), getName())); - }else { - values.put(prefix + entry.getKey(), PropertyValue.of(prefix + entry.getKey(), entry.getValue(), getName())); } } + return values; } @@ -163,4 +184,50 @@ public class EnvironmentPropertySource extends BasePropertySource { return true; } + void setPropertiesProvider(SystemPropertiesProvider spp) { + propertiesProvider = spp; + initFromSystemProperties(); + } + + SystemPropertiesProvider getPropertiesProvider() { + return propertiesProvider; + } + + public String getPrefix() { + return prefix; + } + + public boolean isDisabled() { + return disabled; + } + + /** + * <p>Provides access to the system properties used to configure + * {@linkplain EnvironmentPropertySource}.</p> + * + * <p>This implementation delegates all property lookups + * to {@linkplain System#getProperty(String)}.</p> + */ + static class SystemPropertiesProvider { + String getEnvPropsPrefix() { + return System.getenv(TAMAYA_ENVPROPS_PREFIX); + } + + String getEnvPropsDisable() { + return System.getenv(TAMAYA_ENVPROPS_DISABLE); + } + + String getDefaultsDisable() { + return System.getenv(TAMAYA_DEFAULT_DISABLE); + } + + String getenv(String name) { + return System.getenv(name); + } + + Map<String, String> getenv() { + return System.getenv(); + } + } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1e04cd21/code/core/src/test/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySourceTest.java b/code/core/src/test/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySourceTest.java index 2781158..80c4a65 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySourceTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySourceTest.java @@ -19,19 +19,29 @@ package org.apache.tamaya.core.propertysource; import org.apache.tamaya.spi.PropertyValue; +import org.junit.Before; import org.junit.Test; +import java.util.HashMap; import java.util.Map; +import static java.lang.Boolean.TRUE; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Tests for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource}. */ public class EnvironmentPropertySourceTest { + private EnvironmentPropertySource envPropertySource; - private final EnvironmentPropertySource envPropertySource = new EnvironmentPropertySource(); + @Before + public void setOUT() { + envPropertySource = new EnvironmentPropertySource(); + } @Test public void testGetOrdinal() throws Exception { @@ -64,4 +74,109 @@ public class EnvironmentPropertySourceTest { public void testIsScannable() throws Exception { assertTrue(envPropertySource.isScannable()); } + + @Test + public void ifPrefixHasBeenConfiguredLookedUpEnvVarNameIsPrefixAndKeyName() { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getEnvPropsPrefix()).thenReturn("zzz"); + when(provider.getenv("zzz.VARIABLE")).thenReturn("value"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.get("VARIABLE").getValue()).isEqualTo("value"); + } + + @Test + public void ifPrefixHasNotBeenConfiguredLookedUpEnvVarNameIsKeyName() { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getEnvPropsPrefix()).thenReturn(null); + when(provider.getenv("VARIABLE")).thenReturn("value"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.get("VARIABLE").getValue()).isEqualTo("value"); + } + + @Test + public void ifPrefixHasBeenSetAllEnvVarsWithPrefixWillBeReturnedByGetProperties() { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getEnvPropsPrefix()).thenReturn("zzz"); + when(provider.getenv()).thenReturn(new HashMap<String, String>() {{ + put("zzz.A", "aaa"); + put("zzz.B", "bbb"); + put("C", "ccc"); + put("D", "ddd"); + }}); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.getProperties()).hasSize(2); + + Map<String, PropertyValue> properties = envPropertySource.getProperties(); + + assertThat(properties.keySet()).containsOnly("A", "B"); + assertThat(properties.get("A").getValue()).isEqualTo("aaa"); + assertThat(properties.get("B").getValue()).isEqualTo("bbb"); + } + + @Test + public void canBeDisableBySystemPropertyTamayaDefaultsDisable() { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getDefaultsDisable()).thenReturn(TRUE.toString()); + when(provider.getenv("VARIABLE")).thenReturn("value"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.get("VARIABLE")).isNull(); + } + + @Test + public void canBeDisableBySystemPropertyTamayaEnvpropsDisable() { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getEnvPropsDisable()).thenReturn(TRUE.toString()); + when(provider.getenv("VARIABLE")).thenReturn("value"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.get("VARIABLE")).isNull(); + } + + @Test + public void isDisabledIfEvenIsDefaultsDisableIsFalse() throws Exception { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getDefaultsDisable()).thenReturn("false"); + when(provider.getEnvPropsDisable()).thenReturn("true"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.isDisabled()).isTrue(); + } + + @Test + public void isDisabledIfEvenIsEnvPropsDisableIsFalse() throws Exception { + EnvironmentPropertySource.SystemPropertiesProvider provider = + mock(EnvironmentPropertySource.SystemPropertiesProvider.class); + + when(provider.getDefaultsDisable()).thenReturn("true"); + when(provider.getEnvPropsDisable()).thenReturn("false"); + + envPropertySource.setPropertiesProvider(provider); + + assertThat(envPropertySource.isDisabled()).isTrue(); + } + + + } \ No newline at end of file
