com.tngtech.archunit.base.DescribedPredicate Java Examples

The following examples show how to use com.tngtech.archunit.base.DescribedPredicate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SessionBeanRulesTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
private static DescribedPredicate<JavaClass> haveLocalBeanSubclass() {
    return new DescribedPredicate<JavaClass>("have subclass that is a local bean") {
        @Override
        public boolean apply(JavaClass input) {
            for (JavaClass subClass : input.getAllSubClasses()) {
                if (isLocalBeanImplementation(subClass, input)) {
                    return true;
                }
            }
            return false;
        }

        // NOTE: We assume that in this project by convention @Local is always used as @Local(type) with exactly
        //       one type, otherwise this would need to be more sophisticated
        private boolean isLocalBeanImplementation(JavaClass bean, JavaClass businessInterfaceType) {
            return bean.isAnnotatedWith(Local.class) &&
                    bean.getAnnotationOfType(Local.class).value()[0].getName()
                            .equals(businessInterfaceType.getName());
        }
    };
}
 
Example #2
Source File: PublicAPIRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static DescribedPredicate<JavaClass> enclosedInANonPublicClass() {
    return new DescribedPredicate<JavaClass>("enclosed in a non-public class") {
        @Override
        public boolean apply(JavaClass input) {
            return input.getEnclosingClass().isPresent() &&
                    !input.getEnclosingClass().get().getModifiers().contains(PUBLIC);
        }
    };
}
 
Example #3
Source File: JavaMemberTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void isAnnotatedWith_predicate() {
    assertThat(importField(SomeClass.class, "someField")
            .isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysTrue()))
            .as("predicate matches").isTrue();
    assertThat(importField(SomeClass.class, "someField")
            .isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysFalse()))
            .as("predicate matches").isFalse();
}
 
Example #4
Source File: JavaClassTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void isAnnotatedWith_predicate() {
    assertThat(importClassWithContext(Parent.class)
            .isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysTrue()))
            .as("predicate matches").isTrue();
    assertThat(importClassWithContext(Parent.class)
            .isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysFalse()))
            .as("predicate matches").isFalse();
}
 
Example #5
Source File: ShouldOnlyByClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("should_only_be_by_rule_starts")
public void areAnnotatedWith_predicate(ClassesThat<ClassesShouldConjunction> classesShouldOnlyBeBy) {
    DescribedPredicate<HasType> hasNamePredicate = GET_RAW_TYPE.is(classWithNameOf(SomeAnnotation.class));
    Set<JavaClass> classes = filterClassesAppearingInFailureReport(
            classesShouldOnlyBeBy.areAnnotatedWith(hasNamePredicate))
            .on(ClassBeingAccessedByAnnotatedClass.class, AnnotatedClass.class,
                    SimpleClass.class, ClassAccessingSimpleClass.class);

    assertThatClasses(classes).matchInAnyOrder(SimpleClass.class, ClassAccessingSimpleClass.class);
}
 
Example #6
Source File: ShouldClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void only_areMetaAnnotatedWith_predicate_dependency() {
    DescribedPredicate<HasType> hasNamePredicate = GET_RAW_TYPE.is(classWithNameOf(SomeAnnotation.class));
    Set<JavaClass> classes = filterClassesAppearingInFailureReport(
            classes().should().onlyDependOnClassesThat().areMetaAnnotatedWith(hasNamePredicate))
            .on(ClassAccessingMetaAnnotatedClass.class, ClassAccessingAnnotatedClass.class, ClassAccessingSimpleClass.class,
                    MetaAnnotatedAnnotation.class);

    assertThatClasses(classes).matchInAnyOrder(ClassAccessingAnnotatedClass.class, ClassAccessingSimpleClass.class, MetaAnnotatedAnnotation.class);
}
 
Example #7
Source File: PredicateAggregator.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
static <T> AddMode<T> and() {
    return new AddMode<T>() {
        @Override
        DescribedPredicate<T> apply(Optional<DescribedPredicate<T>> first, DescribedPredicate<? super T> other) {
            DescribedPredicate<T> second = other.forSubType();
            return first.isPresent() ? first.get().and(second) : second;
        }
    };
}
 
