Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException #434

Merged
merged 5 commits into from
Jun 27, 2023
Merged

Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException #434

merged 5 commits into from
Jun 27, 2023

Conversation

orionlibs
Copy link
Contributor

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Jun 26, 2023

Codecov Report

Merging #434 (5b32e42) into master (6c2b7c0) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##             master     #434   +/-   ##
=========================================
  Coverage     97.14%   97.14%           
  Complexity     2333     2333           
=========================================
  Files            84       84           
  Lines          5780     5781    +1     
  Branches        936      937    +1     
=========================================
+ Hits           5615     5616    +1     
  Misses           87       87           
  Partials         78       78           
Impacted Files Coverage Δ
...ava/org/apache/commons/text/AlphabetConverter.java 96.59% <ø> (ø)
...org/apache/commons/text/RandomStringGenerator.java 94.80% <100.00%> (+0.06%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

characterList = new ArrayList<>();
for (final char c : chars) {
characterList.add(c);
if(chars != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkstyle issues in the PR (you can run mvn locally and that should do the same as GH Actions).

 Error:  src/main/java/org/apache/commons/text/RandomStringGenerator.java:[244,13] (whitespace) WhitespaceAfter: 'if' is not followed by whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it. Yes. I see the error locally. Thank you

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1: See comments.

final String str = "abc";
final RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFrom(null).build();
final String randomText = generator.generate(5);
assertThat(randomText.isEmpty()).isFalse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

for (final char c : chars) {
characterList.add(c);
if (chars != null) {
characterList = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-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?

…alled, regardless of the method input

--I changed the test to assert the length of the random text
…to TEXT-make-selectFrom-null-safe-RandomStringGenerator
@garydgregory garydgregory changed the title added null-check to avoid NullPointerException Add null-check in RandomStringGenerator#selectFrom() to avoid NullPointerException Jun 27, 2023
@garydgregory garydgregory merged commit ce20118 into apache:master Jun 27, 2023
6 checks passed
@orionlibs orionlibs deleted the TEXT-make-selectFrom-null-safe-RandomStringGenerator branch June 27, 2023 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants