com.tngtech.archunit.core.domain.JavaMember Java Examples

The following examples show how to use com.tngtech.archunit.core.domain.JavaMember. 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: GivenMembersTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@Test
@UseDataProvider("member_syntax_testcases")
public void test_members(GivenMembers<JavaMember> members, String ruleStart, ArchCondition<JavaMember> condition,
        List<String> expectedViolationDetails) {
    ArchRule rule = members
            .that(have(modifier(PRIVATE)))
            .or(have(modifier(PROTECTED)))
            .and(are(annotatedWith(B.class)))
            .should(condition);

    assertThat(rule.getDescription()).as("rule description")
            .isEqualTo(ruleStart + " that have modifier PRIVATE or have modifier PROTECTED and are annotated with @B "
                    + "should " + condition.getDescription());

    EvaluationResult result = rule.evaluate(importClasses(ClassWithVariousMembers.class));

    assertViolation(result);
    assertThat(result.getFailureReport().getDetails()).containsOnlyElementsOf(expectedViolationDetails);
}
 
Example #2
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
/**
 * We do not support operator precedence, like && and || does, we just aggregate as the predicates come.
 * If someone really needs such precedence, he has to use custom predicates, like a.and(b.or(c)).
 */
@Test
public void conjunctions_aggregate_in_sequence_without_special_precedence() {
    List<JavaMember> members = filterResultOf(
            // (List OR String) AND Collection => empty
            members().that().areDeclaredInClassesThat().haveSimpleName(List.class.getSimpleName())
                    .or().areDeclaredInClassesThat(have(simpleName(String.class.getSimpleName())))
                    .and().areDeclaredInClassesThat().haveSimpleName(Collection.class.getSimpleName()))
            .on(List.class, String.class, Collection.class, Iterable.class);

    assertThat(members).isEmpty();

    members = filterResultOf(
            // (List AND String) OR Collection OR Iterable => [Collection, Iterable]
            members().that().areDeclaredInClassesThat().haveSimpleName(List.class.getSimpleName())
                    .and().areDeclaredInClassesThat(have(simpleName(String.class.getSimpleName())))
                    .or().areDeclaredInClassesThat().haveSimpleName(Collection.class.getSimpleName())
                    .or().areDeclaredInClassesThat().haveSimpleName(Iterable.class.getSimpleName()))
            .on(List.class, String.class, Collection.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(Collection.class, Iterable.class);
}
 
Example #3
Source File: JavaMembersAssertion.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static <T extends Member> void assertCodeUnitModifiers(JavaMember javaMember, T member) {
    assertThat(javaMember.getModifiers().contains(NATIVE))
            .as("member %s is native", javaMember.getFullName())
            .isEqualTo(Modifier.isNative(member.getModifiers()));
    assertThat(javaMember.getModifiers().contains(SYNCHRONIZED))
            .as("member %s is synchronized", javaMember.getFullName())
            .isEqualTo(Modifier.isSynchronized(member.getModifiers()));
}
 
Example #4
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotLocalClasses_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotLocalClasses())
            .on(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());

    assertThatMembers(members)
            .matchInAnyOrderMembersOf(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.getAnonymousClass());
}
 
Example #5
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotAnnotatedWith_typeName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotAnnotatedWith(SomeAnnotation.class.getName()))
            .on(AnnotatedClass.class, SimpleClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(SimpleClass.class);
}
 
Example #6
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void implement_type() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().implement(Collection.class))
            .on(ArrayList.class, List.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(ArrayList.class);

    members = filterResultOf(members().that().areDeclaredInClassesThat().implement(Set.class))
            .on(ArrayList.class, List.class, Iterable.class);

    assertThat(members).isEmpty();
}
 
Example #7
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotMetaAnnotatedWith_predicate() {
    DescribedPredicate<HasType> hasNamePredicate = GET_RAW_TYPE.then(GET_NAME).is(equalTo(SomeAnnotation.class.getName()));
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotMetaAnnotatedWith(hasNamePredicate))
            .on(MetaAnnotatedClass.class, AnnotatedClass.class, SimpleClass.class, MetaAnnotatedAnnotation.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(AnnotatedClass.class, SimpleClass.class, MetaAnnotatedAnnotation.class);
}
 
Example #8
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areMetaAnnotatedWith_typeName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areMetaAnnotatedWith(SomeAnnotation.class.getName()))
            .on(MetaAnnotatedClass.class, AnnotatedClass.class, SimpleClass.class, MetaAnnotatedAnnotation.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(MetaAnnotatedClass.class);
}
 
Example #9
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areInnerClasses_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areInnerClasses())
            .on(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());

    assertThatMembers(members).matchInAnyOrderMembersOf(NestedClassWithSomeMoreClasses.InnerMemberClass.class,
            NestedClassWithSomeMoreClasses.getAnonymousClass(), NestedClassWithSomeMoreClasses.getLocalClass());
}
 
