Skip to content

Commit

Permalink
Provide a new Java Rule for enforcing naming conventions for constants
Browse files Browse the repository at this point in the history
Closes gb-13
  • Loading branch information
mnhock committed Jun 10, 2024
1 parent b09db85 commit 67e3982
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/main/java/com/enofex/taikai/java/NamingConfigurer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.enofex.taikai.java;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;

import com.enofex.taikai.TaikaiRule;
import com.enofex.taikai.TaikaiRule.Configuration;
import com.enofex.taikai.configures.AbstractConfigurer;
import com.enofex.taikai.configures.ConfigurerContext;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import java.util.regex.Pattern;

public final class NamingConfigurer extends AbstractConfigurer {

Expand Down Expand Up @@ -49,4 +52,32 @@ public void check(JavaClass clazz, ConditionEvents events) {
}
};
}

public NamingConfigurer constantsShouldFollowConvention() {
return constantsShouldFollowConvention(null);
}

public NamingConfigurer constantsShouldFollowConvention(Configuration configuration) {
return addRule(TaikaiRule.of(fields().that().areFinal().and().areStatic()
.should(shouldFollowConstantNamingConvention())
.as("Constants should follow constant naming convention"), configuration));
}

private static ArchCondition<JavaField> shouldFollowConstantNamingConvention() {
return new ArchCondition<>("follow constant naming convention") {

private static final Pattern CONSTANT_NAME_PATTERN = Pattern.compile("^[A-Z][A-Z0-9_]*$");

@Override
public void check(JavaField field, ConditionEvents events) {
if (!field.getOwner().isEnum()
&& !CONSTANT_NAME_PATTERN.matcher(field.getName()).matches()) {
String message = String.format(
"Constant %s in class %s does not follow the naming convention",
field.getName(), field.getOwner().getName());
events.add(SimpleConditionEvent.violated(field, message));
}
}
};
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/enofex/taikai/ArchitectureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void shouldFulfilConstrains() {
.shouldNotImport("org.junit.."))
.naming(naming -> naming
.classesShouldNotMatch(".*Impl")
.interfacesShouldNotHavePrefixI()))
.interfacesShouldNotHavePrefixI()
.constantsShouldFollowConvention()))
.build()
.check();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/enofex/taikai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void main(String[] args) {
.shouldNotImport("org.junit.."))
.naming(naming -> naming
.classesShouldNotMatch(".*Impl")
.constantsShouldFollowConvention()
.interfacesShouldNotHavePrefixI()))
.build();

Expand Down

0 comments on commit 67e3982

Please sign in to comment.