Skip to content

Commit

Permalink
Fixed failing integration tests. Added support for annotation depende…
Browse files Browse the repository at this point in the history
…ncies

Issue: #136
Signed-off-by: Alen Kosanović <alen.kosanovic@svgroup.hr>
  • Loading branch information
kosani authored and Alen Kosanović committed Jan 9, 2020
1 parent f53bb4c commit cf95711
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public class LayerDependencyRulesTest {
@ArchTest
public static final ArchRule services_should_only_depend_on_persistence_or_other_services =
classes().that().resideInAPackage("..service..")
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..");
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..", "javax..");

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public class LayerDependencyRulesTest {
@ArchTest
static final ArchRule services_should_only_depend_on_persistence_or_other_services =
classes().that().resideInAPackage("..service..")
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..");
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..", "javax..");

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void services_should_only_be_depended_on_by_controllers_or_other_services
@Test
public void services_should_only_depend_on_persistence_or_other_services() {
classes().that().resideInAPackage("..service..")
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..").check(classes);
.should().onlyDependOnClassesThat().resideInAnyPackage("..service..", "..persistence..", "java..", "javax..")
.check(classes);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import com.tngtech.archunit.example.layers.service.ServiceInterface;
import com.tngtech.archunit.example.layers.service.ServiceViolatingDaoRules;
import com.tngtech.archunit.example.layers.service.ServiceViolatingLayerRules;
import com.tngtech.archunit.example.layers.service.impl.ServiceImplementation;
import com.tngtech.archunit.example.layers.service.impl.SomeInterfacePlacedInTheWrongPackage;
import com.tngtech.archunit.example.layers.service.impl.WronglyNamedSvc;
import com.tngtech.archunit.example.layers.thirdparty.ThirdPartyClassWithProblem;
Expand Down Expand Up @@ -150,6 +151,7 @@
import static com.tngtech.archunit.testutils.ExpectedAccess.callFromConstructor;
import static com.tngtech.archunit.testutils.ExpectedAccess.callFromMethod;
import static com.tngtech.archunit.testutils.ExpectedAccess.callFromStaticInitializer;
import static com.tngtech.archunit.testutils.ExpectedDependency.annotatedClass;
import static com.tngtech.archunit.testutils.ExpectedDependency.constructor;
import static com.tngtech.archunit.testutils.ExpectedDependency.field;
import static com.tngtech.archunit.testutils.ExpectedDependency.inheritanceFrom;
Expand Down Expand Up @@ -709,20 +711,30 @@ Stream<DynamicTest> LayerDependencyRulesTest() {
.by(field(DaoCallingService.class, "service").ofType(ServiceViolatingLayerRules.class))

.ofRule("classes that reside in a package '..service..' should "
+ "only depend on classes that reside in any package ['..service..', '..persistence..', 'java..']")
+ "only depend on classes that reside in any package ['..service..', '..persistence..', 'java..', 'javax..']")
.by(callFromMethod(ServiceViolatingLayerRules.class, illegalAccessToController)
.getting().field(UseCaseOneTwoController.class, UseCaseOneTwoController.someString)
.getting().field(UseCaseOneTwoController.class, someString)
.inLine(16).asDependency())
.by(callFromMethod(ServiceViolatingLayerRules.class, illegalAccessToController)
.toConstructor(UseCaseTwoController.class)
.inLine(17).asDependency())
.by(callFromMethod(ServiceViolatingLayerRules.class, illegalAccessToController)
.toMethod(UseCaseTwoController.class, UseCaseTwoController.doSomethingTwo)
.toMethod(UseCaseTwoController.class, doSomethingTwo)
.inLine(18).asDependency())
.by(method(ServiceViolatingLayerRules.class, ServiceViolatingLayerRules.dependentMethod)
.by(method(ServiceViolatingLayerRules.class, dependentMethod)
.withParameter(UseCaseTwoController.class))
.by(method(ServiceViolatingLayerRules.class, ServiceViolatingLayerRules.dependentMethod)
.by(method(ServiceViolatingLayerRules.class, dependentMethod)
.withReturnType(SomeGuiController.class))
.by(field(ServiceHelper.class, "properlySecured")
.withAnnotationType(Secured.class))
.by(method(ServiceViolatingLayerRules.class, "properlySecured")
.withAnnotationType(Secured.class))
.by(constructor(ServiceHelper.class)
.withAnnotationType(Secured.class))
.by(annotatedClass(ServiceViolatingDaoRules.class).annotatedWith(MyService.class))
.by(annotatedClass(ServiceViolatingLayerRules.class).annotatedWith(MyService.class))
.by(annotatedClass(ServiceImplementation.class).annotatedWith(MyService.class))
.by(annotatedClass(WronglyNamedSvc.class).annotatedWith(MyService.class))

.toDynamicTests();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static InheritanceCreator inheritanceFrom(Class<?> clazz) {
return new InheritanceCreator(clazz);
}

public static AnnotationDependencyCreator annotatedClass(Class<?> clazz) {
return new AnnotationDependencyCreator(clazz);
}

public static AccessCreator accessFrom(Class<?> clazz) {
return new AccessCreator(clazz);
}
Expand Down Expand Up @@ -135,8 +139,25 @@ public ExpectedDependency withReturnType(Class<?> returnType) {
return new ExpectedDependency(owner, returnType, dependencyPattern);
}

public ExpectedDependency withAnnotationType(Class<?> annotationType) {
return new ExpectedDependency(owner, annotationType, getDependencyPattern(getOriginName(), "is annotated with", annotationType.getName(), 0));
}

private String getOriginName() {
return owner.getName() + "." + memberName;
}
}

public static class AnnotationDependencyCreator {
private final Class<?> owner;

AnnotationDependencyCreator(Class<?> owner) {
this.owner = owner;
}

public ExpectedDependency annotatedWith(Class<?> annotationType) {
String dependencyPattern = getDependencyPattern(owner.getName(), "is annotated with", annotationType.getName(), 0);
return new ExpectedDependency(owner, annotationType, dependencyPattern);
}
}
}

0 comments on commit cf95711

Please sign in to comment.