Skip to content

Instantly share code, notes, and snippets.

@dsmiley
Created May 28, 2024 06:18
Show Gist options
  • Save dsmiley/8a34cf16dd5827e5396e6da24e19afd2 to your computer and use it in GitHub Desktop.
Save dsmiley/8a34cf16dd5827e5396e6da24e19afd2 to your computer and use it in GitHub Desktop.
ArchUnit dependency rule Zk and Netty
package org.apache.zookeeper;
import static com.tngtech.archunit.core.importer.ImportOption.Predefined.DO_NOT_INCLUDE_TESTS;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
import java.util.Collections;
import org.junit.jupiter.api.Test;
@AnalyzeClasses(importOptions = {ImportOption.DoNotIncludeTests.class,
ImportOption.DoNotIncludeJars.class})
public class ZkArchUnitTest {
@Test
public void nettyRule() {
JavaClasses importedClasses = new ClassFileImporter(
Collections.singletonList(DO_NOT_INCLUDE_TESTS)).importPackages("org.apache.zookeeper")
.that(new DescribedPredicate<JavaClass>(
"ZK Non-Netty classes") {
@Override
public boolean test(JavaClass javaClass) {
return !javaClass.getName().contains("Netty");
}
});
ArchRule rule = ArchRuleDefinition.noClasses().should()
.dependOnClassesThat()
.resideInAnyPackage("io.netty..")
.orShould().dependOnClassesThat()
.haveSimpleNameContaining("Netty");
rule.check(importedClasses);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment