Skip to content

Commit

Permalink
WW-4090 Removes double evaluation of parsed expression
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Jun 3, 2013
1 parent 01e6b25 commit 54e5c91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ public Object evaluate(char[] openChars, String expression, TextParseUtil.Parsed
// deal with the "pure" expressions first!
//expression = expression.trim();
Object result = expression;
int pos = 0;

for (char open : openChars) {
int loopCount = 1;
int pos = 0;

//this creates an implicit StringBuffer and shouldn't be used in the inner loop
final String lookupChars = open + "{";

while (true) {
int start = expression.indexOf(lookupChars, pos);
if (start == -1) {
pos = 0;
loopCount++;
start = expression.indexOf(lookupChars);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ public void testTranslateVariables() {
assertEquals("count must be between 123 and 456, current value is 98765.", s);
}

public void testNestedExpression() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new HashMap<String, Object>() {{ put("foo", "${%{1+1}}"); }});
String s = TextParseUtil.translateVariables("${foo}", stack);
assertEquals("${%{1+1}}", s);
stack.pop();
}

public void testMixedOpenChars() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new HashMap<String, Object>() {{ put("foo", "bar"); }});
String s = TextParseUtil.translateVariables("${foo}-%{foo}", stack);
assertEquals("bar-bar", s);
s = TextParseUtil.translateVariables("%{foo}-${foo}", stack);
assertEquals("%{foo}-bar", s); // this is bad, but it is the only way not to double evaluate passed expression
stack.pop();
}

public void testCommaDelimitedStringToSet() {
assertEquals(0, TextParseUtil.commaDelimitedStringToSet("").size());
assertEquals(new HashSet<String>(Arrays.asList("foo", "bar", "tee")),
Expand Down Expand Up @@ -132,10 +150,13 @@ public void testTranslateVariablesNoRecursive() {

public void testTranslateVariablesRecursive() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new HashMap<String, Object>() {{ put("foo", "${1+1}"); }});
stack.push(new HashMap<String, Object>() {{ put("foo", "${1+1}"); put("bar", "${${1+2}}"); }});

Object s = TextParseUtil.translateVariables('$', "foo: ${foo}", stack, String.class, null, 2);
assertEquals("foo: 2", s);

s = TextParseUtil.translateVariables('$', "foo: ${bar}", stack, String.class, null, 1);
assertEquals("foo: ${${1+2}}", s);
}

public void testTranslateVariablesWithNull() {
Expand Down

0 comments on commit 54e5c91

Please sign in to comment.