com.tngtech.archunit.lang.SimpleConditionEvent Java Examples

The following examples show how to use com.tngtech.archunit.lang.SimpleConditionEvent. 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: CoreSubscriberArchTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotUseDefaultCurrentContext() {
	classes()
			.that().implement(CoreSubscriber.class)
			.and().doNotHaveModifier(JavaModifier.ABSTRACT)
			.should(new ArchCondition<JavaClass>("not use the default currentContext()") {
				@Override
				public void check(JavaClass item, ConditionEvents events) {
					boolean overridesMethod = item
							.getAllMethods()
							.stream()
							.filter(it -> "currentContext".equals(it.getName()))
							.filter(it -> it.getRawParameterTypes().isEmpty())
							.anyMatch(it -> !it.getOwner().isEquivalentTo(CoreSubscriber.class));

					if (!overridesMethod) {
						events.add(SimpleConditionEvent.violated(
								item,
								item.getFullName() + item.getSourceCodeLocation() + ": currentContext() is not overridden"
						));
					}
				}
			})
			.check(classes);
}
 
Example #2
Source File: BaseArchitectureComplianceCheck.java    From waltz with Apache License 2.0 6 votes vote down vote up
@Override
public void check(JavaClass item, ConditionEvents events) {
    item.getMethods()
            .stream()
            .filter(m -> m.getName().startsWith("find"))
            .filter(m -> m.getModifiers().contains(JavaModifier.PUBLIC))
            .forEach(m -> {
                JavaClass returnType = m.getReturnType();
                if (! any(validReturnTypes, vrt -> returnType.isAssignableTo(vrt))) {
                    String message = String.format(
                            "Method %s.%s does not return a collection, map or optional. It returns: %s",
                            item.getName(),
                            m.getName(),
                            returnType.getName());
                    events.add(SimpleConditionEvent.violated(item, message));
                }
            });
}
 
Example #3
Source File: ApplicationComponentsTest.java    From archunit-examples with MIT License 6 votes vote down vote up
@Override
public void check(JavaClass clazz, ConditionEvents events) {
  Set<JavaMethod> methods = clazz.getMethods();
  Predicate<JavaMethod> hasMethodNamedInvoke = method -> method.getName().equals("invoke");
  if (methods.stream().filter(hasMethodNamedInvoke).count() != 1) {
    events.add(SimpleConditionEvent.violated(clazz, "${clazz.simpleName} does not have a single 'invoke' method"));
    return;
  }
  methods.stream().filter(hasMethodNamedInvoke).findFirst().ifPresent(invokeMethod -> {
    JavaClassList parameters = invokeMethod.getRawParameterTypes();
    if (parameters.size() != 1 || parameters.stream().noneMatch(it -> it.isInnerClass() && it.getSimpleName().equals("Request"))) {
      events.add(SimpleConditionEvent.violated(invokeMethod, "${clazz.simpleName}: method 'invoke' does not have a single parameter that is named 'request' and of inner-class type 'Request'"));
    }
    JavaClass returnType = invokeMethod.getRawReturnType();
    if (!returnType.isInnerClass() || !returnType.getSimpleName().equals("Response")) {
      events.add(SimpleConditionEvent.violated(invokeMethod, "${clazz.simpleName}: method 'invoke' does not have a return type that is of inner-class type 'Response'"));
    }
  });
}
 
Example #4
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean satisfied = predicate.apply(javaClass);
    String message = String.format("simple name of %s %s with '%s' in %s",
            javaClass.getName(),
            satisfied ? "starts" : "does not start",
            prefix,
            javaClass.getSourceCodeLocation());
    events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
 
Example #5
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean satisfied = predicate.apply(javaClass);
    String message = String.format("simple name of %s %s '%s' in %s",
            javaClass.getName(),
            satisfied ? "contains" : "does not contain",
            infix,
            javaClass.getSourceCodeLocation());
    events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
 
Example #6
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean satisfied = predicate.apply(javaClass);
    String message = String.format("simple name of %s %s with '%s' in %s",
            javaClass.getName(),
            satisfied ? "ends" : "does not end",
            suffix,
            javaClass.getSourceCodeLocation());
    events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
 
Example #7
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T item, ConditionEvents events) {
    boolean satisfied = matcher.apply(item);
    String message = createMessage(item,
            String.format("%s '%s'", satisfied ? "matches" : "does not match", regex));
    events.add(new SimpleConditionEvent(item, satisfied, message));
}
 
