Add support for using the class name in parameterised operation name

This commit adds support for three new placeholders that can be used
when specifying the name of an operation: {ClassName}, {class-name},
and {class_name}. This allows the operation name to be configured once
in an abstract superclass and reused across multiple test classes.

Closes gh-116
This commit is contained in:
Andy Wilkinson
2015-09-13 16:39:54 +01:00
parent ebab967e43
commit df40f63238
3 changed files with 64 additions and 11 deletions

View File

@@ -28,15 +28,23 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
* <ul>
* <li>{@code step} the {@link RestDocumentationContext#getStepCount() step current
* count}.
* <li>{@code methodName} - the name of the
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
* using camelCase
* <li>{@code methodName} - the unmodified name of the
* {@link RestDocumentationContext#getTestMethodName() current test method} without
* applying any formatting
* <li>{@code method-name} - the name of the
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
* using kebab-case
* <li>{@code method_name} - the name of the
* {@link RestDocumentationContext#getTestMethodName() current test method} formatted
* using snake_case
* <li>{@code ClassName} - the unmodified {@link Class#getSimpleName() simple name} of the
* {@link RestDocumentationContext#getTestClass() current test class}
* <li>{@code class-name} - the {@link Class#getSimpleName() simple name} of the
* {@link RestDocumentationContext#getTestClass() current test class} formatted using
* kebab-case
* <li>{@code class_name} - the {@link Class#getSimpleName() simple name} of the
* {@link RestDocumentationContext#getTestClass() current test class} formatted using
* snake case
* </ul>
*
* @author Andy Wilkinson
@@ -71,6 +79,15 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR
if ("method_name".equals(placeholderName)) {
return camelCaseToSnakeCase(this.context.getTestMethodName());
}
if ("ClassName".equals(placeholderName)) {
return this.context.getTestClass().getSimpleName();
}
if ("class-name".equals(placeholderName)) {
return camelCaseToKebabCase(this.context.getTestClass().getSimpleName());
}
if ("class_name".equals(placeholderName)) {
return camelCaseToSnakeCase(this.context.getTestClass().getSimpleName());
}
return null;
}
@@ -108,7 +125,9 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR
Matcher matcher = CAMEL_CASE_PATTERN.matcher(string);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, separator + matcher.group(1).toLowerCase());
String replacement = (matcher.start() > 0) ? separator
+ matcher.group(1).toLowerCase() : matcher.group(1).toLowerCase();
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
return result.toString();