-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10399 from Eulbobo/feat/equals_hashcode_verification
feat(archUnit): adding equals/hashcode contract check
- Loading branch information
Showing
5 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...in/resources/generator/server/javatool/archunit/test/EqualsHashcodeArchTest.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package {{packageName}}; | ||
|
||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.domain.JavaMethod; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.core.importer.ImportOption; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@UnitTest | ||
class EqualsHashcodeArchTest { | ||
private static final String ROOT_PACKAGE = "{{packageName}}.."; | ||
private static final JavaClasses classes = new ClassFileImporter() | ||
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) | ||
.importPackages(ROOT_PACKAGE); | ||
@Test | ||
void shouldHaveValidEqualsHashcodeContract() { | ||
classes().that().resideInAnyPackage(ROOT_PACKAGE).and().areNotInterfaces().should(implementBothOrNone()).check(classes); | ||
} | ||
|
||
private ArchCondition.ConditionByPredicate<JavaClass> implementBothOrNone() { | ||
return ArchCondition.from( | ||
new DescribedPredicate<>("Class should implement none or both method equals and hashcode") { | ||
@Override | ||
public boolean test(JavaClass clazz) { | ||
return hasEquals(clazz) == hasHashCode(clazz); | ||
} | ||
|
||
private boolean hasHashCode(JavaClass clazz) { | ||
return clazz.getMethods().stream().anyMatch(this::isMethodHashCode); | ||
} | ||
|
||
private boolean isMethodHashCode(JavaMethod method) { | ||
return method.getName().equals("hashCode") && method.getParameters().isEmpty(); | ||
} | ||
|
||
private boolean hasEquals(JavaClass clazz) { | ||
return clazz.getMethods().stream().anyMatch(this::isMethodEquals); | ||
} | ||
|
||
private boolean isMethodEquals(JavaMethod method) { | ||
return method.getName().equals("equals") && hasOneObjectParameter(method); | ||
} | ||
|
||
private boolean hasOneObjectParameter(JavaMethod method) { | ||
return method.getParameters().size() == 1 && method.getParameters().getFirst().getRawType().isAssignableFrom(Object.class); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/tech/jhipster/lite/EqualsHashcodeArchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package tech.jhipster.lite; | ||
|
||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaClass; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.domain.JavaMethod; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.core.importer.ImportOption; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@UnitTest | ||
class EqualsHashcodeArchTest { | ||
|
||
private static final String ROOT_PACKAGE = "tech.jhipster.lite.."; | ||
|
||
private static final JavaClasses classes = new ClassFileImporter() | ||
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) | ||
.importPackages(ROOT_PACKAGE); | ||
|
||
@Test | ||
void shouldHaveValidEqualsHashcodeContract() { | ||
classes().that().resideInAnyPackage(ROOT_PACKAGE).and().areNotInterfaces().should(implementBothOrNone()).check(classes); | ||
} | ||
|
||
private ArchCondition.ConditionByPredicate<JavaClass> implementBothOrNone() { | ||
return ArchCondition.from( | ||
new DescribedPredicate<>("Class should implement none or both method equals and hashcode") { | ||
@Override | ||
public boolean test(JavaClass clazz) { | ||
return hasEquals(clazz) == hasHashCode(clazz); | ||
} | ||
|
||
private boolean hasHashCode(JavaClass clazz) { | ||
return clazz.getMethods().stream().anyMatch(this::isMethodHashCode); | ||
} | ||
|
||
private boolean isMethodHashCode(JavaMethod method) { | ||
return method.getName().equals("hashCode") && method.getParameters().isEmpty(); | ||
} | ||
|
||
private boolean hasEquals(JavaClass clazz) { | ||
return clazz.getMethods().stream().anyMatch(this::isMethodEquals); | ||
} | ||
|
||
private boolean isMethodEquals(JavaMethod method) { | ||
return method.getName().equals("equals") && hasOneObjectParameter(method); | ||
} | ||
|
||
private boolean hasOneObjectParameter(JavaMethod method) { | ||
return method.getParameters().size() == 1 && method.getParameters().getFirst().getRawType().isAssignableFrom(Object.class); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters