From df40f63238f729d104e8c348bae65472a0a754f4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sun, 13 Sep 2015 16:39:54 +0100 Subject: [PATCH] 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 --- .../docs/asciidoc/documenting-your-api.adoc | 19 +++++++++--- ...cumentationContextPlaceholderResolver.java | 27 ++++++++++++++--- ...tationContextPlaceholderResolverTests.java | 29 +++++++++++++++++-- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/docs/src/docs/asciidoc/documenting-your-api.adoc b/docs/src/docs/asciidoc/documenting-your-api.adoc index e5451383..1bbc2b05 100644 --- a/docs/src/docs/asciidoc/documenting-your-api.adoc +++ b/docs/src/docs/asciidoc/documenting-your-api.adoc @@ -386,7 +386,7 @@ are supported: | Parameter | Description | {methodName} -| The name of the test method, formatted using camelCase +| The unmodified name of the test method | {method-name} | The name of the test method, formatted using kebab-case @@ -394,15 +394,26 @@ are supported: | {method_name} | The name of the test method, formatted using snake_case +| {ClassName} +| The unmodified name of the test class + +| {class-name} +| The name of the test class, formatted using kebab-case + +| {class_name} +| The nome of the test class, formatted using snake_case + | {step} | The count of calls to MockMvc.perform in the current test |=== -For example, `document("{method-name}")` in a test method named `creatingANote` will write -snippets into a directory named `creating-a-note`. +For example, `document("{class-name}/{method-name}")` in a test method named +`creatingANote` on the test class `GettingStartedDocumentaiton`, will write +snippets into a directory named `getting-started-documentation/creating-a-note`. A parameterized output directory is particularly useful in combination with Spring MVC -Test's `alwaysDo` functionality. It allows documentation to be configured once in a setup method: +Test's `alwaysDo` functionality. It allows documentation to be configured once in a setup +method: [source,java,indent=0] ---- diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java index a517598d..51f63c7b 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java @@ -28,15 +28,23 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; * * * @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(); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java index ff06d8b3..37c19160 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java @@ -32,14 +32,14 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; public class RestDocumentationContextPlaceholderResolverTests { @Test - public void dashSeparatedMethodName() throws Exception { + public void kebabCaseMethodName() throws Exception { assertThat( createResolver("dashSeparatedMethodName").resolvePlaceholder( "method-name"), equalTo("dash-separated-method-name")); } @Test - public void underscoreSeparatedMethodName() throws Exception { + public void snakeCaseMethodName() throws Exception { assertThat( createResolver("underscoreSeparatedMethodName").resolvePlaceholder( "method_name"), equalTo("underscore_separated_method_name")); @@ -52,14 +52,37 @@ public class RestDocumentationContextPlaceholderResolverTests { equalTo("camelCaseMethodName")); } + @Test + public void kebabCaseClassName() throws Exception { + assertThat(createResolver().resolvePlaceholder("class-name"), + equalTo("rest-documentation-context-placeholder-resolver-tests")); + } + + @Test + public void snakeCaseClassName() throws Exception { + assertThat(createResolver().resolvePlaceholder("class_name"), + equalTo("rest_documentation_context_placeholder_resolver_tests")); + } + + @Test + public void camelCaseClassName() throws Exception { + assertThat(createResolver().resolvePlaceholder("ClassName"), + equalTo("RestDocumentationContextPlaceholderResolverTests")); + } + @Test public void stepCount() throws Exception { assertThat(createResolver("stepCount").resolvePlaceholder("step"), equalTo("0")); } + private PlaceholderResolver createResolver() { + return new RestDocumentationContextPlaceholderResolver( + new RestDocumentationContext(getClass(), null, null)); + } + private PlaceholderResolver createResolver(String methodName) { return new RestDocumentationContextPlaceholderResolver( - new RestDocumentationContext(null, methodName, null)); + new RestDocumentationContext(getClass(), methodName, null)); } }