From d184425216c809aa28d0be54004bc0b9422ca009 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 26 Jan 2016 12:38:19 +0000 Subject: [PATCH] Polishing --- .../customizing-requests-and-responses.adoc | 6 +- ...yTest.java => EveryTestPreprocessing.java} | 45 +++++------ ...PerTest.java => PerTestPreprocessing.java} | 20 +++-- .../PrettyPrintingContentModifier.java | 19 +++-- ...cumentationContextPlaceholderResolver.java | 14 +++- .../snippet/TemplatedSnippetTests.java | 77 +++++++++++++++++++ 6 files changed, 134 insertions(+), 47 deletions(-) rename docs/src/test/java/com/example/{PreprocessingEveryTest.java => EveryTestPreprocessing.java} (83%) rename docs/src/test/java/com/example/{PreprocessingPerTest.java => PerTestPreprocessing.java} (83%) create mode 100644 spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/TemplatedSnippetTests.java diff --git a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc index 37ca4e0a..2bc11527 100644 --- a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc +++ b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc @@ -12,7 +12,7 @@ example: [source,java,indent=0] ---- -include::{examples-dir}/com/example/PreprocessingPerTest.java[tags=preprocessing] +include::{examples-dir}/com/example/PerTestPreprocessing.java[tags=preprocessing] ---- <1> Apply a request preprocessor that will remove the header named `Foo`. <2> Apply a response preprocessor that will pretty print its content. @@ -24,7 +24,7 @@ output directories>>: [source,java,indent=0] ---- -include::{examples-dir}/com/example/PreprocessingEveryTest.java[tags=setup] +include::{examples-dir}/com/example/EveryTestPreprocessing.java[tags=setup] ---- <1> Create the `RestDocumentationResultHandler`, configured to preprocess the request and response. @@ -36,7 +36,7 @@ test-specific. For example: [source,java,indent=0] ---- -include::{examples-dir}/com/example/PreprocessingEveryTest.java[tags=use] +include::{examples-dir}/com/example/EveryTestPreprocessing.java[tags=use] ---- <1> Document the links specific to the resource that is being tested <2> The `perform` call will automatically produce the documentation snippets due to the diff --git a/docs/src/test/java/com/example/PreprocessingEveryTest.java b/docs/src/test/java/com/example/EveryTestPreprocessing.java similarity index 83% rename from docs/src/test/java/com/example/PreprocessingEveryTest.java rename to docs/src/test/java/com/example/EveryTestPreprocessing.java index 6199ecb4..b189f8ff 100644 --- a/docs/src/test/java/com/example/PreprocessingEveryTest.java +++ b/docs/src/test/java/com/example/EveryTestPreprocessing.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,14 @@ package com.example; +import org.junit.Before; +import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; @@ -23,42 +31,35 @@ import static org.springframework.restdocs.operation.preprocess.Preprocessors.pr import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; -import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; -import org.junit.Before; -import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; +public class EveryTestPreprocessing { -public class PreprocessingEveryTest { - private WebApplicationContext context; - + private MockMvc mockMvc; - + private RestDocumentationResultHandler document; - + // tag::setup[] @Before public void setup() { - this.document = document("{method-name}", // <1> - preprocessRequest(removeHeaders("Foo")), - preprocessResponse(prettyPrint())); - this.mockMvc = MockMvcBuilders - .webAppContextSetup(this.context) - .alwaysDo(this.document) // <2> - .build(); + this.document = document( + "{method-name}", // <1> + preprocessRequest(removeHeaders("Foo")), + preprocessResponse(prettyPrint())); + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .alwaysDo(this.document) // <2> + .build(); } - // end::setup[] + + // end::setup[] public void use() throws Exception { // tag::use[] this.document.snippets( // <1> links(linkWithRel("self").description("Canonical self link"))); this.mockMvc.perform(get("/")) // <2> - .andExpect(status().isOk()); + .andExpect(status().isOk()); // end::use[] } diff --git a/docs/src/test/java/com/example/PreprocessingPerTest.java b/docs/src/test/java/com/example/PerTestPreprocessing.java similarity index 83% rename from docs/src/test/java/com/example/PreprocessingPerTest.java rename to docs/src/test/java/com/example/PerTestPreprocessing.java index 6f0ab77f..504f96c0 100644 --- a/docs/src/test/java/com/example/PreprocessingPerTest.java +++ b/docs/src/test/java/com/example/PerTestPreprocessing.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,27 +16,25 @@ package com.example; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.springframework.test.web.servlet.MockMvc; + import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; -import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders; import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import org.springframework.test.web.servlet.MockMvc; - -public class PreprocessingPerTest { +public class PerTestPreprocessing { private MockMvc mockMvc; public void general() throws Exception { // tag::preprocessing[] - this.mockMvc.perform(get("/")) - .andExpect(status().isOk()) - .andDo(document("index", - preprocessRequest(removeHeaders("Foo")), // <1> - preprocessResponse(prettyPrint()))); // <2> + this.mockMvc.perform(get("/")).andExpect(status().isOk()) + .andDo(document("index", preprocessRequest(removeHeaders("Foo")), // <1> + preprocessResponse(prettyPrint()))); // <2> // end::preprocessing[] } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java index 16c9471a..9665fd09 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifier.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -98,9 +98,8 @@ public class PrettyPrintingContentModifier implements ContentModifier { SAXParser parser = parserFactory.newSAXParser(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(new SilentErrorHandler()); - SAXSource xmlInput = new SAXSource(xmlReader, new InputSource( - new ByteArrayInputStream(original))); - return xmlInput; + return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream( + original))); } private static final class SilentErrorListener implements ErrorListener { @@ -108,18 +107,18 @@ public class PrettyPrintingContentModifier implements ContentModifier { @Override public void warning(TransformerException exception) throws TransformerException { - + // Suppress } @Override public void error(TransformerException exception) throws TransformerException { - + // Suppress } @Override public void fatalError(TransformerException exception) throws TransformerException { - + // Suppress } } @@ -128,17 +127,17 @@ public class PrettyPrintingContentModifier implements ContentModifier { @Override public void warning(SAXParseException exception) throws SAXException { - + // Suppress } @Override public void error(SAXParseException exception) throws SAXException { - + // Suppress } @Override public void fatalError(SAXParseException exception) throws SAXException { - + // Suppress } } } 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 908f3d5e..bc12e872 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,6 +70,14 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR if ("step".equals(placeholderName)) { return Integer.toString(this.context.getStepCount()); } + String converted = tryMethodNameConversion(placeholderName); + if (converted != null) { + return converted; + } + return tryClassNameConversion(placeholderName); + } + + private String tryMethodNameConversion(String placeholderName) { if ("methodName".equals(placeholderName)) { return this.context.getTestMethodName(); } @@ -79,6 +87,10 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR if ("method_name".equals(placeholderName)) { return camelCaseToSnakeCase(this.context.getTestMethodName()); } + return null; + } + + private String tryClassNameConversion(String placeholderName) { if ("ClassName".equals(placeholderName)) { return this.context.getTestClass().getSimpleName(); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/TemplatedSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/TemplatedSnippetTests.java new file mode 100644 index 00000000..f356d2da --- /dev/null +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/TemplatedSnippetTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.snippet; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.springframework.restdocs.operation.Operation; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.hasEntry; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link TemplatedSnippet}. + * + * @author Andy Wilkinson + */ +public class TemplatedSnippetTests { + + @Test + public void attributesAreCopied() { + Map attributes = new HashMap<>(); + attributes.put("a", "alpha"); + TemplatedSnippet snippet = new TestTemplatedSnippet(attributes); + attributes.put("b", "bravo"); + assertThat(snippet.getAttributes().size(), is(1)); + assertThat(snippet.getAttributes(), hasEntry("a", (Object) "alpha")); + } + + @Test + public void nullAttributesAreTolerated() { + assertThat(new TestTemplatedSnippet(null).getAttributes(), is(not(nullValue()))); + assertThat(new TestTemplatedSnippet(null).getAttributes().size(), is(0)); + } + + @Test + public void snippetName() { + assertThat( + new TestTemplatedSnippet(Collections.emptyMap()) + .getSnippetName(), + is(equalTo("test"))); + } + + private static class TestTemplatedSnippet extends TemplatedSnippet { + + protected TestTemplatedSnippet(Map attributes) { + super("test", attributes); + } + + @Override + protected Map createModel(Operation operation) { + return null; + } + + } + +}