http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java new file mode 100644 index 0000000..121c490 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java @@ -0,0 +1,83 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Long, the supported format is one of the following: + * <ul> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * <li>-123 (byte value)</li> + * <li>-0xFF (byte value)</li> + * <li>-0XDF (byte value)</li> + * <li>-0D1 (byte value)</li> + * <li>MIN_VALUE (ignoring case)</li> + * <li>MIN (ignoring case)</li> + * <li>MAX_VALUE (ignoring case)</li> + * <li>MAX (ignoring case)</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class LongConverter implements PropertyConverter<Long>{ + + private static final Logger LOGGER = Logger.getLogger(LongConverter.class.getName()); + + @Override + public Long convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "<long>", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE"); + + String trimmed = Objects.requireNonNull(value).trim(); + switch (trimmed.toUpperCase(Locale.ENGLISH)) { + case "MIN_VALUE": + case "MIN": + return Long.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Long.MAX_VALUE; + default: + try { + return Long.decode(trimmed); + } + catch(Exception e){ + LOGGER.finest("Unable to parse Long value: " + value); + return null; + } + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java new file mode 100644 index 0000000..a7b2c43 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java @@ -0,0 +1,84 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.math.BigDecimal; +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Number. Valid inputs are: + * <pre> + * POSITIVE_INFINITY -> Double.POSITIVE_INFINITY + * NEGATIVE_INFINITY -> Double.NEGATIVE_INFINITY + * NaN > Double.NaN + * 0xFFDCD3D2 -> Long + * 1234566789.23642327352735273752 -> new BigDecimal(input) + * </pre> + */ +@Component(service = PropertyConverter.class) +public class NumberConverter implements PropertyConverter<Number>{ + /** the logger. */ + private static final Logger LOGGER = Logger.getLogger(NumberConverter.class.getName()); + /** Converter used for trying to parse as an integral value. */ + private final LongConverter longConverter = new LongConverter(); + + @Override + public Number convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "<double>, <long>", "0x (hex)", "0X... (hex)", "POSITIVE_INFINITY", + "NEGATIVE_INFINITY", "NAN"); + + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)) { + case "POSITIVE_INFINITY": + return Double.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Double.NEGATIVE_INFINITY; + case "NAN": + return Double.NaN; + default: + Long lVal = longConverter.convert(trimmed, context); + if (lVal != null) { + return lVal; + } + try{ + return new BigDecimal(trimmed); + } + catch(Exception e){ + LOGGER.finest("Unparseable Number: " + trimmed); + return null; + } + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java new file mode 100644 index 0000000..062d584 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetDateTimeConverter.java @@ -0,0 +1,57 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.OffsetDateTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class OffsetDateTimeConverter implements PropertyConverter<OffsetDateTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public OffsetDateTime convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), OffsetDateTime.now().toString()); + try{ + return OffsetDateTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetDateTime: " + value); + return null; + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java new file mode 100644 index 0000000..794ad8a --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OffsetTimeConverter.java @@ -0,0 +1,57 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.time.OffsetTime; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class OffsetTimeConverter implements PropertyConverter<OffsetTime> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public OffsetTime convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), OffsetTime.now().toString()); + try{ + return OffsetTime.parse(value); + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetTime: " + value); + return null; + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java new file mode 100644 index 0000000..2fd4b47 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/OptionalConverter.java @@ -0,0 +1,65 @@ +/* + * 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.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.Optional; + +/** + * Converter, converting from String to Boolean. + */ +@Component(service = PropertyConverter.class) +public class OptionalConverter implements PropertyConverter<Optional> { + + @Override + public Optional convert(String value, ConversionContext context) { + if(value==null){ + return Optional.empty(); + } + try{ + Type targetType = context.getTargetType().getType(); + ParameterizedType pt = (ParameterizedType) targetType; + if(String.class.equals(pt.getActualTypeArguments()[0])){ + return Optional.of(value); + } + ConvertQuery converter = new ConvertQuery(value, TypeLiteral.of(pt.getActualTypeArguments()[0])); + return Optional.ofNullable(context.getConfiguration().query(converter)); + }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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java new file mode 100644 index 0000000..404daee --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/PathConverter.java @@ -0,0 +1,63 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +@Component(service = PropertyConverter.class) +public class PathConverter implements PropertyConverter<Path> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Path convert(String value, ConversionContext context) { + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<File>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return FileSystems.getDefault().getPath(value); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable Path: " + trimmed, e); + } + return null; + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java new file mode 100644 index 0000000..7b561f3 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java @@ -0,0 +1,84 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.util.Locale; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to Short, the supported format is one of the following: + * <ul> + * <li>123 (byte value)</li> + * <li>0xFF (byte value)</li> + * <li>0XDF (byte value)</li> + * <li>0D1 (byte value)</li> + * <li>-123 (byte value)</li> + * <li>-0xFF (byte value)</li> + * <li>-0XDF (byte value)</li> + * <li>-0D1 (byte value)</li> + * <li>MIN_VALUE (ignoring case)</li> + * <li>MIN (ignoring case)</li> + * <li>MAX_VALUE (ignoring case)</li> + * <li>MAX (ignoring case)</li> + * </ul> + */ +@Component(service = PropertyConverter.class) +public class ShortConverter implements PropertyConverter<Short>{ + + /** the logger. */ + private static final Logger LOG = Logger.getLogger(ShortConverter.class.getName()); + + @Override + public Short convert(String value, ConversionContext context) { + context.addSupportedFormats(getClass(), "short", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE"); + String trimmed = Objects.requireNonNull(value).trim(); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Short.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Short.MAX_VALUE; + default: + try{ + return Short.decode(trimmed); + } + catch(Exception e){ + LOG.finest("Unparseable Short: " + trimmed); + return null; + } + } + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/SupplierConverter.java new file mode 100644 index 0000000..6d00322 --- /dev/null +++ b/code/old/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.spi.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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URIConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URIConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URIConverter.java new file mode 100644 index 0000000..793631e --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URIConverter.java @@ -0,0 +1,62 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.net.URI; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URI(value). + */ +@Component(service = PropertyConverter.class) +public class URIConverter implements PropertyConverter<URI> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URI convert(String value, ConversionContext context) { + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(), "<uri> -> new URI(uri)"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new URI(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable URI: " + trimmed, e); + } + return null; + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java new file mode 100644 index 0000000..e1994bf --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/URLConverter.java @@ -0,0 +1,62 @@ +/* + * 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.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.osgi.service.component.annotations.Component; + +import java.net.URL; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Converter, converting from String to URI, using new URL(value). + */ +@Component(service = PropertyConverter.class) +public class URLConverter implements PropertyConverter<URL> { + + private final Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public URL convert(String value, ConversionContext context) { + if(value==null || value.isEmpty()){ + return null; + } + context.addSupportedFormats(getClass(),"<URL>"); + String trimmed = Objects.requireNonNull(value).trim(); + try { + return new URL(trimmed); + } catch (Exception e) { + LOG.log(Level.FINE, "Unparseable URL: " + trimmed, e); + } + return null; + } + + @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/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/package-info.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/package-info.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/package-info.java new file mode 100644 index 0000000..13f66c1 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/converters/package-info.java @@ -0,0 +1,23 @@ +/* + * 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 implementations of the converters provided by default. + */ +package org.apache.tamaya.core.internal.converters; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/internal/package-info.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/internal/package-info.java b/code/old/core/src/main/java/org/apache/tamaya/core/internal/package-info.java new file mode 100644 index 0000000..a99070b --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/internal/package-info.java @@ -0,0 +1,25 @@ +/* + * 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 implementations of different property sources for supporting + * CLI, Environment and System Properties as well as simple properties + * based on the formats defined by {@link java.util.Properties}. + */ +package org.apache.tamaya.core.internal; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/java/org/apache/tamaya/core/package-info.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/java/org/apache/tamaya/core/package-info.java b/code/old/core/src/main/java/org/apache/tamaya/core/package-info.java new file mode 100644 index 0000000..eb7a398 --- /dev/null +++ b/code/old/core/src/main/java/org/apache/tamaya/core/package-info.java @@ -0,0 +1,24 @@ +/* + * 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. + */ + +/** + * This is the root package of the Apache Tamaya API implementation. + */ [email protected]("0.4") +package org.apache.tamaya.core; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration new file mode 100644 index 0000000..e51a247 --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.Configuration @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.internal.DefaultConfiguration http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.core.internal.ConfigValueEvaluator ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.core.internal.ConfigValueEvaluator b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.core.internal.ConfigValueEvaluator new file mode 100644 index 0000000..624f0c1 --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.core.internal.ConfigValueEvaluator @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.internal.DefaultConfigValueEvaluator \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder new file mode 100644 index 0000000..700b2b5 --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.internal.CoreConfigurationContextBuilder \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..c4aa43f --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.internal.CoreConfigurationProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter new file mode 100644 index 0000000..396aef1 --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -0,0 +1,44 @@ +# +# 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 current 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. +# +org.apache.tamaya.core.internal.converters.BooleanConverter +org.apache.tamaya.core.internal.converters.ByteConverter +org.apache.tamaya.core.internal.converters.CharConverter +org.apache.tamaya.core.internal.converters.ClassConverter +org.apache.tamaya.core.internal.converters.DoubleConverter +org.apache.tamaya.core.internal.converters.FloatConverter +org.apache.tamaya.core.internal.converters.IntegerConverter +org.apache.tamaya.core.internal.converters.LongConverter +org.apache.tamaya.core.internal.converters.ShortConverter +org.apache.tamaya.core.internal.converters.BigDecimalConverter +org.apache.tamaya.core.internal.converters.BigIntegerConverter +org.apache.tamaya.core.internal.converters.CurrencyConverter +org.apache.tamaya.core.internal.converters.NumberConverter +org.apache.tamaya.core.internal.converters.URIConverter +org.apache.tamaya.core.internal.converters.URLConverter +org.apache.tamaya.core.internal.converters.FileConverter +org.apache.tamaya.core.internal.converters.PathConverter +org.apache.tamaya.core.internal.converters.DurationConverter +org.apache.tamaya.core.internal.converters.LocalDateConverter +org.apache.tamaya.core.internal.converters.LocalDateTimeConverter +org.apache.tamaya.core.internal.converters.LocalTimeConverter +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/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource new file mode 100644 index 0000000..d1e392d --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -0,0 +1,22 @@ +# +# 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 current 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. +# +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/9bc56a38/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext ---------------------------------------------------------------------- diff --git a/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext new file mode 100644 index 0000000..4364f06 --- /dev/null +++ b/code/old/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.base.DefaultServiceContext http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/9bc56a38/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java new file mode 100644 index 0000000..a7c9635 --- /dev/null +++ b/code/old/core/src/test/java/org/apache/tamaya/core/ConfigurationBuilderTest.java @@ -0,0 +1,438 @@ +/* + * 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; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.TypeLiteral; +import org.apache.tamaya.core.internal.CoreConfigurationBuilder; +import org.apache.tamaya.spi.*; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +import static org.junit.Assert.*; + +/** + * Tests for {@link ConfigurationBuilder} by atsticks on 06.09.16. + */ +public class ConfigurationBuilderTest { + + private TestPropertySource testPropertySource = new TestPropertySource(){}; + + @Test + public void setContext() throws Exception { + Configuration cfg = ConfigurationProvider.getConfiguration(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .setConfiguration(cfg); + assertEquals(cfg, b.build()); + } + + @Test + public void addPropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Array", 1); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Array", 1); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPS2, testPropertySource); + cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "addPropertySources_Array"); + } + + @Test + public void addPropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + ConfigurationBuilder b = new CoreConfigurationBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPropertySource, testPS2})); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "addPropertySources_Collection"); + // Ensure no sorting happens during add, so switch ordinals! + testPS2 = new TestPropertySource("addPropertySources_Collection", 1); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(Arrays.asList(new PropertySource[]{testPS2, testPropertySource})); + cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(cfg.getContext().getPropertySources().get(1).getName(), "TestPropertySource"); + assertEquals(cfg.getContext().getPropertySources().get(0).getName(), "addPropertySources_Collection"); + } + + @Test + public void removePropertySources_Array() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + assertFalse(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + assertEquals(1, cfg.getContext().getPropertySources().size()); + } + + @Test + public void removePropertySources_Collection() throws Exception { + PropertySource testPS2 = new TestPropertySource("removePropertySources_Array", 1); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + Configuration cfg = b.build(); + assertEquals(2, cfg.getContext().getPropertySources().size()); + assertTrue(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertySources(testPropertySource, testPS2); + b.removePropertySources(testPropertySource); + cfg = b.build(); + assertEquals(1, cfg.getContext().getPropertySources().size()); + assertFalse(cfg.getContext().getPropertySources().contains(testPropertySource)); + assertTrue(cfg.getContext().getPropertySources().contains(testPS2)); + } + + @Test + public void addPropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void addPropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters(filter1, filter2); + b.addPropertyFilters(filter1, filter2); + assertEquals(2, ctx.getPropertyFilters().size()); + } + + @Test + public void removePropertyFilters_Array() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(filter1, filter2); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + public void removePropertyFilters_Collection() throws Exception { + PropertyFilter filter1 = (value, context) -> value; + PropertyFilter filter2 = (value, context) -> value; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + assertEquals(2, ctx.getPropertyFilters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyFilters(Arrays.asList(new PropertyFilter[]{filter1, filter2})); + b.removePropertyFilters(filter1); + cfg = b.build(); + ctx = cfg.getContext(); + assertEquals(1, ctx.getPropertyFilters().size()); + assertFalse(ctx.getPropertyFilters().contains(filter1)); + assertTrue(ctx.getPropertyFilters().contains(filter2)); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters().size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(1, ctx.getPropertyConverters().size()); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void addPropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(ctx.getPropertyConverters().size(), 1); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), + Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + b.addPropertyConverters(TypeLiteral.of(String.class), converter); + assertEquals(ctx.getPropertyConverters().size(), 1); + } + + @Test + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void removePropertyConverters_Array() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), converter); + b.removePropertyConverters(TypeLiteral.of(String.class), converter); + cfg = b.build(); + ctx = cfg.getContext(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void removePropertyConverters_Collection() throws Exception { + PropertyConverter converter = (value, context) -> value.toLowerCase(); + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertEquals(1, ctx.getPropertyConverters(TypeLiteral.of(String.class)).size()); + b = ConfigurationProvider.getConfigurationBuilder() + .addPropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + b.removePropertyConverters(TypeLiteral.of(String.class), Arrays.<PropertyConverter<Object>>asList(new PropertyConverter[]{converter})); + cfg = b.build(); + ctx = cfg.getContext(); + assertFalse(ctx.getPropertyConverters(TypeLiteral.of(String.class)).contains(converter)); + assertTrue(ctx.getPropertyConverters(TypeLiteral.of(String.class)).isEmpty()); + } + + @Test + public void setPropertyValueCombinationPolicy() throws Exception { + PropertyValueCombinationPolicy combPol = (currentValue, key, propertySource) -> currentValue; + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder() + .setPropertyValueCombinationPolicy(combPol); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertEquals(ctx.getPropertyValueCombinationPolicy(), combPol); + } + + @Test + public void increasePriority(){ + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + b.increasePriority(propertySources[propertySources.length-1]); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.increasePriority(propertySources[propertySources.length-2]); + for(int i=0;i<propertySources.length-2;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length-1], b.getPropertySources().get(propertySources.length-2)); + assertEquals(propertySources[propertySources.length-2], b.getPropertySources().get(propertySources.length-1)); + } + + @Test + public void decreasePriority(){ + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + b.decreasePriority(propertySources[0]); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.decreasePriority(propertySources[1]); + for(int i=2;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + } + + @Test + public void lowestPriority(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + // test + b.lowestPriority(propertySources[0]); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.lowestPriority(propertySources[1]); + for(int i=2;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[0], b.getPropertySources().get(1)); + assertEquals(propertySources[1], b.getPropertySources().get(0)); + b.lowestPriority(propertySources[5]); + assertEquals(propertySources[5], b.getPropertySources().get(0)); + } + + @Test + public void highestPriority(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + // test + b.highestPriority(propertySources[propertySources.length-1]); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + b.highestPriority(propertySources[propertySources.length-2]); + for(int i=0;i<propertySources.length-2;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + assertEquals(propertySources[propertySources.length-2], b.getPropertySources().get(propertySources.length-1)); + assertEquals(propertySources[propertySources.length-1], b.getPropertySources().get(propertySources.length-2)); + b.highestPriority(propertySources[5]); + assertEquals(propertySources[5], b.getPropertySources().get(propertySources.length-1)); + } + + @Test + public void sortPropertySources(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + TestPropertySource[] propertySources = new TestPropertySource[10]; + for(int i=0;i<propertySources.length;i++){ + propertySources[i] = new TestPropertySource("ps"+i,i); + } + b.addPropertySources(propertySources); + Comparator<PropertySource> psComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + // test + b.sortPropertySources(psComp); + Arrays.sort(propertySources, psComp); + for(int i=0;i<propertySources.length;i++){ + assertEquals(propertySources[i], b.getPropertySources().get(i)); + } + } + + @Test + public void sortPropertyFilter(){ + // setup + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + PropertyFilter[] propertyFilters = new PropertyFilter[10]; + for(int i=0;i<propertyFilters.length;i++){ + propertyFilters[i] = (value, context) -> value.toBuilder().setValue(toString() + " - ").build(); + } + b.addPropertyFilters(propertyFilters); + Comparator<PropertyFilter> pfComp = (o1, o2) -> o1.toString().compareTo(o2.toString()); + // test + b.sortPropertyFilter(pfComp); + Arrays.sort(propertyFilters, pfComp); + for(int i=0;i<propertyFilters.length;i++){ + assertEquals(propertyFilters[i], b.getPropertyFilters().get(i)); + } + } + + @Test + public void build() throws Exception { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + Configuration cfg = b.build(); + ConfigurationContext ctx = cfg.getContext(); + assertNotNull(ctx); + assertTrue(ctx.getPropertySources().isEmpty()); + assertTrue(ctx.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllFilters() throws Exception { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertyFilters((value, context) -> value.toBuilder().setValue(toString() + " - ").build()); + assertFalse(b.getPropertyFilters().isEmpty()); + b.removePropertyFilters(b.getPropertyFilters()); + assertTrue(b.getPropertyFilters().isEmpty()); + } + + @Test + public void testRemoveAllSources() throws Exception { + ConfigurationBuilder b = ConfigurationProvider.getConfigurationBuilder(); + b.addPropertySources(new TestPropertySource()); + assertFalse(b.getPropertySources().isEmpty()); + b.removePropertySources(b.getPropertySources()); + assertTrue(b.getPropertyFilters().isEmpty()); + } +} \ No newline at end of file
