brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/3] git commit: add unit tests to testCoerceStringToPrimitive
Date Tue, 15 Jul 2014 15:20:11 GMT
add unit tests to testCoerceStringToPrimitive


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

Branch: refs/heads/master
Commit: ab9de806b3d51a22c83ec4946804e44d6a8ef8f5
Parents: de6fbd3
Author: Andrea Turli <andrea.turli@gmail.com>
Authored: Mon Jul 14 13:30:00 2014 +0200
Committer: Andrea Turli <andrea.turli@gmail.com>
Committed: Mon Jul 14 13:30:00 2014 +0200

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


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab9de806/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 3036654..e25f1b7 100644
--- a/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
+++ b/core/src/main/java/brooklyn/util/flags/TypeCoercions.java
@@ -325,7 +325,6 @@ public class TypeCoercions {
     @SuppressWarnings("unchecked")
     public static <T> T stringToPrimitive(String value, Class<T> targetType)
{
         assert Primitives.allPrimitiveTypes().contains(targetType) || Primitives.allWrapperTypes().contains(targetType)
: "targetType="+targetType;
-        value = value.trim();
         // If char, then need to do explicit conversion
         if (targetType == Character.class || targetType == char.class) {
             if (value.length() == 1) {
@@ -334,7 +333,7 @@ public class TypeCoercions {
                 throw new ClassCoercionException("Cannot coerce type String to "+targetType.getCanonicalName()+"
("+value+"): adapting failed");
             }
         }
-
+        value = value.trim();
         // For boolean we could use valueOf, but that returns false whereas we'd rather throw
errors on bad values
         if (targetType == Boolean.class || targetType == boolean.class) {
             if ("true".equalsIgnoreCase(value)) return (T) Boolean.TRUE;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ab9de806/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 e6aee19..05b4c72 100644
--- a/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
+++ b/core/src/test/java/brooklyn/util/internal/TypeCoercionsTest.java
@@ -32,17 +32,17 @@ import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-import brooklyn.entity.basic.Lifecycle;
-import brooklyn.util.flags.ClassCoercionException;
-import brooklyn.util.flags.TypeCoercions;
-import brooklyn.util.text.StringPredicates;
-
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.reflect.TypeToken;
 
+import brooklyn.entity.basic.Lifecycle;
+import brooklyn.util.flags.ClassCoercionException;
+import brooklyn.util.flags.TypeCoercions;
+import brooklyn.util.text.StringPredicates;
+
 public class TypeCoercionsTest {
 
     private static final Logger log = LoggerFactory.getLogger(TypeCoercionsTest.class);
@@ -56,6 +56,7 @@ public class TypeCoercionsTest {
     @Test
     public void testCoerceStringToPrimitive() {
         assertEquals(TypeCoercions.coerce("1", Character.class), (Character)'1');
+        assertEquals(TypeCoercions.coerce(" ", Character.class), (Character)' ');
         assertEquals(TypeCoercions.coerce("1", Short.class), (Short)((short)1));
         assertEquals(TypeCoercions.coerce("1", Integer.class), (Integer)1);
         assertEquals(TypeCoercions.coerce("1", Long.class), (Long)1l);
@@ -63,7 +64,8 @@ public class TypeCoercionsTest {
         assertEquals(TypeCoercions.coerce("1", Double.class), (Double)1d);
         assertEquals(TypeCoercions.coerce("true", Boolean.class), (Boolean)true);
         assertEquals(TypeCoercions.coerce("False", Boolean.class), (Boolean)false);
-        
+        assertEquals(TypeCoercions.coerce("true ", Boolean.class), (Boolean)true);
+
         assertEquals(TypeCoercions.coerce("1", char.class), (Character)'1');
         assertEquals(TypeCoercions.coerce("1", short.class), (Short)((short)1));
         assertEquals(TypeCoercions.coerce("1", int.class), (Integer)1);


Mime
View raw message