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

testClassesShouldResideInTheSamePackageAsImplementation fails on duplicated simple class name #918

Closed
timtebeek opened this issue Jul 15, 2022 · 3 comments · Fixed by #920

Comments

@timtebeek
Copy link
Contributor

timtebeek commented Jul 15, 2022

Quite like the new library check to ensure test classes are in the same package.
One small nitpick is that it's currently unable to handle duplicated simple names:

A structure such as this one:

src/main/java/some/package/SomeClass.java
src/test/java/some/package/SomeClassTest.java
src/test/java/some/other/package/SomeClassTest.java

Will fail with:

java.lang.IllegalStateException: Duplicate key SomeClassTest (attempted merging values JavaClass{name='some.package.SomeClassTest'} and JavaClass{name='some.other.package.SomeClassTest'})
	at java.base/java.util.stream.Collectors.duplicateKeyException(Collectors.java:135)
	at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:182)
	at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at com.tngtech.archunit.library.GeneralCodingRules$1.init(GeneralCodingRules.java:458)
	at com.tngtech.archunit.lang.ArchRule$Factory$SimpleArchRule.evaluate(ArchRule.java:229)
	at com.tngtech.archunit.lang.ArchRule$Assertions.check(ArchRule.java:96)
	at com.tngtech.archunit.lang.ArchRule$Factory$SimpleArchRule.check(ArchRule.java:211)
	at com.tngtech.archunit.junit.internal.ArchUnitTestDescriptor$ArchUnitRuleDescriptor.execute(ArchUnitTestDescriptor.java:166)
	at com.tngtech.archunit.junit.internal.ArchUnitTestDescriptor$ArchUnitRuleDescriptor.execute(ArchUnitTestDescriptor.java:149)

Should people ideally not be doing this in the first place? Yes of course.
But the library should probably highlight that with a violation, not an exception.

I'm guessing it could be enough to use the FQCN, with some variable renaming:

-.collect(toMap(JavaClass::getSimpleName, identity()));
+.collect(toMap(JavaClass::getName, identity()));

But figured to first report it here to see if this is something to even fix at all.

Tagging @mslowiak as he contributed the feature recently. :)

@codecholeric
Copy link
Collaborator

Ah, true, didn't think of that case 😬 From my point of view we should probably just do a multi-map simpleName -> classes, then when checking for violations we see if any of the candidates is in the correct package. If yes we count the rule as satisfied. Would that make sense?

@timtebeek
Copy link
Contributor Author

That would work as well; wouldn't mind which way to go. :)

@mslowiak
Copy link
Contributor

Oh well, didn't though about this case. I will raise PR for that ⚔️

codecholeric added a commit that referenced this issue Aug 9, 2022


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

1. Should not fail if there are multiple test classes with the same simple name as long as one test class resides in the correct package
2. Should fail if none of multiple test classes matching by name with implementation class are in correct package

Resolves #918
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants