Consolidate logic in test rules for getting output dir and operation name
This commit is contained in:
@@ -93,6 +93,8 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void undocumentedRequestHeader() throws IOException {
|
||||
this.snippet.expectRequestHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new RequestHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one")))
|
||||
.document(this.operationBuilder.request("http://localhost")
|
||||
|
||||
@@ -81,6 +81,8 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests {
|
||||
|
||||
@Test
|
||||
public void undocumentedResponseHeader() throws IOException {
|
||||
this.snippet.expectResponseHeaders().withContents(
|
||||
tableWithHeader("Name", "Description").row("`X-Test`", "one"));
|
||||
new ResponseHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one"))).document(
|
||||
this.operationBuilder.response().header("X-Test", "test")
|
||||
|
||||
@@ -20,8 +20,6 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.restdocs.snippet.TemplatedSnippet;
|
||||
@@ -38,7 +36,7 @@ import static org.junit.Assert.assertThat;
|
||||
* @author Andy Wilkinson
|
||||
* @author Andreas Evers
|
||||
*/
|
||||
public class ExpectedSnippet implements TestRule {
|
||||
public class ExpectedSnippet extends OperationTestRule {
|
||||
|
||||
private final TemplateFormat templateFormat;
|
||||
|
||||
@@ -56,9 +54,10 @@ public class ExpectedSnippet implements TestRule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, Description description) {
|
||||
this.outputDirectory = new File(
|
||||
"build/" + description.getTestClass().getSimpleName());
|
||||
public Statement apply(final Statement base, File outputDirectory,
|
||||
String operationName) {
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.expectedName = operationName;
|
||||
return new ExpectedSnippetStatement(base);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -57,7 +55,7 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class OperationBuilder implements TestRule {
|
||||
public class OperationBuilder extends OperationTestRule {
|
||||
|
||||
private final Map<String, Object> attributes = new HashMap<>();
|
||||
|
||||
@@ -133,22 +131,9 @@ public class OperationBuilder implements TestRule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(final Statement base, final Description description) {
|
||||
return new Statement() {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
String operationName = description.getMethodName();
|
||||
int index = operationName.indexOf('[');
|
||||
if (index > 0) {
|
||||
operationName = operationName.substring(0, index);
|
||||
}
|
||||
OperationBuilder.this.prepare(operationName,
|
||||
new File("build/" + description.getTestClass().getSimpleName()));
|
||||
base.evaluate();
|
||||
}
|
||||
|
||||
};
|
||||
public Statement apply(Statement base, File outputDirectory, String operationName) {
|
||||
prepare(operationName, outputDirectory);
|
||||
return base;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* Abstract base class for Operation-related {@link TestRule TestRules}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
abstract class OperationTestRule implements TestRule {
|
||||
|
||||
@Override
|
||||
public final Statement apply(Statement base, Description description) {
|
||||
return apply(base, determineOutputDirectory(description),
|
||||
determineOperationName(description));
|
||||
}
|
||||
|
||||
private File determineOutputDirectory(Description description) {
|
||||
return new File("build/" + description.getTestClass().getSimpleName());
|
||||
}
|
||||
|
||||
private String determineOperationName(Description description) {
|
||||
String operationName = description.getMethodName();
|
||||
int index = operationName.indexOf('[');
|
||||
if (index > 0) {
|
||||
operationName = operationName.substring(0, index);
|
||||
}
|
||||
return operationName;
|
||||
}
|
||||
|
||||
protected abstract Statement apply(Statement base, File outputDirectory,
|
||||
String operationName);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user