Skip to content

Commit dabd0db

Browse files
Copilotslachiewicz
andcommitted
Fix MultiDelimiterStringSearchInterpolator cache - populate existingAnswers map when cacheAnswers is true
Co-authored-by: slachiewicz <[email protected]>
1 parent 34439f7 commit dabd0db

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/main/java/org/codehaus/plexus/interpolation/multi/MultiDelimiterStringSearchInterpolator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ private String interpolate(String input, RecursionInterceptor recursionIntercept
236236
// behaviour
237237
result.append(String.valueOf(value));
238238
resolved = true;
239+
240+
if (cacheAnswers) {
241+
existingAnswers.put(realExpr, value);
242+
}
239243
} else {
240244
unresolvable.add(wholeExpr);
241245
}

src/test/java/org/codehaus/plexus/interpolation/multi/MultiDelimiterStringSearchInterpolatorTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,62 @@ public Object getValue(String expression) {
146146

147147
assertEquals("test", interpolator.interpolate("#(test)"));
148148
}
149+
150+
@Test
151+
public void testCacheAnswersTrue() throws InterpolationException {
152+
Map ctx = new HashMap();
153+
ctx.put("key", "value");
154+
155+
final int[] valueSourceCallCount = {0};
156+
157+
ValueSource vs = new AbstractValueSource(false) {
158+
@Override
159+
public Object getValue(String expression) {
160+
valueSourceCallCount[0]++;
161+
return ctx.get(expression);
162+
}
163+
};
164+
165+
MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator();
166+
interpolator.setCacheAnswers(true);
167+
interpolator.addValueSource(vs);
168+
169+
String result = interpolator.interpolate("${key}-${key}-${key}-${key}");
170+
171+
assertEquals("value-value-value-value", result);
172+
// first value is interpolated and saved, then the 3 next answers came from existing answer Map
173+
assertEquals(1, valueSourceCallCount[0]);
174+
175+
// answers are preserved between calls:
176+
result = interpolator.interpolate("${key}-${key}-${key}-${key}");
177+
assertEquals("value-value-value-value", result);
178+
// still 1 from first call as cache is preserved
179+
assertEquals(1, valueSourceCallCount[0]);
180+
}
181+
182+
@Test
183+
public void testCacheAnswersFalse() throws InterpolationException {
184+
Map ctx = new HashMap();
185+
ctx.put("key", "value");
186+
187+
final int[] valueSourceCallCount = {0};
188+
189+
ValueSource vs = new AbstractValueSource(false) {
190+
@Override
191+
public Object getValue(String expression) {
192+
valueSourceCallCount[0]++;
193+
return ctx.get(expression);
194+
}
195+
};
196+
197+
MultiDelimiterStringSearchInterpolator interpolator = new MultiDelimiterStringSearchInterpolator();
198+
interpolator.addValueSource(vs);
199+
200+
String result = interpolator.interpolate("${key}-${key}-${key}-${key}");
201+
202+
assertEquals("value-value-value-value", result);
203+
// Without caching, expressions are evaluated multiple times due to multi-pass resolution
204+
// In this case: 4 expressions evaluated in 2 passes = 8 calls
205+
assertEquals(8, valueSourceCallCount[0]);
206+
}
149207
}

0 commit comments

Comments
 (0)