Repository: incubator-tamaya Updated Branches: refs/heads/master 8f9c11ddc -> bfbaefa43
TAMAYA-58: Added getTargetType to PropertyConverter. Added implementations of converters for base types. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/578c873d Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/578c873d Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/578c873d Branch: refs/heads/master Commit: 578c873d8bce0f175575d55b5a775e60f63c741c Parents: 8f9c11d Author: anatole <[email protected]> Authored: Sun Jan 18 01:01:43 2015 +0100 Committer: anatole <[email protected]> Committed: Sun Jan 18 01:09:24 2015 +0100 ---------------------------------------------------------------------- .../apache/tamaya/spi/PropertyConverter.java | 13 +- .../core/internal/PropertyConverterManager.java | 89 ++++++------- .../converters/BigDecimalConverter.java | 41 ++++++ .../converters/BigIntegerConverter.java | 42 ++++++ .../internal/converters/BooleanConverter.java | 56 ++++++++ .../core/internal/converters/ByteConverter.java | 39 ++++++ .../core/internal/converters/CharConverter.java | 42 ++++++ .../internal/converters/CurrencyConverter.java | 40 ++++++ .../internal/converters/DoubleConverter.java | 40 ++++++ .../core/internal/converters/EnumConverter.java | 65 +++++++++ .../internal/converters/FloatConverter.java | 40 ++++++ .../internal/converters/IntegerConverter.java | 39 ++++++ .../internal/converters/LocalDateConverter.java | 40 ++++++ .../converters/LocalDateTimeConverter.java | 40 ++++++ .../internal/converters/LocalTimeConverter.java | 40 ++++++ .../core/internal/converters/LongConverter.java | 39 ++++++ .../internal/converters/NumberConverter.java | 41 ++++++ .../internal/converters/ShortConverter.java | 39 ++++++ .../internal/converters/ZoneIdConverter.java | 40 ++++++ .../converters/BooleanConverterTest.java | 131 +++++++++++++++++++ .../internal/converters/ByteConverterTest.java | 56 ++++++++ .../ConverterTestsPropertySource.java | 112 ++++++++++++++++ .../org.apache.tamaya.spi.PropertySource | 3 +- 23 files changed, 1069 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java index bb3757d..7c3ee83 100644 --- a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java +++ b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java @@ -26,12 +26,12 @@ package org.apache.tamaya.spi; */ public interface PropertyConverter<T>{ -// /** -// * Access the target type, this converter is producing. This is necessary to determine which converters are -// * to be used for converting a possible value. -// * @return the target type returned by a converter instance, never null. -// */ -// Class<T> getTargetType(); + /** + * Access the target type, this converter is producing. This is necessary to determine which converters are + * to be used for converting a possible value. + * @return the target type returned by a converter instance, never null. + */ + Class<T> getTargetType(); /** * Convert the given configuration keys from it's String representation into the required target type. @@ -45,4 +45,5 @@ public interface PropertyConverter<T>{ //X This could be useful if e.g. no converter in the chain felt responsible //X because a wrongly formatted configuration string had been used. //X This could probably also be handled via an additional Annotation on the converter. + //X Collection<String> getSupportedFormats(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java index f755d0a..58aad71 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java @@ -20,15 +20,8 @@ package org.apache.tamaya.core.internal; import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; -import java.util.Currency; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -39,7 +32,9 @@ import java.util.concurrent.locks.StampedLock; import java.util.logging.Logger; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.core.internal.converters.EnumConverter; import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.ServiceContext; /** * Manager that deals with {@link org.apache.tamaya.spi.PropertyConverter} instances. @@ -57,43 +52,16 @@ public class PropertyConverterManager { * Constructor. */ public PropertyConverterManager() { - initDefaultConverters(); + initConverters(); } - private static final PropertyConverter<Character> CHAR_CONVERTER = - (s) -> Objects.requireNonNull(s,CHAR_NULL_ERROR).charAt(0); - /** * Registers the default converters provided out of the box. */ - protected void initDefaultConverters() { - // Add default converters - register(char.class, CHAR_CONVERTER); - register(byte.class, Byte::parseByte); - register(short.class, Short::parseShort); - register(int.class, Integer::parseInt); - register(long.class, Long::parseLong); - register(boolean.class, Boolean::parseBoolean); - register(float.class, Float::parseFloat); //X TODO not good enough as this is Locale dependent! - register(double.class, Double::parseDouble); //X TODO not good enough as this is Locale dependent! - - register(Character.class, CHAR_CONVERTER); - register(Byte.class, Byte::valueOf); - register(Short.class, Short::valueOf); - register(Integer.class, Integer::valueOf); - register(Long.class, Long::valueOf); - register(Boolean.class, Boolean::valueOf); - register(Float.class, Float::valueOf); //X TODO not good enough as this is Locale dependent! - register(Double.class, Double::valueOf); //X TODO not good enough as this is Locale dependent! - register(BigDecimal.class, BigDecimal::new); //X TODO not good enough as this is Locale dependent! - register(BigInteger.class, BigInteger::new); //X TODO not good enough as this is Locale dependent! - - register(Currency.class, Currency::getInstance); - - register(LocalDate.class, LocalDate::parse); - register(LocalTime.class, LocalTime::parse); - register(LocalDateTime.class, LocalDateTime::parse); - register(ZoneId.class, ZoneId::of); + protected void initConverters() { + for(PropertyConverter conv: ServiceContext.getInstance().getServices(PropertyConverter.class)){ + register(conv.getTargetType(), conv); + } } /** @@ -194,27 +162,46 @@ public class PropertyConverterManager { * @return a new converter, or null. */ protected <T> PropertyConverter<T> createDefaultPropertyConverter(Class<T> targetType) { + if(Enum.class.isAssignableFrom(targetType)){ + return new EnumConverter<T>(targetType); + } PropertyConverter<T> converter = null; Method factoryMethod = getFactoryMethod(targetType, "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse"); if (factoryMethod != null) { - converter = (s) -> { - try { - factoryMethod.setAccessible(true); - return targetType.cast(factoryMethod.invoke(s)); - } catch (Exception e) { - throw new ConfigException("Failed to decode '" + s + "'", e); + converter = new PropertyConverter<T>() { + @Override + public Class<T> getTargetType() { + return targetType; + } + + @Override + public T convert(String value) { + try { + factoryMethod.setAccessible(true); + return targetType.cast(factoryMethod.invoke(value)); + } catch (Exception e) { + throw new ConfigException("Failed to decode '" + value + "'", e); + } } }; } if (converter == null) { try { Constructor<T> constr = targetType.getDeclaredConstructor(String.class); - converter = (s) -> { - try { - constr.setAccessible(true); - return constr.newInstance(s); - } catch (Exception e) { - throw new ConfigException("Failed to decode '" + s + "'", e); + converter = new PropertyConverter<T>() { + @Override + public Class<T> getTargetType() { + return targetType; + } + + @Override + public T convert(String value) { + try { + constr.setAccessible(true); + return constr.newInstance(value); + } catch (Exception e) { + throw new ConfigException("Failed to decode '" + value + "'", e); + } } }; } catch (Exception e) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java new file mode 100644 index 0000000..ab4387b --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java @@ -0,0 +1,41 @@ +/* + * 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.PropertyConverter; + +import java.math.BigDecimal; +import java.util.Objects; + +/** + * Converter, converting from String to BigDecimal. + * //X TODO not good enough as this is Locale dependent! + */ +public class BigDecimalConverter implements PropertyConverter<BigDecimal>{ + @Override + public Class<BigDecimal> getTargetType() { + return BigDecimal.class; + } + + @Override + public BigDecimal convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return new BigDecimal(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java new file mode 100644 index 0000000..4fc9c01 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java @@ -0,0 +1,42 @@ +/* + * 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.PropertyConverter; + +import java.math.BigInteger; +import java.util.Objects; + +/** + * Converter, converting from String to BigInteger. + * //X TODO not good enough as this is Locale dependent! + */ +public class BigIntegerConverter implements PropertyConverter<BigInteger>{ + @Override + public Class<BigInteger> getTargetType() { + return BigInteger.class; + } + + @Override + public BigInteger convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return new BigInteger(trimmed); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java new file mode 100644 index 0000000..47e53ea --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java @@ -0,0 +1,56 @@ +/* + * 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.PropertyConverter; + +import java.util.logging.Logger; + +/** + * Converter, converting from String to Short. + */ +public class BooleanConverter implements PropertyConverter<Boolean> { + + private Logger LOG = Logger.getLogger(getClass().getName()); + + @Override + public Class<Boolean> getTargetType() { + return Boolean.class; + } + + @Override + public Boolean convert(String value) { + String ignoreCaseValue = value.toLowerCase(); + switch(ignoreCaseValue) { + case "yes": + case "y": + case "true": + case "t": + return Boolean.TRUE; + case "no": + case "n": + case "false": + case "f": + return Boolean.FALSE; + default: + LOG.warning("Unknown boolean value encountered: " + value); + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java new file mode 100644 index 0000000..64de0f5 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java @@ -0,0 +1,39 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Byte. + */ +public class ByteConverter implements PropertyConverter<Byte>{ + @Override + public Class<Byte> getTargetType() { + return Byte.class; + } + + @Override + public Byte convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return Byte.decode(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java new file mode 100644 index 0000000..667ede3 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java @@ -0,0 +1,42 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Character. + */ +public class CharConverter implements PropertyConverter<Character>{ + @Override + public Class<Character> getTargetType() { + return Character.class; + } + + @Override + public Character convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + if(trimmed.length()!=1){ + return null; + } + return Character.valueOf(value.charAt(0)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java new file mode 100644 index 0000000..6cd5c67 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.util.Currency; +import java.util.Objects; + +/** + * Converter, converting from String to Currency. + */ +public class CurrencyConverter implements PropertyConverter<Currency>{ + @Override + public Class<Currency> getTargetType() { + return Currency.class; + } + + @Override + public Currency convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return Currency.getInstance(trimmed.toUpperCase()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java new file mode 100644 index 0000000..91c6ac8 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Double. + */ +public class DoubleConverter implements PropertyConverter<Double>{ + @Override + public Class<Double> getTargetType() { + return Double.class; + } + + @Override + public Double convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + //X TODO not good enough as this is Locale dependent! + return Double.valueOf(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java new file mode 100644 index 0000000..0440ef2 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.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.PropertyConverter; + +import java.lang.reflect.Method; +import java.util.Objects; +import java.util.logging.Logger; + +/** + * Converter, converting from String to tge given enum type. + */ +public class EnumConverter<T> implements PropertyConverter<T> { + private 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 Class<T> getTargetType() { + return enumType; + } + + @Override + public T convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + try { + Method m = enumType.getMethod("valueOf", String.class); + return (T) factory.invoke(null, value); + } catch (Exception e) { + throw new ConfigException("Invalid enum value '" + value + "' for " + enumType.getName()); + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java new file mode 100644 index 0000000..e5341b8 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Float. + */ +public class FloatConverter implements PropertyConverter<Float>{ + @Override + public Class<Float> getTargetType() { + return Float.class; + } + + @Override + public Float convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + //X TODO not good enough as this is Locale dependent! + return Float.valueOf(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java new file mode 100644 index 0000000..772baad --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java @@ -0,0 +1,39 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Integer. + */ +public class IntegerConverter implements PropertyConverter<Integer>{ + @Override + public Class<Integer> getTargetType() { + return Integer.class; + } + + @Override + public Integer convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return Integer.decode(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java new file mode 100644 index 0000000..3b57768 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.time.LocalDate; +import java.util.Objects; + +/** + * Converter, converting from String to LocalDate. + */ +public class LocalDateConverter implements PropertyConverter<LocalDate>{ + @Override + public Class<LocalDate> getTargetType() { + return LocalDate.class; + } + + @Override + public LocalDate convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return LocalDate.parse(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java new file mode 100644 index 0000000..e05cbb8 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.time.LocalDateTime; +import java.util.Objects; + +/** + * Converter, converting from String to LocalDateTime. + */ +public class LocalDateTimeConverter implements PropertyConverter<LocalDateTime>{ + @Override + public Class<LocalDateTime> getTargetType() { + return LocalDateTime.class; + } + + @Override + public LocalDateTime convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return LocalDateTime.parse(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java new file mode 100644 index 0000000..b2080ce --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.time.LocalTime; +import java.util.Objects; + +/** + * Converter, converting from String to LocalTime. + */ +public class LocalTimeConverter implements PropertyConverter<LocalTime>{ + @Override + public Class<LocalTime> getTargetType() { + return LocalTime.class; + } + + @Override + public LocalTime convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return LocalTime.parse(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java new file mode 100644 index 0000000..dbfe332 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java @@ -0,0 +1,39 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Long. + */ +public class LongConverter implements PropertyConverter<Long>{ + @Override + public Class<Long> getTargetType() { + return Long.class; + } + + @Override + public Long convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return Long.decode(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java new file mode 100644 index 0000000..543988b --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java @@ -0,0 +1,41 @@ +/* + * 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.PropertyConverter; + +import java.math.BigDecimal; +import java.util.Objects; + +/** + * Converter, converting from String to BigDecimal. + * //X TODO not good enough as this is Locale dependent! + */ +public class NumberConverter implements PropertyConverter<Number>{ + @Override + public Class<Number> getTargetType() { + return Number.class; + } + + @Override + public Number convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return new BigDecimal(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java new file mode 100644 index 0000000..5e12702 --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java @@ -0,0 +1,39 @@ +/* + * 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.PropertyConverter; + +import java.util.Objects; + +/** + * Converter, converting from String to Short. + */ +public class ShortConverter implements PropertyConverter<Short>{ + @Override + public Class<Short> getTargetType() { + return Short.class; + } + + @Override + public Short convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return Short.decode(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java new file mode 100644 index 0000000..eeeb84d --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java @@ -0,0 +1,40 @@ +/* + * 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.PropertyConverter; + +import java.time.ZoneId; +import java.util.Objects; + +/** + * Converter, converting from String to LocalDate. + */ +public class ZoneIdConverter implements PropertyConverter<ZoneId>{ + @Override + public Class<ZoneId> getTargetType() { + return ZoneId.class; + } + + @Override + public ZoneId convert(String value) { + String trimmed = Objects.requireNonNull(value).trim(); + return ZoneId.of(trimmed); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java new file mode 100644 index 0000000..ba4279f --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java @@ -0,0 +1,131 @@ +/* + * 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.Configuration; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Tests the default converter for bytes. + */ +public class BooleanConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte() throws Exception { + Configuration config = Configuration.current(); + // trues + Optional<Boolean> valueRead = config.getOptional("tests.converter.boolean.y1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.y2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.yes1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.yes2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.yes3", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.true1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.true2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.true3", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.t1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.t2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertTrue(valueRead.get().booleanValue()); + // falses + valueRead = config.getOptional("tests.converter.boolean.n1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.n2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.no1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.no2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.no3", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.false1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.false2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.false3", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.f1", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.f2", Boolean.class); + assertTrue(valueRead.isPresent()); + assertFalse(valueRead.get().booleanValue()); + valueRead = config.getOptional("tests.converter.boolean.foo", Boolean.class); + assertFalse(valueRead.isPresent()); + } + /* + + case "tests.converter.boolean.n1": + return "n"; + case "tests.converter.boolean.n2": + return "N"; + case "tests.converter.boolean.no1": + return "no"; + case "tests.converter.boolean.no2": + return "No"; + case "tests.converter.boolean.no3": + return "nO"; + case "tests.converter.boolean.false1": + return "false"; + case "tests.converter.boolean.false2": + return "False"; + case "tests.converter.boolean.false3": + return "falSe"; + case "tests.converter.boolean.f1": + return "f"; + case "tests.converter.boolean.f2": + return "F"; + */ +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java new file mode 100644 index 0000000..5bdee37 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java @@ -0,0 +1,56 @@ +/* + * 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.Configuration; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for bytes. + */ +public class ByteConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte() throws Exception { + Configuration config = Configuration.current(); + Optional<Byte> valueRead = config.getOptional("tests.converter.byte.decimal", Byte.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().byteValue(), 101); + valueRead = config.getOptional("tests.converter.byte.octal", Byte.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().byteValue(), Byte.decode("02").byteValue()); + valueRead = config.getOptional("tests.converter.byte.hex.lowerX", Byte.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().byteValue(), Byte.decode("0x2F").byteValue()); + valueRead = config.getOptional("tests.converter.byte.hex.upperX", Byte.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().byteValue(), Byte.decode("0X3F").byteValue()); + valueRead = config.getOptional("tests.converter.byte.foo", Byte.class); + assertFalse(valueRead.isPresent()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java new file mode 100644 index 0000000..618a8d6 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java @@ -0,0 +1,112 @@ +/* + * 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.PropertySource; + +import java.util.Collections; +import java.util.Map; + +/** + * Test Property Source used by converter tests. + */ +public class ConverterTestsPropertySource implements PropertySource{ + @Override + public int getOrdinal() { + return 0; + } + + @Override + public String get(String key) { + switch(key){ + // Bytes + case "tests.converter.byte.decimal": + return "101"; + case "tests.converter.byte.octal": + return "02"; + case "tests.converter.byte.hex.lowerX": + return "0x2F"; + case "tests.converter.byte.hex.upperX": + return "0X3F"; + // Boolean + case "tests.converter.boolean.y1": + return "y"; + case "tests.converter.boolean.y2": + return "Y"; + case "tests.converter.boolean.yes1": + return "yes"; + case "tests.converter.boolean.yes2": + return "Yes"; + case "tests.converter.boolean.yes3": + return "yeS"; + case "tests.converter.boolean.true1": + return "true"; + case "tests.converter.boolean.true2": + return "True"; + case "tests.converter.boolean.true3": + return "trUe"; + case "tests.converter.boolean.t1": + return "t"; + case "tests.converter.boolean.t2": + return "T"; + case "tests.converter.boolean.n1": + return "n"; + case "tests.converter.boolean.n2": + return "N"; + case "tests.converter.boolean.no1": + return "no"; + case "tests.converter.boolean.no2": + return "No"; + case "tests.converter.boolean.no3": + return "nO"; + case "tests.converter.boolean.false1": + return "false"; + case "tests.converter.boolean.false2": + return "False"; + case "tests.converter.boolean.false3": + return "falSe"; + case "tests.converter.boolean.f1": + return "f"; + case "tests.converter.boolean.f2": + return "F"; + } + return null; + } + + /* + case "yes": + case "y": + case "true": + case "t": + return Boolean.TRUE; + case "no": + case "n": + case "false": + case "f": + */ + @Override + public Map<String, String> getProperties() { + return Collections.emptyMap(); + } + + @Override + public boolean isScannable() { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/578c873d/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource index 1effc9e..72dd3c3 100644 --- a/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource +++ b/java8/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -18,4 +18,5 @@ # org.apache.tamaya.core.testdata.TestPropertyDefaultSource org.apache.tamaya.core.propertysource.SystemPropertySource -org.apache.tamaya.core.propertysource.EnvironmentPropertySource \ No newline at end of file +org.apache.tamaya.core.propertysource.EnvironmentPropertySource +org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource \ No newline at end of file
