org.junit.jupiter.params.ParameterizedTestNameFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-jupiter-params Show documentation
Show all versions of junit-jupiter-params Show documentation
Module "junit-jupiter-params" of JUnit 5.
/*
* 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.jupiter.params;
import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.params.ParameterizedTest.ARGUMENTS_PLACEHOLDER;
import static org.junit.jupiter.params.ParameterizedTest.ARGUMENTS_WITH_NAMES_PLACEHOLDER;
import static org.junit.jupiter.params.ParameterizedTest.DISPLAY_NAME_PLACEHOLDER;
import static org.junit.jupiter.params.ParameterizedTest.INDEX_PLACEHOLDER;
import java.text.Format;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.stream.IntStream;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.StringUtils;
/**
* @since 5.0
*/
class ParameterizedTestNameFormatter {
private final String pattern;
private final String displayName;
private final ParameterizedTestMethodContext methodContext;
ParameterizedTestNameFormatter(String pattern, String displayName, ParameterizedTestMethodContext methodContext) {
this.pattern = pattern;
this.displayName = displayName;
this.methodContext = methodContext;
}
String format(int invocationIndex, Object... arguments) {
try {
return formatSafely(invocationIndex, arguments);
}
catch (Exception ex) {
String message = "The display name pattern defined for the parameterized test is invalid. "
+ "See nested exception for further details.";
throw new JUnitException(message, ex);
}
}
private String formatSafely(int invocationIndex, Object[] arguments) {
String pattern = prepareMessageFormatPattern(invocationIndex, arguments);
MessageFormat format = new MessageFormat(pattern);
Object[] humanReadableArguments = makeReadable(format, arguments);
return format.format(humanReadableArguments);
}
private String prepareMessageFormatPattern(int invocationIndex, Object[] arguments) {
String result = pattern//
.replace(DISPLAY_NAME_PLACEHOLDER, this.displayName)//
.replace(INDEX_PLACEHOLDER, String.valueOf(invocationIndex));
if (result.contains(ARGUMENTS_WITH_NAMES_PLACEHOLDER)) {
result = result.replace(ARGUMENTS_WITH_NAMES_PLACEHOLDER, argumentsWithNamesPattern(arguments));
}
if (result.contains(ARGUMENTS_PLACEHOLDER)) {
result = result.replace(ARGUMENTS_PLACEHOLDER, argumentsPattern(arguments));
}
return result;
}
private String argumentsWithNamesPattern(Object[] arguments) {
return IntStream.range(0, arguments.length) //
.mapToObj(index -> methodContext.getParameterName(index).map(name -> name + "=").orElse("") + "{"
+ index + "}") //
.collect(joining(", "));
}
private String argumentsPattern(Object[] arguments) {
return IntStream.range(0, arguments.length) //
.mapToObj(index -> "{" + index + "}") //
.collect(joining(", "));
}
private Object[] makeReadable(MessageFormat format, Object[] arguments) {
Format[] formats = format.getFormatsByArgumentIndex();
Object[] result = Arrays.copyOf(arguments, Math.min(arguments.length, formats.length), Object[].class);
for (int i = 0; i < result.length; i++) {
if (formats[i] == null) {
result[i] = StringUtils.nullSafeToString(arguments[i]);
}
}
return result;
}
}