0 votes
in Education by (1.7m points)
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.

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? Thanks!

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
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. The first ignoreDependency then excludes all dependencies from classes outside of the given packages (to allow e.g. com.myapp.xx to access com.myapp.foo). The second ignoreDependency excludes all dependencies from the given packages to any outside package (to allow e.g. com.myapp.foo to access com.myapp.xx).
...