camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [03/11] camel git commit: CAMEL-10546: Re-written GlobalOptionsDefinition.asMap() more concisely.
Date Sun, 22 Jan 2017 18:51:49 GMT
CAMEL-10546: Re-written GlobalOptionsDefinition.asMap() more concisely.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/628ceeeb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/628ceeeb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/628ceeeb

Branch: refs/heads/master
Commit: 628ceeeba46ef7139e52ea64d6615ea6d7a541b1
Parents: 6b6ba1c
Author: aldettinger <aldettinger@gmail.com>
Authored: Fri Jan 20 13:59:59 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Jan 22 18:06:43 2017 +0100

----------------------------------------------------------------------
 .../camel/model/GlobalOptionsDefinition.java    |  8 +--
 .../model/GlobalOptionsDefinitionTest.java      | 51 ++++++++++++++++++--
 2 files changed, 48 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/628ceeeb/camel-core/src/main/java/org/apache/camel/model/GlobalOptionsDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/GlobalOptionsDefinition.java
b/camel-core/src/main/java/org/apache/camel/model/GlobalOptionsDefinition.java
index c94c0a5..013823f 100644
--- a/camel-core/src/main/java/org/apache/camel/model/GlobalOptionsDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/GlobalOptionsDefinition.java
@@ -16,9 +16,9 @@
  */
 package org.apache.camel.model;
 
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -50,11 +50,7 @@ public class GlobalOptionsDefinition {
     }
 
     public Map<String, String> asMap() {
-        Map<String, String> globalOptionsAsMap = new HashMap<>();
-        for (GlobalOptionDefinition globalOption : getGlobalOptions()) {
-            globalOptionsAsMap.put(globalOption.getKey(), globalOption.getValue());
-        }
-        return globalOptionsAsMap;
+        return getGlobalOptions().stream().collect(Collectors.toMap(o -> o.getKey(), o
-> o.getValue(), (o1, o2) -> o2));
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/628ceeeb/camel-core/src/test/java/org/apache/camel/model/GlobalOptionsDefinitionTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/model/GlobalOptionsDefinitionTest.java
b/camel-core/src/test/java/org/apache/camel/model/GlobalOptionsDefinitionTest.java
index 0e8f78a..eac497a 100644
--- a/camel-core/src/test/java/org/apache/camel/model/GlobalOptionsDefinitionTest.java
+++ b/camel-core/src/test/java/org/apache/camel/model/GlobalOptionsDefinitionTest.java
@@ -28,16 +28,25 @@ import org.junit.Test;
 public class GlobalOptionsDefinitionTest {
 
     private static final String LOG_DEBUG_BODY_MAX_CHARS_VALUE = "500";
+    private static final String LOG_DEBUG_BODY_MAX_CHARS_DUP_VALUE = "400";
 
     private GlobalOptionsDefinition instance;
+    private List<GlobalOptionDefinition> globalOptions;
+    private GlobalOptionDefinition nominalOption;
+    private GlobalOptionDefinition duplicateOption;
 
     @Before
     public void setup() {
-        GlobalOptionDefinition globalOption = new GlobalOptionDefinition();
-        globalOption.setKey(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
-        globalOption.setValue(LOG_DEBUG_BODY_MAX_CHARS_VALUE);
-        List<GlobalOptionDefinition> globalOptions = new ArrayList<>();
-        globalOptions.add(globalOption);
+        nominalOption = new GlobalOptionDefinition();
+        nominalOption.setKey(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
+        nominalOption.setValue(LOG_DEBUG_BODY_MAX_CHARS_VALUE);
+
+        duplicateOption = new GlobalOptionDefinition();
+        duplicateOption.setKey(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
+        duplicateOption.setValue(LOG_DEBUG_BODY_MAX_CHARS_DUP_VALUE);
+
+        globalOptions = new ArrayList<>();
+        globalOptions.add(nominalOption);
         instance = new GlobalOptionsDefinition();
         instance.setGlobalOptions(globalOptions);
     }
@@ -50,4 +59,36 @@ public class GlobalOptionsDefinitionTest {
         Assert.assertEquals(LOG_DEBUG_BODY_MAX_CHARS_VALUE, map.get(Exchange.LOG_DEBUG_BODY_MAX_CHARS));
     }
 
+    @Test
+    public void asMapWithDuplicateKeyShouldOverride() {
+        globalOptions.add(duplicateOption);
+        Map<String, String> map = instance.asMap();
+        Assert.assertNotNull(map);
+        Assert.assertEquals(1, map.size());
+        Assert.assertEquals(LOG_DEBUG_BODY_MAX_CHARS_DUP_VALUE, map.get(Exchange.LOG_DEBUG_BODY_MAX_CHARS));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void asMapWithNullGlobalOptionsShouldThrowNullPointerException() {
+        instance.setGlobalOptions(null);
+        instance.asMap();
+    }
+
+    @Test
+    public void asMapWithEmptyGlobalOptionsShouldReturnEmptyMap() {
+        globalOptions.clear();
+        Map<String, String> map = instance.asMap();
+        Assert.assertNotNull(map);
+        Assert.assertEquals(0, map.size());
+    }
+
+    @Test
+    public void asMapWithNullKeyShouldReturnEmptyMap() {
+        nominalOption.setKey(null);
+        Map<String, String> map = instance.asMap();
+        Assert.assertNotNull(map);
+        Assert.assertEquals(1, map.size());
+        Assert.assertEquals(LOG_DEBUG_BODY_MAX_CHARS_VALUE, map.get(null));
+    }
+
 }


Mime
View raw message