tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject incubator-tamaya git commit: TAMAYA-60 Added tests for the configuration builder.
Date Wed, 28 Jan 2015 08:27:51 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 1bb8742ba -> 310195342


TAMAYA-60 Added tests for the configuration builder.


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

Branch: refs/heads/master
Commit: 310195342ac29c6333b183c63f5743fc87b63afd
Parents: 1bb8742
Author: Oliver B. Fischer <plexus@apache.org>
Authored: Wed Jan 28 09:27:30 2015 +0100
Committer: Oliver B. Fischer <plexus@apache.org>
Committed: Wed Jan 28 09:27:30 2015 +0100

----------------------------------------------------------------------
 .../spi/PropertyValueCombinationPolicy.java     |   6 +-
 .../tamaya/builder/ConfigurationBuilder.java    |  12 +-
 .../builder/ConfigurationBuilderTest.java       | 136 ++++++++++++++++++-
 .../builder/util/mockito/NotMockedAnswer.java   |  54 ++++++++
 4 files changed, 202 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/31019534/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
index ffc2bd7..3c5fb42 100644
--- a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
+++ b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyValueCombinationPolicy.java
@@ -34,8 +34,10 @@ public interface PropertyValueCombinationPolicy {
      * Default overriding collector, where each existing entry ({@code current} is overridden
by a subsequent non-null
      * entry evaluated by {@code propertySource.get(key)}.
      */
-    public final PropertyValueCombinationPolicy DEFAULT_OVERRIDING_COLLECTOR = (current,
key, propertySource) ->
-            Optional.ofNullable(propertySource.get(key)).filter(s -> !s.isEmpty()).orElse(current);
+    public final PropertyValueCombinationPolicy DEFAULT_OVERRIDING_COLLECTOR =
+        (current, key, propertySource) -> Optional.ofNullable(propertySource.get(key))
+                                                  .filter(s -> !s.isEmpty())
+                                                  .orElse(current);
 
     /**
      * Method that is called for each value evaluated by a PropertySource for the given key.
This method is called

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/31019534/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 cabfe7a..4775990 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
@@ -53,10 +53,18 @@ public class ConfigurationBuilder {
     }
 
     public ConfigurationBuilder addPropertySources(PropertySource... sources){
+        checkBuilderState();
+
         contextBuilder.addPropertySources(Objects.requireNonNull(sources));
         return this;
     }
 
+    private void checkBuilderState() {
+        if (built) {
+            throw new IllegalStateException("Configuration has already been build.");
+        }
+    }
+
     public ConfigurationBuilder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders){
         contextBuilder.addPropertySourceProviders(propertySourceProviders);
         return this;
@@ -86,9 +94,7 @@ public class ConfigurationBuilder {
      * @return a new Configuration instance.
      */
     public Configuration build() {
-        if (built) {
-            throw new IllegalStateException("building a Configuration can only be done once");
-        }
+        checkBuilderState();
 
         built = true;
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/31019534/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 de8d1fe..57cc5da 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
@@ -18,11 +18,18 @@
  */
 package org.apache.tamaya.builder;
 
+import org.apache.tamaya.ConfigException;
 import org.apache.tamaya.Configuration;
+import org.apache.tamaya.spi.PropertySource;
+import org.junit.Ignore;
 import org.junit.Test;
 
+import static org.apache.tamaya.builder.util.mockito.NotMockedAnswer.NOT_MOCKED_ANSWER;
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 
 public class ConfigurationBuilderTest {
 
@@ -47,7 +54,134 @@ public class ConfigurationBuilderTest {
     public void addPropertySourcesDoesNotAcceptNullValue() {
         ConfigurationBuilder builder = new ConfigurationBuilder();
 
-        builder.addPropertySources(null);
+        builder.addPropertySources((PropertySource[])null);
     }
 
+    @Test(expected = IllegalStateException.class)
+    public void propertySourceCanNotBeAddedAfterBuildingTheConfiguration() {
+        PropertySource first = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("first").when(first).getName();
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(first);
+
+        builder.build();
+
+        PropertySource second = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("second").when(first).getName();
+
+        builder.addPropertySources(second);
+    }
+
+    @Test
+    public void singleAddedPropertySourceIsUsed() {
+        PropertySource source = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("one").when(source).getName();
+        doReturn("a").when(source).get("keyOfA");
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(source);
+
+        Configuration config = builder.build();
+
+        String valueOfA = config.get("keyOfA");
+
+        assertThat(valueOfA, notNullValue());
+        assertThat(valueOfA, equalTo("a"));
+    }
+
+    @Test
+    public void twoAddedPropertySourcesAreUsed() {
+        PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("one").when(sourceOne).getName();
+        doReturn("b").when(sourceOne).get("keyOfA");
+        doReturn(10).when(sourceOne).getOrdinal();
+
+        PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+        doReturn("two").when(sourceTwo).getName();
+        doReturn("a").when(sourceTwo).get("keyOfA");
+        doReturn(10).when(sourceTwo).getOrdinal();
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(sourceOne)
+                                                                 .addPropertySources(sourceTwo);
+
+        Configuration config = builder.build();
+
+        String valueOfA = config.get("keyOfA");
+
+        assertThat(valueOfA, notNullValue());
+        assertThat(valueOfA, equalTo("a"));
+    }
+
+    @Ignore
+    @Test(expected = ConfigException.class)
+    public void twoPropertySourcesSamePrioritySameKey() {
+        PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("one").when(sourceOne).getName();
+        doReturn("b").when(sourceOne).get("keyOfA");
+        doReturn(20).when(sourceOne).getOrdinal();
+
+        PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+        doReturn("two").when(sourceTwo).getName();
+        doReturn("a").when(sourceTwo).get("keyOfA");
+        doReturn(20).when(sourceTwo).getOrdinal();
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(sourceOne)
+                                                                 .addPropertySources(sourceTwo);
+
+        Configuration config = builder.build();
+
+        config.get("keyOfA");
+    }
+
+    @Test
+    public void twoPropertySourcesDiffPrioritySameKeyLowerAddedFirst() {
+        PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("one").when(sourceOne).getName();
+        doReturn("b").when(sourceOne).get("keyOfA");
+        doReturn(10).when(sourceOne).getOrdinal();
+
+        PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+        doReturn("two").when(sourceTwo).getName();
+        doReturn("a").when(sourceTwo).get("keyOfA");
+        doReturn(20).when(sourceTwo).getOrdinal();
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(sourceOne)
+                                                                 .addPropertySources(sourceTwo);
+
+        Configuration config = builder.build();
+
+        String valueOfA = config.get("keyOfA");
+
+        assertThat(valueOfA, notNullValue());
+        assertThat(valueOfA, equalTo("a"));
+    }
+
+    @Test
+    public void twoPropertySourcesDiffPrioritySameKeyHigherAddedFirst() {
+        PropertySource sourceOne = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+
+        doReturn("one").when(sourceOne).getName();
+        doReturn("b").when(sourceOne).get("keyOfA");
+        doReturn(30).when(sourceOne).getOrdinal();
+
+        PropertySource sourceTwo = mock(PropertySource.class, NOT_MOCKED_ANSWER);
+        doReturn("two").when(sourceTwo).getName();
+        doReturn("a").when(sourceTwo).get("keyOfA");
+        doReturn(20).when(sourceTwo).getOrdinal();
+
+        ConfigurationBuilder builder = new ConfigurationBuilder().addPropertySources(sourceOne)
+                                                                 .addPropertySources(sourceTwo);
+
+        Configuration config = builder.build();
+
+        String valueOfA = config.get("keyOfA");
+
+        assertThat(valueOfA, notNullValue());
+        assertThat(valueOfA, equalTo("b"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/31019534/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
new file mode 100644
index 0000000..d0ec79b
--- /dev/null
+++ b/modules/builder/src/test/java/org/apache/tamaya/builder/util/mockito/NotMockedAnswer.java
@@ -0,0 +1,54 @@
+/*
+ * 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.mockito;
+
+import org.mockito.exceptions.base.MockitoException;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.io.Serializable;
+
+public class NotMockedAnswer implements Answer<Object>, Serializable {
+    public final static NotMockedAnswer NOT_MOCKED_ANSWER = new NotMockedAnswer();
+
+    private NotMockedAnswer() {
+    }
+
+    @Override
+    public Object answer(InvocationOnMock invocation) throws Throwable {
+        StringBuilder msgBuilder = new StringBuilder();
+
+        msgBuilder.append("Invocation of method not mocked: ")
+                  .append(invocation.getMethod().toGenericString());
+
+        if (invocation.getArguments().length > 0) {
+            msgBuilder.append(" Supplied arguments: ");
+
+            for (int i = 0; i < invocation.getArguments().length; i++) {
+                msgBuilder.append(invocation.getArguments()[i]);
+
+                if (i - 1 < invocation.getArguments().length) {
+                    msgBuilder.append(", ");
+                }
+            }
+        }
+
+        throw new MockitoException(msgBuilder.toString());
+    }
+}


Mime
View raw message