use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.
the class ProxyRulesTest method detects_direct_self_call_to_method_annotated_with_specific_annotation.
@Test
@UseDataProvider("call_own_method_with_specific_annotation_rules")
public void detects_direct_self_call_to_method_annotated_with_specific_annotation(ArchRule rule, String expectedDescription) {
class OtherClass {
@ProxyAnnotation
void otherProxied() {
}
}
@SuppressWarnings("unused")
class SomeClass {
void evil() {
selfProxied();
}
void okay() {
new OtherClass().otherProxied();
nonProxied();
}
void nonProxied() {
}
@ProxyAnnotation
void selfProxied() {
}
}
assertThatRule(rule).hasDescriptionContaining(expectedDescription).checking(new ClassFileImporter().importClasses(SomeClass.class, OtherClass.class)).hasOnlyOneViolationMatching(String.format(".*%s.* calls method .*%s.*", quoteMethod(SomeClass.class, "evil"), quoteMethod(SomeClass.class, "selfProxied")));
}
use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.
the class GivenSlicesTest method restricting_slices_that_should_not_depend_on_each_other.
@Test
public void restricting_slices_that_should_not_depend_on_each_other() {
archConfigurationRule.setFailOnEmptyShould(false);
GivenSlices givenSlices = slices().matching("..testclasses.(*)..");
JavaClasses classes = new ClassFileImporter().importPackages("com.tngtech.archunit.library.testclasses");
EvaluationResult result = givenSlices.that(DescribedPredicate.<Slice>alwaysFalse()).and(DescribedPredicate.<Slice>alwaysTrue()).should().notDependOnEachOther().evaluate(classes);
assertThat(result.hasViolation()).as("Result has violation").isFalse();
result = givenSlices.that(DescribedPredicate.<Slice>alwaysTrue()).or(DescribedPredicate.<Slice>alwaysFalse()).should().notDependOnEachOther().evaluate(classes);
assertThat(result.hasViolation()).as("Result has violation").isTrue();
}
use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.
the class PlantUmlArchConditionTest method getClassesFrom.
private static JavaClasses getClassesFrom(String pkg) {
String packageName = "com.tngtech.archunit.library.diagramtests." + pkg;
JavaClasses classes = new ClassFileImporter().importPackages(packageName);
if (isEmpty(classes)) {
throw new IllegalStateException(String.format("No classes were imported from '%s'", packageName));
}
return classes;
}
use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.
the class ComponentDependencyMetricsTest method component_dependency_metrics_of_an_isolated_component.
@Test
public void component_dependency_metrics_of_an_isolated_component() {
JavaPackage javaPackage = new ClassFileImporter().importPackagesOf(SimpleWithoutDependencies.class).get(SimpleWithoutDependencies.class).getPackage();
MetricsComponents<JavaClass> components = MetricsComponents.fromPackages(singleton(javaPackage));
ComponentDependencyMetrics metrics = ArchitectureMetrics.componentDependencyMetrics(components);
assertThat(metrics.getEfferentCoupling(javaPackage.getName())).as("Efferent Coupling of " + javaPackage.getName()).isEqualTo(0);
assertThat(metrics.getAfferentCoupling(javaPackage.getName())).as("Afferent Coupling of " + javaPackage.getName()).isEqualTo(0);
assertThat(metrics.getInstability(javaPackage.getName())).as("Instability of " + javaPackage.getName()).isEqualTo(1);
assertThat(metrics.getAbstractness(javaPackage.getName())).as("Abstractness of " + javaPackage.getName()).isEqualTo(0);
assertThat(metrics.getNormalizedDistanceFromMainSequence(javaPackage.getName())).as("Normalized Distance from Main Sequence of " + javaPackage.getName()).isEqualTo(0);
}
use of com.tngtech.archunit.core.importer.ClassFileImporter in project ArchUnit by TNG.
the class ShouldClassesThatTest method transitivelyDependOnClassesThat_reports_all_transitive_dependencies.
@Test
@DataProvider(value = { "true", "false" })
public void transitivelyDependOnClassesThat_reports_all_transitive_dependencies(boolean viaPredicate) {
Class<?> testClass = TransitivelyDependOnClassesThatTestCases.TestClass.class;
Class<?> directlyDependentClass1 = TransitivelyDependOnClassesThatTestCases.DirectlyDependentClass1.class;
Class<?> directlyDependentClass2 = TransitivelyDependOnClassesThatTestCases.DirectlyDependentClass2.class;
Class<?> transitivelyDependentClass = TransitivelyDependOnClassesThatTestCases.TransitivelyDependentClass.class;
JavaClasses classes = new ClassFileImporter().importClasses(testClass, directlyDependentClass1, directlyDependentClass2, transitivelyDependentClass);
ClassesShould noClassesShould = noClasses().that().haveFullyQualifiedName(testClass.getName()).should();
ArchRule rule = viaPredicate ? noClassesShould.transitivelyDependOnClassesThat(have(fullyQualifiedName(transitivelyDependentClass.getName()))) : noClassesShould.transitivelyDependOnClassesThat().haveFullyQualifiedName(transitivelyDependentClass.getName());
assertThatRule(rule).checking(classes).hasViolations(2).hasViolationMatching(String.format(".*%s\\.%s.* has type .*%s.*", quote(directlyDependentClass1.getName()), "transitiveDependency1", quote(transitivelyDependentClass.getName()))).hasViolationMatching(String.format(".*%s\\.%s.* has type .*%s.*", quote(directlyDependentClass2.getName()), "transitiveDependency2", quote(transitivelyDependentClass.getName())));
}
Aggregations