-
Notifications
You must be signed in to change notification settings - Fork 752
Adds ArchUnit tests for naming conventions #2077
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
Merged
bitwiseman
merged 8 commits into
hub4j:main
from
solonovamax:tests/archunit-naming-conventions
Mar 30, 2025
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ccec9cb
Add archunit tests for naming conventions
solonovamax e74755e
Fix javadoc warnings
solonovamax 37be904
Update src/test/java/org/kohsuke/github/ArchTests.java
bitwiseman 875b913
Update src/test/java/org/kohsuke/github/ArchTests.java
bitwiseman 7247cc6
Update src/test/java/org/kohsuke/github/ArchTests.java
bitwiseman 6f01f24
Update src/test/java/org/kohsuke/github/ArchTests.java
bitwiseman 55c9085
Update src/test/java/org/kohsuke/github/ArchTests.java
bitwiseman b4687f9
Fix inherited methods
bitwiseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,47 +1,66 @@ | ||
package org.kohsuke.github; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.base.HasDescription; | ||
import com.tngtech.archunit.core.domain.*; | ||
import com.tngtech.archunit.core.domain.properties.HasName; | ||
import com.tngtech.archunit.core.domain.properties.HasOwner; | ||
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.core.importer.ImportOption; | ||
import com.tngtech.archunit.lang.ArchCondition; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
import com.tngtech.archunit.lang.conditions.ArchConditions; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.kohsuke.github.GHDiscussion.Creator; | ||
import org.kohsuke.github.GHPullRequestCommitDetail.Commit; | ||
import org.kohsuke.github.GHPullRequestCommitDetail.CommitPointer; | ||
|
||
import java.io.Closeable; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.lang.reflect.Field; | ||
import java.nio.charset.Charset; | ||
import java.time.Instant; | ||
bitwiseman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.tngtech.archunit.base.DescribedPredicate.not; | ||
import static com.tngtech.archunit.base.DescribedPredicate.or; | ||
import static com.tngtech.archunit.core.domain.JavaCall.Predicates.target; | ||
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAPackage; | ||
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.type; | ||
import static com.tngtech.archunit.core.domain.JavaMember.Predicates.declaredIn; | ||
import static com.tngtech.archunit.core.domain.JavaModifier.FINAL; | ||
import static com.tngtech.archunit.core.domain.JavaModifier.STATIC; | ||
import static com.tngtech.archunit.core.domain.properties.HasModifiers.Predicates.modifier; | ||
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.name; | ||
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.nameContaining; | ||
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.nameMatching; | ||
import static com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With.owner; | ||
import static com.tngtech.archunit.core.domain.properties.HasParameterTypes.Predicates.rawParameterTypes; | ||
import static com.tngtech.archunit.lang.conditions.ArchConditions.*; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noFields; | ||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noMethods; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
|
||
// TODO: Auto-generated Javadoc | ||
/** | ||
* The Class ArchTests. | ||
*/ | ||
@SuppressWarnings({ "LocalVariableNamingConvention", "TestMethodWithoutAssertion", "UnqualifiedStaticUsage", | ||
"unchecked", "MethodMayBeStatic", "FieldNamingConvention", "StaticCollection" }) | ||
public class ArchTests { | ||
|
||
private static final JavaClasses classFiles = new ClassFileImporter() | ||
|
@@ -69,6 +88,45 @@ public static void beforeClass() { | |
assertThat(classFiles.size(), greaterThan(0)); | ||
} | ||
|
||
/** | ||
* Test naming conventions | ||
*/ | ||
@Test | ||
public void testRequireFollowingNamingConvention() { | ||
final String reason = "This project follows standard java naming conventions and does not allow the use of underscores in names."; | ||
|
||
final ArchRule fieldsNotFollowingConvention = noFields().that() | ||
.arePublic() | ||
.and(not(enumConstants())) | ||
.and(not(modifier(STATIC).and(modifier(FINAL)).as("static final"))) | ||
.should(haveNamesContainingUnless("_")) | ||
.because(reason); | ||
|
||
@SuppressWarnings("AccessStaticViaInstance") | ||
final ArchRule methodsNotFollowingConvention = noMethods().that() | ||
.arePublic() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All methods should follow the convention regardless of visibility. |
||
.should(haveNamesContainingUnless("_", | ||
nameMatching("[A-Z0-9\\_]+"), | ||
// currently failing cases | ||
// TODO: 2025-03-28 Fix & remove these | ||
declaredIn(PagedIterable.class).and(name("_iterator")), | ||
declaredIn(GHDeployKey.class).and(name("getAdded_by")), | ||
declaredIn(GHDeployKey.class).and(name("isRead_only")), | ||
declaredIn(Creator.class).and(name("private_")), | ||
declaredIn(GHGistBuilder.class).and(name("public_")), | ||
declaredIn(Commit.class).and(name("getComment_count")), | ||
declaredIn(CommitPointer.class).and(name("getHtml_url")), | ||
declaredIn(GHRelease.class).and(name("getPublished_at")))) | ||
.because(reason); | ||
|
||
final ArchRule classesNotFollowingConvention = noClasses().should(haveNamesContainingUnless("_")) | ||
.because(reason); | ||
|
||
fieldsNotFollowingConvention.check(classFiles); | ||
methodsNotFollowingConvention.check(classFiles); | ||
classesNotFollowingConvention.check(classFiles); | ||
} | ||
|
||
/** | ||
* Test require use of assert that. | ||
*/ | ||
|
@@ -78,10 +136,10 @@ public void testRequireUseOfAssertThat() { | |
final String reason = "This project uses `assertThat(...)` or `assertThrows(...)` instead of other `assert*()` methods."; | ||
|
||
final DescribedPredicate<HasName> assertMethodOtherThanAssertThat = nameContaining("assert") | ||
.and(DescribedPredicate.not(name("assertThat")).and(DescribedPredicate.not(name("assertThrows")))); | ||
.and(not(name("assertThat")).and(not(name("assertThrows")))); | ||
|
||
final ArchRule onlyAssertThatRule = classes() | ||
.should(not(callMethodWhere(target(assertMethodOtherThanAssertThat)))) | ||
.should(ArchConditions.not(callMethodWhere(target(assertMethodOtherThanAssertThat)))) | ||
.because(reason); | ||
|
||
onlyAssertThatRule.check(testClassFiles); | ||
|
@@ -135,6 +193,29 @@ public void testRequireUseOfOnlySpecificApacheCommons() { | |
onlyApprovedApacheCommonsMethods.check(classFiles); | ||
} | ||
|
||
/** | ||
* Have names containing unless. | ||
* | ||
* @param <T> | ||
* the generic type | ||
* @param infix | ||
* the infix | ||
* @param unlessPredicates | ||
* the unless predicates | ||
* @return the arch condition | ||
*/ | ||
public static <T extends HasDescription & HasSourceCodeLocation & HasName> ArchCondition<T> haveNamesContainingUnless( | ||
final String infix, | ||
final DescribedPredicate<? super T>... unlessPredicates) { | ||
DescribedPredicate<? super T> restrictedNameContaining = nameContaining(infix); | ||
|
||
if (unlessPredicates.length > 0) { | ||
final DescribedPredicate<T> allowed = or(unlessPredicates); | ||
restrictedNameContaining = unless(nameContaining(infix), allowed); | ||
} | ||
return have(restrictedNameContaining); | ||
} | ||
|
||
/** | ||
* Not call methods in package unless. | ||
* | ||
|
@@ -156,7 +237,7 @@ public static ArchCondition<JavaClass> notCallMethodsInPackageUnless(final Strin | |
} | ||
restrictedPackageCalls = unless(restrictedPackageCalls, allowed); | ||
} | ||
return not(callMethodWhere(restrictedPackageCalls)); | ||
return ArchConditions.not(callMethodWhere(restrictedPackageCalls)); | ||
} | ||
|
||
/** | ||
|
@@ -200,6 +281,15 @@ public static <T> DescribedPredicate<T> unless(DescribedPredicate<? super T> fir | |
return new UnlessPredicate(first, second); | ||
} | ||
|
||
/** | ||
* Enum constants. | ||
* | ||
* @return the described predicate | ||
*/ | ||
private DescribedPredicate<? super JavaField> enumConstants() { | ||
return new EnumConstantFieldPredicate(); | ||
} | ||
|
||
private static class UnlessPredicate<T> extends DescribedPredicate<T> { | ||
private final DescribedPredicate<T> current; | ||
private final DescribedPredicate<? super T> other; | ||
|
@@ -215,4 +305,16 @@ public boolean test(T input) { | |
return current.test(input) && !other.test(input); | ||
} | ||
} | ||
|
||
private static final class EnumConstantFieldPredicate extends DescribedPredicate<JavaField> { | ||
private EnumConstantFieldPredicate() { | ||
super("are not enum constants"); | ||
} | ||
|
||
@Override | ||
public boolean test(JavaField javaField) { | ||
JavaClass owner = javaField.getOwner(); | ||
return owner.isEnum() && javaField.getRawType().isAssignableTo(owner.reflect()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.