Example #8
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T item, ConditionEvents events) {
    boolean satisfied = startingWith.apply(item);
    String message = createMessage(item,
            String.format("name %s '%s'", satisfied ? "starts with" : "does not start with", prefix));
    events.add(new SimpleConditionEvent(item, satisfied, message));
}
 
Example #9
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T item, ConditionEvents events) {
    boolean satisfied = containing.apply(item);
    String message = createMessage(item,
            String.format("name %s '%s'", satisfied ? "contains" : "does not contain", infix));
    events.add(new SimpleConditionEvent(item, satisfied, message));
}
 
Example #10
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T item, ConditionEvents events) {
    boolean satisfied = endingWith.apply(item);
    String message = createMessage(item,
            String.format("name %s '%s'", satisfied ? "ends with" : "does not end with", suffix));
    events.add(new SimpleConditionEvent(item, satisfied, message));
}
 
Example #11
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T item, ConditionEvents events) {
    boolean satisfied = predicate.apply(item);
    String message = createMessage(item,
            (satisfied ? "does " : "does not ") + predicate.getDescription());
    events.add(new SimpleConditionEvent(item, satisfied, message));
}
 
Example #12
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(T member, ConditionEvents events) {
    boolean satisfied = predicate.apply(member);
    String message = createMessage(member,
            (satisfied ? "is " : "is not ") + eventDescription);
    events.add(new SimpleConditionEvent(member, satisfied, message));
}
 
Example #13
Source File: DependencyRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(final JavaClass clazz, final ConditionEvents events) {
    for (Dependency dependency : clazz.getDirectDependenciesFromSelf()) {
        boolean dependencyOnUpperPackage = isDependencyOnUpperPackage(dependency.getOriginClass(), dependency.getTargetClass());
        events.add(new SimpleConditionEvent(dependency, dependencyOnUpperPackage, dependency.getDescription()));
    }
}
 
Example #14
Source File: SliceCycleArchCondition.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private ConditionEvent newEvent(Cycle<Slice, Dependency> cycle) {
    Map<String, Edge<Slice, Dependency>> descriptionsToEdges = sortEdgesByDescription(cycle);
    String description = createDescription(descriptionsToEdges.keySet());
    String details = createDetails(descriptionsToEdges);
    return new SimpleConditionEvent(cycle,
            false,
            String.format(MESSAGE_TEMPLATE, description, details));
}
 
Example #15
Source File: GivenSlicesInternal.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(Slice slice, ConditionEvents events) {
    Iterable<Dependency> relevantDependencies = filter(slice.getDependenciesFromSelf(), predicate);
    Slices dependencySlices = inputTransformer.transform(relevantDependencies);
    for (Slice dependencySlice : dependencySlices) {
        SliceDependency dependency = SliceDependency.of(slice, relevantDependencies, dependencySlice);
        events.add(SimpleConditionEvent.violated(dependency, dependency.getDescription()));
    }
}
 
Example #16
Source File: GivenMembersTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
static ArchCondition<JavaMember> everythingViolationPrintMemberName() {
    return new ArchCondition<JavaMember>("condition text") {
        @Override
        public void check(JavaMember item, ConditionEvents events) {
            events.add(SimpleConditionEvent.violated(item, formatMember(item)));
        }
    };
}
 
Example #17
Source File: GivenMembersTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
static ArchCondition<JavaMember> beAnnotatedWith(final Class<? extends Annotation> annotationType) {
    return new ArchCondition<JavaMember>("be annotated with @%s", annotationType.getSimpleName()) {
        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = member.isAnnotatedWith(annotationType);
            String message = String.format("Member '%s' %s @%s",
                    formatMember(member), satisfied ? "is annotated with" : "is not annotated with",
                    annotationType.getSimpleName());
            events.add(new SimpleConditionEvent(member, satisfied, message));
        }
    };
}
 
Example #18
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean itemEquivalentToClazz = javaClass.getName().equals(className);
    String message = createMessage(javaClass,
            (itemEquivalentToClazz ? "is " : "is not ") + className);
    events.add(new SimpleConditionEvent(javaClass, itemEquivalentToClazz, message));
}
 