Example #10
Source File: GivenMembersDeclaredInClassesThatTest.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<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotAnnotatedWith(hasNamePredicate))
            .on(AnnotatedClass.class, SimpleClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(SimpleClass.class);
}
 
Example #11
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areAnnotatedWith_predicate() {
    DescribedPredicate<HasType> hasNamePredicate = GET_RAW_TYPE.then(GET_NAME).is(equalTo(SomeAnnotation.class.getName()));
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areAnnotatedWith(hasNamePredicate))
            .on(AnnotatedClass.class, SimpleClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(AnnotatedClass.class);
}
 
Example #12
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotMetaAnnotatedWith_typeName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotMetaAnnotatedWith(SomeAnnotation.class.getName()))
            .on(MetaAnnotatedClass.class, AnnotatedClass.class, SimpleClass.class, MetaAnnotatedAnnotation.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(AnnotatedClass.class, SimpleClass.class, MetaAnnotatedAnnotation.class);
}
 
Example #13
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areProtected() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areProtected())
            .on(getClass(), PrivateClass.class, PackagePrivateClass.class, ProtectedClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(ProtectedClass.class);
}
 
Example #14
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotPublic() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotPublic())
            .on(getClass(), PrivateClass.class, PackagePrivateClass.class, ProtectedClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(PrivateClass.class, PackagePrivateClass.class, ProtectedClass.class);
}
 
Example #15
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void arePublic() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().arePublic())
            .on(getClass(), PrivateClass.class, PackagePrivateClass.class, ProtectedClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(getClass());
}
 
Example #16
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void resideOutsideOfPackages() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat()
            .resideOutsideOfPackages("..tngtech..", "java.lang.ref")
    ).on(getClass(), String.class, Reference.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(String.class);
}
 
Example #17
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areAssignableTo_type() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areAssignableTo(Collection.class))
            .on(List.class, String.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class);

    members = filterResultOf(members().that().areDeclaredInClassesThat().areAssignableTo(AbstractList.class))
            .on(ArrayList.class, List.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(ArrayList.class);
}
 
Example #18
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void arePackagePrivate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().arePackagePrivate())
            .on(getClass(), PrivateClass.class, PackagePrivateClass.class, ProtectedClass.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(PackagePrivateClass.class);
}
 
Example #19
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void haveSimpleNameNotEndingWith() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().haveSimpleNameNotEndingWith("String"))
            .on(String.class, AttributedString.class, StringBuilder.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(StringBuilder.class);
}
 
Example #20
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void haveSimpleNameEndingWith() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().haveSimpleNameEndingWith("String"))
            .on(String.class, AttributedString.class, StringBuilder.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(String.class, AttributedString.class);
}
 
Example #21
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areMemberClasses_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areMemberClasses())
            .on(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());

    assertThatMembers(members)
            .matchInAnyOrderMembersOf(NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class);
}
 
Example #22
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNestedClasses_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNestedClasses())
            .on(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());

    assertThatMembers(members)
            .matchInAnyOrderMembersOf(NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());
}
 
Example #23
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotAssignableFrom_type() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotAssignableFrom(Collection.class))
            .on(List.class, String.class, Collection.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class, String.class);
}
 
Example #24
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void haveNameNotMatching() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().haveNameNotMatching(".*List"))
            .on(List.class, String.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(String.class, Iterable.class);
}
 
Example #25
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotAssignableTo_typeName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotAssignableTo(Collection.class.getName()))
            .on(List.class, String.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(String.class, Iterable.class);
}
 
Example #26
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areInterfaces_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areInterfaces())
            .on(List.class, String.class, Collection.class, Integer.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class, Collection.class);
}
 
Example #27
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void haveSimpleName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().haveSimpleName(List.class.getSimpleName()))
            .on(List.class, String.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class);
}
 
Example #28
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void doNotImplement_typeName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().doNotImplement(Collection.class.getName()))
            .on(ArrayList.class, List.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class, Iterable.class);
}
 
Example #29
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void haveFullyQualifiedName() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().haveFullyQualifiedName(List.class.getName()))
            .on(List.class, String.class, Iterable.class);

    assertThatMembers(members).matchInAnyOrderMembersOf(List.class);
}
 
Example #30
Source File: GivenMembersDeclaredInClassesThatTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void areNotNestedClasses_predicate() {
    List<JavaMember> members = filterResultOf(members().that().areDeclaredInClassesThat().areNotNestedClasses())
            .on(String.class, NestedClassWithSomeMoreClasses.class, NestedClassWithSomeMoreClasses.StaticNestedClass.class,
                    NestedClassWithSomeMoreClasses.InnerMemberClass.class, NestedClassWithSomeMoreClasses.getAnonymousClass(),
                    NestedClassWithSomeMoreClasses.getLocalClass());

    assertThatMembers(members).matchInAnyOrderMembersOf(String.class);
}