Merge branch '1.0.x'

This commit is contained in:
Andy Wilkinson
2016-01-29 11:21:35 +00:00
2 changed files with 21 additions and 8 deletions

View File

@@ -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();
}
}

View File

@@ -46,6 +46,19 @@ public class PatternReplacingContentModifierTests {
is(equalTo("{\"id\" : \"<<uuid>>\"}".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, "<<uuid>>");
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";