View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * Copyright 2001-2008 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
28  import org.junit.Before;
29  
30  import junit.framework.TestCase;
31  
32  public class StringSearchInterpolatorTest
33      extends TestCase
34  {
35  
36      @Before
37      public void setUp()
38      {
39          EnvarBasedValueSource.resetStatics();
40      }
41  
42      public void testLongDelimitersInContext()
43          throws InterpolationException
44      {
45          String src = "This is a <expression>test.label</expression> for long delimiters in context.";
46          String result = "This is a test for long delimiters in context.";
47  
48          Properties p = new Properties();
49          p.setProperty( "test.label", "test" );
50  
51          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
52          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
53  
54          assertEquals( result, interpolator.interpolate( src ) );
55      }
56  
57      public void testLongDelimitersWithNoStartContext()
58          throws InterpolationException
59      {
60          String src = "<expression>test.label</expression> for long delimiters in context.";
61          String result = "test for long delimiters in context.";
62  
63          Properties p = new Properties();
64          p.setProperty( "test.label", "test" );
65  
66          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
67          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
68  
69          assertEquals( result, interpolator.interpolate( src ) );
70      }
71  
72      public void testLongDelimitersWithNoEndContext()
73          throws InterpolationException
74      {
75          String src = "This is a <expression>test.label</expression>";
76          String result = "This is a test";
77  
78          Properties p = new Properties();
79          p.setProperty( "test.label", "test" );
80  
81          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
82          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
83  
84          assertEquals( result, interpolator.interpolate( src ) );
85      }
86  
87      public void testLongDelimitersWithNoContext()
88          throws InterpolationException
89      {
90          String src = "<expression>test.label</expression>";
91          String result = "test";
92  
93          Properties p = new Properties();
94          p.setProperty( "test.label", "test" );
95  
96          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
97          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
98  
99          assertEquals( result, interpolator.interpolate( src ) );
100     }
101 
102     public void testSimpleSubstitution()
103         throws InterpolationException
104     {
105         Properties p = new Properties();
106         p.setProperty( "key", "value" );
107 
108         StringSearchInterpolator interpolator = new StringSearchInterpolator();
109         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
110 
111         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test ${key}." ) );
112     }
113 
114     public void testSimpleSubstitution_TwoExpressions()
115         throws InterpolationException
116     {
117         Properties p = new Properties();
118         p.setProperty( "key", "value" );
119         p.setProperty( "key2", "value2" );
120 
121         StringSearchInterpolator interpolator = new StringSearchInterpolator();
122         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
123 
124         assertEquals( "value-value2", interpolator.interpolate( "${key}-${key2}" ) );
125     }
126 
127     public void testBrokenExpression_LeaveItAlone()
128         throws InterpolationException
129     {
130         Properties p = new Properties();
131         p.setProperty( "key", "value" );
132 
133         StringSearchInterpolator interpolator = new StringSearchInterpolator();
134         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
135 
136         assertEquals( "This is a test ${key.", interpolator.interpolate( "This is a test ${key." ) );
137     }
138 
139     public void testShouldFailOnExpressionCycle()
140     {
141         Properties props = new Properties();
142         props.setProperty( "key1", "${key2}" );
143         props.setProperty( "key2", "${key1}" );
144 
145         StringSearchInterpolator rbi = new StringSearchInterpolator();
146         rbi.addValueSource( new PropertiesBasedValueSource( props ) );
147 
148         try
149         {
150             rbi.interpolate( "${key1}", new SimpleRecursionInterceptor() );
151 
152             fail( "Should detect expression cycle and fail." );
153         }
154         catch ( InterpolationException e )
155         {
156             // expected
157         }
158     }
159 
160     public void testShouldResolveByUsingObject_List_Map()
161         throws InterpolationException
162     {
163         StringSearchInterpolator rbi = new StringSearchInterpolator();
164         rbi.addValueSource( new ObjectBasedValueSource( this ) );
165         String result =
166             rbi.interpolate( "this is a ${var} ${list[1].name} ${anArray[2].name} ${map(Key with spaces).name}" );
167 
168         assertEquals( "this is a testVar testIndexedWithList testIndexedWithArray testMap", result );
169     }
170 
171     public void testShouldResolveByContextValue()
172         throws InterpolationException
173     {
174         StringSearchInterpolator rbi = new StringSearchInterpolator();
175 
176         Map context = new HashMap();
177         context.put( "var", "testVar" );
178 
179         rbi.addValueSource( new MapBasedValueSource( context ) );
180 
181         String result = rbi.interpolate( "this is a ${var}" );
182 
183         assertEquals( "this is a testVar", result );
184     }
185 
186     public void testShouldResolveByEnvar()
187         throws IOException, InterpolationException
188     {
189         OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
190         {
191             public Map<String, String> getEnvMap()
192             {
193                 HashMap<String,String> map = new HashMap<String,String>();
194                 map.put( "SOME_ENV", "variable" );
195                 map.put( "OTHER_ENV", "other variable" );
196                 return map;
197             }
198         } );
199 
200         StringSearchInterpolator rbi = new StringSearchInterpolator();
201 
202         rbi.addValueSource( 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     public void testUsePostProcessor_DoesNotChangeValue()
210         throws InterpolationException
211     {
212         StringSearchInterpolator rbi = new StringSearchInterpolator();
213 
214         Map context = new HashMap();
215         context.put( "test.var", "testVar" );
216 
217         rbi.addValueSource( new MapBasedValueSource( context ) );
218 
219         rbi.addPostProcessor( new InterpolationPostProcessor()
220         {
221             public Object execute( String expression, Object value )
222             {
223                 return null;
224             }
225         } );
226 
227         String result = rbi.interpolate( "this is a ${test.var}" );
228 
229         assertEquals( "this is a testVar", result );
230     }
231 
232     public void testUsePostProcessor_ChangesValue()
233         throws InterpolationException
234     {
235 
236         StringSearchInterpolator rbi = new StringSearchInterpolator();
237 
238         Map context = new HashMap();
239         context.put( "test.var", "testVar" );
240 
241         rbi.addValueSource( new MapBasedValueSource( context ) );
242 
243         rbi.addPostProcessor( new InterpolationPostProcessor()
244         {
245             public Object execute( String expression, Object value )
246             {
247                 return value + "2";
248             }
249         } );
250 
251         String result = rbi.interpolate( "this is a ${test.var}" );
252 
253         assertEquals( "this is a testVar2", result );
254     }
255 
256     public void testSimpleSubstitutionWithDefinedExpr()
257         throws InterpolationException
258     {
259         Properties p = new Properties();
260         p.setProperty( "key", "value" );
261 
262         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
263         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
264 
265         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test @{key}." ) );
266     }
267 
268     public void testEscape()
269         throws InterpolationException
270     {
271         Properties p = new Properties();
272         p.setProperty( "key", "value" );
273 
274         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
275         interpolator.setEscapeString( "\\" );
276         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
277 
278         String result = interpolator.interpolate( "This is a test \\@{key}." );
279 
280         assertEquals( "This is a test @{key}.", result );
281     }
282 
283     public void testEscapeWithLongEscapeStr()
284         throws InterpolationException
285     {
286         Properties p = new Properties();
287         p.setProperty( "key", "value" );
288 
289         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
290         interpolator.setEscapeString( "$$" );
291         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
292 
293         String result = interpolator.interpolate( "This is a test $$@{key}." );
294 
295         assertEquals( "This is a test @{key}.", result );
296     }
297 
298     public void testEscapeWithLongEscapeStrAtStart()
299         throws InterpolationException
300     {
301         Properties p = new Properties();
302         p.setProperty( "key", "value" );
303 
304         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
305         interpolator.setEscapeString( "$$" );
306         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
307 
308         String result = interpolator.interpolate( "$$@{key} This is a test." );
309 
310         assertEquals( "@{key} This is a test.", result );
311     }
312 
313     public void testNotEscapeWithLongEscapeStrAtStart()
314         throws InterpolationException
315     {
316         Properties p = new Properties();
317         p.setProperty( "key", "value" );
318 
319         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
320         interpolator.setEscapeString( "$$" );
321         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
322 
323         String result = interpolator.interpolate( "@{key} This is a test." );
324 
325         assertEquals( "value This is a test.", result );
326     }
327 
328     public void testEscapeNotFailWithNullEscapeStr()
329         throws InterpolationException
330     {
331         Properties p = new Properties();
332         p.setProperty( "key", "value" );
333 
334         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
335         interpolator.setEscapeString( null );
336         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
337 
338         String result = interpolator.interpolate( "This is a test @{key}." );
339 
340         assertEquals( "This is a test value.", result );
341     }
342 
343     public void testOnlyEscapeExprAtStart()
344         throws InterpolationException
345     {
346         Properties p = new Properties();
347         p.setProperty( "key", "value" );
348 
349         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
350         interpolator.setEscapeString( "\\" );
351         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
352 
353         String result = interpolator.interpolate( "\\@{key} This is a test." );
354 
355         assertEquals( "@{key} This is a test.", result );
356     }
357 
358     public void testNotEscapeExprAtStart()
359         throws InterpolationException
360     {
361         Properties p = new Properties();
362         p.setProperty( "key", "value" );
363 
364         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
365         interpolator.setEscapeString( "\\" );
366         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
367 
368         String result = interpolator.interpolate( "@{key} This is a test." );
369 
370         assertEquals( "value This is a test.", result );
371     }
372 
373     public void testEscapeExprAtStart()
374         throws InterpolationException
375     {
376         Properties p = new Properties();
377         p.setProperty( "key", "value" );
378 
379         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@", "@" );
380         interpolator.setEscapeString( "\\" );
381         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
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     public void testNPEFree()
389         throws InterpolationException
390     {
391         Properties p = new Properties();
392         p.setProperty( "key", "value" );
393 
394         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
395         interpolator.setEscapeString( "\\" );
396         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
397 
398         String result = interpolator.interpolate( null );
399 
400         assertEquals( "", result );
401     }
402 
403     public void testInterruptedInterpolate()
404         throws InterpolationException
405     {
406         Interpolator interpolator = new StringSearchInterpolator();
407         RecursionInterceptor recursionInterceptor = new SimpleRecursionInterceptor();
408         final boolean[] error = new boolean[] { false };
409         interpolator.addValueSource( new ValueSource()
410         {
411             public Object getValue( String expression ) {
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             public List getFeedback()
426             {
427                 return Collections.EMPTY_LIST;
428             }
429             public void clearFeedback()
430             {
431             }
432         } );
433         assertEquals( "control case", "-val-" , interpolator.interpolate( "-${key}-", recursionInterceptor ) );
434         error[ 0 ] = true;
435         try
436         {
437             interpolator.interpolate( "-${key}-", recursionInterceptor );
438             fail( "should have thrown exception" );
439         }
440         catch ( IllegalStateException x )
441         {
442             // right
443         }
444         error[ 0 ] = false;
445         assertEquals( "should not believe there is a cycle here", "-val-", interpolator.interpolate( "-${key}-", recursionInterceptor ) );
446     }
447 
448     public String getVar()
449     {
450         return "testVar";
451     }
452 
453     public Person[] getAnArray()
454     {
455         Person[] array = new Person[3];
456         array[0] = new Person( "Gabriel" );
457         array[1] = new Person( "Daniela" );
458         array[2] = new Person( "testIndexedWithArray" );
459         return array;
460     }
461 
462     public List<Person> getList()
463     {
464         List<Person> list = new ArrayList<Person>();
465         list.add( new Person( "Gabriel" ) );
466         list.add( new Person( "testIndexedWithList" ) );
467         list.add( new Person( "Daniela" ) );
468         return list;
469     }
470 
471     public Map<String, Person> getMap()
472     {
473         Map<String, Person> map = new HashMap<String, StringSearchInterpolatorTest.Person>();
474         map.put( "Key with spaces", new Person( "testMap" ) );
475         return map;
476     }
477 
478     public static class Person
479     {
480         private String name;
481 
482         public Person( String name )
483         {
484             this.name = name;
485         }
486 
487         public String getName()
488         {
489             return name;
490         }
491     }
492 
493 }