Polishing

This commit is contained in:
Andy Wilkinson
2015-02-16 20:12:31 +00:00
parent 81c6e88060
commit d918304491
8 changed files with 40 additions and 26 deletions

View File

@@ -52,7 +52,7 @@ public abstract class CurlDocumentation {
@Override
public void handle(MvcResult result, DocumentationWriter writer)
throws Exception {
throws IOException {
writer.shellCommand(new CurlRequestDocumentationAction(writer, result,
getCurlConfiguration()));
}
@@ -71,7 +71,7 @@ public abstract class CurlDocumentation {
@Override
public void handle(MvcResult result, DocumentationWriter writer)
throws Exception {
throws IOException {
writer.codeBlock("http", new CurlResponseDocumentationAction(writer,
result, getCurlConfiguration()));
}
@@ -90,7 +90,7 @@ public abstract class CurlDocumentation {
@Override
public void handle(MvcResult result, DocumentationWriter writer)
throws Exception {
throws IOException {
writer.shellCommand(new CurlRequestDocumentationAction(writer, result,
getCurlConfiguration()));
writer.codeBlock("http", new CurlResponseDocumentationAction(writer,
@@ -116,7 +116,7 @@ public abstract class CurlDocumentation {
}
@Override
public void perform() throws Exception {
public void perform() throws IOException {
MockHttpServletRequest request = this.result.getRequest();
this.writer.print(String.format("curl %s://%s:%d%s", request.getScheme(),
request.getRemoteHost(), request.getRemotePort(),
@@ -169,7 +169,7 @@ public abstract class CurlDocumentation {
}
@Override
public void perform() throws Exception {
public void perform() throws IOException {
if (this.curlConfiguration.isIncludeResponseHeaders()) {
HttpStatus status = HttpStatus.valueOf(this.result.getResponse()
.getStatus());

View File

@@ -37,8 +37,15 @@ public abstract class CurlSnippetResultHandler extends SnippetWritingResultHandl
return this.curlConfiguration;
}
public CurlSnippetResultHandler includeResponseHeaders() {
this.curlConfiguration.setIncludeResponseHeaders(true);
/**
* Specify whether or not the generated cURL snippets should have contents as if cURL
* had been invoked with {@code -i, --include}.
*
* @param include {@code true} to use {@code -i, --include}, otherwise false
* @return {@code this}
*/
public CurlSnippetResultHandler includeResponseHeaders(boolean include) {
this.curlConfiguration.setIncludeResponseHeaders(include);
return this;
}
}

View File

@@ -95,9 +95,9 @@ public abstract class LinkExtractors {
protected abstract Map<String, List<Link>> extractLinks(Map<String, Object> json);
}
@SuppressWarnings("unchecked")
private static class HalLinkExtractor extends JsonContentLinkExtractor {
@SuppressWarnings("unchecked")
@Override
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {
Map<String, List<Link>> extractedLinks = new HashMap<>();
@@ -112,7 +112,6 @@ public abstract class LinkExtractors {
return extractedLinks;
}
@SuppressWarnings("unchecked")
private static List<Link> convertToLinks(Object object, String rel) {
List<Link> links = new ArrayList<>();
if (object instanceof Collection) {
@@ -141,9 +140,9 @@ public abstract class LinkExtractors {
}
}
@SuppressWarnings("unchecked")
private static class AtomLinkExtractor extends JsonContentLinkExtractor {
@SuppressWarnings("unchecked")
@Override
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {
Map<String, List<Link>> extractedLinks = new HashMap<>();

View File

@@ -18,6 +18,7 @@ package org.springframework.restdocs.hypermedia;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -54,7 +55,8 @@ public class LinkSnippetResultHandler extends SnippetWritingResultHandler {
}
@Override
protected void handle(MvcResult result, DocumentationWriter writer) throws Exception {
protected void handle(MvcResult result, DocumentationWriter writer)
throws IOException {
Map<String, List<Link>> links;
if (this.extractor != null) {
links = this.extractor.extractLinks(result.getResponse());

View File

@@ -16,6 +16,7 @@
package org.springframework.restdocs.snippet;
import java.io.IOException;
import java.io.Writer;
/**
@@ -35,11 +36,11 @@ public class AsciidoctorWriter extends DocumentationWriter {
}
@Override
public void shellCommand(final DocumentationAction action) throws Exception {
public void shellCommand(final DocumentationAction action) throws IOException {
codeBlock("bash", new DocumentationAction() {
@Override
public void perform() throws Exception {
public void perform() throws IOException {
AsciidoctorWriter.this.print("$ ");
action.perform();
}
@@ -47,7 +48,7 @@ public class AsciidoctorWriter extends DocumentationWriter {
}
@Override
public void codeBlock(String language, DocumentationAction action) throws Exception {
public void codeBlock(String language, DocumentationAction action) throws IOException {
println();
if (language != null) {
println("[source," + language + "]");

View File

@@ -16,6 +16,7 @@
package org.springframework.restdocs.snippet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
@@ -37,9 +38,9 @@ public abstract class DocumentationWriter extends PrintWriter {
* called the action, any necessary suffix is then written.
*
* @param action the action that will produce the shell command
* @throws Exception if the documentation fails
* @throws IOException if the documentation fails
*/
public abstract void shellCommand(DocumentationAction action) throws Exception;
public abstract void shellCommand(DocumentationAction action) throws IOException;
/**
* Calls the given {@code action} to document a code block. The code block will be
@@ -49,10 +50,10 @@ public abstract class DocumentationWriter extends PrintWriter {
*
* @param language the language in which the code is written
* @param action the action that will produce the code
* @throws Exception if the documentation fails
* @throws IOException if the documentation fails
*/
public abstract void codeBlock(String language, DocumentationAction action)
throws Exception;
throws IOException;
/**
* Encapsulates an action that outputs some documentation. Typically implemented as a
@@ -67,8 +68,8 @@ public abstract class DocumentationWriter extends PrintWriter {
/**
* Perform the encapsulated action
*
* @throws Exception if the action fails
* @throws IOException if the action fails
*/
void perform() throws Exception;
void perform() throws IOException;
}
}

View File

@@ -42,10 +42,10 @@ public abstract class SnippetWritingResultHandler implements ResultHandler {
}
protected abstract void handle(MvcResult result, DocumentationWriter writer)
throws Exception;
throws IOException;
@Override
public void handle(MvcResult result) throws Exception {
public void handle(MvcResult result) throws IOException {
Writer writer = createWriter();
try {
handle(result, new AsciidoctorWriter(writer));
@@ -62,6 +62,11 @@ public abstract class SnippetWritingResultHandler implements ResultHandler {
}
if (outputFile != null) {
File parent = outputFile.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IllegalStateException("Failed to create directory '" + parent
+ "'");
}
outputFile.getParentFile().mkdirs();
return new FileWriter(outputFile);
}

View File

@@ -18,12 +18,11 @@ package org.springframework.restdocs.snippet;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.junit.Test;
import org.springframework.restdocs.snippet.AsciidoctorWriter;
import org.springframework.restdocs.snippet.DocumentationWriter;
import org.springframework.restdocs.snippet.DocumentationWriter.DocumentationAction;
/**
@@ -42,7 +41,7 @@ public class AsciidoctorWriterTests {
this.documentationWriter.codeBlock("java", new DocumentationAction() {
@Override
public void perform() throws Exception {
public void perform() throws IOException {
AsciidoctorWriterTests.this.documentationWriter.println("foo");
}
});
@@ -56,7 +55,7 @@ public class AsciidoctorWriterTests {
this.documentationWriter.shellCommand(new DocumentationAction() {
@Override
public void perform() throws Exception {
public void perform() throws IOException {
AsciidoctorWriterTests.this.documentationWriter.println("foo");
}
});