View Javadoc
1   package org.codehaus.plexus.interpolation.fixed;
2   /*
3    * Copyright 2001-2008 Codehaus Foundation.
4    *
5    * Licensed under the Apache License, VerDefaultInterpolationStatesion 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import static org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator.create;
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.codehaus.plexus.interpolation.FixedInterpolatorValueSource;
30  import org.codehaus.plexus.interpolation.InterpolationException;
31  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
32  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
33  import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class FixedStringSearchInterpolatorTest
38  {
39  
40      @Before
41      public void setUp()
42      {
43          EnvarBasedValueSource.resetStatics();
44      }
45  
46      @Test
47      public void testLongDelimitersInContext()
48      {
49          String src = "This is a <expression>test.label</expression> for long delimiters in context.";
50          String result = "This is a test for long delimiters in context.";
51  
52          Properties p = new Properties();
53          p.setProperty( "test.label", "test" );
54  
55          FixedStringSearchInterpolator interpolator =
56              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
57  
58          assertEquals( result, interpolator.interpolate( src ) );
59      }
60  
61      @Test
62      public void testLongDelimitersWithNoStartContext()
63      {
64          String src = "<expression>test.label</expression> for long delimiters in context.";
65          String result = "test for long delimiters in context.";
66  
67          Properties p = new Properties();
68          p.setProperty( "test.label", "test" );
69  
70          FixedStringSearchInterpolator interpolator =
71              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
72  
73          assertEquals( result, interpolator.interpolate( src ) );
74      }
75  
76      @Test
77      public void testLongDelimitersWithNoEndContext()
78      {
79          String src = "This is a <expression>test.label</expression>";
80          String result = "This is a test";
81  
82          Properties p = new Properties();
83          p.setProperty( "test.label", "test" );
84  
85          FixedStringSearchInterpolator interpolator =
86              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
87  
88          assertEquals( result, interpolator.interpolate( src ) );
89      }
90  
91      @Test
92      public void testLongDelimitersWithNoContext()
93      {
94          String src = "<expression>test.label</expression>";
95          String result = "test";
96  
97          Properties p = new Properties();
98          p.setProperty( "test.label", "test" );
99  
100         FixedStringSearchInterpolator interpolator =
101             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
102 
103         assertEquals( result, interpolator.interpolate( src ) );
104     }
105 
106     @Test
107     public void testSimpleSubstitution()
108     {
109         Properties p = new Properties();
110         p.setProperty( "key", "value" );
111 
112         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
113 
114         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test ${key}." ) );
115     }
116 
117     @Test
118     public void testSimpleSubstitution_TwoExpressions()
119     {
120         Properties p = new Properties();
121         p.setProperty( "key", "value" );
122         p.setProperty( "key2", "value2" );
123 
124         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
125 
126         assertEquals( "value-value2", interpolator.interpolate( "${key}-${key2}" ) );
127     }
128 
129     @Test
130     public void testBrokenExpression_LeaveItAlone()
131     {
132         Properties p = new Properties();
133         p.setProperty( "key", "value" );
134 
135         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
136 
137         assertEquals( "This is a test ${key.", interpolator.interpolate( "This is a test ${key." ) );
138     }
139 
140     @Test
141     public void testShouldFailOnExpressionCycle()
142     {
143         Properties props = new Properties();
144         props.setProperty( "key1", "${key2}" );
145         props.setProperty( "key2", "${key1}" );
146 
147         FixedStringSearchInterpolator rbi = create( new PropertiesBasedValueSource( props ) );
148 
149         try
150         {
151             rbi.interpolate( "${key1}" );
152 
153             fail( "Should detect expression cycle and fail." );
154         }
155         catch ( org.codehaus.plexus.interpolation.fixed.InterpolationCycleException e )
156         {
157             // expected
158         }
159     }
160 
161     @Test
162     public void testShouldResolveByUsingObject_List_Map()
163         throws InterpolationException
164     {
165         FixedStringSearchInterpolator rbi = create( new ObjectBasedValueSource( this ) );
166         String result =
167             rbi.interpolate( "this is a ${var} ${list[1].name} ${anArray[2].name} ${map(Key with spaces).name}" );
168 
169         assertEquals( "this is a testVar testIndexedWithList testIndexedWithArray testMap", result );
170     }
171 
172     @Test
173     public void testShouldResolveByContextValue()
174         throws InterpolationException
175     {
176 
177         Map<String, String> context = new HashMap<String, String>();
178         context.put( "var", "testVar" );
179 
180         FixedStringSearchInterpolator rbi = create( new MapBasedValueSource( context ) );
181 
182         String result = rbi.interpolate( "this is a ${var}" );
183 
184         assertEquals( "this is a testVar", result );
185     }
186 
187     @Test
188     public void testShouldResolveByEnvar()
189         throws IOException, InterpolationException
190     {
191         OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
192         {
193             public Map<String, String> getEnvMap()
194             {
195                 HashMap<String,String> map = new HashMap<String,String>();
196                 map.put( "SOME_ENV", "variable" );
197                 map.put( "OTHER_ENV", "other variable" );
198                 return map;
199             }
200         } );
201 
202         FixedStringSearchInterpolator rbi = create( new EnvarBasedValueSource( false ) );
203 
204         String result = rbi.interpolate( "this is a ${env.SOME_ENV} ${env.OTHER_ENV}" );
205 
206         assertEquals( "this is a variable other variable", result );
207     }
208 
209     @Test
210     public void testUsePostProcessor_DoesNotChangeValue()
211         throws InterpolationException
212     {
213 
214         Map<String, String> context = new HashMap<String, String>();
215         context.put( "test.var", "testVar" );
216 
217         final InterpolationPostProcessor postProcessor = new InterpolationPostProcessor()
218         {
219             public Object execute( String expression, Object value )
220             {
221                 return null;
222             }
223         };
224         FixedStringSearchInterpolator rbi =
225             create( new MapBasedValueSource( context ) ).withPostProcessor( postProcessor );
226 
227         String result = rbi.interpolate( "this is a ${test.var}" );
228 
229         assertEquals( "this is a testVar", result );
230     }
231 
232     @Test
233     public void testUsePostProcessor_ChangesValue()
234         throws InterpolationException
235     {
236 
237         Map context = new HashMap();
238         context.put( "test.var", "testVar" );
239 
240         final InterpolationPostProcessor postProcessor = new InterpolationPostProcessor()
241         {
242             public Object execute( String expression, Object value )
243             {
244                 return value + "2";
245             }
246         };
247 
248         FixedStringSearchInterpolator rbi =
249             create( new MapBasedValueSource( context ) ).withPostProcessor( postProcessor );
250 
251         String result = rbi.interpolate( "this is a ${test.var}" );
252 
253         assertEquals( "this is a testVar2", result );
254     }
255 
256     @Test
257     public void testSimpleSubstitutionWithDefinedExpr()
258         throws InterpolationException
259     {
260         Properties p = new Properties();
261         p.setProperty( "key", "value" );
262 
263         FixedStringSearchInterpolator interpolator = create( "@{", "}", new PropertiesBasedValueSource( p ) );
264 
265         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test @{key}." ) );
266     }
267 
268     @Test
269     public void testEscape()
270         throws InterpolationException
271     {
272         Properties p = new Properties();
273         p.setProperty( "key", "value" );
274 
275         FixedStringSearchInterpolator interpolator =
276             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
277 
278         String result = interpolator.interpolate( "This is a test \\@{key}." );
279 
280         assertEquals( "This is a test @{key}.", result );
281     }
282 
283     @Test
284     public void testEscapeWithLongEscapeStr()
285         throws InterpolationException
286     {
287         Properties p = new Properties();
288         p.setProperty( "key", "value" );
289 
290         FixedStringSearchInterpolator interpolator =
291             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
292 
293         String result = interpolator.interpolate( "This is a test $$@{key}." );
294 
295         assertEquals( "This is a test @{key}.", result );
296     }
297 
298     @Test
299     public void testEscapeWithLongEscapeStrAtStart()
300         throws InterpolationException
301     {
302         Properties p = new Properties();
303         p.setProperty( "key", "value" );
304 
305         FixedStringSearchInterpolator interpolator =
306             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
307 
308         String result = interpolator.interpolate( "$$@{key} This is a test." );
309 
310         assertEquals( "@{key} This is a test.", result );
311     }
312 
313     @Test
314     public void testNotEscapeWithLongEscapeStrAtStart()
315         throws InterpolationException
316     {
317         Properties p = new Properties();
318         p.setProperty( "key", "value" );
319 
320         FixedStringSearchInterpolator interpolator =
321             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
322 
323         String result = interpolator.interpolate( "@{key} This is a test." );
324 
325         assertEquals( "value This is a test.", result );
326     }
327 
328     @Test
329     public void testEscapeNotFailWithNullEscapeStr()
330         throws InterpolationException
331     {
332         Properties p = new Properties();
333         p.setProperty( "key", "value" );
334 
335         FixedStringSearchInterpolator interpolator =
336             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( null );
337 
338         String result = interpolator.interpolate( "This is a test @{key}." );
339 
340         assertEquals( "This is a test value.", result );
341     }
342 
343     @Test
344     public void testOnlyEscapeExprAtStart()
345         throws InterpolationException
346     {
347         Properties p = new Properties();
348         p.setProperty( "key", "value" );
349 
350         FixedStringSearchInterpolator interpolator =
351             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
352 
353         String result = interpolator.interpolate( "\\@{key} This is a test." );
354 
355         assertEquals( "@{key} This is a test.", result );
356     }
357 
358     @Test
359     public void testNotEscapeExprAtStart()
360         throws InterpolationException
361     {
362         Properties p = new Properties();
363         p.setProperty( "key", "value" );
364 
365         FixedStringSearchInterpolator interpolator =
366             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
367 
368         String result = interpolator.interpolate( "@{key} This is a test." );
369 
370         assertEquals( "value This is a test.", result );
371     }
372 
373     @Test
374     public void testEscapeExprAtStart()
375         throws InterpolationException
376     {
377         Properties p = new Properties();
378         p.setProperty( "key", "value" );
379 
380         FixedStringSearchInterpolator interpolator =
381             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@", "@" ).withEscapeString( "\\" );
382 
383         String result = interpolator.interpolate( "\\@key@ This is a test @key@." );
384 
385         assertEquals( "@key@ This is a test value.", result );
386     }
387 
388     @Test
389     public void testNPEFree()
390         throws InterpolationException
391     {
392         Properties p = new Properties();
393         p.setProperty( "key", "value" );
394 
395         FixedStringSearchInterpolator interpolator =
396             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
397 
398         String result = interpolator.interpolate( null );
399 
400         assertEquals( "", result );
401     }
402 
403     @Test
404     public void testInterruptedInterpolate()
405         throws InterpolationException
406     {
407         final boolean[] error = new boolean[]{ false };
408         FixedValueSource valueSource = new FixedValueSource()
409         {
410             public Object getValue( String expression, InterpolationState errorCollector )
411             {
412                 if ( expression.equals( "key" ) )
413                 {
414                     if ( error[0] )
415                     {
416                         throw new IllegalStateException( "broken" );
417                     }
418                     return "val";
419                 }
420                 else
421                 {
422                     return null;
423                 }
424             }
425         };
426 
427         FixedStringSearchInterpolator interpolator = create( valueSource );
428 
429         assertEquals( "control case", "-val-", interpolator.interpolate( "-${key}-" ) );
430         error[0] = true;
431         try
432         {
433             interpolator.interpolate( "-${key}-" );
434             fail( "should have thrown exception" );
435         }
436         catch ( IllegalStateException x )
437         {
438             // right
439         }
440         error[0] = false;
441         assertEquals( "should not believe there is a cycle here", "-val-", interpolator.interpolate( "-${key}-" ) );
442     }
443 
444     public String getVar()
445     {
446         return "testVar";
447     }
448 
449     public Person[] getAnArray()
450     {
451         Person[] array = new Person[3];
452         array[0] = new Person( "Gabriel" );
453         array[1] = new Person( "Daniela" );
454         array[2] = new Person( "testIndexedWithArray" );
455         return array;
456     }
457 
458     public List<Person> getList()
459     {
460         List<Person> list = new ArrayList<Person>();
461         list.add( new Person( "Gabriel" ) );
462         list.add( new Person( "testIndexedWithList" ) );
463         list.add( new Person( "Daniela" ) );
464         return list;
465     }
466 
467     public Map<String, Person> getMap()
468     {
469         Map<String, Person> map = new HashMap<String, Person>();
470         map.put( "Key with spaces", new Person( "testMap" ) );
471         return map;
472     }
473 
474     public static class Person
475     {
476         private String name;
477 
478         public Person( String name )
479         {
480             this.name = name;
481         }
482 
483         public String getName()
484         {
485             return name;
486         }
487     }
488 
489     @Test
490     public void testLinkedInterpolators()
491     {
492         final String EXPR = "${test.label}AND${test2}";
493         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
494 
495         FixedStringSearchInterpolator interWith2Fields =
496             create( properttyBasedValueSource( "test.label", "p", "test2", "x" ) );
497         assertEquals( "pANDx", interWith2Fields.interpolate( EXPR ) );
498 
499         FixedStringSearchInterpolator joined =
500             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
501         assertEquals( "pzzANDx", joined.interpolate( EXPR2 ) );
502     }
503 
504     @Test
505     public void testDominance()
506     {
507         final String EXPR = "${test.label}AND${test2}";
508         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
509 
510         FixedStringSearchInterpolator interWith2Fields =
511             create( properttyBasedValueSource( "test.label", "p", "test2", "x", "test2.label", "dominant" ) );
512         assertEquals( "pANDx", interWith2Fields.interpolate( EXPR ) );
513 
514         FixedStringSearchInterpolator joined =
515             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
516         assertEquals( "pdominantANDx", joined.interpolate( EXPR2 ) );
517     }
518 
519     @Test
520     public void unresolable_linked()
521     {
522         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
523 
524         FixedStringSearchInterpolator interWith2Fields =
525             create( properttyBasedValueSource( "test.label", "p", "test2", "x", "test2.label", "dominant" ) );
526 
527         FixedStringSearchInterpolator joined =
528             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
529         assertEquals( "pdominantANDx", joined.interpolate( EXPR2 ) );
530     }
531 
532     @Test( expected = InterpolationCycleException.class )
533     public void testCyclesWithLinked()
534     {
535         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "${key2}" ) );
536         FixedStringSearchInterpolator second = create( first, properttyBasedValueSource( "key2", "${key1}" ) );
537         second.interpolate( "${key2}" );
538     }
539 
540     @Test( expected = InterpolationCycleException.class )
541     public void testCyclesWithLinked_betweenRootAndOther()
542     {
543         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "${key2}" ) );
544         FixedStringSearchInterpolator second = create( first, properttyBasedValueSource( "key2", "${key1}" ) );
545         second.interpolate( "${key1}" );
546     }
547 
548     @Test
549     public void fixedInjectedIntoRegular()
550         throws InterpolationException
551     {
552         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "v1" ) );
553 
554         Properties p = new Properties();
555         p.setProperty( "key", "X" );
556         StringSearchInterpolator interpolator = new StringSearchInterpolator( "${", "}" );
557         interpolator.setEscapeString( "\\" );
558         interpolator.addValueSource( new org.codehaus.plexus.interpolation.PropertiesBasedValueSource( p ) );
559         interpolator.addValueSource( new FixedInterpolatorValueSource( first ) );
560         assertEquals("v1X", interpolator.interpolate( "${key1}${key}" ));
561 
562     }
563 
564     private PropertiesBasedValueSource properttyBasedValueSource( String... values )
565     {
566         Properties p = new Properties();
567         for ( int i = 0; i < values.length; i += 2 )
568         {
569             p.setProperty( values[i], values[i + 1] );
570         }
571         return new PropertiesBasedValueSource( p );
572     }
573 }
574