TAMAYA-63: Improved/unified implementations for various converters. Aligned tests for Java 7/8.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/62518be0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/62518be0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/62518be0 Branch: refs/heads/master Commit: 62518be0b97230f773a6d01b7f2ca7440a0d05eb Parents: 2d80d49 Author: anatole <anat...@apache.org> Authored: Thu Feb 12 22:26:59 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Thu Feb 12 22:32:25 2015 +0100 ---------------------------------------------------------------------- .../converters/BigIntegerConverter.java | 8 +- .../internal/converters/BooleanConverter.java | 3 +- .../core/internal/converters/ByteConverter.java | 11 +- .../core/internal/converters/CharConverter.java | 2 +- .../internal/converters/CurrencyConverter.java | 16 ++- .../internal/converters/DoubleConverter.java | 13 ++- .../core/internal/converters/EnumConverter.java | 9 +- .../internal/converters/FloatConverter.java | 16 +-- .../internal/converters/IntegerConverter.java | 14 ++- .../core/internal/converters/LongConverter.java | 39 ++++--- .../internal/converters/NumberConverter.java | 40 ++++++- .../internal/converters/ShortConverter.java | 12 ++- .../internal/converters/ZoneIdConverter.java | 14 ++- .../converters/NumberConverterTest.java | 104 +++++++++++++++++++ 14 files changed, 249 insertions(+), 52 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 078ee15..1f174ba 100644 --- 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 @@ -47,12 +47,12 @@ public class BigIntegerConverter implements PropertyConverter<BigInteger>{ public BigInteger convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){ - LOG.finer(() -> "Parsing Hex value to BigInteger: " + value); + LOG.finest(() -> "Parsing Hex value to BigInteger: " + value); trimmed = trimmed.substring(2); StringBuilder decimal = new StringBuilder(); for(int offset = 0;offset < trimmed.length();offset+=2){ if(offset==trimmed.length()-1){ - LOG.info(() -> "Invalid Hex-Byte-String: " + value); + LOG.finest(() -> "Invalid Hex-Byte-String: " + value); return null; } byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); @@ -64,12 +64,12 @@ public class BigIntegerConverter implements PropertyConverter<BigInteger>{ } return new BigInteger(decimal.toString()); } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){ - LOG.finer(() -> "Parsing Hex value to BigInteger: " + value); + LOG.finest(() -> "Parsing Hex value to BigInteger: " + value); trimmed = trimmed.substring(3); StringBuilder decimal = new StringBuilder(); for(int offset = 0;offset < trimmed.length();offset+=2){ if(offset==trimmed.length()-1){ - LOG.info("Invalid Hex-Byte-String: " + trimmed); + LOG.finest("Invalid Hex-Byte-String: " + trimmed); return null; } byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 5e1dabe..da8db3d 100644 --- 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 @@ -44,7 +44,6 @@ public class BooleanConverter implements PropertyConverter<Boolean> { String ignoreCaseValue = Objects.requireNonNull(value) .trim() .toLowerCase(Locale.ENGLISH); - switch(ignoreCaseValue) { case "yes": case "y": @@ -59,7 +58,7 @@ public class BooleanConverter implements PropertyConverter<Boolean> { case "0": return Boolean.FALSE; default: - LOG.warning("Unknown boolean value encountered: " + value); + LOG.finest("Unknown boolean value encountered: " + value); return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index f485c06..8c4257d 100644 --- 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 @@ -22,6 +22,7 @@ import org.apache.tamaya.PropertyConverter; import java.util.Locale; import java.util.Objects; +import java.util.logging.Logger; /** * Converter, converting from String to Byte, the supported format is one of the following: @@ -35,6 +36,8 @@ import java.util.Objects; */ public class ByteConverter implements PropertyConverter<Byte>{ + private Logger LOG = Logger.getLogger(getClass().getName()); + @Override public Byte convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); @@ -46,7 +49,13 @@ public class ByteConverter implements PropertyConverter<Byte>{ case "MAX": return Byte.MAX_VALUE; default: - return Byte.decode(trimmed); + try{ + return Byte.decode(trimmed); + } + catch(Exception e){ + LOG.finest("Failed to parse Byte value: " + value); + return null; + } } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index c3e3004..3af8bb2 100644 --- 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 @@ -51,7 +51,7 @@ public class CharConverter implements PropertyConverter<Character>{ } return trimmed.charAt(0); } catch (Exception e) { - LOG.warning("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); + LOG.finest("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 05d460d..c024158 100644 --- 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 @@ -37,9 +37,15 @@ public class CurrencyConverter implements PropertyConverter<Currency> { public Currency convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); try { - return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + return Currency.getInstance(trimmed); } catch (Exception e) { - LOG.log(Level.INFO, e, () -> "Not a valid textual currency code: " + trimmed + ", checking for numeric..."); + // try converting to upper case, works for all default ISO currencies. + try { + return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + } catch (Exception e2) { + LOG.finest(() -> "Not a valid textual currency code: " + trimmed + + ", checking for numeric code..."); + } } try { // Check for numeric code @@ -50,7 +56,7 @@ public class CurrencyConverter implements PropertyConverter<Currency> { } } } catch (Exception e) { - LOG.log(Level.INFO, e, () -> "Not a valid numeric currency code: " + trimmed + ", checking for locale..."); + LOG.finest(() -> "Not a valid numeric currency code: " + trimmed + ", checking for locale..."); } try { // Check for numeric code @@ -72,9 +78,9 @@ public class CurrencyConverter implements PropertyConverter<Currency> { if (locale != null) { return Currency.getInstance(locale); } - LOG.info(() -> "Not a valid currency: " + trimmed + ", giving up..."); + LOG.finest(() -> "Not a valid currency: " + trimmed + ", giving up..."); } catch (Exception e) { - LOG.log(Level.INFO, e, () -> "Not a valid country locale for currency: " + trimmed + ", giving up..."); + LOG.log(Level.FINEST, e, () -> "Not a valid country locale for currency: " + trimmed + ", giving up..."); } return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 76e6e32..604e90b 100644 --- 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 @@ -57,15 +57,14 @@ public class DoubleConverter implements PropertyConverter<Double>{ return Double.valueOf(trimmed); } catch(Exception e){ // OK perhaps we have an integral number that must be converted to the double type... - LOG.log(Level.FINER, e, () -> "Parsing of double as floating number failed, trying parsing integral" + - " number instead..."); + LOG.log(Level.FINEST, e, () -> "Parsing of double as floating number failed, trying parsing " + + "integral/hex number instead..."); } - try{ - return integerConverter.convert(trimmed).doubleValue(); - } catch(Exception e){ - LOG.log(Level.INFO, e, () -> "Unexpected error from LongConverter for " + trimmed); - return null; + Long val = integerConverter.convert(trimmed); + if(val!=null){ + return val.doubleValue(); } + return null; } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 05f29ad..172a6c4 100644 --- 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 @@ -24,6 +24,7 @@ import org.apache.tamaya.PropertyConverter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Objects; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -53,7 +54,13 @@ public class EnumConverter<T> implements PropertyConverter<T> { try { return (T) factory.invoke(null, value); } catch (InvocationTargetException | IllegalAccessException e) { - throw new ConfigException("Invalid enum value '" + value + "' for " + enumType.getName(), e); + LOG.log(Level.FINEST, e, () -> "Invalid enum value '" + value + "' for " + enumType.getName()); } + try { + return (T) factory.invoke(null, value); + } catch (InvocationTargetException | IllegalAccessException e) { + LOG.log(Level.FINEST, e, () -> "Invalid enum value '" + value + "' for " + enumType.getName()); + } + return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 5f97eca..df202b5 100644 --- 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 @@ -34,7 +34,7 @@ public class FloatConverter implements PropertyConverter<Float> { /** * The logger. */ - private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName()); + private static final Logger LOG = Logger.getLogger(FloatConverter.class.getName()); /** * The converter used, when floating point parse failed. */ @@ -61,15 +61,15 @@ public class FloatConverter implements PropertyConverter<Float> { return Float.valueOf(trimmed); } catch(Exception e){ // OK perhaps we have an integral number that must be converted to the double type... - LOG.log(Level.FINER, e, () -> "Parsing of double as floating number failed, trying parsing integral" + - " number instead..."); + LOG.finest(() -> "Parsing of float as floating number failed, trying parsing integral" + + " number/hex instead..."); } - try{ - return integerConverter.convert(trimmed).floatValue(); - } catch(Exception e){ - LOG.log(Level.INFO, e, () -> "Unexpected error from LongConverter for " + trimmed); - return null; + Integer val = integerConverter.convert(trimmed); + if(val!=null) { + return val.floatValue(); } + LOG.finest(() -> "Unparseable float value: " + trimmed); + return null; } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 0a20e63..6a82b6b 100644 --- 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 @@ -22,6 +22,7 @@ import org.apache.tamaya.PropertyConverter; import java.util.Locale; import java.util.Objects; +import java.util.logging.Logger; /** * Converter, converting from String to Integer, the supported format is one of the following: @@ -42,6 +43,11 @@ import java.util.Objects; */ public class IntegerConverter implements PropertyConverter<Integer>{ + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(IntegerConverter.class.getName()); + @Override public Integer convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); @@ -53,7 +59,13 @@ public class IntegerConverter implements PropertyConverter<Integer>{ case "MAX": return Integer.MAX_VALUE; default: - return Integer.decode(trimmed); + try{ + return Integer.decode(trimmed); + } + catch(Exception e){ + LOG.finest(() -> "Unparseable Integer value: " + trimmed); + return null; + } } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index b1ca144..149f66b 100644 --- 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 @@ -22,30 +22,33 @@ import org.apache.tamaya.PropertyConverter; 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> + * <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> */ -public class LongConverter implements PropertyConverter<Long>{ +public class LongConverter implements PropertyConverter<Long> { + + private static final Logger LOGGER = Logger.getLogger(LongConverter.class.getName()); @Override public Long convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - switch(trimmed.toUpperCase(Locale.ENGLISH)){ + switch (trimmed.toUpperCase(Locale.ENGLISH)) { case "MIN_VALUE": case "MIN": return Long.MIN_VALUE; @@ -53,7 +56,13 @@ public class LongConverter implements PropertyConverter<Long>{ case "MAX": return Long.MAX_VALUE; default: - return Long.decode(trimmed); + try { + return Long.decode(trimmed); + } catch (Exception e) { + LOGGER.finest("Unable to parse Long value: " + value); + return null; + } } } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 3fdc68d..da73341 100644 --- 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 @@ -21,17 +21,49 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; import java.math.BigDecimal; +import java.util.Locale; import java.util.Objects; +import java.util.logging.Logger; /** - * Converter, converting from String to BigDecimal. - * //X TODO not good enough as this is Locale dependent! + * 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> */ -public class NumberConverter implements PropertyConverter<Number>{ +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 LongConverter longConverter = new LongConverter(); @Override public Number convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return new BigDecimal(trimmed); + 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); + if (lVal != null) { + return lVal; + } + try{ + return new BigDecimal(trimmed); + } + catch(Exception e){ + LOGGER.finest("Unparseable Number: " + trimmed); + return null; + } + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index 6495ccd..88dc0a8 100644 --- 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 @@ -22,6 +22,7 @@ import org.apache.tamaya.PropertyConverter; 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: @@ -42,6 +43,9 @@ import java.util.Objects; */ public class ShortConverter implements PropertyConverter<Short>{ + /** the logger. */ + private static final Logger LOG = Logger.getLogger(ShortConverter.class.getName()); + @Override public Short convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); @@ -53,7 +57,13 @@ public class ShortConverter implements PropertyConverter<Short>{ case "MAX": return Short.MAX_VALUE; default: - return Short.decode(trimmed); + try{ + return Short.decode(trimmed); + } + catch(Exception e){ + LOG.finest(() -> "Unparseable Short: " + trimmed); + return null; + } } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/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 index aa9ee66..0ed4bc9 100644 --- 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 @@ -22,15 +22,25 @@ import org.apache.tamaya.PropertyConverter; import java.time.ZoneId; import java.util.Objects; +import java.util.logging.Logger; /** - * Converter, converting from String to LocalDate. + * Converter, converting from String to ZoneId. Valid inputs are as defined by {@link ZoneId#of(String)}. */ public class ZoneIdConverter implements PropertyConverter<ZoneId>{ + /** the logger. */ + private static final Logger LOG = Logger.getLogger(ShortConverter.class.getName()); + @Override public ZoneId convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return ZoneId.of(trimmed); + try{ + return ZoneId.of(trimmed); + } + catch(Exception e){ + LOG.finest("Unparseable ZoneId: " + trimmed); + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/62518be0/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java new file mode 100644 index 0000000..64c6033 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/NumberConverterTest.java @@ -0,0 +1,104 @@ +/* + * 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.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Optional; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for Number. + */ +public class NumberConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Number> valueRead = config.getOptional("tests.converter.bd.decimal", Number.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Long.valueOf(101)); + } + + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Number> valueRead = config.getOptional("tests.converter.bd.hex.lowerX", Number.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Long.valueOf("47")); + valueRead = config.getOptional("tests.converter.bd.hex.upperX", Number.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Long.valueOf("63")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Number> valueRead = config.getOptional("tests.converter.bd.foo", Number.class); + assertFalse(valueRead.isPresent()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Number> valueRead = config.getOptional("tests.converter.bd.big", Number.class); + assertTrue(valueRead.isPresent()); + assertEquals(new BigDecimal("101666666666666662333337263723628763821638923628193612983618293628763"), + valueRead.get()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigFloatValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Number> valueRead = config.getOptional("tests.converter.bd.bigFloat", Number.class); + assertTrue(valueRead.isPresent()); + assertEquals(new BigDecimal("1016666666666666623333372637236287638216389293628763.1016666666666666623333372" + + "63723628763821638923628193612983618293628763"), valueRead.get()); + } +} \ No newline at end of file