TNG / ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
http://archunit.org
Apache License 2.0
3.18k stars 288 forks source link

[FeatureRequest] let me specify which type of transation my class requires #59

Closed md42 closed 6 years ago

md42 commented 6 years ago

Wrote Test

@ArchTest
public static void classIsInRepositoryPackageSoItsAnnotatedWithTransactional(JavaClasses classes) {
    classes()
        .that().resideInAPackage("..repository..")
        .should().beAnnotatedWith(Transactional.class)
        .check(classes);
}

So now no class is allowed in repository package that does not have a Transactional annotation. It would be nice to be able to specify which type of org.springframework.transaction.annotation.Propagation is required.

codecholeric commented 6 years ago

This is one of the things that I did not add to the core API, because it's framework specific. You can easily add that yourself, though:

@ArchTest
public static final ArchRule classIsInRepositoryPackageSoItsAnnotatedWithTransactional =
    classes()
        .that().resideInAPackage("..repository..")
        .should().beAnnotatedWith(transactional(Propagation.REQUIRED));

private static DescribedPredicate<JavaAnnotation> transactional(final Propagation propagation) {
    return new DescribedPredicate<JavaAnnotation>("@Transactional(propagation = %s)", propagation) {
        @Override
        public boolean apply(JavaAnnotation input) {
            return input.getType().isEquivalentTo(Transactional.class) &&
                    input.as(Transactional.class).propagation() == propagation;
        }
    };
}

Does this help you?

md42 commented 6 years ago

Thanks! tried it and works like a charm :) And yes you are right framework specific stuff should not be part of ArchUnit