From 9dfa0b538509b1336a0afa3f26e3fe750d93a7cb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 29 Jan 2016 11:15:45 +0000 Subject: [PATCH] Polish PatternReplacingContentModifier --- .../PatternReplacingContentModifier.java | 16 ++++++++-------- .../PatternReplacingContentModifierTests.java | 13 +++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java index 58d4e3a7..7fe67754 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifier.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * 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. @@ -35,8 +35,8 @@ class PatternReplacingContentModifier implements ContentModifier { private final String replacement; /** - * Creates a new {@link PatternReplacingContentModifier} that will replace occurences - * the given {@code pattern} with the given {@code replacement}. + * Creates a new {@link PatternReplacingContentModifier} that will replace occurrences + * of the given {@code pattern} with the given {@code replacement}. * * @param pattern the pattern * @param replacement the replacement @@ -56,7 +56,7 @@ class PatternReplacingContentModifier implements ContentModifier { original = new String(content); } Matcher matcher = this.pattern.matcher(original); - StringBuilder buffer = new StringBuilder(); + StringBuilder builder = new StringBuilder(); int previous = 0; while (matcher.find()) { String prefix; @@ -68,13 +68,13 @@ class PatternReplacingContentModifier implements ContentModifier { prefix = original.substring(previous, matcher.start()); previous = matcher.end(); } - buffer.append(prefix); - buffer.append(this.replacement); + builder.append(prefix); + builder.append(this.replacement); } if (previous < original.length()) { - buffer.append(original.substring(previous)); + builder.append(original.substring(previous)); } - return buffer.toString().getBytes(); + return builder.toString().getBytes(); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java index 1046cad9..9d37ed78 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PatternReplacingContentModifierTests.java @@ -46,6 +46,19 @@ public class PatternReplacingContentModifierTests { is(equalTo("{\"id\" : \"<>\"}".getBytes()))); } + @Test + public void contentThatDoesNotMatchIsUnchanged() throws Exception { + Pattern pattern = Pattern.compile( + "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", + Pattern.CASE_INSENSITIVE); + PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier( + pattern, "<>"); + assertThat( + contentModifier.modifyContent( + "{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes(), null), + is(equalTo("{\"id\" : \"CA76-ED42-11CE-BACD\"}".getBytes()))); + } + @Test public void encodingIsPreserved() { String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";