From d05f6cdb6a374860fd92117d041ce3adcb34a529 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Fri, 20 Jul 2018 21:00:56 +0900 Subject: [PATCH 1/2] Consistently use ID-based equality check for TemplateFormat Closes gh-530 --- .../restdocs/templates/TemplateFormats.java | 2 +- .../config/RestDocumentationConfigurerTests.java | 8 ++++---- .../restdocs/headers/RequestHeadersSnippetTests.java | 2 +- .../restdocs/headers/ResponseHeadersSnippetTests.java | 2 +- .../restdocs/hypermedia/LinksSnippetTests.java | 2 +- .../restdocs/payload/RequestFieldsSnippetTests.java | 2 +- .../restdocs/payload/ResponseFieldsSnippetTests.java | 2 +- .../restdocs/request/PathParametersSnippetTests.java | 10 ++++------ .../request/RequestParametersSnippetTests.java | 2 +- .../restdocs/request/RequestPartsSnippetTests.java | 2 +- 10 files changed, 16 insertions(+), 18 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java index 1b76d0c0..33bc3a68 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java @@ -17,7 +17,7 @@ package org.springframework.restdocs.templates; /** - * An enumeration of the built-in formats for which templates are provuded. + * An enumeration of the built-in formats for which templates are provided. * * @author Andy Wilkinson * @since 1.1.0 diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java index 4701f996..1e7f8345 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java @@ -86,8 +86,8 @@ public class RestDocumentationConfigurerTests { SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration .get(SnippetConfiguration.class.getName()); assertThat(snippetConfiguration.getEncoding(), is(equalTo("UTF-8"))); - assertThat(snippetConfiguration.getTemplateFormat(), - is(equalTo(TemplateFormats.asciidoctor()))); + assertThat(snippetConfiguration.getTemplateFormat().getId(), + is(equalTo(TemplateFormats.asciidoctor().getId()))); } @Test @@ -168,8 +168,8 @@ public class RestDocumentationConfigurerTests { instanceOf(SnippetConfiguration.class))); SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration .get(SnippetConfiguration.class.getName()); - assertThat(snippetConfiguration.getTemplateFormat(), - is(equalTo(TemplateFormats.markdown()))); + assertThat(snippetConfiguration.getTemplateFormat().getId(), + is(equalTo(TemplateFormats.markdown().getId()))); } @SuppressWarnings("unchecked") diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java index 8b9007bd..81318305 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java @@ -173,7 +173,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/ResponseHeadersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/ResponseHeadersSnippetTests.java index fbf5887b..0156812f 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/ResponseHeadersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/ResponseHeadersSnippetTests.java @@ -164,7 +164,7 @@ public class ResponseHeadersSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java index 5797dda4..71bf83c0 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinksSnippetTests.java @@ -178,7 +178,7 @@ public class LinksSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java index 43eece49..7ceb2ab6 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java @@ -461,7 +461,7 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java index 020131df..9388dd1e 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java @@ -472,7 +472,7 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index e053430d..a511ed53 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -87,8 +87,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { public void missingOptionalPathParameter() throws IOException { this.snippets.expectPathParameters() .withContents(tableWithTitleAndHeader( - this.templateFormat == TemplateFormats.asciidoctor() ? "+/{a}+" - : "`/{a}`", + getTitle("/{a}"), "Parameter", "Description").row("`a`", "one").row("`b`", "two")); new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"), parameterWithName("b").description("two").optional())) @@ -101,8 +100,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { public void presentOptionalPathParameter() throws IOException { this.snippets.expectPathParameters() .withContents(tableWithTitleAndHeader( - this.templateFormat == TemplateFormats.asciidoctor() ? "+/{a}+" - : "`/{a}`", + getTitle("/{a}"), "Parameter", "Description").row("`a`", "one")); new PathParametersSnippet( Arrays.asList(parameterWithName("a").description("one").optional())) @@ -208,7 +206,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); @@ -219,7 +217,7 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { } private String getTitle(String title) { - if (this.templateFormat.equals(TemplateFormats.asciidoctor())) { + if (this.templateFormat.getId().equals(TemplateFormats.asciidoctor().getId())) { return "+" + title + "+"; } return "`" + title + "`"; diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java index 3aa98e02..da1fecbe 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java @@ -200,7 +200,7 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestPartsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestPartsSnippetTests.java index 2c495400..9abbd4ac 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestPartsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestPartsSnippetTests.java @@ -191,7 +191,7 @@ public class RequestPartsSnippetTests extends AbstractSnippetTests { } private String escapeIfNecessary(String input) { - if (this.templateFormat.equals(TemplateFormats.markdown())) { + if (this.templateFormat.getId().equals(TemplateFormats.markdown().getId())) { return input; } return input.replace("|", "\\|"); From d57fa022544f82e91caae22ef345d5d6992d05e9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 1 Aug 2018 11:47:49 +0100 Subject: [PATCH 2/2] Polish "Consistently use ID-based equality check for TemplateFormat" --- .../restdocs/templates/TemplateFormats.java | 2 +- .../request/PathParametersSnippetTests.java | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java index 33bc3a68..32f5b4aa 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/templates/TemplateFormats.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2018 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. diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index a511ed53..47060e89 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -85,10 +85,9 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { @Test public void missingOptionalPathParameter() throws IOException { - this.snippets.expectPathParameters() - .withContents(tableWithTitleAndHeader( - getTitle("/{a}"), - "Parameter", "Description").row("`a`", "one").row("`b`", "two")); + this.snippets.expectPathParameters().withContents( + tableWithTitleAndHeader(getTitle("/{a}"), "Parameter", "Description") + .row("`a`", "one").row("`b`", "two")); new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"), parameterWithName("b").description("two").optional())) .document(this.operationBuilder.attribute( @@ -98,10 +97,9 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { @Test public void presentOptionalPathParameter() throws IOException { - this.snippets.expectPathParameters() - .withContents(tableWithTitleAndHeader( - getTitle("/{a}"), - "Parameter", "Description").row("`a`", "one")); + this.snippets.expectPathParameters().withContents( + tableWithTitleAndHeader(getTitle("/{a}"), "Parameter", "Description") + .row("`a`", "one")); new PathParametersSnippet( Arrays.asList(parameterWithName("a").description("one").optional())) .document(this.operationBuilder.attribute(