Example #8
Source File: JavaPackageTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private DescribedPredicate<JavaClass> startsWith(final String prefix) {
    return GET_SIMPLE_NAME.is(new DescribedPredicate<String>("starts with '%s'", prefix) {
        @Override
        public boolean apply(String input) {
            return input.startsWith(prefix);
        }
    });
}
 
Example #9
Source File: GivenClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotAnnotatedWith_predicate() {
    DescribedPredicate<HasType> hasNamePredicate = GET_RAW_TYPE.then(GET_NAME).is(equalTo(SomeAnnotation.class.getName()));
    List<JavaClass> classes = filterResultOf(classes().that().areNotAnnotatedWith(hasNamePredicate))
            .on(AnnotatedClass.class, SimpleClass.class);

    assertThat(getOnlyElement(classes)).matches(SimpleClass.class);
}
 
Example #10
Source File: ImporterRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static DescribedPredicate<JavaClass> belong_to_the_import_context() {
    return new DescribedPredicate<JavaClass>("belong to the import context") {
        @Override
        public boolean apply(JavaClass input) {
            return input.getPackageName().startsWith(ClassFileImporter.class.getPackage().getName())
                    && !input.getName().contains(DomainBuilders.class.getSimpleName());
        }
    };
}
 
Example #11
Source File: PlantUmlArchCondition.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
/**
 * Considers all dependencies of every imported class, including basic Java classes like {@link Object}
 */
@PublicAPI(usage = ACCESS)
public static Configuration consideringAllDependencies() {
    return new Configuration() {
        @Override
        public DescribedPredicate<Dependency> asIgnorePredicate(JavaClassDiagramAssociation javaClassDiagramAssociation) {
            return DescribedPredicate.<Dependency>alwaysFalse().as("");
        }
    };
}
 
Example #12
Source File: JavaClassTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
public ToEvaluation to(final Class<?> type) {
    firstType = type;
    message = String.format("assignableTo(%s) matches ", type.getSimpleName());
    assignable = ImmutableSet.of(new DescribedPredicate<JavaClass>("direct assignable to") {
        @Override
        public boolean apply(JavaClass input) {
            return input.isAssignableTo(type) && input.isAssignableTo(type.getName());
        }
    }, assignableTo(type), assignableTo(type.getName()));
    return new ToEvaluation();
}
 
Example #13
Source File: JavaClassTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
public FromEvaluation from(final Class<?> type) {
    firstType = type;
    message = String.format("assignableFrom(%s) matches ", type.getSimpleName());
    assignable = ImmutableSet.of(new DescribedPredicate<JavaClass>("direct assignable from") {
        @Override
        public boolean apply(JavaClass input) {
            return input.isAssignableFrom(type) && input.isAssignableFrom(type.getName());
        }
    }, assignableFrom(type), assignableFrom(type.getName()));
    return new FromEvaluation();
}
 
Example #14
Source File: Architectures.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
DescribedPredicate<JavaClass> containsPredicateFor(final Collection<String> layerNames) {
    DescribedPredicate<JavaClass> result = alwaysFalse();
    for (LayerDefinition definition : get(layerNames)) {
        result = result.or(definition.containsPredicate());
    }
    return result;
}
 
Example #15
Source File: JavaAccessTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void origin_predicate() {
    DescribedPredicate<JavaAccess<?>> predicate =
            JavaAccess.Predicates.origin(DescribedPredicate.<JavaCodeUnit>alwaysTrue().as("some text"));

    assertThat(predicate)
            .hasDescription("origin some text")
            .accepts(anyAccess());

    predicate = JavaAccess.Predicates.origin(DescribedPredicate.<JavaCodeUnit>alwaysFalse());
    assertThat(predicate).rejects(anyAccess());
}
 
Example #16
Source File: JavaPackageTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void test_isAnnotatedWith_predicate() {
    JavaPackage annotatedPackage = importPackage("packageexamples.annotated");
    JavaPackage nonAnnotatedPackage = importPackage("packageexamples");

    assertThat(annotatedPackage.isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysTrue())).isTrue();
    assertThat(annotatedPackage.isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysFalse())).isFalse();

    assertThat(nonAnnotatedPackage.isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysTrue())).isFalse();
    assertThat(nonAnnotatedPackage.isAnnotatedWith(DescribedPredicate.<JavaAnnotation<?>>alwaysFalse())).isFalse();
}
 
