From 540dbeab32f542b7f4e3747c191d06c66b5108a9 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 30 Nov 2015 21:26:01 +0100 Subject: [PATCH] [New] Added StringHelper.replacePropertiesIn() with special prefix - and added a test --- .../eitchnet/utils/helper/StringHelper.java | 31 +++- .../utils/helper/ReplacePropertiesInTest.java | 138 ++++++++++++++++++ 2 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 src/test/java/ch/eitchnet/utils/helper/ReplacePropertiesInTest.java diff --git a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java index 8f82b3487..a7a3fcc94 100644 --- a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java @@ -385,6 +385,27 @@ public class StringHelper { */ public static String replacePropertiesIn(Properties properties, String value) { + return replacePropertiesIn(properties, '$', value); + } + + /** + * Traverses the given string searching for occurrences of prefix{...} sequences. Theses sequences are + * replaced with a {@link Properties#getProperty(String)} value if such a value exists in the properties map. If the + * value of the sequence is not in the properties, then the sequence is not replaced + * + * @param properties + * the {@link Properties} in which to get the value + * @param prefix + * the prefix to use, for instance use $ to replace occurrences of ${...} + * @param value + * the value in which to replace any system properties + * + * @return a new string with all defined properties replaced or if an error occurred the original value is returned + */ + public static String replacePropertiesIn(Properties properties, char prefix, String value) { + + String prefixS = String.valueOf(prefix); + // get a copy of the value String tmpValue = value; @@ -393,7 +414,7 @@ public class StringHelper { int stop = 0; // loop on $ character positions - while ((pos = tmpValue.indexOf('$', pos + 1)) != -1) { + while ((pos = tmpValue.indexOf(prefix, pos + 1)) != -1) { // if pos+1 is not { character then continue if (tmpValue.charAt(pos + 1) != '{') { @@ -414,9 +435,9 @@ public class StringHelper { String sequence = tmpValue.substring(pos + 2, stop); // make sure sequence doesn't contain $ { } characters - if (sequence.contains("$") || sequence.contains("{") || sequence.contains("}")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - String msg = "Enclosed sequence in offsets {0} - {1} contains one of the illegal chars: $ { }: {2}"; //$NON-NLS-1$ - msg = MessageFormat.format(msg, pos, stop, sequence); + if (sequence.contains(prefixS) || sequence.contains("{") || sequence.contains("}")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + String msg = "Enclosed sequence in offsets {0} - {1} contains one of the illegal chars: {2} { }: {3}"; + msg = MessageFormat.format(msg, pos, stop, prefixS, sequence); logger.error(msg); tmpValue = value; break; @@ -432,7 +453,7 @@ public class StringHelper { } // property exists, so replace in value - tmpValue = tmpValue.replace("${" + sequence + "}", property); //$NON-NLS-1$ //$NON-NLS-2$ + tmpValue = tmpValue.replace(prefixS + "{" + sequence + "}", property); //$NON-NLS-1$ //$NON-NLS-2$ } return tmpValue; diff --git a/src/test/java/ch/eitchnet/utils/helper/ReplacePropertiesInTest.java b/src/test/java/ch/eitchnet/utils/helper/ReplacePropertiesInTest.java new file mode 100644 index 000000000..f9525899d --- /dev/null +++ b/src/test/java/ch/eitchnet/utils/helper/ReplacePropertiesInTest.java @@ -0,0 +1,138 @@ +package ch.eitchnet.utils.helper; + +import static org.junit.Assert.assertEquals; + +import java.util.Properties; + +import org.junit.Test; + +public class ReplacePropertiesInTest { + + @Test + public void shouldReplaceProps1() { + + String expr = "bla ${foo}"; + String expected = "bla bar"; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps2() { + + String expr = "${foo} bla "; + String expected = "bar bla "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps3() { + + String expr = "bla ${foo} "; + String expected = "bla bar "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps4() { + + String expr = "bla${foo}abr"; + String expected = "blabarabr"; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps5() { + + String expr = "bla '${foo}' "; + String expected = "bla 'bar' "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps6() { + + String expr = "${foo}bla ${foo} "; + String expected = "barbla bar "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps7() { + + String expr = "${foo}bla ${food} "; + String expected = "barbla foofoo "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + properties.setProperty("food", "foofoo"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps8() { + + String expr = "foo"; + String expected = "foo"; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + + String result = StringHelper.replacePropertiesIn(properties, expr); + + assertEquals(expected, result); + } + + @Test + public void shouldReplaceProps9() { + + String expr = "%{foo}bla %{food} "; + String expected = "barbla foofoo "; + + Properties properties = new Properties(); + properties.setProperty("foo", "bar"); + properties.setProperty("food", "foofoo"); + + String result = StringHelper.replacePropertiesIn(properties, '%', expr); + + assertEquals(expected, result); + } +}