[jbehave-scm] [scm-core/jbehave-4.x][1/1] JBEHAVE-1062: Now using Matcher.quoteReplacement() when replacing the named parameter values
commit f3d255f9a197da15ee7c5e51198938864c253b0b Author: rszabi AuthorDate: Fri, 19 Dec 2014 14:04:32 +0200 Commit: Mauro Talevi CommitDate: Sat, 20 Dec 2014 17:28:02 +0100 JBEHAVE-1062: Now using Matcher.quoteReplacement() when replacing the named parameter values diff --git a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java index 5e4137a..32a656e 100755 --- a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java +++ b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java @@ -335,7 +335,7 @@ public class ExamplesTable { String replacedValue = row.get(key); for (String namedKey : namedParameters.keySet()) { String namedValue = namedParameters.get(namedKey); -replacedValue = replacedValue.replaceAll(namedKey, namedValue); +replacedValue = replacedValue.replaceAll(namedKey, Matcher.quoteReplacement(namedValue)); } replaced.put(key, replacedValue); } diff --git a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java index 1368119..1fe66e4 100755 --- a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java +++ b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java @@ -333,6 +333,30 @@ public class ExamplesTableBehaviour { } +/** + * The values given named parameter values as strings should not suffer any modification after are replaced in table. + * @see {@link String#replaceAll(String, String)} to see why are not present in values the '\' and '$' characters. + */ +@Test +public void shouldKeepExactValueInReplacedNamedParameterValues() throws Exception { +// Given +ExamplesTableFactory factory = new ExamplesTableFactory(); +String problematicNamedParameterValueCharacters = "value having the \\ backslash and the $ dollar character"; + +// When +String tableAsString = "|Name|Value|\n|name||"; +Map namedParameters = new HashMap(); +namedParameters.put("", problematicNamedParameterValueCharacters); +ExamplesTable table = factory.createExamplesTable(tableAsString).withNamedParameters(namedParameters); + +// Then +Parameters firstRow = table.getRowsAsParameters(true).get(0); +Map firstRowValues = firstRow.values(); +assertThat(firstRowValues.containsKey("Value"), is(true)); +assertThat(firstRow.valueAs("Value", String.class), is(problematicNamedParameterValueCharacters)); + +} + @Test public void shouldMapParametersToType() throws Exception { // Given
[jbehave-scm] [scm-core][1/1] JBEHAVE-1062: Now using Matcher.quoteReplacement() when replacing the named parameter values
commit 09e5180a8e28b5490ee4d0bb962a61519c673075 Author: rszabi AuthorDate: Fri, 19 Dec 2014 14:04:32 +0200 Commit: Mauro Talevi CommitDate: Sat, 20 Dec 2014 17:24:02 +0100 JBEHAVE-1062: Now using Matcher.quoteReplacement() when replacing the named parameter values diff --git a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java index 598416a..d15551e 100755 --- a/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java +++ b/jbehave-core/src/main/java/org/jbehave/core/model/ExamplesTable.java @@ -336,7 +336,7 @@ public class ExamplesTable { String replacedValue = row.get(key); for (String namedKey : namedParameters.keySet()) { String namedValue = namedParameters.get(namedKey); -replacedValue = replacedValue.replaceAll(namedKey, namedValue); +replacedValue = replacedValue.replaceAll(namedKey, Matcher.quoteReplacement(namedValue)); } replaced.put(key, replacedValue); } diff --git a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java index 1e6fcb2..b0bdcf3 100755 --- a/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java +++ b/jbehave-core/src/test/java/org/jbehave/core/model/ExamplesTableBehaviour.java @@ -333,6 +333,30 @@ public class ExamplesTableBehaviour { } +/** + * The values given named parameter values as strings should not suffer any modification after are replaced in table. + * @see {@link String#replaceAll(String, String)} to see why are not present in values the '\' and '$' characters. + */ +@Test +public void shouldKeepExactValueInReplacedNamedParameterValues() throws Exception { +// Given +ExamplesTableFactory factory = new ExamplesTableFactory(); +String problematicNamedParameterValueCharacters = "value having the \\ backslash and the $ dollar character"; + +// When +String tableAsString = "|Name|Value|\n|name||"; +Map namedParameters = new HashMap(); +namedParameters.put("", problematicNamedParameterValueCharacters); +ExamplesTable table = factory.createExamplesTable(tableAsString).withNamedParameters(namedParameters); + +// Then +Parameters firstRow = table.getRowsAsParameters(true).get(0); +Map firstRowValues = firstRow.values(); +assertThat(firstRowValues.containsKey("Value"), is(true)); +assertThat(firstRow.valueAs("Value", String.class), is(problematicNamedParameterValueCharacters)); + +} + @Test public void shouldMapParametersToType() throws Exception { // Given