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 example illustrating how more advanced method-to-method dependencies can be checked #1058

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

perlun
Copy link
Contributor

@perlun perlun commented Feb 10, 2023

Based on an example provided in #235, this example aims to help people who are working on similar use cases in their projects.

@perlun

This comment was marked as resolved.

…ies can be checked

Based on an example provided in
TNG#235, this example aims to help
people who are working on similar use cases in their projects.

Signed-off-by: Per Lundberg <per.lundberg@hibox.tv>
@perlun perlun force-pushed the add-method-from-only-particular-methods-example branch from 3aa755d to 87e81ec Compare February 10, 2023 14:11
Copy link
Collaborator

@codecholeric codecholeric left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your contribution and sorry for the late follow-up! I looked through it and I think it's a good idea to add another somewhat realistic use case of what can be done with ArchUnit! I added some comments for things I noticed.
One thing I'm not sure about is that it seems a little mingled to me what is about ArchUnit and what is about this specific pattern (singleton with wrong usage). I would try to compress everything that's related to the example itself in one central place, e.g. as Javadoc on the test method. I.e. what is the scenario, what is the anti-pattern, etc. So that inline comments then really only apply to ArchUnit's application itself. Also, I don't think we need comments about how to improve the singleton pattern (like "use Guava"), since that's not the point of the example anyway, right? Cause I'm afraid it might then just clutter the essence how we apply ArchUnit 🤷

*/
public class SingletonClass {

// Poor man's reimplementation of Guava's memoization support. If your project supports Guava,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would drop this comment. The example is about using ArchUnit, not how you should implement a singleton, given library x, right? 🤔 From my perspective you could even drop all code, leave it empty and just write comments // assume implemented, because the internals of this singleton or if it even works is irrelevant for the example / test, no? 🤔

import java.util.concurrent.ConcurrentHashMap;

/**
* @author Per Lundberg
Copy link
Collaborator

Choose a reason for hiding this comment

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

We generally don't us the @author tag, since you can always see that from the commit history anyway...

"to override the dependencies for tests, and also means the dependencies are much harder to identify when " +
"quickly looking at the code. Instead, move all getInstance() calls to the INSTANCE supplier and pass the " +
"dependency to the constructor that way. If this is impossible for a particular case, add the class name to " +
"the whitelist in " + getClass().getName() )
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"the whitelist in " + getClass().getName() )
"the whitelist in the `should` clause above"

}

private ArchCondition<JavaMethod> onlyBeCalledFromWhitelistedOrigins( String... whitelistedOrigins ) {
return new ArchCondition<JavaMethod>( "only be called by whitelisted methods" ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return new ArchCondition<JavaMethod>( "only be called by whitelisted methods" ) {
return new ArchCondition<JavaMethod>( "only be called from whitelisted origins" ) {

Comment on lines +55 to +59
// TODO: Add your own exceptions as needed here, if you have particular
// TODO: use cases where getInstance() calls are permissible.
// Static getInstance() methods are always allowed to call getInstance. This
// does not break dependency injection and does not come with significant
// design flaws.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would drop this comment. It's just an example, it's clear that somebody needs to adjust this to their need. If you want to explain theory behind the test case itself I'd rather do it as Javadoc on the test class or test method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Otherwise, the comments just make it harder to quickly get through the code 🤷‍♂️

.check( classes );
}

private ArchCondition<JavaMethod> onlyBeCalledFromWhitelistedOrigins( String... whitelistedOrigins ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically this is onlyBeCalledFromOtherStaticGetInstanceMethodsOrWhitelistedOrigins? 🤔

// Static getInstance() methods are always allowed to call getInstance. This
// does not break dependency injection and does not come with significant
// design flaws.
.filter( call -> !( Objects.equals( call.getOrigin().getName(), "getInstance" ) && call.getOrigin().getModifiers().contains( JavaModifier.STATIC ) ) )
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe extract this as isNotFromStaticGetInstanceMethod(call)? 🤔

Comment on lines +64 to +70
.filter( call -> {
Optional<String> result = Arrays.stream( whitelistedOrigins )
.filter( o -> call.getOrigin().getFullName().startsWith( o ) )
.findFirst();

return !result.isPresent();
} )
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.filter( call -> {
Optional<String> result = Arrays.stream( whitelistedOrigins )
.filter( o -> call.getOrigin().getFullName().startsWith( o ) )
.findFirst();
return !result.isPresent();
} )
.filter(call -> Arrays.stream(whitelistedOrigins).noneMatch(it -> it.equals(call.getOriginOwner().getName())))

@codecholeric codecholeric self-requested a review January 1, 2024 16:26
Copy link
Collaborator

@codecholeric codecholeric left a comment

Choose a reason for hiding this comment

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

Please let me know what you think about my comments 🙂

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