下面列出了怎么用org.junit.platform.engine.support.descriptor.ClassSource的API类实例代码及写法,或者点击链接到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 DisplayName getDisplayName(TestIdentifier testIdentifier) {
LinkedList<String> names = new LinkedList<>();
Optional<TestIdentifier> id = Optional.of(testIdentifier);
do {
TestIdentifier identifier = id.get();
Optional<ClassSource> classSource = identifier.getSource()
.filter(source -> source instanceof ClassSource)
.map(source -> (ClassSource) source)
.filter(source -> !source.getPosition().isPresent())
.filter(source -> classesToSkip.contains(source.getJavaClass()));
if (classSource.isPresent()) {
break;
}
names.addFirst(identifier.getDisplayName());
id = id.flatMap(testPlan::getParent);
} while (id.isPresent());
return DisplayName.create(names);
}
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;
}
@Test
void an_unique_id() {
UniqueId ruleIdToDiscover = simpleRulesId(engineId)
.append(FIELD_SEGMENT_TYPE, SimpleRules.SIMPLE_RULE_FIELD_ONE_NAME);
EngineDiscoveryTestRequest discoveryRequest = new EngineDiscoveryTestRequest().withUniqueId(ruleIdToDiscover);
TestDescriptor descriptor = getOnlyTest(testEngine.discover(discoveryRequest, engineId));
assertThat(descriptor.getUniqueId()).isEqualTo(ruleIdToDiscover);
assertThat(descriptor.getDisplayName()).isEqualTo(SimpleRules.SIMPLE_RULE_FIELD_ONE_NAME);
assertThat(descriptor.getChildren()).isEmpty();
assertThat(descriptor.getDescendants()).isEmpty();
assertThat(descriptor.getType()).isEqualTo(TEST);
assertThat(descriptor.getSource().get()).isEqualTo(FieldSource.from(field(SimpleRules.class, SimpleRules.SIMPLE_RULE_FIELD_ONE_NAME)));
assertThat(descriptor.getParent().get().getSource().get()).isEqualTo(ClassSource.from(SimpleRules.class));
}
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();
}
ArchUnitRulesDescriptor(ElementResolver resolver, DeclaredArchRules rules, Supplier<JavaClasses> classes, Field field) {
super(resolver.getUniqueId(),
rules.getDisplayName(),
ClassSource.from(rules.getDefinitionLocation()),
field,
rules.getDefinitionLocation());
this.rules = rules;
this.classes = classes;
}
private ArchUnitTestDescriptor(ElementResolver resolver, Class<?> testClass, ClassCache classCache) {
super(resolver.getUniqueId(), testClass.getSimpleName(), ClassSource.from(testClass), testClass);
this.testClass = testClass;
this.classCache = classCache;
}
private void assertClassSource(TestDescriptor child, Class<?> aClass) {
ClassSource classSource = (ClassSource) child.getSource().get();
assertThat(classSource.getClassName()).isEqualTo(aClass.getName());
assertThat(classSource.getJavaClass()).isEqualTo(aClass);
assertThat(classSource.getPosition().isPresent()).as("position is present").isFalse();
}