com.tngtech.archunit.junit.internal.AbstractArchUnitTestDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of archunit-junit5-engine Show documentation
Show all versions of archunit-junit5-engine Show documentation
A Java architecture test library, to specify and assert architecture rules in plain Java - Module 'archunit-junit5-engine'
The newest version!
/*
* Copyright 2014-2024 TNG Technology Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tngtech.archunit.junit.internal;
import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import com.tngtech.archunit.core.domain.Formatters;
import com.tngtech.archunit.junit.ArchIgnore;
import com.tngtech.archunit.junit.ArchTag;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
import org.junit.platform.engine.support.hierarchical.Node;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;
abstract class AbstractArchUnitTestDescriptor extends AbstractTestDescriptor implements Node {
private final Set tags;
private final SkipResult skipResult;
AbstractArchUnitTestDescriptor(UniqueId uniqueId, String displayName, TestSource source, AnnotatedElement... elements) {
super(uniqueId, displayName, source);
tags = Arrays.stream(elements).map(this::findTagsOn).flatMap(Collection::stream).collect(toSet());
skipResult = Arrays.stream(elements)
.map(e -> AnnotationSupport.findAnnotation(e, ArchIgnore.class))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.map(ignore -> SkipResult.skip(ignore.reason()))
.orElse(SkipResult.doNotSkip());
}
private Set findTagsOn(AnnotatedElement annotatedElement) {
return AnnotationSupport.findRepeatableAnnotations(annotatedElement, ArchTag.class)
.stream()
.map(annotation -> TestTag.create(annotation.value()))
.collect(toSet());
}
@Override
public SkipResult shouldBeSkipped(ArchUnitEngineExecutionContext context) {
return skipResult;
}
@Override
public Set getTags() {
Set result = new HashSet<>(tags);
result.addAll(getParent().map(TestDescriptor::getTags).orElse(emptySet()));
return result;
}
static String formatWithPath(UniqueId uniqueId, String name) {
return Stream.concat(
uniqueId.getSegments().stream()
.filter(it -> it.getType().equals(ArchUnitTestDescriptor.CLASS_SEGMENT_TYPE))
.skip(1)
.map(UniqueId.Segment::getValue)
.map(Formatters::ensureSimpleName),
Stream.of(name)
).collect(joining(" > "));
}
}