diff --git a/archunit/src/main/java/com/tngtech/archunit/library/GeneralCodingRules.java b/archunit/src/main/java/com/tngtech/archunit/library/GeneralCodingRules.java index 03af68e14e..52a4b02ada 100644 --- a/archunit/src/main/java/com/tngtech/archunit/library/GeneralCodingRules.java +++ b/archunit/src/main/java/com/tngtech/archunit/library/GeneralCodingRules.java @@ -509,4 +509,18 @@ public void check(JavaClass implementationClass, ConditionEvents events) { .should(accessTargetWhere(JavaAccess.Predicates.target(annotatedWith(Deprecated.class))).as("access @Deprecated members")) .orShould(dependOnClassesThat(annotatedWith(Deprecated.class)).as("depend on @Deprecated classes")) .because("there should be a better alternative"); + + /** + * A rule checking that no classes uses old date and time classes and point out to use the Java 8 time API. + */ + @PublicAPI(usage = ACCESS) + public static final ArchRule OLD_DATE_AND_TIME_CLASSES_SHOULD_NOT_BE_USED = + noClasses() + .should().dependOnClassesThat().haveFullyQualifiedName("java.sql.Date") + .orShould().dependOnClassesThat().haveFullyQualifiedName("java.sql.Time") + .orShould().dependOnClassesThat().haveFullyQualifiedName("java.sql.Timestamp") + .orShould().dependOnClassesThat().haveFullyQualifiedName("java.util.Calendar") + .orShould().dependOnClassesThat().haveFullyQualifiedName("java.util.Date") + .as("since Java 8 (and JavaEE 7 if JPA is needed) java.time-API should be used.") + .because("since Java 8 (and JavaEE 7 if JPA is needed) java.time-API should be used."); } diff --git a/archunit/src/test/java/com/tngtech/archunit/library/GeneralCodingRulesTest.java b/archunit/src/test/java/com/tngtech/archunit/library/GeneralCodingRulesTest.java index 22ef0e6bf0..55853951ac 100644 --- a/archunit/src/test/java/com/tngtech/archunit/library/GeneralCodingRulesTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/library/GeneralCodingRulesTest.java @@ -10,11 +10,17 @@ import com.tngtech.archunit.library.testclasses.packages.incorrect.wrongsubdir.customsuffix.subdir.ImplementationClassWithWrongTestClassPackageCustomSuffixTestingScenario; import com.tngtech.archunit.library.testclasses.packages.incorrect.wrongsubdir.defaultsuffix.ImplementationClassWithWrongTestClassPackage; import com.tngtech.archunit.library.testclasses.packages.incorrect.wrongsubdir.defaultsuffix.subdir.ImplementationClassWithWrongTestClassPackageTest; +import com.tngtech.archunit.library.testclasses.timeapi.incorrect.UsesJavaSqlDate; +import com.tngtech.archunit.library.testclasses.timeapi.incorrect.UsesJavaSqlTime; +import com.tngtech.archunit.library.testclasses.timeapi.incorrect.UsesJavaSqlTimestamp; +import com.tngtech.archunit.library.testclasses.timeapi.incorrect.UsesJavaUtilCalender; +import com.tngtech.archunit.library.testclasses.timeapi.incorrect.UsesJavaUtilDate; import org.junit.Test; import static com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME; import static com.tngtech.archunit.library.GeneralCodingRules.ASSERTIONS_SHOULD_HAVE_DETAIL_MESSAGE; import static com.tngtech.archunit.library.GeneralCodingRules.DEPRECATED_API_SHOULD_NOT_BE_USED; +import static com.tngtech.archunit.library.GeneralCodingRules.OLD_DATE_AND_TIME_CLASSES_SHOULD_NOT_BE_USED; import static com.tngtech.archunit.library.GeneralCodingRules.testClassesShouldResideInTheSamePackageAsImplementation; import static com.tngtech.archunit.testutil.Assertions.assertThatRule; @@ -180,4 +186,44 @@ void origin() { @SuppressWarnings("DeprecatedIsStillUsed") private @interface DeprecatedAnnotation { } + + @Test + public void OLD_DATE_AND_TIME_CLASSES_SHOULD_NOT_BE_USED_should_fail_when_class_uses_java_util_date() { + assertThatRule(OLD_DATE_AND_TIME_CLASSES_SHOULD_NOT_BE_USED) + .hasDescriptionContaining("since Java 8 (and JavaEE 7 if JPA is needed) java.time-API should be used.") + .checking(new ClassFileImporter().importClasses(UsesJavaUtilDate.class)) + .hasViolationContaining("calls method