Polishing

This commit is contained in:
Andy Wilkinson
2016-01-26 12:38:19 +00:00
parent 02fa42445f
commit d184425216
6 changed files with 134 additions and 47 deletions

View File

@@ -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

View File

@@ -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[]
}

View File

@@ -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[]
}

View File

@@ -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
}
}
}

View File

@@ -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();
}

View File

@@ -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<String, Object> 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.<String, Object>emptyMap())
.getSnippetName(),
is(equalTo("test")));
}
private static class TestTemplatedSnippet extends TemplatedSnippet {
protected TestTemplatedSnippet(Map<String, Object> attributes) {
super("test", attributes);
}
@Override
protected Map<String, Object> createModel(Operation operation) {
return null;
}
}
}