http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java new file mode 100644 index 0000000..6fb31c6 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/ConvertQuery.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.ConfigQuery; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Query to convert a String value. + * @param <T> the target type. + */ +final class ConvertQuery<T> implements ConfigQuery<T> { + + private static final Logger LOG = Logger.getLogger(ConvertQuery.class.getName()); + + private String rawValue; + private TypeLiteral<T> type; + + public ConvertQuery(String rawValue, TypeLiteral<T> type) { + this.rawValue = Objects.requireNonNull(rawValue); + this.type = Objects.requireNonNull(type); + } + + @Override + public T query(Configuration config) { + List<PropertyConverter<T>> converters = config.getContext().getPropertyConverters(type); + ConversionContext context = new ConversionContext.Builder(type).setConfigurationContext(config.getContext()) + .setConfiguration(config).setKey(ConvertQuery.class.getName()).build(); + for(PropertyConverter<?> conv: converters) { + try{ + if(conv instanceof OptionalConverter){ + continue; + } + T result = (T)conv.convert(rawValue, context); + if(result!=null){ + return result; + } + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Converter "+ conv +" failed to convert to " + type); + } + } + return null; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java deleted file mode 100644 index f66e638..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal.converters; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.osgi.service.component.annotations.Component; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Locale; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Converter, converting from String to tge given enum type. - */ -@Component(service = PropertyConverter.class) -public class EnumConverter<T> implements PropertyConverter<T> { - private final Logger LOG = Logger.getLogger(EnumConverter.class.getName()); - private Class<T> enumType; - private Method factory; - - public EnumConverter(Class<T> enumType) { - if (!Enum.class.isAssignableFrom(enumType)) { - throw new IllegalArgumentException("Not an Enum: " + enumType.getName()); - } - this.enumType = Objects.requireNonNull(enumType); - try { - this.factory = enumType.getMethod("valueOf", String.class); - } catch (NoSuchMethodException e) { - throw new ConfigException("Uncovertible enum type without valueOf method found, please provide a custom " + - "PropertyConverter for: " + enumType.getName()); - } - } - - @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(getClass(),"<enumValue>"); - try { - return (T) factory.invoke(null, value); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - try { - return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH)); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - return null; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof EnumConverter)) return false; - EnumConverter<?> that = (EnumConverter<?>) o; - return Objects.equals(enumType, that.enumType); - } - - @Override - public int hashCode() { - return Objects.hash(enumType); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java index 0901c9f..e9891be 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/FileConverter.java @@ -23,7 +23,6 @@ import org.apache.tamaya.spi.PropertyConverter; import org.osgi.service.component.annotations.Component; import java.io.File; -import java.net.URL; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java index 9486640..062d584 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java @@ -22,7 +22,6 @@ import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; import org.osgi.service.component.annotations.Component; -import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.logging.Level; import java.util.logging.Logger; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java index ce8ebed..794ad8a 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java @@ -22,7 +22,6 @@ import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; import org.osgi.service.component.annotations.Component; -import java.time.OffsetDateTime; import java.time.OffsetTime; import java.util.logging.Level; import java.util.logging.Logger; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java index 8be1533..dc53c84 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java @@ -19,22 +19,14 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; -import org.apache.tamaya.ConfigQuery; -import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.core.internal.PropertyConverterManager; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; import org.osgi.service.component.annotations.Component; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.Optional; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Converter, converting from String to Boolean. @@ -42,8 +34,6 @@ import java.util.logging.Logger; @Component(service = PropertyConverter.class) public class OptionalConverter implements PropertyConverter<Optional> { - private static final Logger LOG = Logger.getLogger(OptionalConverter.class.getName()); - @Override public Optional convert(String value, ConversionContext context) { if(value==null){ @@ -72,36 +62,4 @@ public class OptionalConverter implements PropertyConverter<Optional> { return getClass().hashCode(); } - - private static final class ConvertQuery<T> implements ConfigQuery<T>{ - - private String rawValue; - private TypeLiteral<T> type; - - public ConvertQuery(String rawValue, TypeLiteral<T> type) { - this.rawValue = Objects.requireNonNull(rawValue); - this.type = Objects.requireNonNull(type); - } - - @Override - public T query(Configuration config) { - List<PropertyConverter<T>> converters = config.getContext().getPropertyConverters(type); - ConversionContext context = new ConversionContext.Builder(type).setConfigurationContext(config.getContext()) - .setConfiguration(config).setKey(ConvertQuery.class.getName()).build(); - for(PropertyConverter<?> conv: converters) { - try{ - if(conv instanceof OptionalConverter){ - continue; - } - T result = (T)conv.convert(rawValue, context); - if(result!=null){ - return result; - } - }catch(Exception e){ - LOG.log(Level.FINEST, e, () -> "Converter "+ conv +" failed to convert to " + type); - } - } - return null; - } - } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java index e59c0b9..404daee 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java @@ -22,11 +22,8 @@ import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; import org.osgi.service.component.annotations.Component; -import java.io.File; -import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java new file mode 100644 index 0000000..05aa3d5 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.function.Supplier; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class SupplierConverter implements PropertyConverter<Supplier> { + + private static final Logger LOG = Logger.getLogger(SupplierConverter.class.getName()); + + @Override + public Supplier convert(String value, ConversionContext context) { + return () -> { + try{ + Type targetType = context.getTargetType().getType(); + ParameterizedType pt = (ParameterizedType) targetType; + if(String.class.equals(pt.getActualTypeArguments()[0])){ + return value; + } + ConvertQuery converter = new ConvertQuery(value, TypeLiteral.of(pt.getActualTypeArguments()[0])); + Object o = context.getConfiguration().query(converter); + if(o==null){ + throw new ConfigException("No such value: " + context.getKey()); + } + return o; + }catch(Exception e){ + throw new ConfigException("Error evaluating config value.", e); + } + }; + } + + @Override + public boolean equals(Object o){ + return getClass().equals(o.getClass()); + } + + @Override + public int hashCode(){ + return getClass().hashCode(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java deleted file mode 100644 index 316dd59..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Abstract {@link org.apache.tamaya.spi.PropertySource} that allows to set a default ordinal that will be used, if no - * ordinal is provided with the config. - */ -public abstract class BasePropertySource implements PropertySource { - /** default ordinal that will be used, if no ordinal is provided with the config. */ - private int defaultOrdinal; - /** Used if the ordinal has been set explicitly. */ - private volatile Integer ordinal; - /** The name of the property source. */ - private String name; - - /** - * Constructor. - * @param name the (unique) property source name, not {@code null}. - */ - protected BasePropertySource(String name){ - this.name = Objects.requireNonNull(name); - this.defaultOrdinal = 0; - } - - /** - * Constructor. - * @param defaultOrdinal default ordinal that will be used, if no ordinal is provided with the config. - */ - protected BasePropertySource(int defaultOrdinal){ - this.name = getClass().getSimpleName(); - this.defaultOrdinal = defaultOrdinal; - } - - /** - * Constructor. - * @param name the (unique) property source name, not {@code null}. - * @param defaultOrdinal default ordinal that will be used, if no ordinal is provided with the config. - */ - protected BasePropertySource(String name, int defaultOrdinal){ - this.name = Objects.requireNonNull(name); - this.defaultOrdinal = defaultOrdinal; - } - - - /** - * Constructor, using a default ordinal of 0. - */ - protected BasePropertySource(){ - this(0); - } - - @Override - public String getName() { - return name; - } - - /** - * Sets the property source's (unique) name. - * @param name the name, not {@code null}. - */ - public void setName(String name){ - this.name = Objects.requireNonNull(name); - } - - /** - * Allows to set the ordinal of this property source explcitly. This will override any evaluated - * ordinal, or default ordinal. To reset an explcit ordinal call {@code setOrdinal(null);}. - * @param ordinal the explicit ordinal, or {@code null}. - */ - public void setOrdinal(Integer ordinal){ - this.ordinal = ordinal; - } - - /** - * Allows to set the ordinal of this property source explcitly. This will override any evaluated - * ordinal, or default ordinal. To reset an explcit ordinal call {@code setOrdinal(null);}. - * @param defaultOrdinal the default ordinal, or {@code null}. - */ - public void setDefaultOrdinal(Integer defaultOrdinal){ - this.defaultOrdinal = defaultOrdinal; - } - - public int getOrdinal() { - Integer ordinal = this.ordinal; - if(ordinal!=null){ - Logger.getLogger(getClass().getName()).finest( - "Using explicit ordinal '"+ordinal+"' for property source: " + getName()); - return ordinal; - } - PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL); - if(configuredOrdinal!=null){ - try { - return Integer.parseInt(configuredOrdinal.getValue()); - } catch (Exception e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, - "Configured ordinal is not an int number: " + configuredOrdinal, e); - } - } - return getDefaultOrdinal(); - } - - /** - * Returns the default ordinal used, when no ordinal is set, or the ordinal was not parseable to an int value. - * @return the default ordinal used, by default 0. - */ - public int getDefaultOrdinal(){ - return defaultOrdinal; - } - - @Override - public PropertyValue get(String key) { - Map<String,PropertyValue> properties = getProperties(); - PropertyValue val = properties.get(key); - if(val==null){ - return null; - } - return val; - } - - @Override - public boolean isScannable(){ - return true; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof BasePropertySource)) return false; - BasePropertySource that = (BasePropertySource) o; - return Objects.equals(getName(), that.getName()); - } - - @Override - public int hashCode() { - return Objects.hash(getName()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java deleted file mode 100644 index 276cc94..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.osgi.service.component.annotations.Component; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -/** - * PropertySource that allows to add the programs main arguments as configuration entries. Unix syntax using '--' and - * '-' params is supported. - */ -@Component(service = PropertySource.class) -public class CLIPropertySource extends BasePropertySource{ - - /** The original main arguments. */ - private static String[] args = new String[0]; - - /** The map of parsed main arguments. */ - private static Map<String,PropertyValue> mainArgs; - - /* Initializes the initial state. */ - static { - initMainArgs(args); - } - - /** - * Creates a new instance. - */ - public CLIPropertySource(){ - this(null); - } - - /** - * Creates a new instance, allows optionally to pass the main arguments. - * @param args the args, or null. - */ - public CLIPropertySource(String... args){ - if(args!=null){ - initMainArgs(args); - } - } - - /** - * Creates a new instance, allows optionally to pass the main arguments. - * @param args the args, or null. - * @param ordinal the ordinal to be applied. - */ - public CLIPropertySource(int ordinal, String... args){ - if(args!=null){ - initMainArgs(args); - } - setOrdinal(ordinal); - } - - - - /** - * Configure the main arguments, hereby parsing and mapping the main arguments into - * configuration propertiesi as key-value pairs. - * @param args the main arguments, not {@code null}. - */ - public static void initMainArgs(String... args){ - CLIPropertySource.args = Objects.requireNonNull(args); - // TODO is there a way to figure out the args? - String argsProp = System.getProperty("main.args"); - if(argsProp!=null){ - CLIPropertySource.args = argsProp.split("\\s"); - } - Map<String,PropertyValue> result; - if(CLIPropertySource.args==null){ - result = Collections.emptyMap(); - }else{ - result = new HashMap<>(); - String prefix = System.getProperty("main.args.prefix"); - if(prefix==null){ - prefix=""; - } - String key = null; - for(String arg:CLIPropertySource.args){ - if(arg.startsWith("--")){ - arg = arg.substring(2); - int index = arg.indexOf("="); - if(index>0){ - key = arg.substring(0,index).trim(); - result.put(prefix+key, PropertyValue.of(key, arg.substring(index+1).trim(), "main-args")); - key = null; - }else{ - result.put(prefix+arg, PropertyValue.of(prefix+arg, arg, "main-args")); - } - }else if(arg.startsWith("-")){ - key = arg.substring(1); - }else{ - if(key!=null){ - result.put(prefix+key, PropertyValue.of(prefix+key, arg, "main-args")); - key = null; - }else{ - result.put(prefix+arg, PropertyValue.of(prefix+arg, arg, "main-args")); - } - } - } - } - CLIPropertySource.mainArgs = Collections.unmodifiableMap(result); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.unmodifiableMap(mainArgs); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/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 deleted file mode 100644 index 0ba4cd4..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.osgi.service.component.annotations.Component; - -import java.util.*; - -/** - * <p>{@link PropertySource} to access environment variables via Tamaya - * which are set via {@code export VARIABLE=value} on UNIX systems or - * {@code set VARIABLE=value} on Windows systems.</p> - * - * <p>Using the {@linkplain EnvironmentPropertySource} without any - * additional configuration gives access to all existing environment - * variables available to the Java process Tamaya is running in.</p> - * - * <h1>Simple usage example</h1> - * - * <pre> - * $ export OPS_MODE=production - * $ export COLOR=false - * $ java -jar application.jar - * </pre> - * - * <p>To access {@code OPS_MODE} and {@code COLOR} with the following code - * fragment could be used:</p> - * - * <pre> - * PropertySource ps = new EnvironmentPropertySource(); - * PropertyValue opsMode = ps.get("OPS_MODE"); - * PropertyValue color = ps.get("COLOR"); - * </pre> - * - * <h1>Application specific environmet variables with prefix</h1> - * - * <p>Given the case where to instances of the same application are running on - * a single machine but need different values for the environment variable - * {@code CUSTOMER}. The {@linkplain EnvironmentPropertySource} allows you - * to prefix the environment variable with an application specific prefix - * and to access it by the non-prefixed variable name.</p> - * - * <pre> - * $ export CUSTOMER=none - * $ export a81.CUSTOMER=moon - * $ export b78.CUSTOMER=luna - * </pre> - * - * <p>Given an environment with these tree variables the application running - * for the customer called Moon could be started with the following command:</p> - * - * <pre> - * $ java -Dtamaya.envprops.prefix=a81 -jar application.jar - * </pre> - * - * <p>The application specific value can now be accessed from the code of the - * application like this:</p> - * - * <pre> - * PropertySource ps = new EnvironmentPropertySource(); - * PropertyValue pv = ps.get("CUSTOMER"); - * System.out.println(pv.getValue()); - * </pre> - * - * <p>The output of application would be {@code moon}.</p> - * - * <h1>Disabling the access to environment variables</h1> - * - * <p>The access to environment variables could be simply - * disabled by the setting the systemproperty {@code tamaya.envprops.disable} - * or {@code tamaya.defaults.disable} to {@code true}.</p> - */ -@Component(service = PropertySource.class) -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} - */ - public static final int DEFAULT_ORDINAL = 300; - - /** - * Prefix that allows environment properties to virtually be mapped on specified sub section. - */ - private String prefix; - - /** - * If true, this property source does not return any properties. This is useful since this - * property source is applied by default, but can be switched off by setting the - * {@code tamaya.envprops.disable} system/environment property to {@code true}. - */ - private boolean disabled = false; - - private SystemPropertiesProvider propertiesProvider = new SystemPropertiesProvider(); - - /** - * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties - * from the system-/ environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - public EnvironmentPropertySource(){ - initFromSystemProperties(); - } - - /** - * Initializes the {@code prefix} and {@code disabled} properties from the system-/ - * environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - private void initFromSystemProperties() { - String temp = getPropertiesProvider().getEnvPropsPrefix(); - - if (temp != null) { - prefix = temp; - } - - temp = getPropertiesProvider().getEnvPropsDisable(); - - if (temp != null) { - this.disabled = Boolean.parseBoolean(temp); - } - - temp = getPropertiesProvider().getDefaultsDisable(); - - if (temp != null) { - disabled |= Boolean.parseBoolean(temp); - } - } - - /** - * Creates a new instance using a fixed ordinal value. - * @param ordinal the ordinal number. - */ - public EnvironmentPropertySource(int ordinal){ - this(null, ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - * @param ordinal the ordinal to be used. - */ - public EnvironmentPropertySource(String prefix, int ordinal){ - this.prefix = prefix; - setOrdinal(ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - */ - public EnvironmentPropertySource(String prefix){ - this.prefix = prefix; - } - - @Override - public int getDefaultOrdinal() { - return DEFAULT_ORDINAL; - } - - @Override - public String getName() { - if (isDisabled()) { - return "environment-properties(disabled)"; - } - return "environment-properties"; - } - - @Override - public PropertyValue get(String key) { - if (isDisabled()) { - return null; - } - - 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 (isDisabled()) { - return Collections.emptyMap(); - } - - 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 (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())); - } - } - - return values; - } - - @Override - public boolean isScannable() { - 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/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java deleted file mode 100644 index 1518fc0..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.core.internal.PropertySourceComparator; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.apache.tamaya.spi.ServiceContextManager; -import org.osgi.service.component.annotations.Component; - -import java.io.IOException; -import java.net.URL; -import java.util.*; - -import static java.lang.String.format; -import static java.lang.Thread.currentThread; - -/** - * Provider which reads all {@value DEFAULT_SIMPLE_PROPERTIES_FILE_NAME} and - * {@value DEFAULT_XML_PROPERTIES_FILE_NAME} files found in the - * classpath. By setting - * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable} - * as system or environment property this feature can be disabled. - */ -@Component(service = PropertySource.class) -public class JavaConfigurationPropertySource extends BasePropertySource { - /** - * Default location in the classpath, where Tamaya looks for simple line based configuration by default. - */ - public static final String DEFAULT_SIMPLE_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties"; - - /** - * Default location in the classpath, where Tamaya looks for XML based configuration by default. - */ - public static final String DEFAULT_XML_PROPERTIES_FILE_NAME = "META-INF/javaconfiguration.xml"; - - private static final int DEFAULT_ORDINAL = 900; - - private boolean enabled = evaluateEnabled(); - - public JavaConfigurationPropertySource(){ - super("resource:META-INF/javaconfiguration.*", DEFAULT_ORDINAL); - } - - private boolean evaluateEnabled() { - String value = System.getProperty("tamaya.defaultprops.disable"); - if(value==null){ - value = System.getenv("tamaya.defaultprops.disable"); - } - if(value==null){ - value = System.getProperty("tamaya.defaults.disable"); - } - if(value==null){ - value = System.getenv("tamaya.defaults.disable"); - } - if(value==null){ - return true; - } - return value.isEmpty() || !Boolean.parseBoolean(value); - } - - private List<PropertySource> getPropertySources() { - List<PropertySource> propertySources = new ArrayList<>(); - propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME)); - propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME)); - Collections.sort(propertySources, PropertySourceComparator.getInstance()); - return propertySources; - } - - private Collection<? extends PropertySource> loadPropertySourcesByName(String filename) { - List<PropertySource> propertySources = new ArrayList<>(); - Enumeration<URL> propertyLocations; - try { - propertyLocations = ServiceContextManager.getServiceContext() - .getResources(filename, currentThread().getContextClassLoader()); - } catch (IOException e) { - String msg = format("Error while searching for %s", filename); - - throw new ConfigException(msg, e); - } - - while (propertyLocations.hasMoreElements()) { - URL currentUrl = propertyLocations.nextElement(); - SimplePropertySource sps = new SimplePropertySource(currentUrl); - - propertySources.add(sps); - } - - return propertySources; - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled){ - this.enabled = enabled; - } - - - @Override - public Map<String, PropertyValue> getProperties() { - if (!isEnabled()) { - return Collections.emptyMap(); - } - Map<String,PropertyValue> result = new HashMap<>(); - for(PropertySource ps:getPropertySources()){ - result.putAll(ps.getProperties()); - } - return result; - } - - @Override - public String toString() { - return "JavaConfigurationPropertySource{" + - "enabled=" + enabled + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java deleted file mode 100644 index 6046e0c..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.PropertyValue; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; -import java.util.logging.Logger; - -/** - * Simple implementation of a {@link org.apache.tamaya.spi.PropertySource} for - * simple property files and XML property files. - */ -public class SimplePropertySource extends BasePropertySource { - - private static final Logger LOG = Logger.getLogger(SimplePropertySource.class.getName()); - - /** - * The current properties. - */ - private Map<String, PropertyValue> properties = new HashMap<>(); - - /** - * Creates a new Properties based PropertySource based on the given URL. - * - * @param propertiesLocation the URL encoded location, not {@code null}. - */ - public SimplePropertySource(File propertiesLocation) { - super(0); - try { - setName(propertiesLocation.toString()); - this.properties = load(propertiesLocation.toURI().toURL()); - } catch (IOException e) { - throw new ConfigException("Failed to load properties from " + propertiesLocation, e); - } - } - - /** - * Creates a new Properties based PropertySource based on the given URL. - * - * @param propertiesLocation the URL encoded location, not {@code null}. - */ - public SimplePropertySource(URL propertiesLocation) { - super(0); - this.properties = load(Objects.requireNonNull(propertiesLocation)); - setName(propertiesLocation.toString()); - } - - /** - * Creates a new Properties based PropertySource based on the given properties map. - * - * @param name the name, not {@code null}. - * @param properties the properties, not {@code null}. - */ - public SimplePropertySource(String name, Map<String, String> properties) { - super(0); - setName(Objects.requireNonNull(name)); - for(Map.Entry<String,String> en:properties.entrySet()){ - this.properties.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), name)); - } - } - - /** - * Creates a new Properties based PropertySource based on the given URL. - * - * @param name The property source name - * @param propertiesLocation the URL encoded location, not {@code null}. - */ - public SimplePropertySource(String name, URL propertiesLocation) { - super(0); - this.properties = load(propertiesLocation); - setName(name); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return this.properties; - } - - /** - * loads the Properties from the given URL - * - * @param propertiesFile {@link java.net.URL} to load Properties from - * @return loaded {@link java.util.Properties} - * @throws IllegalStateException in case of an error while reading properties-file - */ - private Map<String, PropertyValue> load(URL propertiesFile) { - setName(propertiesFile.toString()); - boolean isXML = isXMLPropertieFiles(propertiesFile); - - Map<String, PropertyValue> properties = new HashMap<>(); - try (InputStream stream = propertiesFile.openStream()) { - Properties props = new Properties(); - if (stream != null) { - if (isXML) { - props.loadFromXML(stream); - } else { - props.load(stream); - } - } - - for (String key : props.stringPropertyNames()) { - properties.put(key, PropertyValue.of(key, props.getProperty(key), getName())); - } - } catch (IOException e) { - throw new ConfigException("Error loading properties from " + propertiesFile, e); - } - - return properties; - } - - private boolean isXMLPropertieFiles(URL url) { - return url.getFile().endsWith(".xml"); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java deleted file mode 100644 index b4ded3a..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.osgi.service.component.annotations.Component; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -/** - * This {@link org.apache.tamaya.spi.PropertySource} manages the system properties. You can disable this feature by - * setting {@code tamaya.envprops.disable} or {@code tamaya.defaults.disable}. - */ -@Component(service = PropertySource.class) -public class SystemPropertySource extends BasePropertySource { - - /** - * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource} - */ - public static final int DEFAULT_ORDINAL = 1000; - - private volatile Map<String, PropertyValue> cachedProperties; - - /** - * previous System.getProperties().hashCode() - * so we can check if we need to reload - */ - private volatile int previousHash; - - /** - * Prefix that allows system properties to virtually be mapped on specified sub section. - */ - private String prefix; - - /** - * If true, this property source does not return any properties. This is useful since this - * property source is applied by default, but can be switched off by setting the - * {@code tamaya.envprops.disable} system/environment property to {@code true}. - */ - private boolean disabled = false; - - /** - * Creates a new instance. Also initializes the {@code prefix} and {@code disabled} properties - * from the system-/ environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - public SystemPropertySource(){ - initFromSystemProperties(); - if(!disabled){ - cachedProperties = Collections.unmodifiableMap(loadProperties()); - } - } - - /** - * Initializes the {@code prefix} and {@code disabled} properties from the system-/ - * environment properties: - * <pre> - * tamaya.envprops.prefix - * tamaya.envprops.disable - * </pre> - */ - private void initFromSystemProperties() { - String value = System.getProperty("tamaya.sysprops.prefix"); - if(value==null){ - prefix = System.getenv("tamaya.sysprops.prefix"); - } - value = System.getProperty("tamaya.sysprops.disable"); - if(value==null){ - value = System.getenv("tamaya.sysprops.disable"); - } - if(value==null){ - value = System.getProperty("tamaya.defaults.disable"); - } - if(value==null){ - value = System.getenv("tamaya.defaults.disable"); - } - if(value!=null && !value.isEmpty()) { - this.disabled = Boolean.parseBoolean(value); - } - } - - /** - * Creates a new instance using a fixed ordinal value. - * @param ordinal the ordinal number. - */ - public SystemPropertySource(int ordinal){ - this(null, ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - * @param ordinal the ordinal to be used. - */ - public SystemPropertySource(String prefix, int ordinal){ - this.prefix = prefix; - setOrdinal(ordinal); - } - - /** - * Creates a new instance. - * @param prefix the prefix to be used, or null. - */ - public SystemPropertySource(String prefix){ - this.prefix = prefix; - } - - @Override - public int getDefaultOrdinal() { - return DEFAULT_ORDINAL; - } - - - private Map<String,PropertyValue> loadProperties() { - Properties sysProps = System.getProperties(); - previousHash = System.getProperties().hashCode(); - final String prefix = this.prefix; - Map<String,PropertyValue> values = new HashMap<>(); - for (Map.Entry<Object,Object> entry : sysProps.entrySet()) { - if(prefix==null) { - values.put(entry.getKey().toString(), PropertyValue.of(entry.getKey().toString(), entry.getValue().toString(), getName())); - }else { - values.put(prefix + entry.getKey(), PropertyValue.of(prefix + entry.getKey(), entry.getValue().toString(), getName())); - } - } - return values; - } - - @Override - public String getName() { - if(disabled){ - return "system-properties(disabled)"; - } - return "system-properties"; - } - - @Override - public PropertyValue get(String key) { - if(disabled){ - return null; - } - String prefix = this.prefix; - if(prefix==null) { - return PropertyValue.of(key, System.getProperty(key), getName()); - } - return PropertyValue.of(key, System.getProperty(key.substring(prefix.length())), getName()); - } - - @Override - public Map<String, PropertyValue> getProperties() { - if(disabled){ - return Collections.emptyMap(); - } - // only need to reload and fill our map if something has changed - // 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()) { - this.cachedProperties = Collections.unmodifiableMap(loadProperties()); - } - return this.cachedProperties; - } - - @Override - public boolean isScannable() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/java/org/apache/tamaya/core/propertysource/package-info.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/package-info.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/package-info.java deleted file mode 100644 index 05ed466..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * Contains internal implementations artifacts registered as services. - */ -package org.apache.tamaya.core.propertysource; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter index f934197..396aef1 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -41,3 +41,4 @@ org.apache.tamaya.core.internal.converters.OffsetDateTimeConverter org.apache.tamaya.core.internal.converters.OffsetTimeConverter org.apache.tamaya.core.internal.converters.InstantConverter org.apache.tamaya.core.internal.converters.OptionalConverter +org.apache.tamaya.core.internal.converters.SupplierConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource index 56c599c..d1e392d 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -16,7 +16,7 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.core.propertysource.EnvironmentPropertySource -org.apache.tamaya.core.propertysource.SystemPropertySource -org.apache.tamaya.core.propertysource.CLIPropertySource -org.apache.tamaya.core.propertysource.JavaConfigurationPropertySource \ No newline at end of file +org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource +org.apache.tamaya.spisupport.propertysource.SystemPropertySource +org.apache.tamaya.spisupport.propertysource.CLIPropertySource +org.apache.tamaya.spisupport.propertysource.JavaConfigurationPropertySource \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java index ddb36e2..2c8f627 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/ConfigurationContextBuilderTest.java @@ -20,8 +20,8 @@ package org.apache.tamaya.core; import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.core.internal.DefaultConfigurationContextBuilder; import org.apache.tamaya.spi.*; +import org.apache.tamaya.core.internal.DefaultConfigurationContextBuilder; import org.junit.Test; import java.util.Arrays; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java index 7c483bb..38412f3 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java @@ -22,7 +22,6 @@ import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.core.testdata.TestPropertyDefaultSource; import org.apache.tamaya.spi.*; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java deleted file mode 100644 index d45dbbd..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; -import org.junit.Test; - -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; - -public class DefaultConfigurationTest { - - /** - * Tests for get(String) - */ - @Test(expected = NullPointerException.class) - public void getDoesNotAcceptNull() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.get(null); - } - - /** - * Tests for get(String, Class) - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Test(expected = NullPointerException.class) - public void getDoesNotAcceptNullForClassTargetType() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.get("a", (Class) null); - } - - /** - * Tests for get(String, TypeLiteral) - */ - @Test(expected = NullPointerException.class) - public void getDoesNotAcceptNullForTypeLiteralTargetType() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.get("a", (TypeLiteral<?>)null); - } - - /** - * Tests for getOrDefault(String, Class, String) - */ - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsKeyForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault(null, String.class, "ok"); - } - - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsDefaultValueForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault("a", String.class, null); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsTargetTypeForThreeParameterVariant() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault("a", (Class)null, "b"); - } - - /** - * Tests for getOrDefault(String, TypeLiteral, String) - */ - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsKeyForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault(null, TypeLiteral.of(String.class), "ok"); - } - - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsDefaultValueForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault("a", TypeLiteral.of(String.class), null); - } - - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsTargetTypeForThreeParameterVariantSecondIsTypeLiteral() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault("a", (TypeLiteral<String>) null, "b"); - } - - /** - * Tests for getOrDefault(String, String) - */ - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsKeyForTwoParameterVariantDefaultValueIsSecond() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault(null, "ok"); - } - - @Test(expected = NullPointerException.class) - public void getOrDefaultDoesNotAcceptNullAsDefaultValueForTwoParameterVariantDefaultValueIsSecond() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.getOrDefault("a", null); - } - - @Test(expected = NullPointerException.class) - public void with_Null() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.with(null); - } - - @Test(expected = NullPointerException.class) - public void query_Null() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - - c.query(null); - } - - @Test - public void with() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - assertEquals(c.with(config -> config), c); - } - - @Test - public void query() { - DefaultConfiguration c = new DefaultConfiguration(new DummyConfigurationContext()); - assertEquals(c.query(config -> "testQ"), "testQ"); - } - - public static class DummyConfigurationContext implements ConfigurationContext { - @Override - public void addPropertySources(PropertySource... propertySources) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public List<PropertySource> getPropertySources() { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public PropertySource getPropertySource(String name) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public <T> void addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type) { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public List<PropertyFilter> getPropertyFilters() { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() { - throw new RuntimeException("Method should be never called in this test"); - } - - @Override - public ConfigurationContextBuilder toBuilder() { - throw new RuntimeException("Method should be never called in this test"); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java deleted file mode 100644 index 1b1425b..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/PriorityServiceComparatorTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.core.propertysource.SystemPropertySource; -import org.junit.Test; - -import javax.annotation.Priority; - -import static org.junit.Assert.*; - -/** - * Created by atsticks on 12.09.16. - */ -public class PriorityServiceComparatorTest { - - - @Test - public void compare() throws Exception { - assertTrue(PriorityServiceComparator.getInstance().compare("a", "b")==0); - assertTrue(PriorityServiceComparator.getInstance().compare(getClass(), getClass())==0); - assertTrue(PriorityServiceComparator.getInstance().compare(new A(), new SystemPropertySource())==-1); - assertTrue(PriorityServiceComparator.getInstance().compare(new SystemPropertySource(), new A())==1); - } - - @Priority(100) - private static final class A{} - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java deleted file mode 100644 index fd9c766..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.TypeLiteral; -import org.junit.Test; - -import java.util.List; - -import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; - -@SuppressWarnings("unchecked") -public class PropertyConverterManagerTest { - - private final ConversionContext DUMMY_CONTEXT = new ConversionContext.Builder( - "someKey", TypeLiteral.of(Object.class)).build(); - - @Test - public void customTypeWithFactoryMethodOfIsRecognizedAsSupported() { - PropertyConverterManager manager = new PropertyConverterManager(); - - assertThat(manager.isTargetTypeSupported(TypeLiteral.of(MyType.class)), - is(true)); - } - - @SuppressWarnings({ "rawtypes" }) - @Test - public void factoryMethodOfIsUsedAsConverter() { - PropertyConverterManager manager = new PropertyConverterManager(); - - List<PropertyConverter<MyType>> converters = manager.getPropertyConverters( - (TypeLiteral)TypeLiteral.of(MyType.class)); - - assertThat(converters, hasSize(1)); - - PropertyConverter<MyType> converter = converters.get(0); - - Object result = converter.convert("IN", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(MyType.class)); - assertThat(((MyType)result).getValue(), equalTo("IN")); - } - - @Test - public void testDirectConverterMapping(){ - PropertyConverterManager manager = new PropertyConverterManager(); - List<PropertyConverter<C>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(C.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<C> converter = converters.get(0); - C result = converter.convert("testDirectConverterMapping", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat((result).getInValue(), equalTo("testDirectConverterMapping")); - } - - @Test - public void testDirectSuperclassConverterMapping(){ - PropertyConverterManager manager = new PropertyConverterManager(true); - List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); - assertThat(converters, hasSize(1)); - converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<B> converter = converters.get(0); - B result = converter.convert("testDirectSuperclassConverterMapping", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat(((C)result).getInValue(), equalTo("testDirectSuperclassConverterMapping")); - } - - @Test - public void testMultipleConverterLoad(){ - PropertyConverterManager manager = new PropertyConverterManager(); - List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); - assertThat(converters, hasSize(0)); - manager = new PropertyConverterManager(); - converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); - assertThat(converters, hasSize(0)); - manager.initConverters(); - converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class))); - assertThat(converters, hasSize(1)); - } - - @Test - public void testTransitiveSuperclassConverterMapping(){ - PropertyConverterManager manager = new PropertyConverterManager(true); - List<PropertyConverter<A>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(A.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<A> converter = converters.get(0); - A result = converter.convert("testTransitiveSuperclassConverterMapping", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat(((C)result).getInValue(), equalTo("testTransitiveSuperclassConverterMapping")); - } - - @Test - public void testDirectInterfaceMapping(){ - PropertyConverterManager manager = new PropertyConverterManager(true); - List<PropertyConverter<Readable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Readable.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<Readable> converter = converters.get(0); - Readable result = converter.convert("testDirectInterfaceMapping", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat(((C)result).getInValue(), equalTo("testDirectInterfaceMapping")); - } - - @Test - public void testTransitiveInterfaceMapping1(){ - PropertyConverterManager manager = new PropertyConverterManager(true); - List<PropertyConverter<Runnable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Runnable.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<Runnable> converter = converters.get(0); - Runnable result = converter.convert("testTransitiveInterfaceMapping1", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat(((C)result).getInValue(), equalTo("testTransitiveInterfaceMapping1")); - } - - @Test - public void testTransitiveInterfaceMapping2(){ - PropertyConverterManager manager = new PropertyConverterManager(true); - List<PropertyConverter<AutoCloseable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(AutoCloseable.class))); - assertThat(converters, hasSize(1)); - - PropertyConverter<AutoCloseable> converter = converters.get(0); - AutoCloseable result = converter.convert("testTransitiveInterfaceMapping2", DUMMY_CONTEXT); - - assertThat(result, notNullValue()); - assertThat(result, instanceOf(C.class)); - assertThat(((C)result).getInValue(), equalTo("testTransitiveInterfaceMapping2")); - } - - public static class MyType { - private final String typeValue; - - private MyType(String value) { - typeValue = value; - } - - public static MyType of(String source) { - return new MyType(source); - } - - public String getValue() { - return typeValue; - } - - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyFilterComparatorTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyFilterComparatorTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyFilterComparatorTest.java deleted file mode 100644 index 0ad4c8a..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyFilterComparatorTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.internal; - -import org.apache.tamaya.spi.FilterContext; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertyValue; -import org.junit.Test; - -import javax.annotation.Priority; - -import java.util.Comparator; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PropertyFilterComparatorTest { - - @Test - public void comparationOfFiltersWithSamePriorityIsCorrect() { - Comparator<PropertyFilter> comparator = PropertyFilterComparator.getInstance(); - - int result = comparator.compare(new PropertyFilterA(), new PropertyFilterA()); - - assertThat(result).isEqualTo(0); - } - - @Test - public void comparationOfFiltersFirstHigherThenSecondWorksCorrectly() { - Comparator<PropertyFilter> comparator = PropertyFilterComparator.getInstance(); - - int result = comparator.compare(new PropertyFilterB(), new PropertyFilterA()); - - assertThat(result).isGreaterThan(0); - } - - @Test - public void comparationOfFiltersSecondHigherThenFirstWorksCorrectly() { - Comparator<PropertyFilter> comparator = PropertyFilterComparator.getInstance(); - - int result = comparator.compare(new PropertyFilterA(), new PropertyFilterB()); - - assertThat(result).isLessThan(0); - } - - - @Priority(1) - private static class PropertyFilterA implements PropertyFilter { - public PropertyValue filterProperty(PropertyValue value, FilterContext context) { - throw new RuntimeException("Not implemented or look at me!"); - } - } - - @Priority(2) - private static class PropertyFilterB implements PropertyFilter { - public PropertyValue filterProperty(PropertyValue value, FilterContext context) { - throw new RuntimeException("Not implemented or look at me!"); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7917a9f3/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java b/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java deleted file mode 100644 index 54f3113..0000000 --- a/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.core.propertysource; - -import org.apache.tamaya.core.internal.PropertySourceComparator; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; -import org.junit.Assert; -import org.junit.Test; - -import java.util.*; - -import static org.assertj.core.api.Assertions.assertThat; - -public class BasePropertySourceTest { - - @Test - public void isAlwaysScanable() { - BasePropertySource bs = new BasePropertySource() { - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - }; - - assertThat(bs.isScannable()).isTrue(); - } - - @Test - public void givenOrdinalOverwritesGivenDefaulOrdinal() { - BasePropertySource bs = new BasePropertySource() { - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - }; - - bs.setDefaultOrdinal(10); - - assertThat(bs.getDefaultOrdinal()).isEqualTo(10); - assertThat(bs.getOrdinal()).isEqualTo(10); - - bs.setOrdinal(20); - - assertThat(bs.getOrdinal()).isEqualTo(20); - } - - @Test - public void testGetOrdinal() { - - PropertySource defaultPropertySource = new BasePropertySource(56) { - - @Override - public String getName() { - return "testWithDefault"; - } - - @Override - public PropertyValue get(String key) { - return null; - } - - @Override - public Map<String, PropertyValue> getProperties() { - return Collections.emptyMap(); - } - }; - - Assert.assertEquals(56, PropertySourceComparator.getOrdinal(defaultPropertySource)); - Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal()); - - // propertySource with invalid ordinal - Assert.assertEquals(1, new OverriddenInvalidOrdinalPropertySource().getOrdinal()); - } - - @Test - public void testGet() { - Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal()); - } - - private static class OverriddenOrdinalPropertySource extends BasePropertySource { - - private OverriddenOrdinalPropertySource() { - super(250); - } - - @Override - public String getName() { - return "overriddenOrdinal"; - } - - @Override - public Map<String, PropertyValue> getProperties() { - Map<String,PropertyValue> result = new HashMap<>(1); - result.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "1000", getName())); - return result; - } - } - - private static class OverriddenInvalidOrdinalPropertySource extends BasePropertySource { - - private OverriddenInvalidOrdinalPropertySource() { - super(1); - } - - @Override - public String getName() { - return "overriddenInvalidOrdinal"; - } - - @Override - public Map<String, PropertyValue> getProperties() { - Map<String,PropertyValue> result = new HashMap<>(1); - result.put(PropertySource.TAMAYA_ORDINAL, PropertyValue.of(PropertySource.TAMAYA_ORDINAL, "invalid", getName())); - return result; - } - } - - -}
