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

new General Code Rule: NO_CLASSES_SHOULD_USE_JODATIME #145

Merged
merged 1 commit into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions archunit-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ext.moduleName = 'com.tngtech.archunit.example'
// so to run any test we need to choose a full implementation provider
compile 'org.apache.geronimo.specs:geronimo-ejb_3.1_spec:1.0'
compile 'org.apache.geronimo.specs:geronimo-jpa_2.0_spec:1.0'
// NOTE: using an old version of JodaTime that is compatible with Java7, Java8, and above
compile 'joda-time:joda-time:2.0'

testCompile project(path: ':archunit')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JODATIME;

@Category(Example.class)
@RunWith(ArchUnitRunner.class)
Expand All @@ -34,6 +35,9 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
@ArchTest
private final ArchRule no_java_util_logging = NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING;

@ArchTest
private final ArchRule no_jodatime = NO_CLASSES_SHOULD_USE_JODATIME;

@ArchTest
public static final ArchRule no_classes_should_access_standard_streams_or_throw_generic_exceptions =
CompositeArchRule.of(NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JODATIME;

@ArchTag("example")
@AnalyzeClasses(packages = "com.tngtech.archunit.example")
Expand All @@ -31,6 +32,9 @@ private void no_access_to_standard_streams_as_method(JavaClasses classes) {
@ArchTest
private final ArchRule no_java_util_logging = NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING;

@ArchTest
private final ArchRule no_jodatime = NO_CLASSES_SHOULD_USE_JODATIME;

@ArchTest
static final ArchRule no_classes_should_access_standard_streams_or_throw_generic_exceptions =
CompositeArchRule.of(NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public void throwGenericExceptions() throws Throwable {
}
}

public org.joda.time.DateTime jodaTimeIsBad() { // Violates rule not to use the JodaTime library
return org.joda.time.DateTime.now();
}

public void thisIsOkay() {
throw new SomeCustomException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_JODATIME;

@Category(Example.class)
public class CodingRulesTest {
Expand All @@ -38,6 +39,11 @@ public void classes_should_not_use_java_util_logging() {
NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING.check(classes);
}

@Test
public void classes_should_not_use_jodatime() {
NO_CLASSES_SHOULD_USE_JODATIME.check(classes);
}

@Test
public void no_classes_should_access_standard_streams_or_throw_generic_exceptions() {
CompositeArchRule.of(NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ Stream<DynamicTest> CodingRulesTest() {
.setting().field(ClassViolatingCodingRules.class, "log")
.inLine(9));

expectFailures.ofRule("no classes should use JodaTime, because modern Java projects use the [java.time] API instead")
.by(callFromMethod(ClassViolatingCodingRules.class, "jodaTimeIsBad")
.toMethod(org.joda.time.DateTime.class, "now")
.inLine(31)
.asDependency())
.by(method(ClassViolatingCodingRules.class, "jodaTimeIsBad")
.withReturnType(org.joda.time.DateTime.class));

expectFailures.ofRule("no classes should access standard streams and no classes should throw generic exceptions");
expectAccessToStandardStreams(expectFailures);
expectThrownGenericExceptions(expectFailures);
Expand Down
7 changes: 6 additions & 1 deletion archunit-maven-test/pom.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>#{archunitTestArtifact}</artifactId>
Expand Down Expand Up @@ -121,4 +126,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static com.tngtech.archunit.lang.conditions.ArchConditions.accessField;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callCodeUnitWhere;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callMethodWhere;
import static com.tngtech.archunit.lang.conditions.ArchConditions.dependOnClassesThat;
import static com.tngtech.archunit.lang.conditions.ArchConditions.setFieldWhere;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.is;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
Expand Down Expand Up @@ -133,4 +134,21 @@ private static ArchCondition<JavaClass> throwGenericExceptions() {
@PublicAPI(usage = ACCESS)
public static final ArchRule NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING =
noClasses().should(USE_JAVA_UTIL_LOGGING);

/**
* For information about checking this condition, refer to {@link GeneralCodingRules}.
*/
@PublicAPI(usage = ACCESS)
public static final ArchCondition<JavaClass> USE_JODATIME =
dependOnClassesThat(resideInAPackage("org.joda.time"))
.as("use JodaTime");

/**
* Modern Java projects use the [java.time] API instead of the JodaTime library
* <br>
* For information about checking this rule, refer to {@link GeneralCodingRules}.
*/
@PublicAPI(usage = ACCESS)
public static final ArchRule NO_CLASSES_SHOULD_USE_JODATIME =
noClasses().should(USE_JODATIME).because("modern Java projects use the [java.time] API instead");
}