Search in sources :

Example 36 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.

the class SlicesShouldTest method slice_rules_should_ignore_configured_dependencies.

@Test
@UseDataProvider("rules")
public void slice_rules_should_ignore_configured_dependencies(SliceRule rule) {
    JavaClasses classes = new ClassFileImporter().importPackages(TEST_CLASSES_PACKAGE);
    assertViolations(classes, rule).contain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).contain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(classIn(".*\\.first\\.three\\..*"), DescribedPredicate.<JavaClass>alwaysTrue());
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).contain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(FirstAnyPkgClass.class.getName(), SomePkgSubclass.class.getName());
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).contain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(SecondAnyClass.class, FirstAnyPkgClass.class);
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).doNotContain(SecondAnyClass.class, FirstAnyPkgClass.class).contain(SecondThreeAnyClass.class, SomePkgClass.class);
    rule = rule.ignoreDependency(DescribedPredicate.<JavaClass>alwaysTrue(), classIn(".*\\.some\\.pkg\\..*"));
    assertViolations(classes, rule).doNotContain(FirstThreeAnyClass.class, SecondThreeAnyClass.class).doNotContain(FirstAnyPkgClass.class, SomePkgSubclass.class).doNotContain(SecondAnyClass.class, FirstAnyPkgClass.class).doNotContain(SecondThreeAnyClass.class, SomePkgClass.class);
}
Also used : SecondThreeAnyClass(com.tngtech.archunit.library.testclasses.second.three.any.SecondThreeAnyClass) JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) SecondAnyClass(com.tngtech.archunit.library.testclasses.second.any.pkg.SecondAnyClass) FirstThreeAnyClass(com.tngtech.archunit.library.testclasses.first.three.any.FirstThreeAnyClass) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) FirstAnyPkgClass(com.tngtech.archunit.library.testclasses.first.any.pkg.FirstAnyPkgClass) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 37 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.

the class ClassCacheTest method filters_jars_relative_to_class.

@Test
public void filters_jars_relative_to_class() {
    JavaClasses classes = cache.getClassesToAnalyzeFor(TestClass.class, analyzePackagesOf(Rule.class));
    assertThat(classes).isNotEmpty();
    for (JavaClass clazz : classes) {
        assertThat(clazz.getPackageName()).doesNotContain("tngtech");
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchConfigurationRule(com.tngtech.archunit.testutil.ArchConfigurationRule) Rule(org.junit.Rule) MockitoRule(org.mockito.junit.MockitoRule) Test(org.junit.Test)

Example 38 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.

the class ClassGraphCreator method createEnclosingCodeUnit.

@Override
public Optional<JavaCodeUnit> createEnclosingCodeUnit(JavaClass owner) {
    Optional<CodeUnit> enclosingCodeUnit = importRecord.getEnclosingCodeUnitFor(owner.getName());
    if (!enclosingCodeUnit.isPresent()) {
        return Optional.empty();
    }
    CodeUnit codeUnit = enclosingCodeUnit.get();
    JavaClass enclosingClass = classes.getOrResolve(codeUnit.getDeclaringClassName());
    return enclosingClass.tryGetCodeUnitWithParameterTypeNames(codeUnit.getName(), codeUnit.getRawParameterTypeNames());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) CodeUnit(com.tngtech.archunit.core.importer.RawAccessRecord.CodeUnit)

Example 39 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.

the class ImportedClasses method resolve.

private JavaClass resolve(String typeName) {
    Optional<JavaClass> resolved = resolver.tryResolve(typeName);
    JavaClass javaClass = resolved.isPresent() ? resolved.get() : stubClassOf(typeName);
    if (javaClass.isArray()) {
        ensureAllComponentTypesPresent(javaClass);
    }
    allClasses.put(typeName, javaClass);
    return javaClass;
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass)

Example 40 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.

the class ClassFileImporterLambdaAccessesTest method does_not_add_synthetic_lambda_methods_to_classes.

@Test
public void does_not_add_synthetic_lambda_methods_to_classes() {
    class Target {

        void target() {
        }
    }
    @SuppressWarnings("unused")
    class Caller {

        @SuppressWarnings("Convert2MethodRef")
        Runnable call(Target target) {
            return () -> target.target();
        }
    }
    JavaClass caller = new ClassFileImporter().importClasses(Target.class, Caller.class).get(Caller.class);
    assertThat(caller.getMethods()).doNotHave(syntheticLambdaMethods()).hasSize(1);
}
Also used : MethodCallTarget(com.tngtech.archunit.core.domain.AccessTarget.MethodCallTarget) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9