Skip to content

Commit

Permalink
Fall back to display name if method name is blank
Browse files Browse the repository at this point in the history
Prior to this commit, `VintageTestDescriptor` used the `Description`'s
method name when it wasn't null. However, there are edge cases where it
can be empty which lead to blank display names which are not allowed by
the Platform. Thus, it is now only used if not blank.

Fixes #2248.
  • Loading branch information
marcphilipp committed Apr 7, 2020
1 parent 78c989f commit 7868b97
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
Expand Up @@ -55,7 +55,8 @@ public VintageTestDescriptor(UniqueId uniqueId, Description description, TestSou
}

private static String generateDisplayName(Description description) {
return description.getMethodName() != null ? description.getMethodName() : description.getDisplayName();
String methodName = description.getMethodName();
return isNotBlank(methodName) ? methodName : description.getDisplayName();
}

public Description getDescription() {
Expand Down
Expand Up @@ -58,12 +58,12 @@
import org.junit.vintage.engine.samples.junit4.Categories.SkippedWithReason;
import org.junit.vintage.engine.samples.junit4.EmptyIgnoredTestCase;
import org.junit.vintage.engine.samples.junit4.IgnoredJUnit4TestCase;
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames;
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithPlainJUnit4TestCaseWithSingleTestWhichIsIgnored;
import org.junit.vintage.engine.samples.junit4.JUnit4SuiteWithTwoTestCases;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithDistinguishableOverloadedMethod;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithIndistinguishableOverloadedMethod;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithNotFilterableRunner;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIds;
import org.junit.vintage.engine.samples.junit4.ParameterizedTestCase;
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods;
import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithSingleInheritedTestWhichFails;
Expand Down Expand Up @@ -662,19 +662,23 @@ void resolvesClassForMethodSelectorForClassWithNonFilterableRunner() {
}

@Test
void usesCustomUniqueIdsWhenPresent() {
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIds.class;
LauncherDiscoveryRequest request = request().selectors(selectClass(testClass)).build();
void usesCustomUniqueIdsAndDisplayNamesWhenPresent() {
Class<?> suiteClass = JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class;
LauncherDiscoveryRequest request = request().selectors(selectClass(suiteClass)).build();

TestDescriptor engineDescriptor = discoverTests(request);

TestDescriptor runnerDescriptor = getOnlyElement(engineDescriptor.getChildren());
assertRunnerTestDescriptor(runnerDescriptor, testClass);
assertRunnerTestDescriptor(runnerDescriptor, suiteClass);

TestDescriptor childDescriptor = getOnlyElement(runnerDescriptor.getChildren());
TestDescriptor testClassDescriptor = getOnlyElement(runnerDescriptor.getChildren());
assertEquals("(TestClass)", testClassDescriptor.getDisplayName());

TestDescriptor childDescriptor = getOnlyElement(testClassDescriptor.getChildren());

UniqueId prefix = VintageUniqueIdBuilder.uniqueIdForClass(testClass);
UniqueId prefix = VintageUniqueIdBuilder.uniqueIdForClass(suiteClass);
assertThat(childDescriptor.getUniqueId().toString()).startsWith(prefix.toString());
assertEquals("(TestMethod)", childDescriptor.getDisplayName());

String customUniqueIdValue = childDescriptor.getUniqueId().getSegments().get(2).getType();
assertNotNull(Base64.getDecoder().decode(customUniqueIdValue.getBytes(StandardCharsets.UTF_8)),
Expand Down
Expand Up @@ -75,7 +75,7 @@
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithExceptionThrowingRunner;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithFailingDescriptionThatIsNotReportedAsFinished;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithIndistinguishableOverloadedMethod;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIds;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames;
import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithDuplicateChangingChildDescriptions;
import org.junit.vintage.engine.samples.junit4.MalformedJUnit4TestCase;
import org.junit.vintage.engine.samples.junit4.ParameterizedTestCase;
Expand Down Expand Up @@ -610,7 +610,7 @@ void ignoreEventsForUnknownDescriptionsByMisbehavingChildlessRunner() {

@Test
void executesJUnit4TestCaseWithRunnerWithCustomUniqueIds() {
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIds.class;
Class<?> testClass = JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class;

execute(testClass).allEvents().assertEventsMatchExactly( //
event(engine(), started()), //
Expand Down
@@ -0,0 +1,23 @@
/*
* Copyright 2015-2020 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.vintage.engine.samples.junit4;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

/**
* @since 5.6.2
*/
@RunWith(Suite.class)
@SuiteClasses(JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames.class)
public class JUnit4SuiteWithJUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames {
}
Expand Up @@ -12,15 +12,18 @@

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;

/**
* @since 4.12
*/
@RunWith(RunnerWithCustomUniqueIds.class)
public class JUnit4TestCaseWithRunnerWithCustomUniqueIds {
@DisplayName("(TestClass)")
@RunWith(RunnerWithCustomUniqueIdsAndDisplayNames.class)
public class JUnit4TestCaseWithRunnerWithCustomUniqueIdsAndDisplayNames {

@Test
@DisplayName("(TestMethod)")
public void test() {
Assert.fail();
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
import java.io.Serializable;
import java.util.Objects;

import org.junit.jupiter.api.DisplayName;
import org.junit.runner.Description;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
Expand All @@ -23,18 +24,29 @@
/**
* @since 4.12
*/
public class RunnerWithCustomUniqueIds extends BlockJUnit4ClassRunner {
public class RunnerWithCustomUniqueIdsAndDisplayNames extends BlockJUnit4ClassRunner {

public RunnerWithCustomUniqueIds(Class<?> klass) throws InitializationError {
public RunnerWithCustomUniqueIdsAndDisplayNames(Class<?> klass) throws InitializationError {
super(klass);
}

@Override
protected String getName() {
DisplayName displayName = getTestClass().getAnnotation(DisplayName.class);
return displayName == null ? super.getName() : displayName.value();
}

@Override
protected Description describeChild(FrameworkMethod method) {
String testName = testName(method);
String testName = getTestName(method);
return createTestDescription(getTestClass().getJavaClass().getName(), testName, new CustomUniqueId(testName));
}

private String getTestName(FrameworkMethod method) {
DisplayName displayName = method.getAnnotation(DisplayName.class);
return displayName == null ? testName(method) : displayName.value();
}

private static class CustomUniqueId implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit 7868b97

Please sign in to comment.