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

improve rule to not fail for classes with no tests in their packages #1368

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

krzysztof-owczarek
Copy link

@krzysztof-owczarek krzysztof-owczarek commented Oct 23, 2024

This will improve the existing rule to test that test classes reside in the same package as their implementation. In particular the rule:

Should not fail if there are multiple test classes with the same simple name and some of the classes have no tests at all

Issue: #1367

@krzysztof-owczarek krzysztof-owczarek force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from 3012ef7 to 28c2894 Compare October 23, 2024 20:02
Copy link
Member

@hankem hankem 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! 💚
I'd like to take a closer look whether we can simplify the rule, but I already wanted to leave one comment on the test. 😉

@@ -0,0 +1,4 @@
package com.tngtech.archunit.library.testclasses.packages.correct.twoimplementationsonetestdir1;

public class OnlyOneImplementationHaveTestAndItIsMatchingImplPackageTest {
Copy link
Member

Choose a reason for hiding this comment

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

For your test to cover the use case of #1367, this should be

Suggested change
public class OnlyOneImplementationHaveTestAndItIsMatchingImplPackageTest {
class MultipleImplementationsWithOnlyOneTestAndInRightPackageTest {

to make your new test actually fail with the old implementation. 🙂

In addition: I'd drop the AndInRightPackage part from this name: It's a relevant aspect to have classes with the same simple name in different packages, so: Is it right or is it wrong??
How about SimpleNameThatOccursInSeveralPackages?

Copy link
Author

Choose a reason for hiding this comment

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

I was reneming classes at the end and forgot about that one 🤦 Thanks for this suggestion - applied!

@krzysztof-owczarek krzysztof-owczarek force-pushed the issue-1367-fix-multiple-impl-not-all-with-tests branch from d494fc0 to b87bd2f Compare October 25, 2024 08:47
@krzysztof-owczarek
Copy link
Author

krzysztof-owczarek commented Oct 25, 2024

Thanks a lot for your contribution! 💚 I'd like to take a closer look whether we can simplify the rule, but I already wanted to leave one comment on the test. 😉

I have a feeling it might be done in with a simpler check. Sorry for not persuing that before I have created this PR, but I am just before my vacation and had a little time to create the change 🌴

I have another, simpler draft of such a condition, but it requires a bit sloppy name creation function:

Map<String, List<JavaClass>> testClassesBySimpleClassName = new HashMap<>();
Map<String, JavaClass> classesByPackageName = new HashMap<>();

@Override
public void init(Collection<JavaClass> allClasses) {
    testClassesBySimpleClassName = allClasses.stream()
        .filter(clazz -> clazz.getName().endsWith(testClassSuffix))
        .collect(groupingBy(JavaClass::getSimpleName));

    classesByPackageName = allClasses.stream()
         .collect(toMap(JavaClass::getFullName, Function.identity()));
}

and then:

possibleTestClasses.stream()
    .map(testClazz -> classesByPackageName.getOrDefault(createImplClassNameFromTestClassName(testClazz), null))
    .noneMatch(Objects::nonNull);

having:

private String createImplClassNameFromTestClassName(JavaClass testClazz) {
    return testClazz.getPackageName() + "." + testClazz.getSimpleName().replace(testClassSuffix, "");
}

I am not super happy with this draft, but the condition is much simpler that way. Maybe it will be a step towords some better solution.

@krzysztof-owczarek
Copy link
Author

Hey @hankem, I will paste the part of annonynous class here, that has a simpler condition, but is based on name resolving as described before. Give me heads up what do you think about it.

private static ArchCondition<JavaClass> resideInTheSamePackageAsTheirTestClasses(String testClassSuffix) {
        return new ArchCondition<JavaClass>("reside in the same package as their test classes") {
            Map<String, List<JavaClass>> testClassesBySimpleClassName = new HashMap<>();
            Map<String, JavaClass> classesByPackageName = new HashMap<>();

            @Override
            public void init(Collection<JavaClass> allClasses) {
                testClassesBySimpleClassName = allClasses.stream()
                        .filter(clazz -> clazz.getName().endsWith(testClassSuffix))
                        .collect(groupingBy(JavaClass::getSimpleName));

                classesByPackageName = allClasses.stream()
                        .collect(toMap(JavaClass::getFullName, Function.identity()));
            }

            @Override
            public void check(JavaClass implementationClass,
                              ConditionEvents events) {
                String implementationClassName = implementationClass.getSimpleName();
                String implementationClassPackageName = implementationClass.getPackageName();
                String possibleTestClassName = implementationClassName + testClassSuffix;
                List<JavaClass> possibleTestClasses = testClassesBySimpleClassName.getOrDefault(possibleTestClassName,
                        emptyList());

                boolean isTestClassInWrongPackage = !possibleTestClasses.isEmpty()
                        && possibleTestClasses.stream().noneMatch(
                        clazz -> clazz.getPackageName().equals(implementationClassPackageName))
                        && implementationNotFoundInTestClassPackages(possibleTestClasses);

                if (isTestClassInWrongPackage) {
                    possibleTestClasses.forEach(wrongTestClass -> {
                        String message = createMessage(wrongTestClass,
                                String.format("does not reside in same package as implementation class <%s>",
                                        implementationClass.getName()));
                        events.add(violated(wrongTestClass, message));
                    });
                }
            }

            private boolean implementationNotFoundInTestClassPackages(List<JavaClass> possibleTestClasses) {
                return possibleTestClasses.stream()
                        .map(testClazz -> classesByPackageName.getOrDefault(
                                resolveImplClassNameFromTestClassName(testClazz), null)
                        )
                        .noneMatch(Objects::nonNull);
            }

            private String resolveImplClassNameFromTestClassName(JavaClass testClazz) {
                return testClazz.getPackageName() + "." + testClazz.getSimpleName().replace(testClassSuffix, "");
            }

        };
    }

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