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

Refactor: AssertJ best practices by @timtebeek #1454

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lprimak
Copy link
Contributor

@lprimak lprimak commented May 3, 2024

Introduce AssertJ by @timtebeek
Supersedes #1450 and #1452

Following this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a GitHub issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a GitHub issue. Your pull request should address just this issue, without pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [#XXX] - Fixes bug in SessionManager,
    where you replace #XXX with the appropriate GitHub issue. Best practice
    is to use the GitHub issue title in the pull request title and in the first line of the commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • add fixes #XXX if merging the PR should close a related issue.
  • Run mvn verify to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
  • If you have a group of commits related to the same change, please squash your commits into one and force push your branch using git rebase -i.
  • Committers: Make sure a milestone is set on the PR

Trivial changes like typos do not require a GitHub issue (javadoc, comments...).
In this case, just format the pull request title like [DOC] - Add javadoc in SessionManager.

If this is your first contribution, you have to read the Contribution Guidelines

If your pull request is about ~20 lines of code you don't need to sign an Individual Contributor License Agreement
if you are unsure please ask on the developers list.

To make clear that you license your contribution under the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@lprimak lprimak added this to the 2.0.1 milestone May 3, 2024
@lprimak lprimak added java Pull requests that update Java code core Core Modules labels May 3, 2024
Copy link
Contributor Author

@lprimak lprimak left a comment

Choose a reason for hiding this comment

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

@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

Hi, @timtebeek
Unfortunately, I had to revert the AssertJ refactor. There are 66 new warnings that have resulted from the compiler.

@timtebeek
Copy link
Contributor

It seems 51 of those warnings are in JndiLdapContextFactoryTest, with cases like this one:
image

For comparison, this is what was there before, with assertEquals taking two objects as input:
image

What would you recommend we do about such instances?

@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

And once again, proper testing reveals underlying code issues :) working on it

@lprimak lprimak marked this pull request as ready for review May 3, 2024 15:37
@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

yay! It's green https://jenkins.hope.nyc.ny.us/job/docker-shiro-ci/job/assert-j-best-practices/1/

@lprimak lprimak requested review from bdemers and bmarwell May 3, 2024 15:59
@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

Please pay special attention to this commit: 798cd18

I believe this is safe to put into 2.0.1 version because the API doesn't really change and just the generics are made explicit.
What do you think?

@bmarwell
Copy link
Contributor

bmarwell commented May 3, 2024

Please pay special attention to this commit: 798cd18

I believe this is safe to put into 2.0.1 version because the API doesn't really change and just the generics are made explicit.
What do you think?

Urgh. By definition a breaking change! Maybe put the 3.x label on that commit?

It's probably safe though

@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

My understanding of Java generics erasure is that this change is source and binary compatible with the previous, thus not a breaking change... but I could be wrong.
In former case it would be safe to merge for 2.0.1 and latter it isn't :)
I also think it's early enough in the lifecycle of 2.x that this would be safe to merge

@bmarwell
Copy link
Contributor

bmarwell commented May 3, 2024

Binary: yes.
Source compatible: most likely yes, but not if a user explicitly defined something else like Object,Object. Then no.

I'd be okay with this one as it is early 2.0.x for that reason.

@bdemers @fpapon Opinions?

@lprimak
Copy link
Contributor Author

lprimak commented May 3, 2024

but not if a user explicitly defined something else like Object,Object.

Since previous code was "raw" class without any parameters, they would have to cast in that case anyway...

@fpapon
Copy link
Member

fpapon commented May 4, 2024

I think the problem can be this one about api change:

public void setEnvironment(Map<String, Object> env) {

For now user can put any type in the key and we can imagine that he should use a String but we are not sure.

I understand the purpose of the best practice but it's not a good idea to update our api because of unit test framework change.

@bmarwell
Copy link
Contributor

bmarwell commented May 4, 2024

I think the problem can be this one about api change:

public void setEnvironment(Map<String, Object> env) {

For now user can put any type in the key and we can imagine that he should use a String but we are not sure.

I understand the purpose of the best practice but it's not a good idea to update our api because of unit test framework change.

Yes, that's a real no go and needs to wait until 3.

@lprimak
Copy link
Contributor Author

lprimak commented May 5, 2024

Yup. As disappointing as this sounds, this one will have to wait

@lprimak lprimak modified the milestones: 2.0.1, 3.0.0 May 5, 2024
@bmarwell
Copy link
Contributor

bmarwell commented May 6, 2024

@lprimak just modify your branch to leave that one out and merge it into 2.0.1

Then do another for 3.0.0 with that change.

@bdemers
Copy link
Member

bdemers commented May 6, 2024

Just catching up after some travel.

Would most of the concerns here go away if we created a custom assertion for JndiLdapContextFactory and PrincipalCollection?

That way we would at least get the migration to AssertJ in (thanks again @timtebeek).
And we can create an issue/TODO to cleanup the type <> everywhere.
Speaking of... is there an OpenRewrite recipe for that? 😄

@bdemers
Copy link
Member

bdemers commented May 6, 2024

(I can volunteer to write the assertion class, if that resolves the issues), it would probably read something like:

Shiro.assertThat(factory).containsEnvironment("foo, "bar");

@timtebeek
Copy link
Contributor

And we can create an issue/TODO to cleanup the type <> everywhere. Speaking of... is there an OpenRewrite recipe for that? 😄

Absolutely! Here's a quick recipe run: https://app.moderne.io/results/j2iJu9Xxx/
And a branch you can use to create a PR to clear out those 152 cases.
https://github.com/apache/shiro/compare/main...timtebeek:shiro:refactor/use-diamond-operator?expand=1

@lprimak
Copy link
Contributor Author

lprimak commented May 6, 2024

Yea, that sounds great! If there are no warnings it's all good by me :)

@lprimak lprimak marked this pull request as draft May 6, 2024 16:47
@lprimak lprimak modified the milestones: 3.0.0, 2.0.1 May 6, 2024
@lprimak
Copy link
Contributor Author

lprimak commented May 6, 2024

I have reverted the API change commit.

@bdemers
Copy link
Member

bdemers commented May 6, 2024

I started hacking a custom Assertion class up, and IMHO, it's too complex for the value it provides.

The value is essentially, working around a valid compiler warning. The better solution for that is doing something like:
@SuppressWarnings("unchecked") to the relevant tests. We have a bunch of suppressions in the tests already.

My current plan is to open an issue to fix the generic type defs, and mark the effected test methods with something like:

@SuppressWarning("unchecked") // TODO: Remove this warning when issue #xxxx is resolved
public void someTestMethod() {
    // ...
    assertThat(env).containsEntry(Context.SECURITY_PRINCIPAL, "foo");
    // ...
}

Any objections to this approach?

Note

I do think custom assertion classes could be useful in other places, just in this case.

@lprimak
Copy link
Contributor Author

lprimak commented May 6, 2024

No onjections from me.

@lprimak lprimak modified the milestones: 2.0.1, 2.0.2 May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core Modules java Pull requests that update Java code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants