@@ -406,6 +406,75 @@ public void clearFeedback() {}
406
406
"should not believe there is a cycle here" );
407
407
}
408
408
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
+
409
478
public String getVar () {
410
479
return "testVar" ;
411
480
}
0 commit comments