property placeholders can deal with nested expressions which happen to use the same suffix (SPR-7098)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3282 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-04-21 09:22:20 +00:00
parent c0dd3a83fc
commit 546d9968d9
2 changed files with 72 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 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.
@ -16,7 +16,9 @@
package org.springframework.util; package org.springframework.util;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -37,10 +39,21 @@ public class PropertyPlaceholderHelper {
private static final Log logger = LogFactory.getLog(PropertyPlaceholderHelper.class); private static final Log logger = LogFactory.getLog(PropertyPlaceholderHelper.class);
private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<String, String>(4);
static {
wellKnownSimplePrefixes.put("}", "{");
wellKnownSimplePrefixes.put("]", "[");
wellKnownSimplePrefixes.put(")", "(");
}
private final String placeholderPrefix; private final String placeholderPrefix;
private final String placeholderSuffix; private final String placeholderSuffix;
private final String simplePrefix;
private final String valueSeparator; private final String valueSeparator;
private final boolean ignoreUnresolvablePlaceholders; private final boolean ignoreUnresolvablePlaceholders;
@ -70,6 +83,13 @@ public class PropertyPlaceholderHelper {
Assert.notNull(placeholderSuffix, "placeholderSuffix must not be null"); Assert.notNull(placeholderSuffix, "placeholderSuffix must not be null");
this.placeholderPrefix = placeholderPrefix; this.placeholderPrefix = placeholderPrefix;
this.placeholderSuffix = placeholderSuffix; this.placeholderSuffix = placeholderSuffix;
String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix);
if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) {
this.simplePrefix = simplePrefixForSuffix;
}
else {
this.simplePrefix = this.placeholderPrefix;
}
this.valueSeparator = valueSeparator; this.valueSeparator = valueSeparator;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
} }
@ -176,9 +196,9 @@ public class PropertyPlaceholderHelper {
return index; return index;
} }
} }
else if (StringUtils.substringMatch(buf, index, this.placeholderPrefix)) { else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) {
withinNestedPlaceholder++; withinNestedPlaceholder++;
index = index + this.placeholderPrefix.length(); index = index + this.simplePrefix.length();
} }
else { else {
index++; index++;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 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.
@ -39,12 +39,60 @@ public class SystemPropertyUtilsTests {
} }
} }
@Test
public void testReplaceFromSystemPropertyWithDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceFromSystemPropertyWithExpressionDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testReplaceFromSystemPropertyWithExpressionContainingDefault() {
System.setProperty("test.prop", "bar");
try {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}");
assertEquals("bar", resolved);
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test @Test
public void testReplaceWithDefault() { public void testReplaceWithDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}"); String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:foo}");
assertEquals("foo", resolved); assertEquals("foo", resolved);
} }
@Test
public void testReplaceWithExpressionDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:#{foo.bar}}");
assertEquals("#{foo.bar}", resolved);
}
@Test
public void testReplaceWithExpressionContainingDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop:Y#{foo.bar}X}");
assertEquals("Y#{foo.bar}X", resolved);
}
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testReplaceWithNoDefault() { public void testReplaceWithNoDefault() {
String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}"); String resolved = SystemPropertyUtils.resolvePlaceholders("${test.prop}");