You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "garydgregory (via GitHub)" <gi...@apache.org> on 2023/06/26 20:37:38 UTC

[GitHub] [commons-text] garydgregory commented on a diff in pull request #434: added null-check to avoid NullPointerException

garydgregory commented on code in PR #434:
URL: https://github.com/apache/commons-text/pull/434#discussion_r1242733312


##########
src/test/java/org/apache/commons/text/RandomStringGeneratorTest.java:
##########
@@ -212,6 +212,14 @@ public void testSelectFromCharVarargs() {
         }
     }
 
+    @Test
+    public void testSelectFromNullCharVarargs() {
+        final String str = "abc";
+        final RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFrom(null).build();
+        final String randomText = generator.generate(5);
+        assertThat(randomText.isEmpty()).isFalse();

Review Comment:
   You need to assert the results are correct, not that any old output was generated.



##########
src/main/java/org/apache/commons/text/RandomStringGenerator.java:
##########
@@ -183,9 +183,11 @@ public Builder filteredBy(final CharacterPredicate... predicates) {
          * @since 1.2
          */
         public Builder selectFrom(final char... chars) {
-            characterList = new ArrayList<>();
-            for (final char c : chars) {
-                characterList.add(c);
+            if (chars != null) {
+                characterList = new ArrayList<>();

Review Comment:
   -1: This PR breaks the documented contract in the Javadoc (the contract is still broken for null). This is unexpected because the consequence of null input should be the same as empty (size 0) input but in fact, is not. 
   For example:
   ```
   builder.
     selectFrom('a')
     selectFrom();
   ```
   gives us a `characterList` of `[]` but:
   ```
   builder.
     selectFrom('a')
     selectFrom(null);
   ```
   gives us a `characterList` of `['a']` instead of `[]`.
   IOW, null input and empty should be the same. If not, why not? Or am I missing something? Why doesn't `selectFrom()` always reset `characterList`? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org