[java-support] branch master updated: IDP-1450 align booleanOf with the spec for xs:boolean

Rod Widdowson rdw at steadingsoftware.com
Mon Jul 22 08:43:32 EDT 2019


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=e49aed5fe3b92caeecde26645f6ba49d54391c8b

The following commit(s) were added to refs/heads/master by this push:
       new  e49aed5   IDP-1450 align booleanOf with the spec for xs:boolean
e49aed5 is described below

commit e49aed5fe3b92caeecde26645f6ba49d54391c8b
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Mon Jul 22 13:42:22 2019 +0100

    IDP-1450 align booleanOf with the spec for xs:boolean
    
    https://issues.shibboleth.net/jira/browse/IDP-1450
---
 .../java/support/primitive/StringSupport.java      | 14 +++-
 .../java/support/primitive/StringSupportTest.java  | 88 ++++++++++++----------
 2 files changed, 62 insertions(+), 40 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/primitive/StringSupport.java b/src/main/java/net/shibboleth/utilities/java/support/primitive/StringSupport.java
index 800ea61..f41ec0d 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/primitive/StringSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/primitive/StringSupport.java
@@ -39,6 +39,7 @@ import com.google.common.collect.Collections2;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.annotation.constraint.NullableElements;
 import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
 import net.shibboleth.utilities.java.support.logic.TrimOrNullStringFunction;
 
 /** String utility methods. */
@@ -182,7 +183,7 @@ public final class StringSupport {
                 Predicates.notNull());
     }
 
-    /** Null/empty preserving conversion from string to {@link Boolean}.
+    /** Null/empty preserving conversion from xs:boolean to {@link Boolean}.
      * @param what the string: potentially empty or null
      * @return null or the boolean equivalent.
      */
@@ -191,6 +192,15 @@ public final class StringSupport {
         if (trimmed == null) {
             return null;
         }
-        return Boolean.valueOf(trimmed);
+        if ("1".equals(what)) {
+            return true;
+        } else if ("0".equals(what)) {
+            return false;
+        } else if ("true".equals(what)) {
+            return true;
+        } else if ("false".equals(what)) {
+            return false;
+        }
+        throw new ConstraintViolationException("XML Booleans must be 0/1/true/false");
     }
 }
diff --git a/src/test/java/net/shibboleth/utilities/java/support/primitive/StringSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/primitive/StringSupportTest.java
index 3a33d58..33a6624 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/primitive/StringSupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/primitive/StringSupportTest.java
@@ -17,6 +17,13 @@
 
 package net.shibboleth.utilities.java.support.primitive;
 
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
@@ -28,7 +35,6 @@ import javax.annotation.Nonnull;
 
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
-import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
@@ -55,15 +61,15 @@ public class StringSupportTest {
         try (final InputStream stream = resource.getInputStream()) {
             str = StringSupport.inputStreamToString(stream, null);
         }
-        Assert.assertNotNull(str);
-        Assert.assertEquals(str,
+        assertNotNull(str);
+        assertEquals(str,
                 "The quick, brown lizard jumped over the lazy fish.\n" +
                 "Wait, I mean the slow, blue elephant jumped over the motivated squirrel.\n" +
                 "No, that's wrong too.\n");
     }
     
     @Test public void testListToStringValue() {
-        Assert.assertEquals(StringSupport.listToStringValue(TEST_LIST_AS_LIST, SEPARATOR), TEST_LIST,
+        assertEquals(StringSupport.listToStringValue(TEST_LIST_AS_LIST, SEPARATOR), TEST_LIST,
                 "toList<String> fails");
         boolean thrown = false;
         try {
@@ -71,7 +77,7 @@ public class StringSupportTest {
         } catch (ConstraintViolationException e) {
             thrown = true;
         }
-        Assert.assertTrue(thrown, "null separator should throw an assertion");
+        assertTrue(thrown, "null separator should throw an assertion");
 
         thrown = false;
         try {
@@ -79,13 +85,13 @@ public class StringSupportTest {
         } catch (ConstraintViolationException e) {
             thrown = true;
         }
-        Assert.assertTrue(thrown, "null list should throw an assertion");
+        assertTrue(thrown, "null list should throw an assertion");
     }
 
     @Test public void testStringToList() {
-        Assert.assertEquals(StringSupport.stringToList(TEST_LIST, SEPARATOR), TEST_LIST_AS_LIST,
+        assertEquals(StringSupport.stringToList(TEST_LIST, SEPARATOR), TEST_LIST_AS_LIST,
                 "from List<String> fails");
-        Assert.assertTrue(StringSupport.stringToList("", SEPARATOR).isEmpty(), "Empty input should give empty list");
+        assertTrue(StringSupport.stringToList("", SEPARATOR).isEmpty(), "Empty input should give empty list");
 
         boolean thrown = false;
         try {
@@ -93,7 +99,7 @@ public class StringSupportTest {
         } catch (ConstraintViolationException e) {
             thrown = true;
         }
-        Assert.assertTrue(thrown, "Null input should throw an assertion");
+        assertTrue(thrown, "Null input should throw an assertion");
 
         thrown = false;
         try {
@@ -101,25 +107,25 @@ public class StringSupportTest {
         } catch (ConstraintViolationException e) {
             thrown = true;
         }
-        Assert.assertTrue(thrown, "Null separator should throw an assertion");
+        assertTrue(thrown, "Null separator should throw an assertion");
     }
 
     @Test public void testTrim() {
 
-        Assert.assertEquals(StringSupport.trim(null), null, "Trimming Null should be OK");
-        Assert.assertEquals(StringSupport.trim(EMPTY_TRIM_TEST2).length(), 0,
+        assertEquals(StringSupport.trim(null), null, "Trimming Null should be OK");
+        assertEquals(StringSupport.trim(EMPTY_TRIM_TEST2).length(), 0,
                 "Trimming an empty string should return a string of zero length");
 
-        Assert.assertEquals(StringSupport.trim(TRIM_TEST1), TRIM_TEST1.trim(), "Trimming a string");
+        assertEquals(StringSupport.trim(TRIM_TEST1), TRIM_TEST1.trim(), "Trimming a string");
 
     }
 
     @Test public void testTrimOrNull() {
-        Assert.assertEquals(StringSupport.trimOrNull(null), null, "Trimming Null should be OK");
-        Assert.assertEquals(StringSupport.trimOrNull(EMPTY_TRIM_TEST2), null,
+        assertEquals(StringSupport.trimOrNull(null), null, "Trimming Null should be OK");
+        assertEquals(StringSupport.trimOrNull(EMPTY_TRIM_TEST2), null,
                 "Trimming an empty string should return null");
 
-        Assert.assertEquals(StringSupport.trim(TRIM_TEST1), TRIM_TEST1.trim(), "Trimming a string");
+        assertEquals(StringSupport.trim(TRIM_TEST1), TRIM_TEST1.trim(), "Trimming a string");
 
     }
     
@@ -127,26 +133,26 @@ public class StringSupportTest {
         Collection<String> output;
         
         output = StringSupport.normalizeStringCollection(new HashSet<>(Arrays.asList("foo", "bar", "baz")));
-        Assert.assertEquals(output.size(), 3);
-        Assert.assertTrue(output.contains("foo"));
-        Assert.assertTrue(output.contains("bar"));
-        Assert.assertTrue(output.contains("baz"));
+        assertEquals(output.size(), 3);
+        assertTrue(output.contains("foo"));
+        assertTrue(output.contains("bar"));
+        assertTrue(output.contains("baz"));
         
         output = StringSupport.normalizeStringCollection(new HashSet<>(Arrays.asList(" \t\t foo  ", "  ", "  baz \r\n")));
-        Assert.assertEquals(output.size(), 2);
-        Assert.assertTrue(output.contains("foo"));
-        Assert.assertTrue(output.contains("baz"));
+        assertEquals(output.size(), 2);
+        assertTrue(output.contains("foo"));
+        assertTrue(output.contains("baz"));
         
         output = StringSupport.normalizeStringCollection(new HashSet<>(Arrays.asList("   foo   ", null, "baz")));
-        Assert.assertEquals(output.size(), 2);
-        Assert.assertTrue(output.contains("foo"));
-        Assert.assertTrue(output.contains("baz"));
+        assertEquals(output.size(), 2);
+        assertTrue(output.contains("foo"));
+        assertTrue(output.contains("baz"));
         
         output = StringSupport.normalizeStringCollection(new HashSet<String>());
-        Assert.assertEquals(output.size(), 0);
+        assertEquals(output.size(), 0);
         
         output = StringSupport.normalizeStringCollection(null);
-        Assert.assertEquals(output.size(), 0);
+        assertEquals(output.size(), 0);
     }
 
     private <T> T nullValue() {
@@ -154,15 +160,21 @@ public class StringSupportTest {
     }
 
     @Test public void testToBoolean() {
-        Assert.assertNull(StringSupport.booleanOf(""));
-        Assert.assertFalse(Boolean.valueOf(""));
-        Assert.assertNull(StringSupport.booleanOf(null));
-        Assert.assertFalse(Boolean.valueOf(null));
-        Assert.assertTrue(StringSupport.booleanOf("true"));
-        Assert.assertTrue(Boolean.valueOf("true"));
-        Assert.assertFalse(StringSupport.booleanOf("false"));
-        Assert.assertFalse(Boolean.valueOf("false"));
-        Assert.assertFalse(StringSupport.booleanOf("elephant"));
-        Assert.assertFalse(Boolean.valueOf("elephant"));
+        assertNull(StringSupport.booleanOf(""));
+        assertFalse(Boolean.valueOf(""));
+        assertNull(StringSupport.booleanOf(null));
+        assertFalse(Boolean.valueOf(null));
+        assertTrue(StringSupport.booleanOf("true"));
+        assertTrue(Boolean.valueOf("true"));
+        assertFalse(StringSupport.booleanOf("false"));
+        assertFalse(Boolean.valueOf("false"));
+        assertFalse(StringSupport.booleanOf("0"));
+        assertTrue(StringSupport.booleanOf("1"));
+        try {
+            StringSupport.booleanOf("elephant");
+            fail("Should have thrown");
+        } catch (ConstraintViolationException e) {
+            // OK
+        }
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list