Search in sources :

Example 1 with Segment

use of org.junit.platform.engine.UniqueId.Segment in project junit5 by junit-team.

the class JavaElementsResolver method resolveAllSegments.

/**
 * Attempt to resolve all segments for the supplied unique ID.
 */
private Deque<TestDescriptor> resolveAllSegments(UniqueId uniqueId) {
    List<Segment> segments = uniqueId.getSegments();
    Deque<TestDescriptor> resolvedDescriptors = new LinkedList<>();
    resolvedDescriptors.addFirst(this.engineDescriptor);
    for (int index = 1; index < segments.size() && resolvedDescriptors.size() == index; index++) {
        Segment segment = segments.get(index);
        TestDescriptor parent = resolvedDescriptors.getLast();
        UniqueId partialUniqueId = parent.getUniqueId().append(segment);
        Optional<TestDescriptor> resolvedDescriptor = findTestDescriptorByUniqueId(partialUniqueId);
        if (!resolvedDescriptor.isPresent()) {
            // @formatter:off
            resolvedDescriptor = this.resolvers.stream().map(resolver -> resolver.resolveUniqueId(segment, parent)).filter(Optional::isPresent).map(Optional::get).findFirst();
            // @formatter:on
            resolvedDescriptor.ifPresent(parent::addChild);
        }
        resolvedDescriptor.ifPresent(resolvedDescriptors::addLast);
    }
    return resolvedDescriptors;
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) Optional(java.util.Optional) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) TestDescriptor(org.junit.platform.engine.TestDescriptor) Segment(org.junit.platform.engine.UniqueId.Segment) LinkedList(java.util.LinkedList)

Example 2 with Segment

use of org.junit.platform.engine.UniqueId.Segment in project junit5 by junit-team.

the class UniqueIdSelectorResolver method determineTestClassName.

private Optional<String> determineTestClassName(UniqueId uniqueId) {
    // skip engine node
    Segment runnerSegment = uniqueId.getSegments().get(1);
    if (SEGMENT_TYPE_RUNNER.equals(runnerSegment.getType())) {
        return Optional.of(runnerSegment.getValue());
    }
    logger.warn(() -> format("Unresolvable Unique ID (%s): Unique ID segment after engine segment must be of type \"" + SEGMENT_TYPE_RUNNER + "\"", uniqueId));
    return Optional.empty();
}
Also used : Segment(org.junit.platform.engine.UniqueId.Segment)

Example 3 with Segment

use of org.junit.platform.engine.UniqueId.Segment in project junit5 by junit-team.

the class UniqueIdFormat method createSegment.

private Segment createSegment(String segmentString) throws JUnitException {
    Matcher segmentMatcher = this.segmentPattern.matcher(segmentString);
    if (!segmentMatcher.matches()) {
        throw new JUnitException(String.format("'%s' is not a well-formed UniqueId segment", segmentString));
    }
    String type = decode(checkAllowed(segmentMatcher.group(1)));
    String value = decode(checkAllowed(segmentMatcher.group(2)));
    return new Segment(type, value);
}
Also used : Matcher(java.util.regex.Matcher) JUnitException(org.junit.platform.commons.JUnitException) Segment(org.junit.platform.engine.UniqueId.Segment)

Example 4 with Segment

use of org.junit.platform.engine.UniqueId.Segment in project junit5 by junit-team.

the class ClassSelectorResolver method resolve.

@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
    Segment lastSegment = selector.getUniqueId().getLastSegment();
    if (SEGMENT_TYPE_RUNNER.equals(lastSegment.getType())) {
        String testClassName = lastSegment.getValue();
        Class<?> testClass = // 
        ReflectionUtils.tryToLoadClass(testClassName).getOrThrow(cause -> new JUnitException("Unknown class: " + testClassName, cause));
        return resolveTestClass(testClass, context);
    }
    return unresolved();
}
Also used : JUnitException(org.junit.platform.commons.JUnitException) Segment(org.junit.platform.engine.UniqueId.Segment)

Example 5 with Segment

use of org.junit.platform.engine.UniqueId.Segment in project junit5 by junit-team.

the class ClassSelectorResolver method resolve.

@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
    UniqueId uniqueId = selector.getUniqueId();
    UniqueId engineId = suiteEngineDescriptor.getUniqueId();
    List<Segment> resolvedSegments = engineId.getSegments();
    // @formatter:off
    return uniqueId.getSegments().stream().skip(resolvedSegments.size()).findFirst().filter(suiteSegment -> SuiteTestDescriptor.SEGMENT_TYPE.equals(suiteSegment.getType())).flatMap(ClassSelectorResolver::tryLoadSuiteClass).filter(isSuiteClass).map(suiteClass -> context.addToParent(parent -> newSuiteDescriptor(suiteClass, parent)).map(suite -> uniqueId.equals(suite.getUniqueId()) ? // The uniqueId selector either targeted a class annotated with @Suite;
    suite.addDiscoveryRequestFrom(suiteClass) : // or a specific test in that suite
    suite.addDiscoveryRequestFrom(uniqueId))).map(ClassSelectorResolver::toResolution).orElseGet(Resolution::unresolved);
// @formatter:on
}
Also used : List(java.util.List) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) Predicate(java.util.function.Predicate) Segment(org.junit.platform.engine.UniqueId.Segment) Optional(java.util.Optional) UniqueIdSelector(org.junit.platform.engine.discovery.UniqueIdSelector) Resolution.unresolved(org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.unresolved) TestDescriptor(org.junit.platform.engine.TestDescriptor) UniqueId(org.junit.platform.engine.UniqueId) SelectorResolver(org.junit.platform.engine.support.discovery.SelectorResolver) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) ClassSelector(org.junit.platform.engine.discovery.ClassSelector) UniqueId(org.junit.platform.engine.UniqueId) Segment(org.junit.platform.engine.UniqueId.Segment)

Aggregations

Segment (org.junit.platform.engine.UniqueId.Segment)5 Optional (java.util.Optional)2 JUnitException (org.junit.platform.commons.JUnitException)2 TestDescriptor (org.junit.platform.engine.TestDescriptor)2 UniqueId (org.junit.platform.engine.UniqueId)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Matcher (java.util.regex.Matcher)1 ClassTestDescriptor (org.junit.jupiter.engine.descriptor.ClassTestDescriptor)1 ReflectionUtils (org.junit.platform.commons.util.ReflectionUtils)1 ConfigurationParameters (org.junit.platform.engine.ConfigurationParameters)1 ClassSelector (org.junit.platform.engine.discovery.ClassSelector)1 UniqueIdSelector (org.junit.platform.engine.discovery.UniqueIdSelector)1 SelectorResolver (org.junit.platform.engine.support.discovery.SelectorResolver)1 Resolution.unresolved (org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.unresolved)1