org.junit.platform.engine.TestSource Java Examples

The following examples show how to use org.junit.platform.engine.TestSource. 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: AllureJunitPlatform.java    From allure-java with Apache License 2.0 7 votes vote down vote up
private List<Label> getSourceLabels(final TestSource source) {
    if (source instanceof MethodSource) {
        final MethodSource ms = (MethodSource) source;
        return Arrays.asList(
                createPackageLabel(ms.getClassName()),
                createTestClassLabel(ms.getClassName()),
                createTestMethodLabel(ms.getMethodName())
        );
    }
    if (source instanceof ClassSource) {
        final ClassSource cs = (ClassSource) source;
        return Arrays.asList(
                createPackageLabel(cs.getClassName()),
                createTestClassLabel(cs.getClassName())
        );
    }
    return Collections.emptyList();
}
 
Example #2
Source File: JupiterTestCollector.java    From sbt-jupiter-interface with Apache License 2.0 6 votes vote down vote up
private String fullyQualifiedName(TestIdentifier testIdentifier) {

        TestSource testSource = testIdentifier.getSource().orElse(null);

        if (testSource instanceof ClassSource) {

            ClassSource classSource = (ClassSource)testSource;
            return classSource.getClassName();
        }

        if (testSource instanceof MethodSource) {

            MethodSource methodSource = (MethodSource)testSource;
            return methodSource.getClassName()
                    + '#' + methodSource.getMethodName()
                    + '(' + methodSource.getMethodParameterTypes() + ')';
        }

        return testIdentifier.getLegacyReportingName();
    }
 
Example #3
Source File: Configuration.java    From sbt-jupiter-interface with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the class-name from the specified test identifier.
 *
 * @param identifier The identifier from which to extract the class-name.
 * @return The class-name of the specified test identifier.
 */
public String extractClassName(TestIdentifier identifier) {

    TestSource testSource = identifier.getSource().orElseThrow(() ->
            new RuntimeException("Test identifier without source: " + identifier));

    if (testSource instanceof ClassSource) {
        return ((ClassSource)testSource).getClassName();
    }

    if (testSource instanceof MethodSource) {
        return ((MethodSource)testSource).getClassName();
    }

    throw new RuntimeException("Test identifier with unknown source: " + identifier);
}
 
Example #4
Source File: TaskName.java    From sbt-jupiter-interface with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a task name for the specified {@code identifier}.
 *
 * @param testSuite The name of the test suite.
 * @param identifier The test identifier.
 * @return A task name representing the given identifier.
 */
static TaskName of(String testSuite, TestIdentifier identifier) {

    TaskName result = new TaskName();
    result.fullyQualifiedName = testSuite;

    TestSource testSource = identifier.getSource().orElse(null);

    if (testSource instanceof ClassSource) {

        ClassSource classSource = (ClassSource)testSource;
        result.nestedSuiteId = nestedSuiteId(testSuite, classSource.getClassName());
    }

    if (testSource instanceof MethodSource) {

        MethodSource methodSource = (MethodSource)testSource;
        result.nestedSuiteId = nestedSuiteId(testSuite, methodSource.getClassName());
        result.invocation = invocation(UniqueId.parse(identifier.getUniqueId()));
        result.testName = testName(methodSource.getMethodName(),
                methodSource.getMethodParameterTypes());
    }

    return result;
}
 
Example #5
Source File: TestHelper.java    From sbt-jupiter-interface with Apache License 2.0 6 votes vote down vote up
public TestIdentifier findByMethodName(String testMethodName) {

        final Set<TestIdentifier> descendantIdentifiers = getAllDescendants();
        return descendantIdentifiers.stream()
                .filter(testIdentifier -> {

                    TestSource testSource = testIdentifier.getSource().orElse(null);
                    if (testSource instanceof MethodSource) {
                        return ((MethodSource) testSource).getMethodName().equals(testMethodName);
                    }
                    return false;
                })
                .findAny()
                .orElseThrow(() -> {
                    String message = "Could not find test method " + testMethodName + " in:\n";
                    String identifiers = descendantIdentifiers.stream()
                            .map(TestIdentifier::getUniqueId)
                            .collect(Collectors.joining(",\n"));

                    return new AssertionError(message + identifiers);
                });
    }
 
Example #6
Source File: AllureJunitPlatform.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private Optional<String> getFullName(final TestSource source) {
    if (source instanceof MethodSource) {
        final MethodSource ms = (MethodSource) source;
        return Optional.of(String.format("%s.%s", ms.getClassName(), ms.getMethodName()));
    }
    if (source instanceof ClassSource) {
        final ClassSource cs = (ClassSource) source;
        return Optional.ofNullable(cs.getClassName());
    }
    return Optional.empty();
}
 
Example #7
Source File: AllureJunitPlatform.java    From allure-java with Apache License 2.0 5 votes vote down vote up
private Optional<Class<?>> getTestClass(final TestSource source) {
    if (source instanceof MethodSource) {
        return getTestClass(((MethodSource) source).getClassName());
    }
    if (source instanceof ClassSource) {
        return Optional.of(((ClassSource) source).getJavaClass());
    }
    return Optional.empty();
}
 
Example #8
Source File: Configuration.java    From sbt-jupiter-interface with Apache License 2.0 5 votes vote down vote up
private String toVintageName(TestIdentifier identifier, Segment lastSegment) {

            final String type = lastSegment.getType();

            if ("runner".equals(type)) {

                String className = identifier.getDisplayName();
                return colorClassName(className, colorTheme.container());
            }

            if ("test".equals(type)) {

                final TestSource source = identifier.getSource().orElse(null);

                if (null == source) {
                    // Caused by Parameterized test runner, display name usually is the index name in brackets.
                    // Ignored since the index name is repeated in the display name of the test method.
                    return null;
                }

                if (source instanceof ClassSource) {
                    String nestedClassName = "$" + identifier.getDisplayName().replaceFirst(".*?\\$", "");
                    return colorTheme.container().format(nestedClassName);
                }

                if (source instanceof MethodSource) {
                    String testName = "#" + identifier.getDisplayName();
                    return colorTheme.testMethod().format(testName);
                }
            }

            return "/" + identifier.getDisplayName();
        }
 
Example #9
Source File: TestFilter.java    From sbt-jupiter-interface with Apache License 2.0 5 votes vote down vote up
private Optional<String> toTestName(TestDescriptor object) {

        TestSource testSource = object.getSource().orElse(null);

        if (!object.isTest() || !(testSource instanceof MethodSource)) {
            return Optional.empty();
        }

        MethodSource methodSource = (MethodSource)testSource;
        return Optional.of(methodSource.getClassName()
                + '#' + methodSource.getMethodName()
                + '(' + methodSource.getMethodParameterTypes() + ')'
        );
    }
 
Example #10
Source File: AbstractArchUnitTestDescriptor.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #11
Source File: AllureJunitPlatform.java    From allure-java with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("PMD.NcssCount")
private void startTestCase(final TestIdentifier testIdentifier) {
    final String uuid = tests.getOrCreate(testIdentifier);

    final Optional<TestSource> testSource = testIdentifier.getSource();
    final Optional<Method> testMethod = testSource.flatMap(this::getTestMethod);
    final Optional<Class<?>> testClass = testSource.flatMap(this::getTestClass);

    final TestResult result = new TestResult()
            .setUuid(uuid)
            .setName(testIdentifier.getDisplayName())
            .setLabels(getTags(testIdentifier))
            .setHistoryId(getHistoryId(testIdentifier))
            .setStage(Stage.RUNNING);

    result.getLabels().addAll(getProvidedLabels());

    testClass.map(AnnotationUtils::getLabels).ifPresent(result.getLabels()::addAll);
    testMethod.map(AnnotationUtils::getLabels).ifPresent(result.getLabels()::addAll);

    testClass.map(AnnotationUtils::getLinks).ifPresent(result.getLinks()::addAll);
    testMethod.map(AnnotationUtils::getLinks).ifPresent(result.getLinks()::addAll);

    result.getLabels().addAll(Arrays.asList(
            createHostLabel(),
            createThreadLabel(),
            createFrameworkLabel("junit-platform"),
            createLanguageLabel("java")
    ));

    testSource.flatMap(this::getFullName).ifPresent(result::setFullName);
    testSource.map(this::getSourceLabels).ifPresent(result.getLabels()::addAll);
    testClass.ifPresent(aClass -> {
        final String suiteName = getDisplayName(aClass).orElse(aClass.getCanonicalName());
        result.getLabels().add(createSuiteLabel(suiteName));
    });

    final Optional<String> classDescription = testClass.flatMap(this::getDescription);
    final Optional<String> methodDescription = testMethod.flatMap(this::getDescription);

    final String description = Stream.of(classDescription, methodDescription)
            .filter(Optional::isPresent)
            .map(Optional::get)
            .collect(Collectors.joining("\n\n"));

    result.setDescription(description);

    testMethod.map(this::getSeverity)
            .filter(Optional::isPresent)
            .orElse(testClass.flatMap(this::getSeverity))
            .map(ResultsUtils::createSeverityLabel)
            .ifPresent(result.getLabels()::add);

    testMethod.ifPresent(method -> ResultsUtils.processDescription(
            method.getDeclaringClass().getClassLoader(),
            method,
            result
    ));

    getLifecycle().scheduleTestCase(result);
    getLifecycle().startTestCase(uuid);
}
 
Example #12
Source File: AllureJunitPlatform.java    From allure-java with Apache License 2.0 4 votes vote down vote up
private Optional<Method> getTestMethod(final TestSource source) {
    if (source instanceof MethodSource) {
        return getTestMethod((MethodSource) source);
    }
    return Optional.empty();
}
 
Example #13
Source File: Configuration.java    From sbt-jupiter-interface with Apache License 2.0 3 votes vote down vote up
/**
 * Extracts the method-name from the specified test identifier.
 *
 * @param identifier The identifier from which to extract the method-name.
 * @return The method-name of the specified test identifier.
 */
public Optional<String> extractMethodName(TestIdentifier identifier) {

    TestSource testSource = identifier.getSource().orElse(null);

    if (testSource instanceof MethodSource) {

        MethodSource methodSource = (MethodSource)testSource;
        return Optional.of(methodSource.getMethodName());
    }

    return Optional.empty();
}