You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Woonsan Ko (JIRA)" <ji...@apache.org> on 2015/07/23 16:53:04 UTC

[jira] [Comment Edited] (LANG-1158) StrSubstitutor - escape character not working just before a variable

    [ https://issues.apache.org/jira/browse/LANG-1158?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14638933#comment-14638933 ] 

Woonsan Ko edited comment on LANG-1158 at 7/23/15 2:52 PM:
-----------------------------------------------------------

I also tried this one as a workaround, but it also fails:
{code}
    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");
        values.put("dollar", "$");
        doTestReplace("The quick brown fox paid $20.00 to jump over the lazy dog.",
                      "The ${animal} paid ${dollar}${amount} to jump over the ${target}.", false);
    }
{code}
{noformat}
org.junit.ComparisonFailure: expected:<...ick brown fox paid $[20.00] to jump over the la...> but was:<...ick brown fox paid $[{amount}] to jump over the la...>
	at org.junit.Assert.assertEquals(Assert.java:115)
	at org.junit.Assert.assertEquals(Assert.java:144)
	at org.apache.commons.lang3.text.StrSubstitutorTest.doTestReplace(StrSubstitutorTest.java:643)
	at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:171)
{noformat}

If you set the 'amount' variable with a dollar sign prefix, or if you put a space between dollar variable and amount variable, then it may look working, but it gives a big limitation in my use case.


was (Author: woon_san):
I also tried this one as a workaround, but it also fails:
{code}
    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");
        values.put("dollar", "$");
        doTestReplace("The quick brown fox paid $20.00 to jump over the lazy dog.",
                      "The ${animal} paid ${dollar}${amount} to jump over the ${target}.", false);
    }
{code}
{noformat}
org.junit.ComparisonFailure: expected:<...ick brown fox paid $[20.00] to jump over the la...> but was:<...ick brown fox paid $[{amount}] to jump over the la...>
	at org.junit.Assert.assertEquals(Assert.java:115)
	at org.junit.Assert.assertEquals(Assert.java:144)
	at org.apache.commons.lang3.text.StrSubstitutorTest.doTestReplace(StrSubstitutorTest.java:643)
	at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:171)
{noformat}

If you set the 'amount' variable with a dollar sign prefix, or if you put a space between dollar variable and amount variable, then it looks working, but it gives a big limitation in my use case.

> StrSubstitutor - escape character not working just before a variable
> --------------------------------------------------------------------
>
>                 Key: LANG-1158
>                 URL: https://issues.apache.org/jira/browse/LANG-1158
>             Project: Commons Lang
>          Issue Type: Bug
>    Affects Versions: 3.4
>            Reporter: Woonsan Ko
>
> I tried to use the following, expecting "...ick brown fox paid $20.00 to jump over the la…":
> {code}
>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>     // after cloning https://github.com/woonsan/commons-lang.
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>         doTestReplace("The quick brown fox paid $20.00 to jump over
> the lazy dog.",
>                       "The ${animal} paid $$${amount} to jump over the
> ${target}.", true);
>     }
> {code}
> {noformat}
> (I put double dollar signs like $$${amount} because $ is the default escape character.)
> Because I put three dollar signs in total, it should give one dollar sign followed by the resolved amount value: "$20.00".
> But, the result was:"...ick brown fox paid $${amount} to jump over the la…".
> {noformat}
> I also tried using a different escape character like this:
> {code}
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>         final StrSubstitutor sub = new StrSubstitutor(values);
>         sub.setEscapeChar('<');
>         String replaceTemplate = "The <${animal} jumps over the ${target}.";
>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>         String replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>         replaceTemplate = "The ${animal} paid <$${amount} to jump over the ${target}.";
>         expectedResult = "The quick brown fox paid $20.00 to jump over the lazy dog.";
>         replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>     }
> {code}
> However, it fails like this:
> {noformat}
> Tests run: 41, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.048 sec <<< FAILURE! - in org.apache.commons.lang3.text.StrSubstitutorTest
> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)  Time elapsed: 0.009 sec  <<< FAILURE!
> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00 to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump over ...>
> 	at org.junit.Assert.assertEquals(Assert.java:115)
> 	at org.junit.Assert.assertEquals(Assert.java:144)
> 	at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
> {noformat}
> {noformat}
> The second assertion failed. So, it seems working in case of "<${animal}", but not working in case of "<$${amount}".
> {noformat}
> I think escape character handling is inconsistent.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)