| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api; |
| 12 |
|
| 13 |
import static org.apiguardian.api.API.Status.STABLE; |
| 14 |
import static org.junit.platform.commons.support.ModifierSupport.isStatic; |
| 15 |
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; |
| 16 |
|
| 17 |
import java.lang.reflect.Method; |
| 18 |
import java.util.Optional; |
| 19 |
import java.util.function.Predicate; |
| 20 |
|
| 21 |
import org.apiguardian.api.API; |
| 22 |
import org.junit.platform.commons.util.ClassUtils; |
| 23 |
import org.junit.platform.commons.util.Preconditions; |
| 24 |
import org.junit.platform.commons.util.ReflectionUtils; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@API(status = STABLE, since = "5.7") |
| 54 |
public interface DisplayNameGenerator { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@API(status = STABLE, since = "5.9") |
| 71 |
String DEFAULT_GENERATOR_PROPERTY_NAME = "junit.jupiter.displayname.generator.default"; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
String generateDisplayNameForClass(Class<?> testClass); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
String generateDisplayNameForNestedClass(Class<?> nestedClass); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
String generateDisplayNameForMethod(Class<?> testClass, Method testMethod); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static String parameterTypesAsString(Method method) { |
| 119 |
Preconditions.notNull(method, "Method must not be null"); |
| 120 |
return '(' + ClassUtils.nullSafeToString(Class::getSimpleName, method.getParameterTypes()) + ')'; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
class Standard implements DisplayNameGenerator { |
| 130 |
|
| 131 |
static final DisplayNameGenerator INSTANCE = new Standard(); |
| 132 |
|
| 133 |
public Standard() { |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public String generateDisplayNameForClass(Class<?> testClass) { |
| 138 |
String name = testClass.getName(); |
| 139 |
int lastDot = name.lastIndexOf('.'); |
| 140 |
return name.substring(lastDot + 1); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public String generateDisplayNameForNestedClass(Class<?> nestedClass) { |
| 145 |
return nestedClass.getSimpleName(); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { |
| 150 |
return testMethod.getName() + parameterTypesAsString(testMethod); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
class Simple extends Standard { |
| 163 |
|
| 164 |
static final DisplayNameGenerator INSTANCE = new Simple(); |
| 165 |
|
| 166 |
public Simple() { |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { |
| 171 |
String displayName = testMethod.getName(); |
| 172 |
if (hasParameters(testMethod)) { |
| 173 |
displayName += ' ' + parameterTypesAsString(testMethod); |
| 174 |
} |
| 175 |
return displayName; |
| 176 |
} |
| 177 |
|
| 178 |
private static boolean hasParameters(Method method) { |
| 179 |
return method.getParameterCount() > 0; |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
class ReplaceUnderscores extends Simple { |
| 192 |
|
| 193 |
static final DisplayNameGenerator INSTANCE = new ReplaceUnderscores(); |
| 194 |
|
| 195 |
public ReplaceUnderscores() { |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public String generateDisplayNameForClass(Class<?> testClass) { |
| 200 |
return replaceUnderscores(super.generateDisplayNameForClass(testClass)); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public String generateDisplayNameForNestedClass(Class<?> nestedClass) { |
| 205 |
return replaceUnderscores(super.generateDisplayNameForNestedClass(nestedClass)); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { |
| 210 |
return replaceUnderscores(super.generateDisplayNameForMethod(testClass, testMethod)); |
| 211 |
} |
| 212 |
|
| 213 |
private static String replaceUnderscores(String name) { |
| 214 |
return name.replace('_', ' '); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
@API(status = STABLE, since = "5.10") |
| 232 |
class IndicativeSentences implements DisplayNameGenerator { |
| 233 |
|
| 234 |
static final DisplayNameGenerator INSTANCE = new IndicativeSentences(); |
| 235 |
|
| 236 |
public IndicativeSentences() { |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public String generateDisplayNameForClass(Class<?> testClass) { |
| 241 |
return getGeneratorFor(testClass).generateDisplayNameForClass(testClass); |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public String generateDisplayNameForNestedClass(Class<?> nestedClass) { |
| 246 |
return getSentenceBeginning(nestedClass); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) { |
| 251 |
return getSentenceBeginning(testClass) + getFragmentSeparator(testClass) |
| 252 |
+ getGeneratorFor(testClass).generateDisplayNameForMethod(testClass, testMethod); |
| 253 |
} |
| 254 |
|
| 255 |
private String getSentenceBeginning(Class<?> testClass) { |
| 256 |
Class<?> enclosingClass = testClass.getEnclosingClass(); |
| 257 |
boolean topLevelTestClass = (enclosingClass == null || isStatic(testClass)); |
| 258 |
Optional<String> displayName = findAnnotation(testClass, DisplayName.class) |
| 259 |
.map(DisplayName::value).map(String::trim); |
| 260 |
|
| 261 |
if (topLevelTestClass) { |
| 262 |
if (displayName.isPresent()) { |
| 263 |
return displayName.get(); |
| 264 |
} |
| 265 |
Class<? extends DisplayNameGenerator> generatorClass = findDisplayNameGeneration(testClass) |
| 266 |
.map(DisplayNameGeneration::value) |
| 267 |
.filter(not(IndicativeSentences.class)) |
| 268 |
.orElse(null); |
| 269 |
if (generatorClass != null) { |
| 270 |
return getDisplayNameGenerator(generatorClass).generateDisplayNameForClass(testClass); |
| 271 |
} |
| 272 |
return generateDisplayNameForClass(testClass); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
boolean buildPrefix = findDisplayNameGeneration(enclosingClass) |
| 278 |
.map(DisplayNameGeneration::value) |
| 279 |
.filter(IndicativeSentences.class::equals) |
| 280 |
.isPresent(); |
| 281 |
|
| 282 |
String prefix = (buildPrefix ? getSentenceBeginning(enclosingClass) + getFragmentSeparator(testClass) : ""); |
| 283 |
|
| 284 |
return prefix + displayName.orElseGet( |
| 285 |
() -> getGeneratorFor(testClass).generateDisplayNameForNestedClass(testClass)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private static String getFragmentSeparator(Class<?> testClass) { |
| 301 |
return findIndicativeSentencesGeneration(testClass) |
| 302 |
.map(IndicativeSentencesGeneration::separator) |
| 303 |
.orElse(IndicativeSentencesGeneration.DEFAULT_SEPARATOR); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
private static DisplayNameGenerator getGeneratorFor(Class<?> testClass) { |
| 319 |
return findIndicativeSentencesGeneration(testClass) |
| 320 |
.map(IndicativeSentencesGeneration::generator) |
| 321 |
.filter(not(IndicativeSentences.class)) |
| 322 |
.map(DisplayNameGenerator::getDisplayNameGenerator) |
| 323 |
.orElseGet(() -> getDisplayNameGenerator(IndicativeSentencesGeneration.DEFAULT_GENERATOR)); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
private static Optional<DisplayNameGeneration> findDisplayNameGeneration(Class<?> testClass) { |
| 335 |
return findAnnotation(testClass, DisplayNameGeneration.class, true); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private static Optional<IndicativeSentencesGeneration> findIndicativeSentencesGeneration(Class<?> testClass) { |
| 347 |
return findAnnotation(testClass, IndicativeSentencesGeneration.class, true); |
| 348 |
} |
| 349 |
|
| 350 |
private static Predicate<Class<?>> not(Class<?> clazz) { |
| 351 |
return ((Predicate<Class<?>>) clazz::equals).negate(); |
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
static DisplayNameGenerator getDisplayNameGenerator(Class<?> generatorClass) { |
| 365 |
Preconditions.notNull(generatorClass, "Class must not be null"); |
| 366 |
Preconditions.condition(DisplayNameGenerator.class.isAssignableFrom(generatorClass), |
| 367 |
"Class must be a DisplayNameGenerator implementation"); |
| 368 |
if (generatorClass == Standard.class) { |
| 369 |
return Standard.INSTANCE; |
| 370 |
} |
| 371 |
if (generatorClass == Simple.class) { |
| 372 |
return Simple.INSTANCE; |
| 373 |
} |
| 374 |
if (generatorClass == ReplaceUnderscores.class) { |
| 375 |
return ReplaceUnderscores.INSTANCE; |
| 376 |
} |
| 377 |
if (generatorClass == IndicativeSentences.class) { |
| 378 |
return IndicativeSentences.INSTANCE; |
| 379 |
} |
| 380 |
return (DisplayNameGenerator) ReflectionUtils.newInstance(generatorClass); |
| 381 |
} |
| 382 |
|
| 383 |
} |