brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/5] brooklyn-server git commit: JavaGroovyEquivalents improvements/tests
Date Thu, 12 Jan 2017 10:32:31 GMT
JavaGroovyEquivalents improvements/tests


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

Branch: refs/heads/master
Commit: 70c1bae18d2a7f5f912b2f63a47f58b07132665c
Parents: 1e24e82
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Jan 11 14:40:05 2017 +0000
Committer: Вальо <bostko@users.noreply.github.com>
Committed: Thu Jan 12 12:07:51 2017 +0200

----------------------------------------------------------------------
 .../brooklyn/util/JavaGroovyEquivalents.java    |  7 +-
 .../util/JavaGroovyEquivalentsTest.java         | 83 ++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70c1bae1/utils/common/src/main/java/org/apache/brooklyn/util/JavaGroovyEquivalents.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/JavaGroovyEquivalents.java
b/utils/common/src/main/java/org/apache/brooklyn/util/JavaGroovyEquivalents.java
index 6c25ea7..c6b8050 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/JavaGroovyEquivalents.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/JavaGroovyEquivalents.java
@@ -66,8 +66,9 @@ public class JavaGroovyEquivalents {
     public static String elvisString(Object preferred, Object fallback) {
         return elvis(asString(preferred), asString(fallback));
     }
+    @SuppressWarnings("unchecked")
     public static <T> T elvis(T preferred, T fallback) {
-        return groovyTruth(preferred) ? preferred : fallback;
+        return (T) fix(groovyTruth(preferred) ? preferred : fallback);
     }
     public static <T> T elvis(Iterable<?> preferences) {
         return elvis(Iterables.toArray(preferences, Object.class));
@@ -117,6 +118,10 @@ public class JavaGroovyEquivalents {
             return ((Iterator<?>)o).hasNext();
         } else if (o instanceof Enumeration) {
             return ((Enumeration<?>)o).hasMoreElements();
+        } else if (o instanceof Number) {
+            return ((Number)o).doubleValue() != 0d;
+        } else if (o instanceof CharSequence) {
+            return ((CharSequence)o).length() > 0;
         } else {
             return true;
         }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70c1bae1/utils/common/src/test/java/org/apache/brooklyn/util/JavaGroovyEquivalentsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/JavaGroovyEquivalentsTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/JavaGroovyEquivalentsTest.java
new file mode 100644
index 0000000..8871cce
--- /dev/null
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/JavaGroovyEquivalentsTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.brooklyn.util;
+
+import static org.apache.brooklyn.util.JavaGroovyEquivalents.elvis;
+import static org.apache.brooklyn.util.JavaGroovyEquivalents.groovyTruth;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.util.List;
+
+import org.codehaus.groovy.runtime.GStringImpl;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+
+import groovy.lang.GString;
+
+public class JavaGroovyEquivalentsTest {
+
+    private String gstringVal = "exampleGString";
+    private GString gstring = new GStringImpl(new Object[0], new String[] {gstringVal});
+    private GString emptyGstring = new GStringImpl(new Object[0], new String[] {""});
+
+    @Test
+    public void testTruth() {
+        assertFalse(groovyTruth((Object)null));
+        assertTrue(groovyTruth("someString"));
+        assertFalse(groovyTruth(""));
+        assertTrue(groovyTruth(1));
+        assertTrue(groovyTruth((byte)1));
+        assertTrue(groovyTruth((short)1));
+        assertTrue(groovyTruth(1L));
+        assertTrue(groovyTruth(1F));
+        assertTrue(groovyTruth(1D));
+        assertFalse(groovyTruth(0));
+        assertFalse(groovyTruth((byte)0));
+        assertFalse(groovyTruth((short)0));
+        assertFalse(groovyTruth(0L));
+        assertFalse(groovyTruth(0F));
+        assertFalse(groovyTruth(0D));
+        assertTrue(groovyTruth(true));
+        assertFalse(groovyTruth(false));
+        assertTrue(groovyTruth(gstring));
+        assertFalse(groovyTruth(emptyGstring));
+    }
+
+    @Test
+    public void testElvis() {
+        final List<?> emptyList = ImmutableList.of();
+        final List<?> singletonList = ImmutableList.of("myVal");
+        final List<?> differentList = ImmutableList.of("differentVal");
+        
+        assertEquals(elvis("", "string2"), "string2");
+        assertEquals(elvis("string1", "string2"), "string1");
+        assertEquals(elvis(null, "string2"), "string2");
+        assertEquals(elvis("", "string2"), "string2");
+        assertEquals(elvis(1, 2), Integer.valueOf(1));
+        assertEquals(elvis(0, 2), Integer.valueOf(2));
+        assertEquals(elvis(singletonList, differentList), singletonList);
+        assertEquals(elvis(emptyList, differentList), differentList);
+        assertEquals(elvis(gstring, "other"), gstringVal);
+        assertEquals(elvis(emptyGstring, "other"), "other");
+        assertEquals(elvis(emptyGstring, gstring), gstringVal);
+    }
+}


Mime
View raw message