Skip to content

Fixes a bug when failing assertions on DirectoryStream types#3036

Merged
joel-costigliola merged 1 commit intoassertj:mainfrom
ascopes:bugfix/assert-on-directory-streams
May 8, 2023
Merged

Fixes a bug when failing assertions on DirectoryStream types#3036
joel-costigliola merged 1 commit intoassertj:mainfrom
ascopes:bugfix/assert-on-directory-streams

Conversation

@ascopes
Copy link
Copy Markdown
Contributor

@ascopes ascopes commented May 7, 2023

The DirectoryStream class is somewhat special because the API allows it to only support .iterator() being called at most once on any implementations. Due to the nature of how the StandardRepresentation currently works for iterable types, any assertion error that is thrown as a result of assertions failing on a DirectoryStream object can result in IllegalStateExceptions being propagated to the caller if .iterator() has already been called on the class. This can break further if using soft assertions, as the state of the class could change unexpectedly resulting in different exceptions being raised depending on the execution order.

I've added a mechanism to deal with allowing the definition of so-called 'blacklisted' iterable types, just in case other cases like this appear in the JRE standard library anywhere in the future. This will prevent unexpected errors occuring when dealing with the NIO FileSystem APIs.

This was discovered as a result of ascopes/java-compiler-testing#450, where @marschall pointed out that the #iterator() on a DirectoryStream should only be called once in some implementations.

@ascopes ascopes force-pushed the bugfix/assert-on-directory-streams branch 2 times, most recently from a3da260 to f2fdf62 Compare May 7, 2023 14:07
@joel-costigliola
Copy link
Copy Markdown
Member

Thanks for the analysis and the PR @ascopes

Copy link
Copy Markdown
Member

@joel-costigliola joel-costigliola left a comment

Choose a reason for hiding this comment

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

looks good, just minor comments

The DirectoryStream class is somewhat special because the API allows it
to only support .iterator() being called at most once on any
implementations. Due to the nature of how the StandardRepresentation
currently works for iterable types, any assertion error that
is thrown as a result of assertions failing on a DirectoryStream
object can result in IllegalStateExceptions being propagated to
the caller if .iterator() has already been called on the class.
This can break further if using soft assertions, as the state of
the class could change unexpectedly resulting in different
exceptions being raised depending on the execution order.

I've added a mechanism to deal with allowing the definition of
so-called 'blacklisted' iterable types, just in case other
cases like this appear in the JRE standard library anywhere
in the future. This will prevent unexpected errors occuring
when dealing with the NIO FileSystem APIs.
@ascopes ascopes force-pushed the bugfix/assert-on-directory-streams branch from f2fdf62 to 51792a2 Compare May 8, 2023 10:07
@ascopes ascopes requested a review from joel-costigliola May 8, 2023 10:07
@ascopes
Copy link
Copy Markdown
Contributor Author

ascopes commented May 8, 2023

@joel-costigliola updated. Thanks for the quick response.

@joel-costigliola joel-costigliola merged commit 5f7b722 into assertj:main May 8, 2023
@joel-costigliola
Copy link
Copy Markdown
Member

Integrated thanks @ascopes !

@ascopes ascopes deleted the bugfix/assert-on-directory-streams branch May 24, 2023 12:22
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.

2 participants