diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index 82e75f8b7..a3d7ad096 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -283,8 +283,8 @@ public class StringUtils { } public static String randomString(final int length, Random random) { - if (length < 1) { - return null; + if (length == 0) { + return ""; } byte[] randomBytes = new byte[length]; diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java b/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java index 0cff5a730..7679a118e 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java @@ -1,6 +1,6 @@ /** * - * Copyright 2003-2007 Jive Software. + * Copyright 2003-2007 Jive Software, 2019 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,13 +88,7 @@ public class StringUtilsTest { @Test public void testRandomString() { - // Boundary test - String result = StringUtils.randomString(-1); - assertNull(result); - - // Zero length string test - result = StringUtils.randomString(0); - assertNull(result); + String result; // Test various lengths - make sure the same length is returned result = StringUtils.randomString(4); @@ -104,4 +98,17 @@ public class StringUtilsTest { result = StringUtils.randomString(128); assertTrue(result.length() == 128); } + + @Test(expected = NegativeArraySizeException.class) + public void testNegativeArraySizeException() { + // Boundary test + StringUtils.randomString(-1); + } + + @Test + public void testZeroLengthRandomString() { + // Zero length string test + String result = StringUtils.randomString(0); + assertEquals("", result); + } }