Repository: incubator-tamaya
Updated Branches:
  refs/heads/master cc284de3c -> 317ad067a


TAMAYA-60 Wrote tests for the ConfigurationBuilder.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/317ad067
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/317ad067
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/317ad067

Branch: refs/heads/master
Commit: 317ad067a17d860b873e1ebb1df2b8a4f45c3ec2
Parents: cc284de
Author: Oliver B. Fischer <[email protected]>
Authored: Wed Jan 28 18:41:15 2015 +0100
Committer: Oliver B. Fischer <[email protected]>
Committed: Wed Jan 28 18:41:15 2015 +0100

----------------------------------------------------------------------
 .../tamaya/builder/ConfigurationBuilder.java    | 10 +++
 .../ProgrammaticConfigurationContext.java       | 12 +++
 .../builder/ConfigurationBuilderTest.java       | 91 ++++++++++++++++++++
 .../tamaya/builder/util/types/CustomTypeA.java  | 34 ++++++++
 .../tamaya/builder/util/types/CustomTypeB.java  | 39 +++++++++
 5 files changed, 186 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/317ad067/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
 
b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
index 4775990..aa9c766 100644
--- 
a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
+++ 
b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
@@ -80,7 +80,17 @@ public class ConfigurationBuilder {
         return this;
     }
 
+    public <T> ConfigurationBuilder addPropertyConverter(Class<T> type, 
PropertyConverter<T> propertyConverter) {
+        Objects.requireNonNull(type);
+        Objects.requireNonNull(propertyConverter);
+
+        return addPropertyConverter(TypeLiteral.of(type), propertyConverter);
+    }
+
     public <T> ConfigurationBuilder addPropertyConverter(TypeLiteral<T> type, 
PropertyConverter<T> propertyConverter){
+        Objects.requireNonNull(type);
+        Objects.requireNonNull(propertyConverter);
+
         contextBuilder.addPropertyConverter(type, propertyConverter);
         return this;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/317ad067/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
 
b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
index b90c271..31850c4 100644
--- 
a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
+++ 
b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
@@ -37,6 +37,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.StringJoiner;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.StampedLock;
@@ -100,6 +101,17 @@ class ProgrammaticConfigurationContext implements 
ConfigurationContext {
                 createStringList(immutablePropertyFilters, f -> 
f.getClass().getName()));
 
         propertyValueCombinationPolicy = 
builder.propertyValueCombinationPolicy;
+
+        Set<Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>>> converters 
= builder.propertyConverters.entrySet();
+
+        for (Map.Entry<TypeLiteral<?>, List<PropertyConverter<?>>> entry : 
converters) {
+            TypeLiteral<?> literal = entry.getKey();
+
+            for (PropertyConverter<?> converter : entry.getValue()) {
+                
propertyConverterManager.register((TypeLiteral<Object>)literal, 
(PropertyConverter<Object>)converter);
+            }
+        }
+
         LOG.info(() -> "Using PropertyValueCombinationPolicy: " + 
propertyValueCombinationPolicy);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/317ad067/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
 
b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
index 57cc5da..b504f4e 100644
--- 
a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
+++ 
b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
@@ -20,7 +20,13 @@ package org.apache.tamaya.builder;
 
 import org.apache.tamaya.ConfigException;
 import org.apache.tamaya.Configuration;
+import org.apache.tamaya.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.builder.util.mockito.NotMockedAnswer;
+import org.apache.tamaya.builder.util.types.CustomTypeA;
+import org.apache.tamaya.builder.util.types.CustomTypeB;
 import org.apache.tamaya.spi.PropertySource;
+import org.hamcrest.CoreMatchers;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -184,4 +190,89 @@ public class ConfigurationBuilderTest {
         assertThat(valueOfA, notNullValue());
         assertThat(valueOfA, equalTo("b"));
     }
+
+    @Test(expected = NullPointerException.class)
+    public void canNotAddNullPropertyConverter() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class), null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void canNotAddNullTypeLiteralButPropertyConverter() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.addPropertyConverter((TypeLiteral<CustomTypeA>)null,
+                                     prop -> new CustomTypeA(prop, prop));
+    }
+
+    @Test
+    public void addedPropertyConverterWithTypeLiteralIsUsedByConfiguration() {
+        PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("source").when(source).getName();
+        doReturn("A").when(source).get("key");
+
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.addPropertyConverter(TypeLiteral.of(CustomTypeA.class),
+                                     prop -> new CustomTypeA(prop, prop))
+               .addPropertySources(source);
+
+        Configuration config = builder.build();
+
+        Object resultRaw = config.get("key", CustomTypeA.class);
+
+        assertThat(resultRaw, CoreMatchers.notNullValue());
+
+        CustomTypeA result = (CustomTypeA)resultRaw;
+
+        assertThat(result.getName(), equalTo("AA"));
+    }
+
+    @Test
+    public void addedPropertyConverterWithClassIsUsedByConfiguration() {
+        PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("source").when(source).getName();
+        doReturn("A").when(source).get("key");
+
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.addPropertyConverter(CustomTypeA.class,
+                                     prop -> new CustomTypeA(prop, prop))
+               .addPropertySources(source);
+
+        Configuration config = builder.build();
+
+        Object resultRaw = config.get("key", CustomTypeA.class);
+
+        assertThat(resultRaw, CoreMatchers.notNullValue());
+
+        CustomTypeA result = (CustomTypeA)resultRaw;
+
+        assertThat(result.getName(), equalTo("AA"));
+    }
+
+    @Test
+    public void canGetAndConvertPropertyViaOfMethod() {
+        PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("source").when(source).getName();
+        doReturn("A").when(source).get("key");
+
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.addPropertySources(source);
+
+        Configuration config = builder.build();
+
+        Object resultRaw = config.get("key", CustomTypeB.class);
+
+        assertThat(resultRaw, CoreMatchers.notNullValue());
+
+        CustomTypeB result = (CustomTypeB)resultRaw;
+
+        assertThat(result.getName(), equalTo("A"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/317ad067/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
 
b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
new file mode 100644
index 0000000..89b2f5b
--- /dev/null
+++ 
b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeA.java
@@ -0,0 +1,34 @@
+/*
+ * 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.builder.util.types;
+
+/**
+ * Custom type with two argument constructor.
+ */
+public class CustomTypeA {
+    private String name;
+
+    public CustomTypeA(String name, String other) {
+        this.name = name + other;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/317ad067/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
 
b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
new file mode 100644
index 0000000..f7f4d99
--- /dev/null
+++ 
b/modules/builder/src/test/java/org/apache/tamaya/builder/util/types/CustomTypeB.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.builder.util.types;
+
+/**
+ * Custom type with factory method
+ * {@link org.apache.tamaya.builder.util.types.CustomTypeB#of(String)}
+ */
+public class CustomTypeB {
+    private String name;
+
+    private CustomTypeB(String value) {
+        this.name = value;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public static CustomTypeB of(String source) {
+        return new CustomTypeB(source);
+    }
+}

Reply via email to