Skip to content

Better docs in IOUtils and IOUtils.byteArray(int size)#374

Merged
garydgregory merged 15 commits intoapache:masterfrom
ArdenL-Liu:master
Sep 14, 2022
Merged

Better docs in IOUtils and IOUtils.byteArray(int size)#374
garydgregory merged 15 commits intoapache:masterfrom
ArdenL-Liu:master

Conversation

@ArdenL-Liu
Copy link
Contributor

IOUtils.byteArray(int size) add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.

…ize is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.
bytes = IOUtils.byteArray(size);
}catch (Exception e) {
assertEquals(e.getClass().getName(), IllegalArgumentException.class.getName());
}
Copy link
Member

Choose a reason for hiding this comment

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

I think instead it could use JUnit's @Test(expected=IllegalArgumentException.class), or assertThrows.

Copy link
Member

Choose a reason for hiding this comment

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

And I think when the first exception happens, the second is part of the test is never executed? Probably needs to be broken down into two separate tests, maybe with @ParameterizedTest too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I changed IOUtilsTest.testByteArrayWithIllegalSize() with @ParameterizedTest.
you can review the code.

Copy link
Member

@garydgregory garydgregory Aug 25, 2022

Choose a reason for hiding this comment

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

Why would we trade a more vague exception when the JVM throws a more precise and quite specific exception? Maybe this issue can be simplified with better Javadoc? This PR would also introduce an inconsistency since other places would throw the JVM exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi garydgregory,
Thank you for your good suggestion, I updated the doc and changed the unittest, you can review it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to improve my work efficiency,I will study github workflow in the next week.

* @param size array size.
* @return a new byte array of the given size.
*
* @exception IllegalArgumentException If {@code size <= 0}
Copy link
Member

Choose a reason for hiding this comment

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

@throws instead of @exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, update it with @throws

* Instances should NOT be constructed in standard programming.
* @deprecated Will be private in 3.0.
*/
@Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

This is doing more than what's described in the PR title. Maybe worth moving to a separate PR to make it easier to track in the changelog.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, I will rollback it

Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this deprecation as @kinow requested?

Copy link
Member

Choose a reason for hiding this comment

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

Wait... this deprecation is fine. It's slightly out of scope for the PR but it's fine.

@ArdenL-Liu ArdenL-Liu requested a review from kinow August 24, 2022 03:45
* Instances should NOT be constructed in standard programming.
* @deprecated Will be private in 3.0.
*/
@Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

Should this @Deprecated be removed? There was a comment to remove a different @Deprecated but this one should be kept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rollback it

Copy link
Member

Choose a reason for hiding this comment

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

-1: Don't remove this tag,

Copy link
Member

Choose a reason for hiding this comment

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

this is not resolved - you have still removed a comment that was in the code before - can you put it back?

Copy link
Member

Choose a reason for hiding this comment

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

The @deprecated tag should NOT be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi All,
sorry for my error operation, I fixed the doc and add @deprecated tag about FileUtils.


/**
* Returns a new byte array of the given size.
* Throws java.lang.NegativeArraySizeException if the size is negative.
Copy link
Member

Choose a reason for hiding this comment

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

This is in the wrong place, use a throws tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi garydgregory,
I fixed it with @throws, you can review it again.

@Test
public void testByteArrayWithNegativeSize() {
int size = -1;
assertThrows(NegativeArraySizeException.class,() -> {
Copy link
Member

@garydgregory garydgregory Aug 26, 2022

Choose a reason for hiding this comment

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

You can just say assertThrows(NegativeArraySizeException.class,() -> IOUtils.byteArray(-1)); but I am not even sure we should have this test since it does not improve our code coverage, it just test what the JVM does. @kinow , any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi garydgregory,
from your comments, I checked the code and fixed again, you can review it again.
Thanks.

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.

Hello @ArdenL-Liu
Thank you for the PR, please see my comments.

@codecov-commenter
Copy link

Codecov Report

Merging #374 (00a1e25) into master (e5683ee) will decrease coverage by 0.08%.
The diff coverage is n/a.

@@             Coverage Diff              @@
##             master     #374      +/-   ##
============================================
- Coverage     85.71%   85.63%   -0.09%     
  Complexity     3101     3101              
============================================
  Files           204      204              
  Lines          7365     7365              
  Branches        904      904              
============================================
- Hits           6313     6307       -6     
- Misses          804      811       +7     
+ Partials        248      247       -1     
Impacted Files Coverage Δ
src/main/java/org/apache/commons/io/FileUtils.java 94.42% <ø> (ø)
src/main/java/org/apache/commons/io/IOUtils.java 85.82% <ø> (ø)
.../apache/commons/io/input/ReadAheadInputStream.java 68.53% <0.00%> (-5.06%) ⬇️
.../main/java/org/apache/commons/io/input/Tailer.java 87.56% <0.00%> (+1.49%) ⬆️

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

*
* @param size array size.
* @return a new byte array of the given size.
* @throws NegativeArraySizeException if the size is negative.
Copy link
Member

Choose a reason for hiding this comment

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

This PR seems ok to me now except for this addition. We do not document this type of JVM exception anywhere else IIRC. If we were to doc this, then it would make sense to doc all callers as well, then this would touch almost everything which would not help anyone IMO.

Copy link
Contributor Author

@ArdenL-Liu ArdenL-Liu Aug 29, 2022

Choose a reason for hiding this comment

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

As yours, this is why I want to add throws NegativeArraySizeException tag to this method, In fact, in order to help users to use and understand these methods, I think we should add it to the doc of all the methods like it.
Of course , we can add the parameters validation to the head position of the method's logic. some methods do it, such as : read(...., int), skip(... ,int).

*/
package org.apache.commons.io;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Copy link
Member

Choose a reason for hiding this comment

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

Remove noise in the PR: There is no need to update these imports.

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.*;
Copy link
Member

Choose a reason for hiding this comment

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

"*" imports are a no go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@garydgregory
Copy link
Member

FYI: I've handled the Javadoc "java.lang" issue separately throughout most Apache Commons components.

@garydgregory
Copy link
Member

@ArdenL-Liu
Please rebase on git master and see any comments that may be outstanding.

@ArdenL-Liu
Copy link
Contributor Author

Hi garydgregory,
You're great. the comments updated in master line.

@ArdenL-Liu ArdenL-Liu requested review from garydgregory and removed request for kinow September 1, 2022 02:00
@ArdenL-Liu ArdenL-Liu requested review from garydgregory and kinow and removed request for garydgregory and kinow September 2, 2022 01:00
@garydgregory garydgregory changed the title IOUtils.byteArray(int size) add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException. Better docs in IOUtils and IOUtils.byteArray(int size) Sep 14, 2022
@garydgregory garydgregory merged commit d259b21 into apache:master Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants