Merge pull request #9730 from Dylian Bego

* gh-9730:
  Polish "Handle possible regexes defensively in NamePatternFilter"
  Handle possible regexes defensively in NamePatternFilter
pull/9828/head
Andy Wilkinson 7 years ago
commit 20a417f469

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/** /**
* Utility class that can be used to filter source data using a name regular expression. * Utility class that can be used to filter source data using a name regular expression.
@ -31,6 +32,7 @@ import java.util.regex.Pattern;
* @author Phillip Webb * @author Phillip Webb
* @author Sergei Egorov * @author Sergei Egorov
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Dylian Bego
* @since 1.3.0 * @since 1.3.0
*/ */
abstract class NamePatternFilter<T> { abstract class NamePatternFilter<T> {
@ -44,13 +46,13 @@ abstract class NamePatternFilter<T> {
} }
public Map<String, Object> getResults(String name) { public Map<String, Object> getResults(String name) {
if (!isRegex(name)) { Pattern pattern = compilePatternIfNecessary(name);
if (pattern == null) {
Object value = getValue(this.source, name); Object value = getValue(this.source, name);
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
result.put(name, value); result.put(name, value);
return result; return result;
} }
Pattern pattern = Pattern.compile(name);
ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback( ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback(
pattern); pattern);
getNames(this.source, resultCollector); getNames(this.source, resultCollector);
@ -58,13 +60,18 @@ abstract class NamePatternFilter<T> {
} }
private boolean isRegex(String name) { private Pattern compilePatternIfNecessary(String name) {
for (String part : REGEX_PARTS) { for (String part : REGEX_PARTS) {
if (name.contains(part)) { if (name.contains(part)) {
return true; try {
return Pattern.compile(name);
}
catch (PatternSyntaxException ex) {
return null;
}
} }
} }
return false; return null;
} }
protected abstract void getNames(T source, NameCallback callback); protected abstract void getNames(T source, NameCallback callback);

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Dylian Bego
*/ */
public class NamePatternFilterTests { public class NamePatternFilterTests {
@ -38,6 +39,13 @@ public class NamePatternFilterTests {
assertThat(filter.isGetNamesCalled()).isFalse(); assertThat(filter.isGetNamesCalled()).isFalse();
} }
@Test
public void nonRegexThatContainsRegexPart() throws Exception {
MockNamePatternFilter filter = new MockNamePatternFilter();
assertThat(filter.getResults("*")).containsEntry("*", "*");
assertThat(filter.isGetNamesCalled()).isFalse();
}
@Test @Test
public void regexRepetitionZeroOrMore() { public void regexRepetitionZeroOrMore() {
MockNamePatternFilter filter = new MockNamePatternFilter(); MockNamePatternFilter filter = new MockNamePatternFilter();

Loading…
Cancel
Save