Skip to content

Commit 3fa15e0

Browse files
committed
Fix #16: StringSearchInterpolator does not cache answers.
- Added tests exposing the issue. - Fixed code. - Added missing @OverRide annotations.
1 parent 1d05c5f commit 3fa15e0

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/main/java/org/codehaus/plexus/interpolation/StringSearchInterpolator.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private String interpolate(String input, RecursionInterceptor recursionIntercept
156156

157157
recursionInterceptor.expressionResolutionStarted(realExpr);
158158
try {
159-
Object value = existingAnswers.get(realExpr);
159+
Object value = getExistingAnswer(realExpr);
160160
Object bestAnswer = null;
161161

162162
for (ValueSource valueSource : valueSources) {
@@ -197,6 +197,10 @@ private String interpolate(String input, RecursionInterceptor recursionIntercept
197197
// behaviour
198198
result.append(String.valueOf(value));
199199
resolved = true;
200+
201+
if (cacheAnswers) {
202+
existingAnswers.put(realExpr, value);
203+
}
200204
} else {
201205
unresolvable.add(wholeExpr);
202206
}
@@ -275,4 +279,13 @@ public String getEscapeString() {
275279
public void setEscapeString(String escapeString) {
276280
this.escapeString = escapeString;
277281
}
282+
283+
/**
284+
* For testing purposes only. Not part of the public API.
285+
* @param key the key of a possible existing answer.
286+
* @return the associated interpolated object, or null if there is none.
287+
*/
288+
protected Object getExistingAnswer(String key) {
289+
return existingAnswers.get(key);
290+
}
278291
}

src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java

+69
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,75 @@ public void clearFeedback() {}
406406
"should not believe there is a cycle here");
407407
}
408408

409+
public void testCacheAnswersTrue() throws InterpolationException {
410+
Properties p = new Properties();
411+
p.setProperty("key", "value");
412+
413+
class CountingStringSearchInterpolator extends StringSearchInterpolator {
414+
private int existingCallCount;
415+
416+
@Override
417+
protected Object getExistingAnswer(String key) {
418+
Object value = super.getExistingAnswer(key);
419+
if (value != null) {
420+
++existingCallCount;
421+
}
422+
return value;
423+
}
424+
425+
public int getExistingCallCount() {
426+
return existingCallCount;
427+
}
428+
}
429+
430+
CountingStringSearchInterpolator interpolator = new CountingStringSearchInterpolator();
431+
interpolator.setCacheAnswers(true);
432+
interpolator.addValueSource(new PropertiesBasedValueSource(p));
433+
434+
String result = interpolator.interpolate("${key}-${key}-${key}-${key}");
435+
436+
assertEquals("value-value-value-value", result);
437+
// first value is interpolated and saved, then the 3 next answers came from existing answer Map
438+
assertEquals(3, interpolator.getExistingCallCount());
439+
440+
// answers are preserved between calls:
441+
result = interpolator.interpolate("${key}-${key}-${key}-${key}");
442+
assertEquals("value-value-value-value", result);
443+
// 3 from the first call to interpolate(), plus 4 from second call
444+
assertEquals(3 + 4, interpolator.getExistingCallCount());
445+
}
446+
447+
public void testCacheAnswersFalse() throws InterpolationException {
448+
Properties p = new Properties();
449+
p.setProperty("key", "value");
450+
451+
class CountingStringSearchInterpolator extends StringSearchInterpolator {
452+
private int existingCallCount;
453+
454+
@Override
455+
protected Object getExistingAnswer(String key) {
456+
Object value = super.getExistingAnswer(key);
457+
if (value != null) {
458+
++existingCallCount;
459+
}
460+
return value;
461+
}
462+
463+
public int getExistingCallCount() {
464+
return existingCallCount;
465+
}
466+
}
467+
468+
CountingStringSearchInterpolator interpolator = new CountingStringSearchInterpolator();
469+
interpolator.addValueSource(new PropertiesBasedValueSource(p));
470+
471+
String result = interpolator.interpolate("${key}-${key}-${key}-${key}");
472+
473+
assertEquals("value-value-value-value", result);
474+
// all values are interpolated each time
475+
assertEquals(0, interpolator.getExistingCallCount());
476+
}
477+
409478
public String getVar() {
410479
return "testVar";
411480
}

0 commit comments

Comments
 (0)