下面列出了org.junit.jupiter.api.extension.ExecutionCondition#org.junit.platform.engine.support.descriptor.MethodSource 实例代码,或者点击链接到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 Optional<Method> getTestMethod(final MethodSource source) {
try {
final Class<?> aClass = Class.forName(source.getClassName());
return Stream.of(aClass.getDeclaredMethods())
.filter(method -> MethodSource.from(method).equals(source))
.findAny();
} catch (ClassNotFoundException e) {
LOGGER.trace("Could not get test method from method source {}", source, e);
}
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() + ')'
);
}
ArchUnitMethodDescriptor(UniqueId uniqueId, Method method, Supplier<JavaClasses> classes) {
super(uniqueId.append("method", method.getName()), method.getName(), MethodSource.from(method), method);
validate(method);
this.method = method;
this.classes = classes;
this.method.setAccessible(true);
}
@Test
void a_class_with_simple_rule_method() {
EngineDiscoveryTestRequest discoveryRequest = new EngineDiscoveryTestRequest().withClass(SimpleRuleMethod.class);
TestDescriptor descriptor = testEngine.discover(discoveryRequest, engineId);
TestDescriptor ruleDescriptor = getOnlyTest(descriptor);
assertThat(ruleDescriptor.getUniqueId()).isEqualTo(simpleRuleMethodTestId(engineId));
MethodSource testSource = (MethodSource) ruleDescriptor.getSource().get();
assertThat(testSource.getClassName()).isEqualTo(SimpleRuleMethod.class.getName());
assertThat(testSource.getMethodName()).isEqualTo(SIMPLE_RULE_METHOD_NAME);
assertThat(testSource.getMethodParameterTypes()).isEqualTo(JavaClasses.class.getName());
}
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();
}