Example #17
Source File: AnyDependencyCondition.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@PublicAPI(usage = ACCESS)
public AnyDependencyCondition ignoreDependency(DescribedPredicate<? super Dependency> ignorePredicate) {
    return new AnyDependencyCondition(getDescription(),
            conditionPredicate,
            javaClassToRelevantDependencies,
            this.ignorePredicate.or(ignorePredicate));
}
 
Example #18
Source File: JavaFieldAccessTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void predicate_field_access_target_by_predicate() throws Exception {
    assertThat(target(DescribedPredicate.<FieldAccessTarget>alwaysTrue()))
            .accepts(stringFieldAccess(GET));
    assertThat(target(DescribedPredicate.<FieldAccessTarget>alwaysFalse().as("any message")))
            .rejects(stringFieldAccess(GET))
            .hasDescription("target any message");
}
 
Example #19
Source File: SlicesIsolationTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static DescribedPredicate<Slice> containDescription(final String descriptionPart) {
    return new DescribedPredicate<Slice>("contain description '%s'", descriptionPart) {
        @Override
        public boolean apply(Slice input) {
            return input.getDescription().contains(descriptionPart);
        }
    };
}
 
Example #20
Source File: HasParameterTypes.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@PublicAPI(usage = ACCESS)
public static DescribedPredicate<HasParameterTypes> rawParameterTypes(final Class<?>... types) {
    return rawParameterTypes(namesOf(types));
}
 
Example #21
Source File: JavaClass.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
/**
 * @see JavaClass#isEquivalentTo(Class)
 */
@PublicAPI(usage = ACCESS)
public static DescribedPredicate<JavaClass> equivalentTo(final Class<?> clazz) {
    return new EquivalentToPredicate(clazz);
}
 
Example #22
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaClass> haveSimpleNameStartingWith(final String prefix) {
    final DescribedPredicate<JavaClass> predicate = have(simpleNameStartingWith(prefix));

    return new SimpleNameStartingWithCondition(predicate, prefix);
}
 
Example #23
Source File: ClassesShouldInternal.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@Override
public ClassesShouldConjunction callCodeUnitWhere(DescribedPredicate<? super JavaCall<?>> predicate) {
    return addCondition(ArchConditions.callCodeUnitWhere(predicate));
}
 
Example #24
Source File: PublicAPIRules.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
private static DescribedPredicate<JavaMember> withoutAPIMarking() {
    return not(annotatedWith(PublicAPI.class)).<JavaMember>forSubType()
            .and(not(annotatedWith(Internal.class)).forSubType())
            .and(declaredIn(modifier(PUBLIC)))
            .as("without API marking");
}
 
Example #25
Source File: DescribedPredicateAssertion.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
public DescribedPredicateAssertion<T> hasSameDescriptionAs(DescribedPredicate<T> otherPredicate) {
    return hasDescription(otherPredicate.getDescription());
}
 
Example #26
Source File: ClassesShouldInternal.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@Override
public ClassesShouldConjunction onlyAccessClassesThat(DescribedPredicate<? super JavaClass> predicate) {
    return addCondition(ArchConditions.onlyAccessClassesThat(predicate));
}
 
Example #27
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@PublicAPI(usage = ACCESS)
public static ArchCondition<JavaCodeUnit> haveRawParameterTypes(DescribedPredicate<? super List<JavaClass>> predicate) {
    return new HaveConditionByPredicate<>(rawParameterTypes(predicate));
}
 
Example #28
Source File: HasParameterTypes.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@PublicAPI(usage = ACCESS)
public static DescribedPredicate<HasParameterTypes> rawParameterTypes(final String... types) {
    return rawParameterTypes(ImmutableList.copyOf(types));
}
 
Example #29
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
ImplementsCondition(DescribedPredicate<? super JavaClass> implement) {
    super(implement.getDescription());
    this.implement = implement;
}
 
Example #30
Source File: SyntaxPredicates.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
static DescribedPredicate<HasModifiers> areNotPublic() {
    return not(modifier(PUBLIC)).as("are not public");
}