brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [1/2] git commit: TypeCoercions: handle enums in wrong case
Date Tue, 10 Jun 2014 14:55:14 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master f418aca81 -> 0389553f6


TypeCoercions: handle enums in wrong case


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

Branch: refs/heads/master
Commit: 81d9ae7d8b9c8b0aa9289d9054194089b0fe0b26
Parents: ba8209b
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Jun 10 15:36:55 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Jun 10 15:48:27 2014 +0100

----------------------------------------------------------------------
 .../main/java/brooklyn/util/flags/TypeCoercions.java |  8 ++++++--
 .../brooklyn/util/internal/TypeCoercionsTest.java    | 15 ++++++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/81d9ae7d/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
index d647b97..93fe775 100644
--- a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
+++ b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
@@ -32,6 +32,8 @@ import brooklyn.entity.basic.ConfigurableEntityFactoryFromEntityFactory;
 import brooklyn.entity.basic.EntityFactory;
 import brooklyn.util.JavaGroovyEquivalents;
 import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.javalang.Enums;
 import brooklyn.util.net.Cidr;
 import brooklyn.util.net.Networking;
 import brooklyn.util.net.UserAndHostAndPort;
@@ -181,7 +183,8 @@ public class TypeCoercions {
      * Type coercion {@link Function function} for {@link Enum enums}.
      * <p>
      * Tries to convert the string to {@link CaseFormat#UPPER_UNDERSCORE} first,
-     * handling all of the different {@link CaseFormat format} possibilites.
+     * handling all of the different {@link CaseFormat format} possibilites. Failing 
+     * that, it tries a case-insensitive comparison with the valid enum values.
      * <p>
      * Returns {@code defaultValue} if the string cannot be converted.
      *
@@ -206,7 +209,8 @@ public class TypeCoercions {
                         continue;
                     }
                 }
-                return defaultValue;
+                Maybe<E> result = Enums.valueOfIgnoreCase(type, input);
+                return (result.isPresent()) ? result.get() : defaultValue;
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/81d9ae7d/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java b/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
index c330f4e..90642b5 100644
--- a/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
+++ b/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
@@ -101,8 +101,21 @@ public class TypeCoercionsTest {
         assertEquals(TypeCoercions.coerce("STARTING", Lifecycle.class), Lifecycle.STARTING);
         assertEquals(TypeCoercions.coerce("Starting", Lifecycle.class), Lifecycle.STARTING);
         assertEquals(TypeCoercions.coerce("starting", Lifecycle.class), Lifecycle.STARTING);
+        
+        assertEquals(TypeCoercions.coerce("LOWERCASE", PerverseEnum.class), PerverseEnum.lowercase);
+        assertEquals(TypeCoercions.coerce("CAMELCASE", PerverseEnum.class), PerverseEnum.camelCase);
+        assertEquals(TypeCoercions.coerce("upper", PerverseEnum.class), PerverseEnum.UPPER);
+        assertEquals(TypeCoercions.coerce("upper_with_underscore", PerverseEnum.class), PerverseEnum.UPPER_WITH_UNDERSCORE);
+        assertEquals(TypeCoercions.coerce("LOWER_WITH_UNDERSCORE", PerverseEnum.class), PerverseEnum.lower_with_underscore);
+    }
+    public static enum PerverseEnum {
+        lowercase,
+        camelCase,
+        UPPER,
+        UPPER_WITH_UNDERSCORE,
+        lower_with_underscore;
     }
-
+    
     @Test(expectedExceptions = ClassCoercionException.class)
     public void testCoerceStringToEnumFailure() {
         TypeCoercions.coerce("scrambled-eggs", Lifecycle.class);


Mime
View raw message