Skip to content

Commit 9dfa0b5

Browse files
committed
Polish PatternReplacingContentModifier
1 parent 2b21e8e commit 9dfa0b5

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,8 +35,8 @@ class PatternReplacingContentModifier implements ContentModifier {
3535
private final String replacement;
3636

3737
/**
38-
* Creates a new {@link PatternReplacingContentModifier} that will replace occurences
39-
* the given {@code pattern} with the given {@code replacement}.
38+
* Creates a new {@link PatternReplacingContentModifier} that will replace occurrences
39+
* of the given {@code pattern} with the given {@code replacement}.
4040
*
4141
* @param pattern the pattern
4242
* @param replacement the replacement
@@ -56,7 +56,7 @@ public byte[] modifyContent(byte[] content, MediaType contentType) {
5656
original = new String(content);
5757
}
5858
Matcher matcher = this.pattern.matcher(original);
59-
StringBuilder buffer = new StringBuilder();
59+
StringBuilder builder = new StringBuilder();
6060
int previous = 0;
6161
while (matcher.find()) {
6262
String prefix;
@@ -68,13 +68,13 @@ public byte[] modifyContent(byte[] content, MediaType contentType) {
6868
prefix = original.substring(previous, matcher.start());
6969
previous = matcher.end();
7070
}
71-
buffer.append(prefix);
72-
buffer.append(this.replacement);
71+
builder.append(prefix);
72+
builder.append(this.replacement);
7373
}
7474
if (previous < original.length()) {
75-
buffer.append(original.substring(previous));
75+
builder.append(original.substring(previous));
7676
}
77-
return buffer.toString().getBytes();
77+
return builder.toString().getBytes();
7878
}
7979

8080
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ public void patternsAreReplaced() throws Exception {
4646
is(equalTo("{\"id\" : \"<<uuid>>\"}".getBytes())));
4747
}
4848

49+
@Test
50+
public void contentThatDoesNotMatchIsUnchanged() throws Exception {
51+
Pattern pattern = Pattern.compile(
52+
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
53+
Pattern.CASE_INSENSITIVE);
54+
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(
55+
pattern, "<<uuid>>");
56+
assertThat(
57+
contentModifier.modifyContent(
58+
"{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes(), null),
59+
is(equalTo("{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes())));
60+
}
61+
4962
@Test
5063
public void encodingIsPreserved() {
5164
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";

0 commit comments

Comments
 (0)