简体   繁体   English

ArchUnit:检查软件包是否相互依赖的最优雅的方法

[英]ArchUnit: Most elegant way to check if packages depend on each other

I have two packages com.myapp.foo and com.myapp.bar and I want to know the most elegant way to explicitly check if those two packages (and only those, as there are some more com.myapp.XX ) do not depend on each other. 我有两个包com.myapp.foocom.myapp.bar ,我想知道最优雅的方法来显式检查这两个包(并且只有那些,因为还有更多com.myapp.XX )不依赖彼此。

This is what I have right now (working splendid): 这就是我现在所拥有的(出色的工作):

    SliceAssignment packagesFooAndBar = new SliceAssignment() {
        @Override
        public String getDescription() {
            return "foo and bar";
        }

        @Override
        public SliceIdentifier getIdentifierOf(JavaClass javaClass) {
            if (javaClass.getPackageName().startsWith("com.myapp.foo")) {
                return SliceIdentifier.of("foo");
            }
            if (javaClass.getPackageName().startsWith("com.myapp.bar")) {
                return SliceIdentifier.of("bar");
            }
            return SliceIdentifier.ignore();
        }
    };
    @ArchTest
    final ArchRule packagesFooAndBarNotDependOnEachOther = SlicesRuleDefinition
        .slices()
        .assignedFrom(packagesFooAndBar)
        .should()
        .notDependOnEachOther();

Is there a more elegant way, maybe without using the SliceAssignment ? 有没有一种更优雅的方法,也许不使用SliceAssignment Thanks! 谢谢!

I would probably use two rules. 我可能会使用两个规则。

@ArchTest
final ArchRule fooShouldNotDependOnBar = ArchRuleDefinition
.classes()
.that().resideInAnyPackage("com.myapp.foo")
.should().onlyDependOnClassesThat()
.resideOutsideOfPackage("com.myapp.bar");

@ArchTest
final ArchRule barShouldNotDependOnFoo = ArchRuleDefinition
.classes()
.that().resideInAnyPackage("com.myapp.bar")
.should().onlyDependOnClassesThat()
.resideOutsideOfPackage("com.myapp.foo");

If you want to write the same in a single rule without custom classes, the following should work. 如果要在没有自定义类的单个规则中编写相同的代码,则应执行以下操作。 I'm not sure if I would call it elegant though, since it's seems a bit complex. 我不确定是否可以称它为优雅 ,因为它看起来有点复杂。 There are probably better ways to write it in a single rule. 可能有更好的方法来编写单个规则。

@ArchTest
final ArchRule packagesFooAndBarNotDependOnEachOther = SlicesRuleDefinition
    .slices()
    .matching("com.myapp.(*)")
    .should()
    .notDependOnEachOther()
    .ignoreDependency(
        JavaClass.Predicates.resideOutsideOfPackages("com.myapp.foo", "com.myapp.bar"),
        DescribedPredicate.alwaysTrue())
    .ignoreDependency(
        DescribedPredicate.alwaysTrue(),
        JavaClass.Predicates.resideOutsideOfPackages("com.myapp.foo", "com.myapp.bar"))
    );

The rule first matches all direct sub-packages of com.myapp . 该规则首先匹配com.myapp所有直接子包。 The first ignoreDependency then excludes all dependencies from classes outside of the given packages (to allow eg com.myapp.xx to access com.myapp.foo ). 然后,第一个ignoreDependency会排除给定程序包之外的类中的所有依赖项(以允许com.myapp.xx访问com.myapp.foo )。 The second ignoreDependency excludes all dependencies from the given packages to any outside package (to allow eg com.myapp.foo to access com.myapp.xx ). 第二个ignoreDependency排除了从给定包到任何外部包的所有依赖关系(以允许com.myapp.foo访问com.myapp.xx )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM