Repository: incubator-tamaya Updated Branches: refs/heads/master 1a3ecf119 -> de354a1eb
TAMAYA-63: Aligned implementations for CharConverter, CurrencyConverter. 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/3e8d8deb Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3e8d8deb Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3e8d8deb Branch: refs/heads/master Commit: 3e8d8deb9e1f69203a6a9aa49bb909d2b2030e76 Parents: 1a3ecf1 Author: anatole <[email protected]> Authored: Mon Jan 26 11:50:29 2015 +0100 Committer: anatole <[email protected]> Committed: Mon Jan 26 12:13:56 2015 +0100 ---------------------------------------------------------------------- .../core/internal/converters/CharConverter.java | 26 +++- .../internal/converters/CurrencyConverter.java | 49 +++++- .../internal/converters/CharConverterTest.java | 88 +++++++++++ .../ConverterTestsPropertySource.java | 50 ++++-- .../converters/CurrencyConverterTest.java | 154 ++++++++++++++++++ .../core/internal/converters/CharConverter.java | 25 ++- .../internal/converters/CurrencyConverter.java | 49 +++++- .../internal/converters/ByteConverterTest.java | 38 ++++- .../internal/converters/CharConverterTest.java | 89 +++++++++++ .../ConverterTestsPropertySource.java | 38 +++++ .../converters/CurrencyConverterTest.java | 155 +++++++++++++++++++ 11 files changed, 740 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java index f3773bb..a2c718f 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java @@ -21,18 +21,40 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; import java.util.Objects; +import java.util.logging.Logger; /** * Converter, converting from String to Character. */ public class CharConverter implements PropertyConverter<Character>{ + private static final Logger LOG = Logger.getLogger(CharConverter.class.getName()); + @Override public Character convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - if(trimmed.length()!=1){ + if(trimmed.isEmpty()){ return null; } - return Character.valueOf(value.charAt(0)); + if(trimmed.startsWith("'")) { + try { + trimmed = trimmed.substring(1, trimmed.length() - 1); + if (trimmed.isEmpty()) { + return null; + } + return trimmed.charAt(0); + } catch (Exception e) { + LOG.warning("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); + return null; + } + } + try { + Integer val = Integer.parseInt(trimmed); + return (char) val.shortValue(); + } catch (Exception e) { + LOG.finest("Character format is not numeric: '" + value + "', using first character."); + return trimmed.charAt(0); + } } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java index 4d033eb..c0920b8 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java @@ -23,15 +23,62 @@ import org.apache.tamaya.PropertyConverter; import java.util.Currency; import java.util.Locale; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Converter, converting from String to Currency. */ public class CurrencyConverter implements PropertyConverter<Currency>{ + private static final Logger LOG = Logger.getLogger(CurrencyConverter.class.getName()); + @Override public Currency convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + try{ + return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + } + catch(Exception e){ + LOG.log(Level.INFO, "Not a valid textual currency code: " + trimmed +", checking for numeric...", e); + } + try { + // Check for numeric code + Integer numCode = Integer.parseInt(trimmed); + for (Currency currency : Currency.getAvailableCurrencies()) { + if (currency.getNumericCode() == numCode) { + return currency; + } + } + } + catch(Exception e){ + LOG.log(Level.INFO, "Not a valid numeric currency code: " + trimmed +", checking for locale...", e); + } + try { + // Check for numeric code + String[] parts = trimmed.split("\\_"); + Locale locale; + switch(parts.length){ + case 1: + locale = new Locale("", parts[0]); + break; + case 2: + locale = new Locale(parts[0], parts[1]); + break; + case 3: + locale = new Locale(parts[0], parts[1], parts[2]); + break; + default: + locale = null; + } + if(locale!=null){ + return Currency.getInstance(locale); + } + LOG.info("Not a valid currency: " + trimmed +", giving up..."); + } + catch(Exception e){ + LOG.log(Level.INFO, "Not a valid country locale for currency: " + trimmed +", giving up...", e); + } + return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java new file mode 100644 index 0000000..6ae1222 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java @@ -0,0 +1,88 @@ +/* + * 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.util.Optional; + +import static org.junit.Assert.*; + +/** + * Tests conversion of the {@link CharConverter}. + */ +public class CharConverterTest { + + @Test + public void testConvert_Character() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_Numeric() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-numeric", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), (char)101); + } + + @Test + public void testConvert_Character_Quoted() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.d", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), 'd'); + } + + @Test + public void testConvert_Character_WithWhitspace_Before() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-before", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitspace_After() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-after", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitspace_Around() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.f-around", Character.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.charValue(), 'f'); + } + + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Character valueRead = config.get("tests.converter.char.foo", Character.class); + assertNull(valueRead); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java index ec7c01f..918b97f 100644 --- a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java @@ -90,21 +90,49 @@ public class ConverterTestsPropertySource implements PropertySource{ return "f"; case "tests.converter.boolean.f2": return "F"; + case "tests.converter.char.f": + return "f"; + case "tests.converter.char.d": + return "'d'"; + case "tests.converter.char.f-before": + return " f"; + case "tests.converter.char.f-after": + return "f "; + case "tests.converter.char.f-around": + return " f "; + case "tests.converter.char.f-numeric": + return "101"; + case "tests.converter.currency.code1": + return "CHF"; + case "tests.converter.currency.code2": + return "cHf"; + case "tests.converter.currency.code3": + return " CHF"; + case "tests.converter.currency.code4": + return "CHF "; + case "tests.converter.currency.code5": + return " CHF "; + case "tests.converter.currency.code-numeric1": + return "100"; + case "tests.converter.currency.code-numeric2": + return " 100"; + case "tests.converter.currency.code-numeric3": + return "100 "; + case "tests.converter.currency.code-numeric4": + return " 100 "; + case "tests.converter.currency.code-locale1": + return "DE"; + case "tests.converter.currency.code-locale2": + return " DE"; + case "tests.converter.currency.code-locale3": + return "DE "; + case "tests.converter.currency.code-locale4": + return " DE "; } 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(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java new file mode 100644 index 0000000..9113ca2 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java @@ -0,0 +1,154 @@ +/* + * 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.util.Currency; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for bytes. + */ +public class CurrencyConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code1", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead, Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_cHf() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code2", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead, Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_Before() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code3", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead, Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_After() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code4", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead, Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_Around() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code5", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead, Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_Numeric() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code-numeric1", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.get("tests.converter.currency.code-numeric2", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.get("tests.converter.currency.code-numeric3", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.get("tests.converter.currency.code-numeric4", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_Locale() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Currency valueRead = config.get("tests.converter.currency.code-locale1", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getCurrencyCode(), "EUR"); + valueRead = config.get("tests.converter.currency.code-locale2", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getCurrencyCode(), "EUR"); + valueRead = config.get("tests.converter.currency.code-locale3", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getCurrencyCode(), "EUR"); + valueRead = config.get("tests.converter.currency.code-locale4", Currency.class); + assertTrue(valueRead != null); + assertEquals(valueRead.getCurrencyCode(), "EUR"); + } + + /** + * 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(); + Byte valueRead = config.get("tests.converter.byte.foo", Byte.class); + assertFalse(valueRead!=null); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/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 f3773bb..5dfea5b 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 @@ -21,18 +21,39 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; import java.util.Objects; +import java.util.logging.Logger; /** * Converter, converting from String to Character. */ public class CharConverter implements PropertyConverter<Character>{ + private static final Logger LOG = Logger.getLogger(CharConverter.class.getName()); + @Override public Character convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - if(trimmed.length()!=1){ + if(trimmed.isEmpty()){ return null; } - return Character.valueOf(value.charAt(0)); + if(trimmed.startsWith("'")) { + try { + trimmed = trimmed.substring(1, trimmed.length() - 1); + if (trimmed.isEmpty()) { + return null; + } + return trimmed.charAt(0); + } catch (Exception e) { + LOG.warning("Invalid character format encountered: '" + value + "', valid formats are 'a', 101 and a."); + return null; + } + } + try { + Integer val = Integer.parseInt(trimmed); + return (char) val.shortValue(); + } catch (Exception e) { + LOG.finest("Character format is not numeric: '" + value + "', using first character."); + return trimmed.charAt(0); + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/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 4d033eb..59f9358 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 @@ -23,15 +23,62 @@ import org.apache.tamaya.PropertyConverter; import java.util.Currency; import java.util.Locale; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Converter, converting from String to Currency. */ public class CurrencyConverter implements PropertyConverter<Currency>{ + private static final Logger LOG = Logger.getLogger(CurrencyConverter.class.getName()); + @Override public Currency convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + try{ + return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH)); + } + catch(Exception e){ + LOG.log(Level.INFO, e, () -> "Not a valid textual currency code: " + trimmed +", checking for numeric..."); + } + try { + // Check for numeric code + Integer numCode = Integer.parseInt(trimmed); + for (Currency currency : Currency.getAvailableCurrencies()) { + if (currency.getNumericCode() == numCode) { + return currency; + } + } + } + catch(Exception e){ + LOG.log(Level.INFO, e, () -> "Not a valid numeric currency code: " + trimmed +", checking for locale..."); + } + try { + // Check for numeric code + String[] parts = trimmed.split("\\_"); + Locale locale; + switch(parts.length){ + case 1: + locale = new Locale("", parts[0]); + break; + case 2: + locale = new Locale(parts[0], parts[1]); + break; + case 3: + locale = new Locale(parts[0], parts[1], parts[2]); + break; + default: + locale = null; + } + if(locale!=null){ + return Currency.getInstance(locale); + } + LOG.info(() -> "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..."); + } + return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/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 index fd16d8b..e82b355 100644 --- 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 @@ -37,21 +37,51 @@ public class ByteConverterTest { * @throws Exception */ @Test - public void testConvert_Byte() throws Exception { + public void testConvert_Byte_Decimal() throws Exception { Configuration config = ConfigurationProvider.getConfiguration(); 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); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_Octal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Byte> 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); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Byte> 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); + } + + /** + * 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<Byte> 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/3e8d8deb/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java new file mode 100644 index 0000000..334f89e --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java @@ -0,0 +1,89 @@ +/* + * 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.util.Optional; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +/** + * Tests conversion of the {@link org.apache.tamaya.core.internal.converters.CharConverter}. + */ +public class CharConverterTest { + + @Test + public void testConvert_Character() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.f", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), 'f'); + } + + @Test + public void testConvert_Character_Numeric() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.f-numeric", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), (char)101); + } + + @Test + public void testConvert_Character_Quoted() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.d", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), 'd'); + } + + @Test + public void testConvert_Character_WithWhitspace_Before() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.f-before", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitspace_After() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.f-after", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), 'f'); + } + + @Test + public void testConvert_Character_WithWhitspace_Around() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.f-around", Character.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().charValue(), 'f'); + } + + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Character> valueRead = config.getOptional("tests.converter.char.foo", Character.class); + assertFalse(valueRead.isPresent()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/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 index 618a8d6..3dac272 100644 --- 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 @@ -85,6 +85,44 @@ public class ConverterTestsPropertySource implements PropertySource{ return "f"; case "tests.converter.boolean.f2": return "F"; + case "tests.converter.char.f": + return "f"; + case "tests.converter.char.d": + return "'d'"; + case "tests.converter.char.f-before": + return " f"; + case "tests.converter.char.f-after": + return "f "; + case "tests.converter.char.f-around": + return " f "; + case "tests.converter.char.f-numeric": + return "101"; + case "tests.converter.currency.code1": + return "CHF"; + case "tests.converter.currency.code2": + return "cHf"; + case "tests.converter.currency.code3": + return " CHF"; + case "tests.converter.currency.code4": + return "CHF "; + case "tests.converter.currency.code5": + return " CHF "; + case "tests.converter.currency.code-numeric1": + return "100"; + case "tests.converter.currency.code-numeric2": + return " 100"; + case "tests.converter.currency.code-numeric3": + return "100 "; + case "tests.converter.currency.code-numeric4": + return " 100 "; + case "tests.converter.currency.code-locale1": + return "DE"; + case "tests.converter.currency.code-locale2": + return " DE"; + case "tests.converter.currency.code-locale3": + return "DE "; + case "tests.converter.currency.code-locale4": + return " DE "; } return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3e8d8deb/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java new file mode 100644 index 0000000..4c498f6 --- /dev/null +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java @@ -0,0 +1,155 @@ +/* + * 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.util.Currency; +import java.util.Optional; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for bytes. + */ +public class CurrencyConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code1", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_cHf() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code2", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_Before() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code3", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_After() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code4", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_CHF_Whitespace_Around() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code5", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get(), Currency.getInstance("CHF")); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_Numeric() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code-numeric1", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.getOptional("tests.converter.currency.code-numeric2", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.getOptional("tests.converter.currency.code-numeric3", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + valueRead = config.getOptional("tests.converter.currency.code-numeric4", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getNumericCode(), Currency.getInstance("BGL").getNumericCode()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Currency_Code_Locale() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Currency> valueRead = config.getOptional("tests.converter.currency.code-locale1", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getCurrencyCode(), "EUR"); + valueRead = config.getOptional("tests.converter.currency.code-locale2", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getCurrencyCode(), "EUR"); + valueRead = config.getOptional("tests.converter.currency.code-locale3", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getCurrencyCode(), "EUR"); + valueRead = config.getOptional("tests.converter.currency.code-locale4", Currency.class); + assertTrue(valueRead.isPresent()); + assertEquals(valueRead.get().getCurrencyCode(), "EUR"); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Optional<Byte> valueRead = config.getOptional("tests.converter.byte.foo", Byte.class); + assertFalse(valueRead.isPresent()); + } +} \ No newline at end of file
