http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/resolver/src/main/java/org/apache/tamaya/resolver/internal/ExpressionResolutionFilter.java ---------------------------------------------------------------------- diff --git a/modules/resolver/src/main/java/org/apache/tamaya/resolver/internal/ExpressionResolutionFilter.java b/modules/resolver/src/main/java/org/apache/tamaya/resolver/internal/ExpressionResolutionFilter.java index bd90083..2f2de62 100644 --- a/modules/resolver/src/main/java/org/apache/tamaya/resolver/internal/ExpressionResolutionFilter.java +++ b/modules/resolver/src/main/java/org/apache/tamaya/resolver/internal/ExpressionResolutionFilter.java @@ -22,6 +22,7 @@ import org.apache.tamaya.ConfigException; import org.apache.tamaya.resolver.spi.ExpressionEvaluator; import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; import org.apache.tamaya.spi.ServiceContextManager; import javax.annotation.Priority; @@ -84,9 +85,13 @@ public class ExpressionResolutionFilter implements PropertyFilter { * @return the resolved value, or the input in case where no expression was detected. */ @Override - public String filterProperty(String valueToBeFiltered, FilterContext context){ + public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context){ LOG.finest("Resolving " + valueToBeFiltered + "(key=" + context.getKey() + ")"); - return evaluator().evaluateExpression(context.getKey(), valueToBeFiltered, true); + String newVal = evaluator().evaluateExpression(context.getKey(), valueToBeFiltered.getValue(), true); + if(newVal!=null){ + return valueToBeFiltered.toBuilder().setValue(newVal).build(); + } + return null; }
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/resolver/src/test/java/org/apache/tamaya/resolver/MyTestPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/resolver/src/test/java/org/apache/tamaya/resolver/MyTestPropertySource.java b/modules/resolver/src/test/java/org/apache/tamaya/resolver/MyTestPropertySource.java index 1c26f31..eee7fa4 100644 --- a/modules/resolver/src/test/java/org/apache/tamaya/resolver/MyTestPropertySource.java +++ b/modules/resolver/src/test/java/org/apache/tamaya/resolver/MyTestPropertySource.java @@ -76,6 +76,11 @@ public class MyTestPropertySource implements PropertySource{ } @Override + public int getOrdinal() { + return 0; + } + + @Override public String getName() { return "test"; } @@ -86,8 +91,12 @@ public class MyTestPropertySource implements PropertySource{ } @Override - public Map<String, String> getProperties() { - return properties; + public Map<String, PropertyValue> getProperties() { + Map<String, PropertyValue> res = new HashMap<>(); + for(Map.Entry<String,String> en:properties.entrySet()){ + res.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), "test")); + } + return res; } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java b/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java index 6a6398a..760e688 100644 --- a/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java +++ b/modules/resources/src/main/java/org/apache/tamaya/resource/AbstractPathPropertySourceProvider.java @@ -126,7 +126,7 @@ public abstract class AbstractPathPropertySourceProvider implements PropertySour /** The property source's name. */ private final String name; /** The properties. */ - private final Map<String,String> properties = new HashMap<>(); + private final Map<String,PropertyValue> properties = new HashMap<>(); /** * Constructor for a simple properties configuration. @@ -134,9 +134,10 @@ public abstract class AbstractPathPropertySourceProvider implements PropertySour * @param props the properties, not null */ public PropertiesBasedPropertySource(String name, Properties props) { - this.name = name; + this.name = Objects.requireNonNull(name); for (Entry<Object, Object> en : props.entrySet()) { - this.properties.put(en.getKey().toString(), String.valueOf(en.getValue())); + this.properties.put(en.getKey().toString(), + PropertyValue.of(en.getKey().toString(), String.valueOf(en.getValue()), name)); } } @@ -147,7 +148,10 @@ public abstract class AbstractPathPropertySourceProvider implements PropertySour */ public PropertiesBasedPropertySource(String name, Map<String,String> props) { this.name = Objects.requireNonNull(name); - this.properties.putAll(props); + for (Entry<String, String> en : props.entrySet()) { + this.properties.put(en.getKey(), + PropertyValue.of(en.getKey(), en.getValue(), name)); + } } public int getOrdinal() { @@ -179,11 +183,11 @@ public abstract class AbstractPathPropertySourceProvider implements PropertySour @Override public PropertyValue get(String key) { - return PropertyValue.of(key, getProperties().get(key), getName()); + return this.properties.get(key); } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return properties; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java b/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java index 8fe1b2b..0dc6c91 100644 --- a/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java +++ b/modules/resources/src/test/java/org/apache/tamaya/resource/AbstractPathPropertySourceProviderTest.java @@ -20,7 +20,6 @@ package org.apache.tamaya.resource; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueBuilder; import org.junit.Test; import java.net.URL; @@ -121,7 +120,7 @@ public class AbstractPathPropertySourceProviderTest { } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return Collections.emptyMap(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java index 1299a0c..7e2f622 100644 --- a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java +++ b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java @@ -21,16 +21,10 @@ package org.apache.tamaya.resource.internal; import org.apache.tamaya.resource.AbstractPathPropertySourceProvider; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueBuilder; import java.io.InputStream; import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; /** * Created by Anatole on 03.03.2015. @@ -60,27 +54,35 @@ public class PathBasedPropertySourceProvider extends AbstractPathPropertySourceP private final static class PropertiesBasedPropertySource implements PropertySource{ private final String name; - private final Map<String,String> properties = new HashMap<>(); + private final Map<String,PropertyValue> properties = new HashMap<>(); public PropertiesBasedPropertySource(String name, Properties props) { - this.name = name; + this.name = Objects.requireNonNull(name); for (Map.Entry en : props.entrySet()) { - this.properties.put(en.getKey().toString(), String.valueOf(en.getValue())); + this.properties.put(en.getKey().toString(), + PropertyValue.of(en.getKey().toString(), + String.valueOf(en.getValue()), + name)); } } @Override + public int getOrdinal() { + return 0; + } + + @Override public String getName() { return name; } @Override public PropertyValue get(String key) { - return PropertyValue.of(key,properties.get(key), getName()); + return properties.get(key); } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return properties; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java index 9ae130a..685bffa 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java @@ -135,19 +135,12 @@ public abstract class BasePropertySource implements PropertySource{ @Override public PropertyValue get(String key) { - Map<String,String> properties = getProperties(); - String val = properties.get(key); + Map<String,PropertyValue> properties = getProperties(); + PropertyValue val = properties.get(key); if(val==null){ return null; } - PropertyValueBuilder b = new PropertyValueBuilder(key, val, getName()); - String metaKeyStart = "_" + key + "."; - for(Map.Entry<String,String> en:properties.entrySet()) { - if(en.getKey().startsWith(metaKeyStart) && en.getValue()!=null){ - b.addContextData(en.getKey().substring(metaKeyStart.length()), en.getValue()); - } - } - return b.build(); + return val; } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java index e8a6077..20a62bb 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java @@ -18,6 +18,8 @@ */ package org.apache.tamaya.spisupport; +import org.apache.tamaya.spi.PropertyValue; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -33,7 +35,7 @@ public class CLIPropertySource extends BasePropertySource{ private static String[] args = new String[0]; /** The map of parsed main arguments. */ - private static Map<String,String> mainArgs; + private static Map<String,PropertyValue> mainArgs; /** Initializes the initial state. */ static{ @@ -117,11 +119,16 @@ public class CLIPropertySource extends BasePropertySource{ } } } - CLIPropertySource.mainArgs = Collections.unmodifiableMap(result); + Map<String,PropertyValue> finalProps = new HashMap<>(); + for(Map.Entry<String,String> en:result.entrySet()) { + finalProps.put(en.getKey(), + PropertyValue.of(en.getKey(), en.getValue(), "main-args")); + } + CLIPropertySource.mainArgs = Collections.unmodifiableMap(finalProps); } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return Collections.unmodifiableMap(mainArgs); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java index 8547a5a..a0a621a 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/DefaultConfiguration.java @@ -29,6 +29,7 @@ import org.apache.tamaya.spi.PropertyConverter; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import org.apache.tamaya.spi.ServiceContextManager; import java.util.ArrayList; import java.util.Collections; @@ -55,6 +56,25 @@ public class DefaultConfiguration implements Configuration { */ private final ConfigurationContext configurationContext; + /** + * EvaluationStrategy + */ + private ConfigValueEvaluator configEvaluator = loadConfigValueEvaluator(); + + private ConfigValueEvaluator loadConfigValueEvaluator() { + ConfigValueEvaluator eval = null; + try{ + eval = ServiceContextManager.getServiceContext() + .getService(ConfigValueEvaluator.class); + }catch(Exception e){ + LOG.log(Level.WARNING, "Failed to load ConfigValueEvaluator from ServiceContext, using default.", e); + } + if(eval==null){ + eval = new DefaultConfigValueEvaluator(); + } + return eval; + } + /** * Constructor. @@ -71,11 +91,15 @@ public class DefaultConfiguration implements Configuration { */ @Override public String get(String key) { - PropertyValue configData = evaluteRawValue(key); - if(configData==null){ + PropertyValue value = configEvaluator.evaluteRawValue(key, configurationContext); + if(value==null || value.getValue()==null){ return null; } - return PropertyFilterManager.applyFilter(key, configData.getConfigEntries(), configurationContext); + value = PropertyFilterManager.applyFilter(key, value, configurationContext); + if(value!=null){ + return value.getValue(); + } + return null; } /** @@ -85,16 +109,13 @@ public class DefaultConfiguration implements Configuration { */ protected PropertyValue evaluteRawValue(String key) { List<PropertySource> propertySources = configurationContext.getPropertySources(); - Map<String,String> unfilteredValue = null; + PropertyValue filteredValue = null; PropertyValueCombinationPolicy combinationPolicy = this.configurationContext .getPropertyValueCombinationPolicy(); for (PropertySource propertySource : propertySources) { - unfilteredValue = combinationPolicy.collect(unfilteredValue, key, propertySource); + filteredValue = combinationPolicy.collect(filteredValue, key, propertySource); } - if(unfilteredValue==null){ - return null; - } - return PropertyValue.of(key, unfilteredValue.get(key), unfilteredValue.get("_"+key+".source")); + return filteredValue; } @@ -124,32 +145,21 @@ public class DefaultConfiguration implements Configuration { */ @Override public Map<String, String> getProperties() { - return PropertyFilterManager.applyFilters(evaluateUnfilteredMap(), configurationContext); - } - - /** - * Evaluate all properties, but do not apply filtering on the output. - * @return the unfiltered key, value map. - */ - protected Map<String, String> evaluateUnfilteredMap() { - List<PropertySource> propertySources = new ArrayList<>(configurationContext.getPropertySources()); - Collections.reverse(propertySources); - Map<String, String> result = new HashMap<>(); - for (PropertySource propertySource : propertySources) { - try { - int origSize = result.size(); - Map<String, String> otherMap = propertySource.getProperties(); - LOG.log(Level.FINEST, null, "Overriding with properties from " + propertySource.getName()); - result.putAll(otherMap); - LOG.log(Level.FINEST, null, "Handled properties from " + propertySource.getName() + "(new: " + - (result.size() - origSize) + ", overrides: " + origSize + ", total: " + result.size()); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Error adding properties from PropertySource: " + propertySource + ", ignoring PropertySource.", e); + Map<String, PropertyValue> filtered = PropertyFiltering.applyFilters( + configEvaluator.evaluateRawValues(configurationContext), + configurationContext); + Map<String,String> result = new HashMap<>(); + for(PropertyValue val:filtered.values()){ + if(val.getValue()!=null) { + result.put(val.getKey(), val.getValue()); + // TODO: Discuss metadata handling... + result.putAll(val.getMetaEntries()); } } return result; } + /** * Accesses the current String value for the given key and tries to convert it * using the {@link PropertyConverter} instances provided by the current @@ -185,7 +195,8 @@ public class DefaultConfiguration implements Configuration { protected <T> T convertValue(String key, String value, TypeLiteral<T> type) { if (value != null) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(this, configurationContext, key, type).build(); + ConversionContext context = new ConversionContext.Builder(this, this.configurationContext, key, type) + .build(); for (PropertyConverter<T> converter : converters) { try { T t = converter.convert(value, context); @@ -196,8 +207,13 @@ public class DefaultConfiguration implements Configuration { LOG.log(Level.FINEST, "PropertyConverter: " + converter + " failed to convert value: " + value, e); } } + // if the target type is a String, we can return the value, no conversion required. + if(type.equals(TypeLiteral.of(String.class))){ + return (T)value; + } + // unsupported type, throw an exception throw new ConfigException("Unparseable config value for type: " + type.getRawType().getName() + ": " + key + - ", supported formats: "+context.getSupportedFormats()); + ", supported formats: " + context.getSupportedFormats()); } return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java index 1dd4ce7..7cbb713 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java @@ -147,22 +147,21 @@ public class EnvironmentPropertySource extends BasePropertySource { } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { if(disabled){ return Collections.emptyMap(); } String prefix = this.prefix; if(prefix==null) { - Map<String, String> entries = new HashMap<>(System.getenv()); + Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { - entries.put("_" + entry.getKey() + ".source", getName()); + entries.put(entry.getKey(), PropertyValue.of(entry.getKey(), entry.getValue(), getName())); } return entries; }else{ - Map<String, String> entries = new HashMap<>(); + Map<String, PropertyValue> entries = new HashMap<>(System.getenv().size()); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { - entries.put(prefix + entry.getKey(), entry.getValue()); - entries.put("_" + prefix + entry.getKey() + ".source", getName()); + entries.put(prefix + entry.getKey(), PropertyValue.of(prefix + entry.getKey(), entry.getValue(), getName())); } return entries; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java index de2beef..437cf64 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java @@ -16,6 +16,8 @@ */ package org.apache.tamaya.spisupport; +import org.apache.tamaya.spi.PropertyValue; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -30,7 +32,7 @@ public class MapPropertySource extends BasePropertySource { /** * The current properties. */ - private final Map<String, String> props = new HashMap<>(); + private final Map<String, PropertyValue> props = new HashMap<>(); /** * Creates a new instance, hereby using the default mechanism for evaluating the property source's @@ -54,10 +56,14 @@ public class MapPropertySource extends BasePropertySource { public MapPropertySource(String name, Map<String, String> props, String prefix) { super(name); if (prefix == null) { - this.props.putAll(props); + for (Map.Entry<String, String> en : props.entrySet()) { + this.props.put(en.getKey(), + PropertyValue.of(en.getKey(), en.getValue(), name)); + } } else { for (Map.Entry<String, String> en : props.entrySet()) { - this.props.put(prefix + en.getKey(), en.getValue()); + this.props.put(prefix + en.getKey(), + PropertyValue.of(prefix + en.getKey(), en.getValue(), name)); } } } @@ -89,7 +95,7 @@ public class MapPropertySource extends BasePropertySource { @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return Collections.unmodifiableMap(this.props); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFilterManager.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFilterManager.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFilterManager.java index 611722a..cdef84d 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFilterManager.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFilterManager.java @@ -21,6 +21,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.ConfigurationContext; import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; import java.util.HashMap; import java.util.Map; @@ -48,14 +49,13 @@ public final class PropertyFilterManager { */ private PropertyFilterManager(){} - public static String applyFilter(String key, Map<String,String> configData, ConfigurationContext configurationContext) { + public static PropertyValue applyFilter(String key, PropertyValue unfilteredValue, ConfigurationContext configurationContext) { // Apply filters to values, prevent values filtered to null! - String unfilteredValue = configData.get(key); for (int i = 0; i < MAX_FILTER_LOOPS; i++) { boolean changed = false; // Apply filters to values, prevent values filtered to null! for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - String newValue = filter.filterProperty(unfilteredValue, new FilterContext(key, configData, true)); + PropertyValue newValue = filter.filterProperty(unfilteredValue, new FilterContext(key, unfilteredValue)); if (newValue != null && !newValue.equals(unfilteredValue)) { changed = true; if (LOG.isLoggable(Level.FINEST)) { @@ -85,17 +85,17 @@ public final class PropertyFilterManager { return unfilteredValue; } - public static Map<String, String> applyFilters(Map<String, String> inputMap, ConfigurationContext configurationContext) { - Map<String, String> resultMap = new HashMap<>(inputMap); + public static Map<String, PropertyValue> applyFilters(Map<String, PropertyValue> inputMap, ConfigurationContext configurationContext) { + Map<String, PropertyValue> resultMap = new HashMap<>(inputMap); // Apply filters to values, prevent values filtered to null! for (int i = 0; i < MAX_FILTER_LOOPS; i++) { AtomicInteger changes = new AtomicInteger(); for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - for (Map.Entry<String, String> entry : inputMap.entrySet()) { + for (Map.Entry<String, PropertyValue> entry : inputMap.entrySet()) { final String k = entry.getKey(); - final String v = entry.getValue(); + final PropertyValue v = entry.getValue(); - String newValue = filter.filterProperty(v, new FilterContext(k, inputMap, false)); + PropertyValue newValue = filter.filterProperty(v, new FilterContext(k, inputMap)); if (newValue != null && !newValue.equals(v)) { changes.incrementAndGet(); LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + " by " + filter); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java index eef758b..f614471 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertyFiltering.java @@ -21,6 +21,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.ConfigurationContext; import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; import java.util.HashMap; import java.util.Map; @@ -48,14 +49,14 @@ public final class PropertyFiltering{ */ private PropertyFiltering(){} - public static String applyFilter(String key, Map<String,String> configData, ConfigurationContext configurationContext) { + public static PropertyValue applyFilter(String key, Map<String,PropertyValue> configData, ConfigurationContext configurationContext) { // Apply filters to values, prevent values filtered to null! - String unfilteredValue = configData.get(key); + PropertyValue unfilteredValue = configData.get(key); for (int i = 0; i < MAX_FILTER_LOOPS; i++) { boolean changed = false; // Apply filters to values, prevent values filtered to null! for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - String newValue = filter.filterProperty(unfilteredValue, new FilterContext(key, configData, true)); + PropertyValue newValue = filter.filterProperty(unfilteredValue, new FilterContext(key, configData)); if (newValue != null && !newValue.equals(unfilteredValue)) { changed = true; if (LOG.isLoggable(Level.FINEST)) { @@ -85,17 +86,17 @@ public final class PropertyFiltering{ return unfilteredValue; } - public static Map<String, String> applyFilters(Map<String, String> inputMap, ConfigurationContext configurationContext) { - Map<String, String> resultMap = new HashMap<>(inputMap); + public static Map<String, PropertyValue> applyFilters(Map<String, PropertyValue> inputMap, ConfigurationContext configurationContext) { + Map<String, PropertyValue> resultMap = new HashMap<>(inputMap); // Apply filters to values, prevent values filtered to null! for (int i = 0; i < MAX_FILTER_LOOPS; i++) { AtomicInteger changes = new AtomicInteger(); for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - for (Map.Entry<String, String> entry : inputMap.entrySet()) { + for (Map.Entry<String, PropertyValue> entry : inputMap.entrySet()) { final String k = entry.getKey(); - final String v = entry.getValue(); + final PropertyValue v = entry.getValue(); - String newValue = filter.filterProperty(v, new FilterContext(k, inputMap, false)); + PropertyValue newValue = filter.filterProperty(v, new FilterContext(k, inputMap)); if (newValue != null && !newValue.equals(v)) { changes.incrementAndGet(); LOG.finest("Filter - " + k + ": " + v + " -> " + newValue + " by " + filter); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java index 96fffad..98290b6 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/PropertySourceComparator.java @@ -68,32 +68,32 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser } public static int getOrdinal(PropertySource propertySource) { - PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL); - if(ordinalValue!=null){ - try{ - return Integer.parseInt(ordinalValue.getValue().trim()); - }catch(Exception e){ - LOG.finest("Failed to parse ordinal from " + PropertySource.TAMAYA_ORDINAL + - " in " + propertySource.getName()+": "+ordinalValue.getValue()); - } - } - try { - Method method = propertySource.getClass().getMethod("getOrdinal"); - if(int.class.equals(method.getReturnType())){ - try { - return (int)method.invoke(propertySource); - } catch (Exception e) { - LOG.log(Level.FINEST, "Error calling int getOrdinal() on " + propertySource.getName(), e); - } - } - } catch (NoSuchMethodException e) { - LOG.finest("No int getOrdinal() method found in " + propertySource.getName()); - } - Priority prio = propertySource.getClass().getAnnotation(Priority.class); - if(prio!=null){ - return prio.value(); - } - return 0; +// PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL); +// if(ordinalValue!=null){ +// try{ +// return Integer.parseInt(ordinalValue.getValue().trim()); +// }catch(Exception e){ +// LOG.finest("Failed to parse ordinal from " + PropertySource.TAMAYA_ORDINAL + +// " in " + propertySource.getName()+": "+ordinalValue.getValue()); +// } +// } +// try { +// Method method = propertySource.getClass().getMethod("getOrdinal"); +// if(int.class.equals(method.getReturnType())){ +// try { +// return (int)method.invoke(propertySource); +// } catch (Exception e) { +// LOG.log(Level.FINEST, "Error calling int getOrdinal() on " + propertySource.getName(), e); +// } +// } +// } catch (NoSuchMethodException e) { +// LOG.finest("No int getOrdinal() method found in " + propertySource.getName()); +// } +// Priority prio = propertySource.getClass().getAnnotation(Priority.class); +// if(prio!=null){ +// return prio.value(); +// } + return propertySource.getOrdinal(); } @Override public int compare(PropertySource source1, PropertySource source2) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java index e186015..cb08193 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/RegexPropertyFilter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.FilterContext; import org.apache.tamaya.spi.PropertyFilter; +import org.apache.tamaya.spi.PropertyValue; import java.util.Arrays; import java.util.List; @@ -53,7 +54,7 @@ public final class RegexPropertyFilter implements PropertyFilter{ } @Override - public String filterProperty(String valueToBeFiltered, FilterContext context) { + public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { if(includes!=null){ for(String expression:includes){ if(context.getKey().matches(expression)){ http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java index 37e3a7a..f1a5a57 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.PropertyValue; import java.io.File; import java.io.IOException; @@ -43,7 +44,7 @@ public class SimplePropertySource extends BasePropertySource { /** * The current properties. */ - private Map<String, String> properties; + private Map<String, PropertyValue> properties = new HashMap<>(); /** * Creates a new Properties based PropertySource based on the given URL. @@ -78,7 +79,9 @@ public class SimplePropertySource extends BasePropertySource { */ public SimplePropertySource(String name, Map<String, String> properties, int defaultOrdinal){ super(name, defaultOrdinal); - this.properties = new HashMap<>(properties); + for(Map.Entry<String,String> en: properties.entrySet()) { + this.properties.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), name)); + } } /** @@ -88,8 +91,7 @@ public class SimplePropertySource extends BasePropertySource { * @param properties the properties, not null. */ public SimplePropertySource(String name, Map<String, String> properties) { - super(name, 0); - this.properties = new HashMap<>(properties); + this(name, properties, 0); } /** @@ -119,7 +121,7 @@ public class SimplePropertySource extends BasePropertySource { } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return this.properties; } @@ -130,10 +132,10 @@ public class SimplePropertySource extends BasePropertySource { * @return loaded {@link Properties} * @throws IllegalStateException in case of an error while reading properties-file */ - private static Map<String, String> load(URL propertiesFile) { + private static Map<String, PropertyValue> load(URL propertiesFile) { boolean isXML = isXMLPropertieFiles(propertiesFile); - Map<String, String> properties = new HashMap<>(); + Map<String, PropertyValue> properties = new HashMap<>(); try (InputStream stream = propertiesFile.openStream()) { Properties props = new Properties(); if (stream != null) { @@ -145,8 +147,7 @@ public class SimplePropertySource extends BasePropertySource { } String source = propertiesFile.toString(); for (String key : props.stringPropertyNames()) { - properties.put(key, props.getProperty(key)); - properties.put("_" + key + ".source", source); + properties.put(key, PropertyValue.of(key, props.getProperty(key), source)); } } catch (IOException e) { throw new ConfigException("Error loading properties from " + propertiesFile, e); @@ -167,7 +168,7 @@ public class SimplePropertySource extends BasePropertySource { private String name; private Integer defaultOrdinal; private Integer ordinal; - private Map<String, String> properties = new HashMap<>(); + private Map<String, PropertyValue> properties = new HashMap<>(); private Builder() { } @@ -254,7 +255,9 @@ public class SimplePropertySource extends BasePropertySource { * @return a reference to this Builder */ public Builder withProperties(Map<String, String> val) { - this.properties.putAll(val); + for(Map.Entry<String,String> en: val.entrySet()) { + this.properties.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), name)); + } return this; } @@ -265,18 +268,7 @@ public class SimplePropertySource extends BasePropertySource { * @return a reference to this Builder */ public Builder withProperty(String key, String val) { - this.properties.put(key, val); - return this; - } - - /** - * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. - * - * @param val the {@code properties} to set - * @return a reference to this Builder - */ - public Builder withSource(String val) { - this.properties.put("_source", val); + this.properties.put(key, PropertyValue.of(key, val, name)); return this; } @@ -286,7 +278,6 @@ public class SimplePropertySource extends BasePropertySource { * @return a {@code SimplePropertySource} built with parameters of this {@code SimplePropertySource.Builder} */ public SimplePropertySource build() { - this.properties.put("_builtAt", String.valueOf(System.currentTimeMillis())); return new SimplePropertySource(this); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java index 25863d6..61aa449 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java @@ -36,7 +36,7 @@ public class SystemPropertySource extends BasePropertySource { */ public static final int DEFAULT_ORDINAL = 1000; - private volatile Map<String,String> cachedProperties; + private volatile Map<String,PropertyValue> cachedProperties; /** * previous System.getProperties().hashCode() @@ -127,18 +127,22 @@ public class SystemPropertySource extends BasePropertySource { } - private Map<String, String> loadProperties() { + private Map<String, PropertyValue> loadProperties() { Properties sysProps = System.getProperties(); previousHash = System.getProperties().hashCode(); final String prefix = this.prefix; - Map<String, String> entries = new HashMap<>(); + Map<String, PropertyValue> entries = new HashMap<>(); for (Map.Entry<Object,Object> entry : sysProps.entrySet()) { if(prefix==null) { - entries.put("_" + entry.getKey() + ".source", getName()); - entries.put((String) entry.getKey(), (String) entry.getValue()); + entries.put((String) entry.getKey(), + PropertyValue.of((String) entry.getKey(), + (String) entry.getValue(), + getName())); }else { - entries.put(prefix + entry.getKey(), (String)entry.getValue()); - entries.put("_" + prefix + entry.getKey() + ".source", getName()); + entries.put(prefix + entry.getKey(), + PropertyValue.of(prefix + entry.getKey(), + (String) entry.getValue(), + getName())); } } return entries; @@ -165,7 +169,7 @@ public class SystemPropertySource extends BasePropertySource { } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { if(disabled){ return Collections.emptyMap(); } @@ -173,7 +177,7 @@ public class SystemPropertySource extends BasePropertySource { // synchronization was removed, Instance was marked as volatile. In the worst case it // is reloaded twice, but the values will be the same. if (previousHash != System.getProperties().hashCode()) { - Map<String, String> properties = loadProperties(); + Map<String, PropertyValue> properties = loadProperties(); this.cachedProperties = Collections.unmodifiableMap(properties); } return this.cachedProperties; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java index 2cdac70..a986b7b 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java @@ -21,13 +21,10 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.PropertyValueBuilder; import org.junit.Assert; import org.junit.Test; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public class BasePropertySourceTest { @@ -42,7 +39,7 @@ public class BasePropertySourceTest { } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return Collections.emptyMap(); } }; @@ -56,7 +53,7 @@ public class BasePropertySourceTest { @Test public void testGet() { - Assert.assertEquals("1000", new OverriddenOrdinalPropertySource().get(PropertySource.TAMAYA_ORDINAL).get(PropertySource.TAMAYA_ORDINAL)); + Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal()); } private static class OverriddenOrdinalPropertySource extends BasePropertySource { @@ -66,10 +63,10 @@ public class BasePropertySourceTest { } @Override - public Map<String, String> getProperties() { - Map<String, String> map = new HashMap<>(1); - map.put(PropertySource.TAMAYA_ORDINAL, "1000"); - return map; + public Map<String, PropertyValue> getProperties() { + Map<String,PropertyValue> props = new HashMap<>(1); + props.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "1000", getName())); + return props; } } @@ -80,10 +77,10 @@ public class BasePropertySourceTest { } @Override - public Map<String, String> getProperties() { - Map<String, String> map = new HashMap<>(1); - map.put(PropertySource.TAMAYA_ORDINAL, "invalid"); - return map; + public Map<String, PropertyValue> getProperties() { + Map<String,PropertyValue> props = new HashMap<>(1); + props.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "invalid", getName())); + return props; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/CLIPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/CLIPropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/CLIPropertySourceTest.java index 2c2baa4..e08bf80 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/CLIPropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/CLIPropertySourceTest.java @@ -35,25 +35,25 @@ public class CLIPropertySourceTest { assertTrue(ps.getProperties().isEmpty()); CLIPropertySource.initMainArgs("-a", "b"); assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("a"), "b"); + assertEquals(ps.getProperties().get("a").getValue(), "b"); CLIPropertySource.initMainArgs("--c"); assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("c"), "c"); + assertEquals(ps.getProperties().get("c").getValue(), "c"); CLIPropertySource.initMainArgs("sss"); assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("sss"), "sss"); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); CLIPropertySource.initMainArgs("-a", "b", "--c", "sss", "--val=vvv"); assertFalse(ps.getProperties().isEmpty()); - assertEquals(ps.getProperties().get("a"), "b"); - assertEquals(ps.getProperties().get("c"), "c"); - assertEquals(ps.getProperties().get("sss"), "sss"); + assertEquals(ps.getProperties().get("a").getValue(), "b"); + assertEquals(ps.getProperties().get("c").getValue(), "c"); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); // getProperties() throws Exception { System.setProperty("main.args", "-a b\t--c sss "); ps = new CLIPropertySource(); assertFalse(ps.getProperties().isEmpty()); System.clearProperty("main.args"); - assertEquals(ps.getProperties().get("a"), "b"); - assertEquals(ps.getProperties().get("c"), "c"); - assertEquals(ps.getProperties().get("sss"), "sss"); + assertEquals(ps.getProperties().get("a").getValue(), "b"); + assertEquals(ps.getProperties().get("c").getValue(), "c"); + assertEquals(ps.getProperties().get("sss").getValue(), "sss"); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java index f1f34c6..5e748ce 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/DefaultConfigurationContextTest.java @@ -21,13 +21,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.FilterContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; +import org.apache.tamaya.spi.*; import org.junit.Test; import static org.junit.Assert.*; @@ -151,7 +145,7 @@ public class DefaultConfigurationContextTest { PropertyFilter testFilter = new PropertyFilter() { @Override - public String filterProperty(String value, FilterContext context) { + public PropertyValue filterProperty(PropertyValue value, FilterContext context) { return value; } }; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/EnvironmentPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/EnvironmentPropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/EnvironmentPropertySourceTest.java index a7f0497..877d30d 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/EnvironmentPropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/EnvironmentPropertySourceTest.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.core.propertysource.EnvironmentPropertySource; +import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; import java.util.Map; @@ -46,16 +47,16 @@ public class EnvironmentPropertySourceTest { @Test public void testGet() throws Exception { for (Map.Entry<String, String> envEntry : System.getenv().entrySet()) { - assertEquals(envPropertySource.get(envEntry.getKey()).get(envEntry.getKey()), envEntry.getValue()); + assertEquals(envPropertySource.get(envEntry.getKey()).getValue(), envEntry.getValue()); } } @Test public void testGetProperties() throws Exception { - Map<String, String> props = envPropertySource.getProperties(); - for(Map.Entry<String,String> en: props.entrySet()){ + Map<String, PropertyValue> props = envPropertySource.getProperties(); + for(Map.Entry<String,PropertyValue> en: props.entrySet()){ if(!en.getKey().startsWith("_")){ - assertEquals(System.getenv(en.getKey()), en.getValue()); + assertEquals(System.getenv(en.getKey()), en.getValue().getValue()); } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertiesFilePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertiesFilePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertiesFilePropertySourceTest.java index 889e905..984be08 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertiesFilePropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/PropertiesFilePropertySourceTest.java @@ -35,15 +35,15 @@ public class PropertiesFilePropertySourceTest { public void testGetOrdinal() { Assert.assertEquals(0, testfilePropertySource.getOrdinal()); Assert.assertEquals(Integer.parseInt(overrideOrdinalPropertySource.get(PropertySource.TAMAYA_ORDINAL) - .get(PropertySource.TAMAYA_ORDINAL)), + .getValue()), overrideOrdinalPropertySource.getOrdinal()); } @Test public void testGet() { - Assert.assertEquals("val3", testfilePropertySource.get("key3").get("key3")); - Assert.assertEquals("myval5", overrideOrdinalPropertySource.get("mykey5").get("mykey5")); + Assert.assertEquals("val3", testfilePropertySource.get("key3").getValue()); + Assert.assertEquals("myval5", overrideOrdinalPropertySource.get("mykey5").getValue()); Assert.assertNull(testfilePropertySource.get("nonpresentkey")); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java index 79f2fb9..44364b8 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/RegexPropertyFilterTest.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyValue; import java.util.HashMap; import java.util.Map; @@ -34,18 +35,23 @@ public class RegexPropertyFilterTest { public void testFilterProperty() throws Exception { RegexPropertyFilter filter = new RegexPropertyFilter(); filter.setIncludes("test1.*"); - Map<String,String> map = new HashMap<>(); - map.put("test1", "test1"); - map.put("test2", "test2"); - map.put("test1.test3", "test.test3"); - assertEquals(filter.filterProperty("test1.", new FilterContext("test1.", map, true)), "test1."); - assertNull(filter.filterProperty("test2", new FilterContext("test2.", map, true))); - assertEquals(filter.filterProperty("test1.test3", new FilterContext("test1.test3", map, true)), "test1.test3"); + Map<String,PropertyValue> map = new HashMap<>(); + map.put("test1", PropertyValue.of("test1", "test1", "test")); + map.put("test2", PropertyValue.of("test2", "test2", "test")); + map.put("test1.test3", PropertyValue.of("test1.test3", "test.test3", "test")); + assertEquals(filter.filterProperty(PropertyValue.of("test1.", "test1", "test"), new FilterContext("test1.", map)).getValue(), "test1"); + assertNull(filter.filterProperty(PropertyValue.of("test2", "test2", "test"), new FilterContext("test2.", map))); + assertEquals(filter.filterProperty( + PropertyValue.of("test1.test3", "testx.test3", "test"), + new FilterContext("test1.test3", map)).getValue(), "testx.test3"); + assertEquals(filter.filterProperty( + PropertyValue.of("test1.test3", "testx.test3", "test"), + new FilterContext("test1.test3", map)).getValue(), "testx.test3"); filter = new RegexPropertyFilter(); filter.setIncludes("test1.*"); - assertNotNull(filter.filterProperty("test1", new FilterContext("test1", map, true))); - assertNull(filter.filterProperty("test2", new FilterContext("test2", map, true))); - assertNull(filter.filterProperty("test.test3", new FilterContext("test.test3", map, true))); + assertNotNull(filter.filterProperty(PropertyValue.of("test1", "test1", "test"), new FilterContext("test1", map))); + assertNull(filter.filterProperty(PropertyValue.of("test2", "test2", "test"), new FilterContext("test2", map))); + assertNull(filter.filterProperty(PropertyValue.of("test.test3", "test1", "test"), new FilterContext("test.test3", map))); } @org.junit.Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java index a7409a4..7ef56c7 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.spi.PropertyValue; import org.junit.Test; import java.net.URL; @@ -36,9 +37,9 @@ public class SimplePropertySourceTest { SimplePropertySource source = new SimplePropertySource(resource); assertThat(source, notNullValue()); - assertThat(source.getProperties(), aMapWithSize(4)); - assertThat(source.getProperties(), hasEntry("a", "b")); - assertThat(source.getProperties(), hasEntry("b", "1")); + assertThat(source.getProperties(), aMapWithSize(2)); + assertThat(source.getProperties(), hasEntry("a", PropertyValue.of("a","b", source.getName()))); + assertThat(source.getProperties(), hasEntry("b", PropertyValue.of("b", "1", source.getName()))); } @@ -80,6 +81,6 @@ public class SimplePropertySourceTest { SimplePropertySource source = new SimplePropertySource(resource); assertThat(source, notNullValue()); - assertThat(source.getProperties(), aMapWithSize(10)); // 5 * 2 meta entries. + assertThat(source.getProperties(), aMapWithSize(5)); // 5 * 2 meta entries. } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SystemPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SystemPropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SystemPropertySourceTest.java index 54e970c..2e1625a 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SystemPropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SystemPropertySourceTest.java @@ -39,8 +39,8 @@ public class SystemPropertySourceTest { Assert.assertEquals(SystemPropertySource.DEFAULT_ORDINAL, testPropertySource.getOrdinal()); // set the ordinal to 1000 - System.setProperty(PropertySource.TAMAYA_ORDINAL, "1000"); - Assert.assertEquals(1000, new SystemPropertySource().getOrdinal()); + System.setProperty(PropertySource.TAMAYA_ORDINAL, "1001"); + Assert.assertEquals(1001, new SystemPropertySource().getOrdinal()); // currently its not possible to change ordinal at runtime // reset it to not destroy other tests!! @@ -73,30 +73,24 @@ public class SystemPropertySourceTest { // cleanup System.clearProperty("test"); - - // no modifaction - try { - testPropertySource.getProperties().put("add.new.keys", "must throw exception"); - Assert.fail(UnsupportedOperationException.class.getName() + " expected"); - } - catch (UnsupportedOperationException e) { - // expected -> all is fine - } } - private void checkWithSystemProperties(Map<String, String> toCheck) { + private void checkWithSystemProperties(Map<String,PropertyValue> toCheck) { Properties systemEntries = System.getProperties(); - Assert.assertEquals("size of System.getProperties().entrySet() must be the same as SystemPropertySrouce.getProperties().entrySet()", - systemEntries.size(), toCheck.size()/2); + int num = 0; - for (Map.Entry<String, String> propertySourceEntry : toCheck.entrySet()) { + for (PropertyValue propertySourceEntry : toCheck.values()) { if(propertySourceEntry.getKey().startsWith("_")){ continue; // meta entry } + num++; Assert.assertEquals("Entry values for key '" + propertySourceEntry.getKey() + "' do not match", systemEntries.getProperty(propertySourceEntry.getKey()), propertySourceEntry.getValue()); } + Assert.assertEquals("size of System.getProperties().entrySet() must be the same as SystemPropertySrouce.getProperties().entrySet()", + systemEntries.size(), num); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/9f915b25/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/TestPropertyDefaultSource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/TestPropertyDefaultSource.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/TestPropertyDefaultSource.java index f55d8e4..33b2462 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/TestPropertyDefaultSource.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/TestPropertyDefaultSource.java @@ -19,6 +19,7 @@ package org.apache.tamaya.spisupport; import org.apache.tamaya.core.propertysource.BasePropertySource; +import org.apache.tamaya.spi.PropertyValue; import java.util.Collections; import java.util.HashMap; @@ -29,12 +30,12 @@ import java.util.Map; */ public class TestPropertyDefaultSource extends BasePropertySource{ - private Map<String,String> properties = new HashMap<>(); + private Map<String,PropertyValue> properties = new HashMap<>(); public TestPropertyDefaultSource() { super(100); - properties.put("name","Anatole"); - properties.put("name2","Sabine"); + properties.put("name",PropertyValue.of("name", "Anatole", "Test")); + properties.put("name2",PropertyValue.of("name2", "Sabine", "Test")); properties = Collections.unmodifiableMap(properties); } @@ -44,7 +45,7 @@ public class TestPropertyDefaultSource extends BasePropertySource{ } @Override - public Map<String, String> getProperties() { + public Map<String, PropertyValue> getProperties() { return properties; }