DELTASPIKE-378 add getPropertyAwarePropertyValue feel free to propose a better name if you find one :)
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/7631ac6f Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/7631ac6f Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/7631ac6f Branch: refs/heads/master Commit: 7631ac6f526edf472d1383f7b82171c7ac29f0fb Parents: 96d9ec7 Author: Mark Struberg <[email protected]> Authored: Fri Jun 7 10:00:44 2013 +0200 Committer: Mark Struberg <[email protected]> Committed: Fri Jun 7 12:06:30 2013 +0200 ---------------------------------------------------------------------- .../deltaspike/core/api/config/ConfigResolver.java | 98 ++++++++++++++- .../test/api/config/ConfigResolverTest.java | 34 +++++- .../test/api/config/TestConfigSource.java | 14 ++ 3 files changed, 142 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7631ac6f/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java index 05cbc59..6b3ada2 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java @@ -162,18 +162,110 @@ public final class ConfigResolver * <p><b>Attention</b> This method must only be used after all ConfigSources * got registered and it also must not be used to determine the ProjectStage itself.</p> * @param key + * @return the configured value or if non found the defaultValue + * + */ + public static String getProjectStageAwarePropertyValue(String key) + { + ProjectStage ps = getProjectStage(); + + String value = getPropertyValue(key + '.' + ps); + if (value == null) + { + value = getPropertyValue(key); + } + + return value; + } + /** + * {@link #getProjectStageAwarePropertyValue(String)} which returns the defaultValue + * if the property is <code>null</code> or empty. + * @param key * @param defaultValue * @return the configured value or if non found the defaultValue * */ public static String getProjectStageAwarePropertyValue(String key, String defaultValue) { - ProjectStage ps = getProjectStage(); + String value = getProjectStageAwarePropertyValue(key); + + if (value == null || value.length() == 0) + { + value = defaultValue; + } + + return value; + } + + /** + * <p>Search for the configured value in all {@link ConfigSource}s and take the + * current {@link org.apache.deltaspike.core.api.projectstage.ProjectStage} + * and the value configured for the given property into account.</p> + * + * <p>The first step is to resolve the value of the given property. This will + * take the current ProjectStage into account. E.g. given the property is 'dbvendor' + * and the ProjectStage is 'UnitTest', the first lookup is + * <ul><li>'dbvendor.UnitTest'</li></ul>. + * If this value is not found then we will do a 2nd lookup for + * <ul><li>'dbvendor'</li></ul></p> + * + * <p>If a value was found for the given property (e.g. dbvendor = 'mysql' + * then we will use this value to lookup in the following order until we + * found a non-null value. If there was no value found for the property + * we will only do the key+ProjectStage and key lookup. + * In the following sample 'dataSource' is used as key parameter: + * + * <ul> + * <li>'datasource.mysql.UnitTest'</li> + * <li>'datasource.mysql'</li> + * <li>'datasource.UnitTest'</li> + * <li>'datasource'</li> + * </ul> + * </p> + * + * + * <p><b>Attention</b> This method must only be used after all ConfigSources + * got registered and it also must not be used to determine the ProjectStage itself.</p> + * @param key + * @param property the property to look up first + * @return the configured value or if non found the defaultValue + * + */ + public static String getPropertyAwarePropertyValue(String key, String property) + { + String propertyValue = getProjectStageAwarePropertyValue(property); + + String value = null; + + if (propertyValue != null && propertyValue.length() > 0) + { + value = getProjectStageAwarePropertyValue(key + '.' + propertyValue); + } - String value = getPropertyValue(key + '.' + ps, defaultValue); if (value == null) { - value = getPropertyValue(key, defaultValue); + value = getProjectStageAwarePropertyValue(key); + } + + return value; + } + + /* + * <p><b>Attention</b> This method must only be used after all ConfigSources + * got registered and it also must not be used to determine the ProjectStage itself.</p> + * @param key + * @param property the property to look up first + * @param defaultValue + * @return the configured value or if non found the defaultValue + * + */ + public static String getPropertyAwarePropertyValue(String key, String property, String defaultValue) + { + String value = getPropertyAwarePropertyValue(key, property); + + if (value == null || value.length() == 0) + { + value = defaultValue; } return value; http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7631ac6f/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java index 91ebb22..d30e7de 100644 --- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java +++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java @@ -28,6 +28,7 @@ import java.util.List; public class ConfigResolverTest { + private static final String DEFAULT_VALUE = "defaultValue"; @Test public void testOverruledValue() { @@ -60,12 +61,43 @@ public class ConfigResolverTest Assert.assertNull(ConfigResolver.getProjectStageAwarePropertyValue("notexisting", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey", null)); + Assert.assertEquals("unittestvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey")); Assert.assertEquals("unittestvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey2", null)); + Assert.assertEquals("testvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey2")); Assert.assertEquals("testvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey2", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey3", null)); - Assert.assertEquals("", ConfigResolver.getProjectStageAwarePropertyValue("testkey3", null)); + Assert.assertEquals("", ConfigResolver.getProjectStageAwarePropertyValue("testkey3")); + Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.getProjectStageAwarePropertyValue("testkey3", DEFAULT_VALUE)); + } + + @Test + public void testGetPropertyAwarePropertyValue() { + ProjectStageProducer.setProjectStage(ProjectStage.UnitTest); + + Assert.assertNull(ConfigResolver.getPropertyAwarePropertyValue("notexisting", null)); + + Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey", null)); + Assert.assertEquals("unittestvalue", ConfigResolver.getPropertyAwarePropertyValue("testkey", "dbvendor")); + Assert.assertEquals("unittestvalue", ConfigResolver.getPropertyAwarePropertyValue("testkey", "dbvendor", null)); + + Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey2", null)); + Assert.assertEquals("testvalue", ConfigResolver.getPropertyAwarePropertyValue("testkey2", "dbvendor")); + Assert.assertEquals("testvalue", ConfigResolver.getPropertyAwarePropertyValue("testkey2", "dbvendor", null)); + + Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey3", null)); + Assert.assertEquals("", ConfigResolver.getPropertyAwarePropertyValue("testkey3", "dbvendor")); + Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.getPropertyAwarePropertyValue("testkey3", "dbvendor", DEFAULT_VALUE)); + + Assert.assertEquals("TestDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendor")); + Assert.assertEquals("PostgreDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendor2")); + Assert.assertEquals("DefaultDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendorX")); + + Assert.assertEquals("TestDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendor", null)); + Assert.assertEquals("PostgreDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendor2", null)); + Assert.assertEquals("DefaultDataSource", ConfigResolver.getPropertyAwarePropertyValue("dataSource", "dbvendorX", null)); + Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.getPropertyAwarePropertyValue("dataSourceX", "dbvendorX", DEFAULT_VALUE)); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7631ac6f/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java index e9e066f..d0f2c93 100644 --- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java +++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java @@ -48,6 +48,20 @@ public class TestConfigSource implements ConfigSource // a value which got ProjectStage overloaded to an empty value props.put("testkey3", "testvalue"); props.put("testkey3.UnitTest", ""); + + // now for the PropertyAware tests + props.put("dbvendor.UnitTest", "mysql"); + props.put("dbvendor", "postgresql"); + + props.put("dataSource.mysql.Production", "java:/comp/env/MyDs"); + props.put("dataSource.mysql.UnitTest", "TestDataSource"); + props.put("dataSource.postgresql", "PostgreDataSource"); + props.put("dataSource", "DefaultDataSource"); + + // another one + props.put("dbvendor2.Production", "mysql"); + props.put("dbvendor2", "postgresql"); + } @Override
