@@ -33,6 +33,7 @@ public class StringSearchInterpolatorTest
33
33
extends TestCase
34
34
{
35
35
36
+ @ Override
36
37
@ Before
37
38
public void setUp ()
38
39
{
@@ -188,6 +189,7 @@ public void testShouldResolveByEnvar()
188
189
{
189
190
OperatingSystemUtils .setEnvVarSource ( new OperatingSystemUtils .EnvVarSource ()
190
191
{
192
+ @ Override
191
193
public Map <String , String > getEnvMap ()
192
194
{
193
195
HashMap <String ,String > map = new HashMap <String ,String >();
@@ -218,6 +220,7 @@ public void testUsePostProcessor_DoesNotChangeValue()
218
220
219
221
rbi .addPostProcessor ( new InterpolationPostProcessor ()
220
222
{
223
+ @ Override
221
224
public Object execute ( String expression , Object value )
222
225
{
223
226
return null ;
@@ -242,6 +245,7 @@ public void testUsePostProcessor_ChangesValue()
242
245
243
246
rbi .addPostProcessor ( new InterpolationPostProcessor ()
244
247
{
248
+ @ Override
245
249
public Object execute ( String expression , Object value )
246
250
{
247
251
return value + "2" ;
@@ -408,6 +412,7 @@ public void testInterruptedInterpolate()
408
412
final boolean [] error = new boolean [] { false };
409
413
interpolator .addValueSource ( new ValueSource ()
410
414
{
415
+ @ Override
411
416
public Object getValue ( String expression ) {
412
417
if ( expression .equals ( "key" ) )
413
418
{
@@ -422,10 +427,12 @@ public Object getValue( String expression ) {
422
427
return null ;
423
428
}
424
429
}
430
+ @ Override
425
431
public List getFeedback ()
426
432
{
427
433
return Collections .EMPTY_LIST ;
428
434
}
435
+ @ Override
429
436
public void clearFeedback ()
430
437
{
431
438
}
@@ -445,6 +452,89 @@ public void clearFeedback()
445
452
assertEquals ( "should not believe there is a cycle here" , "-val-" , interpolator .interpolate ( "-${key}-" , recursionInterceptor ) );
446
453
}
447
454
455
+ public void testCacheAnswersTrue ()
456
+ throws InterpolationException
457
+ {
458
+ Properties p = new Properties ();
459
+ p .setProperty ( "key" , "value" );
460
+
461
+ class CountingStringSearchInterpolator
462
+ extends StringSearchInterpolator
463
+ {
464
+ private int existingCallCount ;
465
+
466
+ @ Override
467
+ protected Object getExistingAnswer ( String key )
468
+ {
469
+ Object value = super .getExistingAnswer ( key );
470
+ if ( value != null )
471
+ {
472
+ ++existingCallCount ;
473
+ }
474
+ return value ;
475
+ }
476
+
477
+ public int getExistingCallCount ()
478
+ {
479
+ return existingCallCount ;
480
+ }
481
+ }
482
+
483
+ CountingStringSearchInterpolator interpolator = new CountingStringSearchInterpolator ();
484
+ interpolator .setCacheAnswers ( true );
485
+ interpolator .addValueSource ( new PropertiesBasedValueSource ( p ) );
486
+
487
+ String result = interpolator .interpolate ( "${key}-${key}-${key}-${key}" );
488
+
489
+ assertEquals ( "value-value-value-value" , result );
490
+ // first value is interpolated and saved, then the 3 next answers came from existing answer Map
491
+ assertEquals ( 3 , interpolator .getExistingCallCount () );
492
+
493
+ // answers are preserved between calls:
494
+ result = interpolator .interpolate ( "${key}-${key}-${key}-${key}" );
495
+ assertEquals ( "value-value-value-value" , result );
496
+ // 3 from the first call to interpolate(), plus 4 from second call
497
+ assertEquals ( 3 + 4 , interpolator .getExistingCallCount () );
498
+ }
499
+
500
+ public void testCacheAnswersFalse ()
501
+ throws InterpolationException
502
+ {
503
+ Properties p = new Properties ();
504
+ p .setProperty ( "key" , "value" );
505
+
506
+ class CountingStringSearchInterpolator
507
+ extends StringSearchInterpolator
508
+ {
509
+ private int existingCallCount ;
510
+
511
+ @ Override
512
+ protected Object getExistingAnswer ( String key )
513
+ {
514
+ Object value = super .getExistingAnswer ( key );
515
+ if ( value != null )
516
+ {
517
+ ++existingCallCount ;
518
+ }
519
+ return value ;
520
+ }
521
+
522
+ public int getExistingCallCount ()
523
+ {
524
+ return existingCallCount ;
525
+ }
526
+ }
527
+
528
+ CountingStringSearchInterpolator interpolator = new CountingStringSearchInterpolator ();
529
+ interpolator .addValueSource ( new PropertiesBasedValueSource ( p ) );
530
+
531
+ String result = interpolator .interpolate ( "${key}-${key}-${key}-${key}" );
532
+
533
+ assertEquals ( "value-value-value-value" , result );
534
+ // all values are interpolated each time
535
+ assertEquals ( 0 , interpolator .getExistingCallCount () );
536
+ }
537
+
448
538
public String getVar ()
449
539
{
450
540
return "testVar" ;
0 commit comments