You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2016/07/15 12:04:43 UTC
[17/21] incubator-tamaya git commit: - Minimalized current API for
further discussions.
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/C.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/C.java b/code/core/src/test/java/org/apache/tamaya/core/internal/C.java
deleted file mode 100644
index fdd3476..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/C.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal;
-
-import java.io.IOException;
-import java.nio.CharBuffer;
-
-/**
- * Test class for testing transitively evaluated property converters.
- */
-public class C extends B implements Readable{
-
- private final String inValue;
-
- public C(String inValue){
- this.inValue = inValue;
- }
-
- @Override
- public int read(CharBuffer cb) throws IOException {
- return 0;
- }
-
- /**
- * Returns the input value, set on creation. Used for test assertion.
- * @return the in value.
- */
- public String getInValue() {
- return inValue;
- }
-
- @Override
- public String toString() {
- return "C{" +
- "inValue='" + inValue + '\'' +
- '}';
- }
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java
deleted file mode 100644
index 7ee2a35..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/CTestConverter.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal;
-
-import org.apache.tamaya.spi.ConversionContext;
-import org.apache.tamaya.spi.PropertyConverter;
-
-/**
- * Created by Anatole on 13.06.2015.
- */
-public class CTestConverter implements PropertyConverter<C>{
- @Override
- public C convert(String value, ConversionContext context) {
- return new C(value);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java
deleted file mode 100644
index 9999a8e..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultServiceContextTest.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.spi.ConfigurationProviderSpi;
-import org.junit.Assert;
-import org.junit.Test;
-
-import javax.annotation.Priority;
-import java.util.Collection;
-import java.util.List;
-
-public class DefaultServiceContextTest {
-
- /**
- * context to test
- */
- private final DefaultServiceContext context = new DefaultServiceContext();
-
-
- @Test
- public void testGetService() {
- ConfigurationProviderSpi providerSpi = context.getService(ConfigurationProviderSpi.class);
- Assert.assertNotNull(providerSpi);
- Assert.assertTrue(providerSpi instanceof DefaultConfigurationProvider);
- }
-
- @Test(expected = ConfigException.class)
- public void testGetService_multipleServicesWithoutPriority_shouldThrowConfigException() {
- context.getService(InvalidPriorityInterface.class);
- }
-
- @Test
- public void testGetService_multipleService_shouldReturnServiceWithHighestPriority() {
- MultiImplsInterface service = context.getService(MultiImplsInterface.class);
-
- Assert.assertNotNull(service);
- Assert.assertTrue(service instanceof MultiImpl2);
- }
-
- @Test
- public void testGetService_noImpl_shouldReturnEmptyOpional() {
- NoImplInterface service = context.getService(NoImplInterface.class);
- Assert.assertNull(service);
- }
-
-
- @Test
- public void testGetServices_shouldReturnServices() {
- {
- Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class);
- Assert.assertNotNull(services);
- Assert.assertEquals(2, services.size());
-
- for (InvalidPriorityInterface service : services) {
- Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2);
- }
- }
-
- {
- Collection<MultiImplsInterface> services = context.getServices(MultiImplsInterface.class);
- Assert.assertNotNull(services);
- Assert.assertEquals(3, services.size());
-
- for (MultiImplsInterface service : services) {
- Assert.assertTrue(service instanceof MultiImpl1 ||
- service instanceof MultiImpl2 ||
- service instanceof MultiImpl3);
- }
- }
- }
-
- @Test
- public void testGetServices_redundantAccessToServices() {
- for(int i=0;i<10;i++){
- Collection<InvalidPriorityInterface> services = context.getServices(InvalidPriorityInterface.class);
- Assert.assertNotNull(services);
- Assert.assertEquals(2, services.size());
- for (InvalidPriorityInterface service : services) {
- Assert.assertTrue(service instanceof InvalidPriorityImpl1 || service instanceof InvalidPriorityImpl2);
- }
- }
- }
-
- @Test
- public void testGetServices_noImpl_shouldReturnEmptyList() {
- Collection<NoImplInterface> services = context.getServices(NoImplInterface.class);
- Assert.assertNotNull(services);
- Assert.assertTrue(services.isEmpty());
- }
-
-
- // some test interfaces and classes
-
- public interface InvalidPriorityInterface {
- }
-
- @Priority(value = 50)
- public static class InvalidPriorityImpl1 implements InvalidPriorityInterface {
- }
-
- @Priority(value = 50)
- public static class InvalidPriorityImpl2 implements InvalidPriorityInterface {
- }
-
-
- public interface MultiImplsInterface {
- }
-
- public static class MultiImpl1 implements MultiImplsInterface {
- }
-
- @Priority(value = 500)
- public static class MultiImpl2 implements MultiImplsInterface {
- }
-
- @Priority(value = -10)
- public static class MultiImpl3 implements MultiImplsInterface {
- }
-
- private interface NoImplInterface {
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java
deleted file mode 100644
index ec420f8..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java
+++ /dev/null
@@ -1,179 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal;
-
-
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.spi.ConversionContext;
-import org.apache.tamaya.spi.PropertyConverter;
-import org.apache.tamaya.TypeLiteral;
-import org.junit.Test;
-
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.*;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
-
-public class PropertyConverterManagerTest {
-
- private final ConversionContext DUMMY_CONTEXT = new ConversionContext.Builder(
- "someKey", TypeLiteral.of(Object.class)).build();
-
- @Test
- public void customTypeWithFactoryMethodOfIsRecognizedAsSupported() {
- PropertyConverterManager manager = new PropertyConverterManager();
-
- assertThat(manager.isTargetTypeSupported(TypeLiteral.of(MyType.class)),
- is(true));
- }
-
- @Test
- public void factoryMethodOfIsUsedAsConverter() {
- PropertyConverterManager manager = new PropertyConverterManager();
-
- List<PropertyConverter<MyType>> converters = manager.getPropertyConverters(
- (TypeLiteral)TypeLiteral.of(MyType.class));
-
- assertThat(converters, hasSize(1));
-
- PropertyConverter<MyType> converter = converters.get(0);
-
- Object result = converter.convert("IN", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(MyType.class));
- assertThat(((MyType)result).getValue(), equalTo("IN"));
- }
-
- @Test
- public void testDirectConverterMapping(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<C>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(C.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<C> converter = converters.get(0);
- C result = converter.convert("testDirectConverterMapping", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat((result).getInValue(), equalTo("testDirectConverterMapping"));
- }
-
- @Test
- public void testDirectSuperclassConverterMapping(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class)));
- assertThat(converters, hasSize(1));
- converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<B> converter = converters.get(0);
- B result = converter.convert("testDirectSuperclassConverterMapping", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat(((C)result).getInValue(), equalTo("testDirectSuperclassConverterMapping"));
- }
-
- @Test
- public void testMultipleConverterLoad(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<B>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class)));
- assertThat(converters, hasSize(1));
- manager = new PropertyConverterManager();
- converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(B.class)));
- assertThat(converters, hasSize(1));
- }
-
- @Test
- public void testTransitiveSuperclassConverterMapping(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<A>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(A.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<A> converter = converters.get(0);
- A result = converter.convert("testTransitiveSuperclassConverterMapping", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat(((C)result).getInValue(), equalTo("testTransitiveSuperclassConverterMapping"));
- }
-
- @Test
- public void testDirectInterfaceMapping(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<Readable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Readable.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<Readable> converter = converters.get(0);
- Readable result = converter.convert("testDirectInterfaceMapping", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat(((C)result).getInValue(), equalTo("testDirectInterfaceMapping"));
- }
-
- @Test
- public void testTransitiveInterfaceMapping1(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<Runnable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(Runnable.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<Runnable> converter = converters.get(0);
- Runnable result = converter.convert("testTransitiveInterfaceMapping1", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat(((C)result).getInValue(), equalTo("testTransitiveInterfaceMapping1"));
- }
-
- @Test
- public void testTransitiveInterfaceMapping2(){
- PropertyConverterManager manager = new PropertyConverterManager();
- List<PropertyConverter<AutoCloseable>> converters = List.class.cast(manager.getPropertyConverters(TypeLiteral.of(AutoCloseable.class)));
- assertThat(converters, hasSize(1));
-
- PropertyConverter<AutoCloseable> converter = converters.get(0);
- AutoCloseable result = converter.convert("testTransitiveInterfaceMapping2", DUMMY_CONTEXT);
-
- assertThat(result, notNullValue());
- assertThat(result, instanceOf(C.class));
- assertThat(((C)result).getInValue(), equalTo("testTransitiveInterfaceMapping2"));
- }
-
- public static class MyType {
- private final String typeValue;
-
- private MyType(String value) {
- typeValue = value;
- }
-
- public static MyType of(String source) {
- return new MyType(source);
- }
-
- public String getValue() {
- return typeValue;
- }
-
- }
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java
new file mode 100644
index 0000000..2c17214
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Objects;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to BigDecimal, the supported format is one of the following:
+ * <ul>
+ * <li>232573527352.76352753</li>
+ * <li>-23257352.735276352753</li>
+ * <li>-0xFFFFFF (integral numbers only)</li>
+ * <li>-0XFFFFAC (integral numbers only)</li>
+ * <li>0xFFFFFF (integral numbers only)</li>
+ * <li>0XFFFFAC (integral numbers only)</li>
+ * </ul>
+ */
+public class BigDecimalConverter implements PropertyConverter<BigDecimal>{
+ /** The logger. */
+ private static final Logger LOG = Logger.getLogger(BigDecimalConverter.class.getName());
+ /** Converter to be used if the format is not directly supported by BigDecimal, e.g. for integral hex values. */
+ private final BigIntegerConverter integerConverter = new BigIntegerConverter();
+
+ @Override
+ public BigDecimal convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(), "<bigDecimal> -> new BigDecimal(String)");
+
+ String trimmed = Objects.requireNonNull(value).trim();
+ try{
+ return new BigDecimal(trimmed);
+ } catch(Exception e){
+ LOG.finest("Parsing BigDecimal failed, trying BigInteger for: " + value);
+ BigInteger bigInt = integerConverter.convert(value, context);
+ if(bigInt!=null){
+ return new BigDecimal(bigInt);
+ }
+ LOG.finest("Failed to parse BigDecimal from: " + value);
+ return null;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java
deleted file mode 100644
index 9c71688..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.junit.Test;
-
-import java.math.BigDecimal;
-
-import static org.junit.Assert.*;
-
-/**
- * Tests the default converter for bytes.
- */
-public class BigDecimalConverterTest {
-
- /**
- * Test conversion. The value are provided by
- * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_BigDecimal_Decimal() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- BigDecimal valueRead = config.get("tests.converter.bd.decimal", BigDecimal.class);
- assertTrue(valueRead != null);
- assertEquals(valueRead, new BigDecimal(101));
- }
-
-
- /**
- * Test conversion. The value are provided by
- * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_BigDecimal_Hex() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- BigDecimal valueRead = config.get("tests.converter.bd.hex.lowerX", BigDecimal.class);
- assertTrue(valueRead != null);
- assertEquals(valueRead, new BigDecimal("47"));
- valueRead = config.get("tests.converter.bd.hex.upperX", BigDecimal.class);
- assertTrue(valueRead != null);
- assertEquals(valueRead, new BigDecimal("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();
- BigDecimal valueRead = config.get("tests.converter.bd.foo", BigDecimal.class);
- assertFalse(valueRead != null);
- }
-
- /**
- * Test conversion. The value are provided by
- * {@link ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_BigDecimal_BigValue() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- BigDecimal valueRead = config.get("tests.converter.bd.big", BigDecimal.class);
- assertTrue(valueRead != null);
- assertEquals(new BigDecimal("101666666666666662333337263723628763821638923628193612983618293628763"),
- valueRead);
- }
-
- /**
- * Test conversion. The value are provided by
- * {@link ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_BigDecimal_BigFloatValue() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- BigDecimal valueRead = config.get("tests.converter.bd.bigFloat", BigDecimal.class);
- assertTrue(valueRead != null);
- assertEquals(new BigDecimal("1016666666666666623333372637236287638216389293628763.1016666666666666623333372" +
- "63723628763821638923628193612983618293628763"), valueRead);
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java
new file mode 100644
index 0000000..a5a32b3
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.math.BigInteger;
+import java.util.Objects;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to BigInteger, the supported format is one of the following:
+ * <ul>
+ * <li>0xFFFFFF</li>
+ * <li>0XFFFFAC</li>
+ * <li>23257352735276352753</li>
+ * <li>-0xFFFFFF</li>
+ * <li>-0XFFFFAC</li>
+ * <li>-23257352735276352753</li>
+ * </ul>
+ */
+public class BigIntegerConverter implements PropertyConverter<BigInteger>{
+
+ /** The logger. */
+ private static final Logger LOG = Logger.getLogger(BigIntegerConverter.class.getName());
+ /** Converter used to decode hex, octal values. */
+ private final ByteConverter byteConverter = new ByteConverter();
+
+ @Override
+ public BigInteger convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(), "[-]0X.. (hex)", "[-]0x... (hex)", "<bigint> -> new BigInteger(bigint)");
+ String trimmed = Objects.requireNonNull(value).trim();
+ if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){
+ 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.finest("Invalid Hex-Byte-String: " + value);
+ return null;
+ }
+ byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2), context);
+ if(val<10){
+ decimal.append('0').append(val);
+ } else{
+ decimal.append(val);
+ }
+ }
+ return new BigInteger(decimal.toString());
+ } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){
+ 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.finest("Invalid Hex-Byte-String: " + trimmed);
+ return null;
+ }
+ byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2), context);
+ if(val<10){
+ decimal.append('0').append(val);
+ } else{
+ decimal.append(val);
+ }
+ }
+ return new BigInteger('-' + decimal.toString());
+ }
+ try{
+ return new BigInteger(trimmed);
+ } catch(Exception e){
+ LOG.log(Level.FINEST, "Failed to parse BigInteger from: " + value, e);
+ return null;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java
new file mode 100644
index 0000000..93b1da3
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.Locale;
+import java.util.Objects;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to Boolean.
+ */
+public class BooleanConverter implements PropertyConverter<Boolean> {
+
+ private final Logger LOG = Logger.getLogger(getClass().getName());
+
+ @Override
+ public Boolean convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(), "yes (ignore case)", "y (ignore case)", "true (ignore case)", "t (ignore case)", "no (ignore case)", "n (ignore case)", "false (ignore case)", "f (ignore case)");
+ String ignoreCaseValue = Objects.requireNonNull(value)
+ .trim()
+ .toLowerCase(Locale.ENGLISH);
+ 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.finest("Unknown boolean value encountered: " + value);
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java
deleted file mode 100644
index ac75c34..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.junit.Test;
-
-/**
- * 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 = ConfigurationProvider.getConfiguration();
- // trues
- Boolean valueRead = config.get("tests.converter.boolean.y1", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.y2", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.yes1", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.yes2", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.yes3", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.true1", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.true2", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.true3", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.t1", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- valueRead = config.get("tests.converter.boolean.t2", Boolean.class);
- assertNotNull(valueRead);
- assertEquals(valueRead, Boolean.TRUE);
- // falses
- valueRead = config.get("tests.converter.boolean.n1", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.n2", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.no1", Boolean.class);
- assertFalse(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.no2", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.no3", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.false1", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.false2", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.false3", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.f1", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.f2", Boolean.class);
- assertNotNull(valueRead);
- assertFalse(valueRead);
- valueRead = config.get("tests.converter.boolean.foo", Boolean.class);
- assertNull(valueRead);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverter.java
new file mode 100644
index 0000000..fc2bf9d
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverter.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.Locale;
+import java.util.Objects;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to Byte, the supported format is one of the following:
+ * <ul>
+ * <li>123 (byte value)</li>
+ * <li>0xFF (byte value)</li>
+ * <li>0XDF (byte value)</li>
+ * <li>0D1 (byte value)</li>
+ * <li>-123 (byte value)</li>
+ * <li>-0xFF (byte value)</li>
+ * <li>-0XDF (byte value)</li>
+ * <li>-0D1 (byte value)</li>
+ * <li>MIN_VALUE (ignoring case)</li>
+ * <li>MIN (ignoring case)</li>
+ * <li>MAX_VALUE (ignoring case)</li>
+ * <li>MAX (ignoring case)</li>
+ * </ul>
+ */
+public class ByteConverter implements PropertyConverter<Byte>{
+
+ private final Logger LOG = Logger.getLogger(getClass().getName());
+
+ @Override
+ public Byte convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(),"<byte>", "MIN_VALUE", "MIN", "MAX_VALUE", "MAX");
+ String trimmed = Objects.requireNonNull(value).trim();
+ switch(trimmed.toUpperCase(Locale.ENGLISH)){
+ case "MIN_VALUE":
+ case "MIN":
+ return Byte.MIN_VALUE;
+ case "MAX_VALUE":
+ case "MAX":
+ return Byte.MAX_VALUE;
+ default:
+ try{
+ return Byte.decode(trimmed);
+ }
+ catch(Exception e){
+ LOG.log(Level.FINEST, "Unparseable Byte: " + value);
+ return null;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java
deleted file mode 100644
index 132674d..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.junit.Test;
-
-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 = ConfigurationProvider.getConfiguration();
- Byte valueRead = config.get("tests.converter.byte.decimal", Byte.class);
- assertNotNull(valueRead);
- assertEquals(valueRead.byteValue(), 101);
- valueRead = config.get("tests.converter.byte.octal", Byte.class);
- assertNotNull(valueRead);
- assertEquals(valueRead.byteValue(), Byte.decode("02").byteValue());
- valueRead = config.get("tests.converter.byte.hex.lowerX", Byte.class);
- assertNotNull(valueRead);
- assertEquals(valueRead.byteValue(), Byte.decode("0x2F").byteValue());
- valueRead = config.get("tests.converter.byte.hex.upperX", Byte.class);
- assertNotNull(valueRead);
- assertEquals(valueRead.byteValue(), Byte.decode("0X3F").byteValue());
- valueRead = config.get("tests.converter.byte.foo", Byte.class);
- assertNull(valueRead);
- }
-
- /**
- * Test conversion. The value are provided by
- * {@link ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_Byte_MinValue() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- Byte valueRead = config.get("tests.converter.byte.min", Byte.class);
- assertTrue(valueRead!=null);
- assertEquals(Byte.MIN_VALUE, valueRead.byteValue());
- }
-
- /**
- * Test conversion. The value are provided by
- * {@link ConverterTestsPropertySource}.
- * @throws Exception
- */
- @Test
- public void testConvert_Byte_MaxValue() throws Exception {
- Configuration config = ConfigurationProvider.getConfiguration();
- Byte valueRead = config.get("tests.converter.byte.max", Byte.class);
- assertTrue(valueRead!=null);
- assertEquals(Byte.MAX_VALUE, valueRead.byteValue());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverter.java
new file mode 100644
index 0000000..dd6f557
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverter.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.Objects;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to Character, the supported format is one of the following:
+ * <ul>
+ * <li>'a'</li>
+ * <li>123 (byte value)</li>
+ * <li>0xFF (byte value)</li>
+ * <li>0XDF (byte value)</li>
+ * <li>0D1 (byte value)</li>
+ * </ul>
+ */
+public class CharConverter implements PropertyConverter<Character>{
+
+ private static final Logger LOG = Logger.getLogger(CharConverter.class.getName());
+
+ @Override
+ public Character convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(),"\\'<char>\\'", "<char>", "<charNum>");
+ String trimmed = Objects.requireNonNull(value).trim();
+ if(trimmed.isEmpty()){
+ return null;
+ }
+ if(trimmed.startsWith("'")) {
+ try {
+ trimmed = trimmed.substring(1, trimmed.length() - 1);
+ if (trimmed.isEmpty()) {
+ return null;
+ }
+ return trimmed.charAt(0);
+ } catch (Exception e) {
+ LOG.finest("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/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java
deleted file mode 100644
index f27a465..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CharConverterTest.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import org.apache.tamaya.Configuration;
-import org.apache.tamaya.ConfigurationProvider;
-import org.junit.Test;
-
-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);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverter.java
new file mode 100644
index 0000000..15e78d2
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ClassConverter.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.Objects;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to Class, hereby using the following classloaders:
+ * <ul>
+ * <li>The current ThreadContext ClassLoader</li>
+ * <li>The Classloader of this class</li>
+ * <li>The system Classloader</li>
+ * </ul>
+ */
+public class ClassConverter implements PropertyConverter<Class<?>>{
+
+ private final Logger LOG = Logger.getLogger(getClass().getName());
+
+ @Override
+ public Class<?> convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(),"<fullyQualifiedClassName>");
+ String trimmed = Objects.requireNonNull(value).trim();
+ try{
+ return Class.forName(trimmed, false, Thread.currentThread().getContextClassLoader());
+ }
+ catch(Exception e){
+ LOG.finest("Class not found in context CL: " + trimmed);
+ }
+ try{
+ return Class.forName(trimmed, false, ClassConverter.class.getClassLoader());
+ }
+ catch(Exception e){
+ LOG.finest("Class not found in ClassConverter's CL: " + trimmed);
+ }
+ try{
+ return Class.forName(trimmed, false, ClassLoader.getSystemClassLoader());
+ }
+ catch(Exception e){
+ LOG.finest("Class not found in System CL (giving up): " + trimmed);
+ return null;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
deleted file mode 100644
index c2fe2d3..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
+++ /dev/null
@@ -1,253 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import org.apache.tamaya.spi.PropertySource;
-import org.apache.tamaya.spi.PropertyValue;
-
-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 getName(){
- return "ConverterTestsPropertySource";
- }
-
- @Override
- public PropertyValue get(String key) {
- switch(key){
- // Bytes
- case "tests.converter.byte.decimal":
- return PropertyValue.of(key, "101", getName());
- case "tests.converter.byte.octal":
- return PropertyValue.of(key, "02", getName());
- case "tests.converter.byte.hex.lowerX":
- return PropertyValue.of(key, "0x2F", getName());
- case "tests.converter.byte.hex.upperX":
- return PropertyValue.of(key, "0X3F", getName());
- case "tests.converter.byte.min":
- return PropertyValue.of(key, "min", getName());
- case "tests.converter.byte.max":
- return PropertyValue.of(key, "MAX_Value", getName());
- // Boolean
- case "tests.converter.boolean.y1":
- return PropertyValue.of(key, "y", getName());
- case "tests.converter.boolean.y2":
- return PropertyValue.of(key, "Y", getName());
- case "tests.converter.boolean.yes1":
- return PropertyValue.of(key, "yes", getName());
- case "tests.converter.boolean.yes2":
- return PropertyValue.of(key, "Yes", getName());
- case "tests.converter.boolean.yes3":
- return PropertyValue.of(key, "yeS", getName());
- case "tests.converter.boolean.true1":
- return PropertyValue.of(key, "true", getName());
- case "tests.converter.boolean.true2":
- return PropertyValue.of(key, "True", getName());
- case "tests.converter.boolean.true3":
- return PropertyValue.of(key, "trUe", getName());
- case "tests.converter.boolean.t1":
- return PropertyValue.of(key, "t", getName());
- case "tests.converter.boolean.t2":
- return PropertyValue.of(key, "T", getName());
- case "tests.converter.boolean.n1":
- return PropertyValue.of(key, "n", getName());
- case "tests.converter.boolean.n2":
- return PropertyValue.of(key, "N", getName());
- case "tests.converter.boolean.no1":
- return PropertyValue.of(key, "no", getName());
- case "tests.converter.boolean.no2":
- return PropertyValue.of(key, "No", getName());
- case "tests.converter.boolean.no3":
- return PropertyValue.of(key, "nO", getName());
- case "tests.converter.boolean.false1":
- return PropertyValue.of(key, "false", getName());
- case "tests.converter.boolean.false2":
- return PropertyValue.of(key, "False", getName());
- case "tests.converter.boolean.false3":
- return PropertyValue.of(key, "falSe", getName());
- case "tests.converter.boolean.f1":
- return PropertyValue.of(key, "f", getName());
- case "tests.converter.boolean.f2":
- return PropertyValue.of(key, "F", getName());
- // Character
- case "tests.converter.char.f":
- return PropertyValue.of(key, "f", getName());
- case "tests.converter.char.d":
- return PropertyValue.of(key, "'d'", getName());
- case "tests.converter.char.f-before":
- return PropertyValue.of(key, " f", getName());
- case "tests.converter.char.f-after":
- return PropertyValue.of(key, "f ", getName());
- case "tests.converter.char.f-around":
- return PropertyValue.of(key, " f ", getName());
- case "tests.converter.char.f-numeric":
- return PropertyValue.of(key, "101", getName());
- // currency
- case "tests.converter.currency.code1":
- return PropertyValue.of(key, "CHF", getName());
- case "tests.converter.currency.code2":
- return PropertyValue.of(key, "cHf", getName());
- case "tests.converter.currency.code3":
- return PropertyValue.of(key, " CHF", getName());
- case "tests.converter.currency.code4":
- return PropertyValue.of(key, "CHF ", getName());
- case "tests.converter.currency.code5":
- return PropertyValue.of(key, " CHF ", getName());
- case "tests.converter.currency.code-numeric1":
- return PropertyValue.of(key, "100", getName());
- case "tests.converter.currency.code-numeric2":
- return PropertyValue.of(key, " 100", getName());
- case "tests.converter.currency.code-numeric3":
- return PropertyValue.of(key, "100 ", getName());
- case "tests.converter.currency.code-numeric4":
- return PropertyValue.of(key, " 100 ", getName());
- case "tests.converter.currency.code-locale1":
- return PropertyValue.of(key, "DE", getName());
- case "tests.converter.currency.code-locale2":
- return PropertyValue.of(key, " DE", getName());
- case "tests.converter.currency.code-locale3":
- return PropertyValue.of(key, "DE ", getName());
- case "tests.converter.currency.code-locale4":
- return PropertyValue.of(key, " DE ", getName());
- //double
- case "tests.converter.double.decimal":
- return PropertyValue.of(key, "1.23456789", getName());
- case "tests.converter.double.decimalNegative":
- return PropertyValue.of(key, "-1.23456789", getName());
- case "tests.converter.double.integer":
- return PropertyValue.of(key, " 100", getName());
- case "tests.converter.double.hex1":
- return PropertyValue.of(key, " 0XFF", getName());
- case "tests.converter.double.hex2":
- return PropertyValue.of(key, "-0xFF ", getName());
- case "tests.converter.double.hex3":
- return PropertyValue.of(key, "#FF", getName());
- case "tests.converter.double.octal":
- return PropertyValue.of(key, "0013", getName());
- case "tests.converter.double.min":
- return PropertyValue.of(key, "MIN_Value", getName());
- case "tests.converter.double.max":
- return PropertyValue.of(key, "max", getName());
- case "tests.converter.double.nan":
- return PropertyValue.of(key, "NAN", getName());
- case "tests.converter.double.pi":
- return PropertyValue.of(key, "positive_infinity", getName());
- case "tests.converter.double.ni":
- return PropertyValue.of(key, "Negative_Infinity", getName());
- //float
- case "tests.converter.float.decimal":
- return PropertyValue.of(key, "1.23456789", getName());
- case "tests.converter.float.decimalNegative":
- return PropertyValue.of(key, "-1.23456789", getName());
- case "tests.converter.float.integer":
- return PropertyValue.of(key, " 100", getName());
- case "tests.converter.float.hex1":
- return PropertyValue.of(key, " 0XFF", getName());
- case "tests.converter.float.hex2":
- return PropertyValue.of(key, "-0xFF ", getName());
- case "tests.converter.float.hex3":
- return PropertyValue.of(key, "#FF", getName());
- case "tests.converter.float.octal":
- return PropertyValue.of(key, "0013", getName());
- case "tests.converter.float.min":
- return PropertyValue.of(key, "MIN_Value", getName());
- case "tests.converter.float.max":
- return PropertyValue.of(key, "max", getName());
- case "tests.converter.float.nan":
- return PropertyValue.of(key, "NAN", getName());
- case "tests.converter.float.pi":
- return PropertyValue.of(key, "positive_infinity", getName());
- case "tests.converter.float.ni":
- return PropertyValue.of(key, "Negative_Infinity", getName());
- // Integer
- case "tests.converter.integer.decimal":
- return PropertyValue.of(key, "101", getName());
- case "tests.converter.integer.octal":
- return PropertyValue.of(key, "02", getName());
- case "tests.converter.integer.hex.lowerX":
- return PropertyValue.of(key, "0x2F", getName());
- case "tests.converter.integer.hex.upperX":
- return PropertyValue.of(key, "0X3F", getName());
- case "tests.converter.integer.min":
- return PropertyValue.of(key, "min", getName());
- case "tests.converter.integer.max":
- return PropertyValue.of(key, "MAX_Value", getName());
- // Long
- case "tests.converter.long.decimal":
- return PropertyValue.of(key, "101", getName());
- case "tests.converter.long.octal":
- return PropertyValue.of(key, "02", getName());
- case "tests.converter.long.hex.lowerX":
- return PropertyValue.of(key, "0x2F", getName());
- case "tests.converter.long.hex.upperX":
- return PropertyValue.of(key, "0X3F", getName());
- case "tests.converter.long.min":
- return PropertyValue.of(key, "min", getName());
- case "tests.converter.long.max":
- return PropertyValue.of(key, "MAX_Value", getName());
- // Short
- case "tests.converter.short.decimal":
- return PropertyValue.of(key, "101", getName());
- case "tests.converter.short.octal":
- return PropertyValue.of(key, "02", getName());
- case "tests.converter.short.hex.lowerX":
- return PropertyValue.of(key, "0x2F", getName());
- case "tests.converter.short.hex.upperX":
- return PropertyValue.of(key, "0X3F", getName());
- case "tests.converter.short.min":
- return PropertyValue.of(key, "min", getName());
- case "tests.converter.short.max":
- return PropertyValue.of(key, "MAX_Value", getName());
- // BigDecimal
- case "tests.converter.bd.decimal":
- return PropertyValue.of(key, "101", getName());
- case "tests.converter.bd.float":
- return PropertyValue.of(key, "101.36438746", getName());
- case "tests.converter.bd.big":
- return PropertyValue.of(key, "101666666666666662333337263723628763821638923628193612983618293628763", getName());
- case "tests.converter.bd.bigFloat":
- return PropertyValue.of(key, "1016666666666666623333372637236287638216389293628763.101666666666666662333337263723628763821638923628193612983618293628763", getName());
- case "tests.converter.bd.hex.lowerX":
- return PropertyValue.of(key, "0x2F", getName());
- case "tests.converter.bd.hex.upperX":
- return PropertyValue.of(key, "0X3F", getName());
- }
- return null;
- }
-
- @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/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
new file mode 100644
index 0000000..f774693
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import 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, the supported format is one of the following:
+ * <ul>
+ * <li>CHF (currency code)</li>
+ * <li>123 (numeric currency value >
+ * = 0)</li>
+ * <li>DE (ISO 2-digit country)</li>
+ * <li>de_DE, de_DE_123 (Locale)</li>
+ * </ul>
+ */
+public class CurrencyConverter implements PropertyConverter<Currency> {
+
+ private static final Logger LOG = Logger.getLogger(CurrencyConverter.class.getName());
+
+ @Override
+ public Currency convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(), "<currencyCode>, using Locale.ENGLISH", "<numericValue>", "<locale>");
+ String trimmed = Objects.requireNonNull(value).trim();
+ try {
+ return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH));
+ } catch (Exception e) {
+ LOG.log(Level.FINEST, "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.FINEST, "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.finest("Not a valid currency: " + trimmed + ", giving up...");
+ } catch (Exception e) {
+ LOG.log(Level.FINEST, "Not a valid country locale for currency: " + trimmed + ", giving up...", e);
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java
deleted file mode 100644
index 9113ca2..0000000
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/CurrencyConverterTest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tamaya.core.internal.converters;
-
-import org.apache.tamaya.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/43468987/code/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java
new file mode 100644
index 0000000..791632d
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal.converters;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.util.Locale;
+import java.util.Objects;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to Double, using the Java number syntax:
+ * (-)?[0-9]*\.[0-9]*. In case of error the value given also is tried being parsed as integral number using
+ * {@link LongConverter}. Additionally the following values are supported:
+ * <ul>
+ * <li>NaN (ignoring case)</li>
+ * <li>POSITIVE_INFINITY (ignoring case)</li>
+ * <li>NEGATIVE_INFINITY (ignoring case)</li>
+ * </ul>
+ */
+public class DoubleConverter implements PropertyConverter<Double> {
+ /**
+ * The logger.
+ */
+ private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName());
+ /**
+ * The converter used, when floating point parse failed.
+ */
+ private final LongConverter integerConverter = new LongConverter();
+
+ @Override
+ public Double convert(String value, ConversionContext context) {
+ context.addSupportedFormats(getClass(), "<double>", "MIN", "MIN_VALUE", "MAX", "MAX_VALUE", "POSITIVE_INFINITY", "NEGATIVE_INFINITY", "NAN");
+ String trimmed = Objects.requireNonNull(value).trim();
+ switch (trimmed.toUpperCase(Locale.ENGLISH)) {
+ case "POSITIVE_INFINITY":
+ return Double.POSITIVE_INFINITY;
+ case "NEGATIVE_INFINITY":
+ return Double.NEGATIVE_INFINITY;
+ case "NAN":
+ return Double.NaN;
+ case "MIN_VALUE":
+ case "MIN":
+ return Double.MIN_VALUE;
+ case "MAX_VALUE":
+ case "MAX":
+ return Double.MAX_VALUE;
+ default:
+ try {
+ return Double.valueOf(trimmed);
+ } catch (Exception e) {
+ // OK perhaps we have an integral number that must be converted to the double type...
+ LOG.finest("Parsing of double as floating number failed, trying parsing integral" +
+ " number/hex instead...");
+ }
+ Long val = integerConverter.convert(trimmed, context);
+ if(val!=null){
+ return val.doubleValue();
+ }
+ return null;
+ }
+
+ }
+}