Skip to content

Commit 4715351

Browse files
committed
Make it easier to provide a placeholder resolver that uses the context
Closes gh-235
1 parent a0f82f2 commit 4715351

File tree

6 files changed

+110
-17
lines changed

6 files changed

+110
-17
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import org.springframework.restdocs.RestDocumentationContext;
2525
import org.springframework.restdocs.mustache.Mustache;
26-
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver;
26+
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolverFactory;
2727
import org.springframework.restdocs.snippet.StandardWriterResolver;
2828
import org.springframework.restdocs.snippet.WriterResolver;
2929
import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
@@ -138,7 +138,7 @@ public void apply(Map<String, Object> configuration,
138138
SnippetConfiguration snippetConfiguration = (SnippetConfiguration) configuration
139139
.get(SnippetConfiguration.class.getName());
140140
resolverToUse = new StandardWriterResolver(
141-
new RestDocumentationContextPlaceholderResolver(context),
141+
new RestDocumentationContextPlaceholderResolverFactory(),
142142
snippetConfiguration.getEncoding(),
143143
snippetConfiguration.getTemplateFormat());
144144
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.snippet;
18+
19+
import org.springframework.restdocs.RestDocumentationContext;
20+
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
21+
22+
/**
23+
* A factory for creating {@link PlaceholderResolver} instances.
24+
*
25+
* @author Andy Wilkinson
26+
* @since 1.1
27+
*/
28+
public interface PlaceholderResolverFactory {
29+
30+
/**
31+
* Creates a new {@link PlaceholderResolver} using the given {@code context}.
32+
*
33+
* @param context the context
34+
* @return the placeholder resolver
35+
*/
36+
PlaceholderResolver create(RestDocumentationContext context);
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2014-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.snippet;
18+
19+
import org.springframework.restdocs.RestDocumentationContext;
20+
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
21+
22+
/**
23+
* A {@link PlaceholderResolverFactory} that creates
24+
* {@link RestDocumentationContextPlaceholderResolver} instances.
25+
*
26+
* @author Andy Wilkinson
27+
* @since 1.1
28+
*/
29+
public final class RestDocumentationContextPlaceholderResolverFactory
30+
implements PlaceholderResolverFactory {
31+
32+
@Override
33+
public PlaceholderResolver create(RestDocumentationContext context) {
34+
return new RestDocumentationContextPlaceholderResolver(context);
35+
}
36+
37+
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/StandardWriterResolver.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
public final class StandardWriterResolver implements WriterResolver {
3737

38-
private final PlaceholderResolver placeholderResolver;
38+
private final PlaceholderResolverFactory placeholderResolverFactory;
3939

4040
private final PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper(
4141
"{", "}");
@@ -52,27 +52,29 @@ public final class StandardWriterResolver implements WriterResolver {
5252
*
5353
* @param placeholderResolver the placeholder resolver
5454
* @deprecated since 1.1.0 in favor of
55-
* {@link #StandardWriterResolver(PropertyPlaceholderHelper.PlaceholderResolver, String, TemplateFormat)}
55+
* {@link #StandardWriterResolver(PlaceholderResolverFactory, String, TemplateFormat)}
5656
*/
5757
@Deprecated
5858
public StandardWriterResolver(PlaceholderResolver placeholderResolver) {
59-
this(placeholderResolver, "UTF-8", TemplateFormats.asciidoctor());
59+
this(new SingleInstancePlaceholderResolverFactory(placeholderResolver), "UTF-8",
60+
TemplateFormats.asciidoctor());
6061
}
6162

6263
/**
63-
* Creates a new {@code StandardWriterResolver} that will use the given
64-
* {@code placeholderResolver} to resolve any placeholders in the
64+
* Creates a new {@code StandardWriterResolver} that will use a
65+
* {@link PlaceholderResolver} created from the given
66+
* {@code placeholderResolverFactory} to resolve any placeholders in the
6567
* {@code operationName}. Writers will use the given {@code encoding} and, when
6668
* writing to a file, will use a filename appropriate for content generated from
6769
* templates in the given {@code templateFormat}.
6870
*
69-
* @param placeholderResolver the placeholder resolver
71+
* @param placeholderResolverFactory the placeholder resolver factory
7072
* @param encoding the encoding
7173
* @param templateFormat the snippet format
7274
*/
73-
public StandardWriterResolver(PlaceholderResolver placeholderResolver,
75+
public StandardWriterResolver(PlaceholderResolverFactory placeholderResolverFactory,
7476
String encoding, TemplateFormat templateFormat) {
75-
this.placeholderResolver = placeholderResolver;
77+
this.placeholderResolverFactory = placeholderResolverFactory;
7678
this.encoding = encoding;
7779
this.templateFormat = templateFormat;
7880
}
@@ -82,7 +84,7 @@ public Writer resolve(String operationName, String snippetName,
8284
RestDocumentationContext context) throws IOException {
8385
File outputFile = resolveFile(
8486
this.propertyPlaceholderHelper.replacePlaceholders(operationName,
85-
this.placeholderResolver),
87+
this.placeholderResolverFactory.create(context)),
8688
snippetName + "." + this.templateFormat.getFileExtension(), context);
8789

8890
if (outputFile != null) {
@@ -127,4 +129,21 @@ private void createDirectoriesIfNecessary(File outputFile) {
127129
}
128130
}
129131

132+
private static final class SingleInstancePlaceholderResolverFactory
133+
implements PlaceholderResolverFactory {
134+
135+
private final PlaceholderResolver placeholderResolver;
136+
137+
private SingleInstancePlaceholderResolverFactory(
138+
PlaceholderResolver placeholderResolver) {
139+
this.placeholderResolver = placeholderResolver;
140+
}
141+
142+
@Override
143+
public PlaceholderResolver create(RestDocumentationContext context) {
144+
return this.placeholderResolver;
145+
}
146+
147+
}
148+
130149
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/StandardWriterResolverTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import org.springframework.restdocs.ManualRestDocumentation;
2424
import org.springframework.restdocs.RestDocumentationContext;
25-
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
2625

2726
import static org.hamcrest.CoreMatchers.is;
2827
import static org.junit.Assert.assertThat;
@@ -36,11 +35,11 @@
3635
*/
3736
public class StandardWriterResolverTests {
3837

39-
private final PlaceholderResolver placeholderResolver = mock(
40-
PlaceholderResolver.class);
38+
private final PlaceholderResolverFactory placeholderResolverFactory = mock(
39+
PlaceholderResolverFactory.class);
4140

4241
private final StandardWriterResolver resolver = new StandardWriterResolver(
43-
this.placeholderResolver, "UTF-8", asciidoctor());
42+
this.placeholderResolverFactory, "UTF-8", asciidoctor());
4443

4544
@Test
4645
public void absoluteInput() {

spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.springframework.restdocs.operation.OperationResponseFactory;
4040
import org.springframework.restdocs.operation.Parameters;
4141
import org.springframework.restdocs.operation.StandardOperation;
42-
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolver;
42+
import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolverFactory;
4343
import org.springframework.restdocs.snippet.StandardWriterResolver;
4444
import org.springframework.restdocs.snippet.WriterResolver;
4545
import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
@@ -107,7 +107,7 @@ public Operation build() {
107107
this.attributes.put(RestDocumentationContext.class.getName(), context);
108108
this.attributes.put(WriterResolver.class.getName(),
109109
new StandardWriterResolver(
110-
new RestDocumentationContextPlaceholderResolver(context), "UTF-8",
110+
new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8",
111111
this.templateFormat));
112112
return new StandardOperation(this.name,
113113
(this.requestBuilder == null

0 commit comments

Comments
 (0)