From 5ca2fb5da3dd23111e896b0dffe1f1fc26643b51 Mon Sep 17 00:00:00 2001 From: mnhock Date: Sat, 6 Jul 2024 16:26:34 +0200 Subject: [PATCH] Provide new Java Rule for checking classes reside outside a specified package Closes gh-65 --- docs/USERGUIDE.md | 12 ++++++++++++ .../java/com/enofex/taikai/java/JavaConfigurer.java | 11 +++++++++++ src/test/java/com/enofex/taikai/Usage.java | 1 + 3 files changed, 24 insertions(+) diff --git a/docs/USERGUIDE.md b/docs/USERGUIDE.md index 0725716..493c20f 100644 --- a/docs/USERGUIDE.md +++ b/docs/USERGUIDE.md @@ -109,6 +109,7 @@ The default mode is `WITHOUT_TESTS`, which excludes test classes from the import |----------|--------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| | General | `classesShouldImplementHashCodeAndEquals` | Classes should implement `hashCode` and `equals` together | | General | `classesShouldResideInPackage` | Classes match a specific naming patterns should reside in a specified package | +| General | `classesShouldResideOutsidePackage` | Classes match a specific naming patterns should reside outside a specified package | | General | `fieldsShouldNotBePublic` | Fields should not be `public`, except constants | | General | `methodsShouldNotDeclareGenericExceptions` | Methods should not declare generic exceptions, like `Exception`, `RuntimeException` | | General | `noUsageOf` | Disallow usage of specific classes | @@ -220,6 +221,17 @@ Taikai.builder() .check(); ``` +- **Classes Should Reside outside Specified Package**: Ensure that classes matching a specific regex pattern reside outside the specified package. + +```java +Taikai.builder() + .namespace("com.company.yourproject") + .java(java -> java + .classesShouldResideOutsidePackage(".*Utils", "com.company.yourproject.utils")) + .build() + .check(); +``` + - **Methods Should Not Throw Generic Exception**: Ensure that methods do not throw generic exceptions like `Exception` and `RuntimeException` and use specific exception types instead. ```java diff --git a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java index eb344ed..39d5c80 100644 --- a/src/main/java/com/enofex/taikai/java/JavaConfigurer.java +++ b/src/main/java/com/enofex/taikai/java/JavaConfigurer.java @@ -77,6 +77,17 @@ public JavaConfigurer classesShouldResideInPackage(String regex, String packageI .should().resideInAPackage(packageIdentifier), configuration)); } + public JavaConfigurer classesShouldResideOutsidePackage(String regex, String packageIdentifier) { + return classesShouldResideOutsidePackage(regex, packageIdentifier, null); + } + + public JavaConfigurer classesShouldResideOutsidePackage(String regex, String packageIdentifier, + Configuration configuration) { + return addRule(TaikaiRule.of(classes() + .that().haveNameMatching(regex) + .should().resideOutsideOfPackage(packageIdentifier), configuration)); + } + public JavaConfigurer classesShouldImplementHashCodeAndEquals() { return classesShouldImplementHashCodeAndEquals(null); } diff --git a/src/test/java/com/enofex/taikai/Usage.java b/src/test/java/com/enofex/taikai/Usage.java index 73a818a..9954095 100644 --- a/src/test/java/com/enofex/taikai/Usage.java +++ b/src/test/java/com/enofex/taikai/Usage.java @@ -21,6 +21,7 @@ public static void main(String[] args) { .noUsageOfDeprecatedAPIs() .classesShouldImplementHashCodeAndEquals() .classesShouldResideInPackage("regex", "com.enofex.taikai") + .classesShouldResideOutsidePackage("regex", "com.enofex.taikai") .methodsShouldNotDeclareGenericExceptions() .finalClassesShouldNotHaveProtectedMembers() .utilityClassesShouldBeFinalAndHavePrivateConstructor()