下面列出了org.junit.platform.engine.support.descriptor.ClassSource#org.junit.platform.engine.TestSource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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();
}
/**
* 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);
}
/**
* 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;
}
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);
});
}
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();
}
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();
}
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();
}
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() + ')'
);
}
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());
}
@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);
}
private Optional<Method> getTestMethod(final TestSource source) {
if (source instanceof MethodSource) {
return getTestMethod((MethodSource) source);
}
return Optional.empty();
}
/**
* 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();
}