Example #19
Source File: ArchitectureTest.java    From taskana with Apache License 2.0 5 votes vote down vote up
@Test
void everySubpackageShouldBeTestsForCyclicDependencies() {
  List<Pattern> excludePackages =
      Stream.of(
              "pro.taskana", // from TaskanaEngineConfiguration
              "acceptance.*" // all our acceptance tests
              )
          .map(Pattern::compile)
          .collect(Collectors.toList());
  ArchCondition<JavaClass> condition =
      new ArchCondition<JavaClass>("all be defined in TASKANA_SUB_PACKAGES") {
        @Override
        public void check(JavaClass item, ConditionEvents events) {
          if (TASKANA_SUB_PACKAGES.stream().noneMatch(p -> item.getPackageName().startsWith(p))
              && excludePackages.stream()
                  .noneMatch(p -> p.matcher(item.getPackageName()).matches())) {
            String message =
                String.format(
                    "Package '%s' was not declared in TASKANA_SUB_PACKAGES",
                    item.getPackageName());
            events.add(SimpleConditionEvent.violated(item, message));
          }
        }
      };
  ArchRule myRule = classes().should(condition);
  myRule.check(importedClasses);
}
 
Example #20
Source File: RuleThatFails.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
public static ArchRule on(Class<?> input) {
    return classes().should(new ArchCondition<JavaClass>("not be " + input.getName()) {
        @Override
        public void check(JavaClass item, ConditionEvents events) {
            if (item.isEquivalentTo(input)) {
                events.add(SimpleConditionEvent.violated(item, "Got class " + item.getName()));
            }
        }
    });
}
 
Example #21
Source File: PublicAPIRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static ArchCondition<JavaMember> notBePublic() {
    return new ArchCondition<JavaMember>("not be public") {
        @Override
        public void check(JavaMember member, ConditionEvents events) {
            boolean satisfied = !member.getModifiers().contains(PUBLIC);
            events.add(new SimpleConditionEvent(member, satisfied,
                    String.format("member %s.%s is %spublic in %s",
                            member.getOwner().getName(),
                            member.getName(),
                            satisfied ? "not " : "",
                            member.getSourceCodeLocation())));
        }
    };
}
 
Example #22
Source File: PublicAPIRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static ArchCondition<? super JavaClass> beInterfaces() {
    return new ArchCondition<JavaClass>("be interfaces") {
        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isInterface();
            events.add(new SimpleConditionEvent(item, satisfied,
                    String.format("class %s is %sinterface", item.getName(), satisfied ? "" : "no ")));
        }
    };
}
 
Example #23
Source File: PublicAPIRules.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
private static ArchCondition<JavaClass> bePublicAPIForInheritance() {
    return new ArchCondition<JavaClass>("be public API for inheritance") {
        @Override
        public void check(JavaClass item, ConditionEvents events) {
            boolean satisfied = item.isAnnotatedWith(publicApiForInheritance()) ||
                    markedAsPublicAPIForInheritance().apply(item);
            events.add(new SimpleConditionEvent(item, satisfied,
                    String.format("class %s is %smeant for inheritance", item.getName(), satisfied ? "" : "not ")));
        }
    };
}
 
Example #24
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean satisfied = implement.apply(javaClass);
    String description = satisfied
            ? implement.getDescription().replace("implement", "implements")
            : implement.getDescription().replace("implement", "does not implement");
    String message = createMessage(javaClass, description);
    events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
 
Example #25
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean isInterface = javaClass.isInterface();
    String message = createMessage(javaClass,
            (isInterface ? "is an" : "is not an") + " interface");
    events.add(new SimpleConditionEvent(javaClass, isInterface, message));
}
 
Example #26
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean isEnum = javaClass.isEnum();
    String message = createMessage(javaClass,
            (isEnum ? "is an" : "is not an") + " enum");
    events.add(new SimpleConditionEvent(javaClass, isEnum, message));
}
 
Example #27
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void finish(ConditionEvents events) {
    int size = allClassNames.size();
    boolean conditionSatisfied = predicate.apply(size);
    String message = String.format("there is/are %d element(s) in classes %s", size, join(allClassNames));
    events.add(new SimpleConditionEvent(size, conditionSatisfied, message));
}
 
Example #28
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
    boolean satisfied = haveSimpleName.apply(javaClass);
    String message = createMessage(javaClass,
            String.format("%s simple name '%s'", satisfied ? "has" : "does not have", name));
    events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
 
Example #29
Source File: ArchConditions.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@Override
public void check(T hasModifiers, ConditionEvents events) {
    boolean satisfied = hasModifiers.getModifiers().contains(modifier);
    String infix = (satisfied ? "is " : "is not ") + modifier.toString().toLowerCase();
    events.add(new SimpleConditionEvent(hasModifiers, satisfied, createMessage(hasModifiers, infix)));
}
 
Example #30
Source File: FieldAccessCondition.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
@Override
public void check(JavaFieldAccess item, ConditionEvents events) {
    events.add(new SimpleConditionEvent(item, fieldAccessIdentifier.apply(item), item.getDescription()));
}