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 org.codehaus.plexus.interpolation.FixedInterpolatorValueSource;
19  import org.codehaus.plexus.interpolation.InterpolationException;
20  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
21  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
22  import org.junit.Test;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import static org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator.create;
30  import static org.junit.Assert.*;
31  
32  public class FixedStringSearchInterpolatorTest
33  {
34  
35      @Test
36      public void testLongDelimitersInContext()
37      {
38          String src = "This is a <expression>test.label</expression> for long delimiters in context.";
39          String result = "This is a test for long delimiters in context.";
40  
41          Properties p = new Properties();
42          p.setProperty( "test.label", "test" );
43  
44          FixedStringSearchInterpolator interpolator =
45              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
46  
47          assertEquals( result, interpolator.interpolate( src ) );
48      }
49  
50      @Test
51      public void testLongDelimitersWithNoStartContext()
52      {
53          String src = "<expression>test.label</expression> for long delimiters in context.";
54          String result = "test for long delimiters in context.";
55  
56          Properties p = new Properties();
57          p.setProperty( "test.label", "test" );
58  
59          FixedStringSearchInterpolator interpolator =
60              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
61  
62          assertEquals( result, interpolator.interpolate( src ) );
63      }
64  
65      @Test
66      public void testLongDelimitersWithNoEndContext()
67      {
68          String src = "This is a <expression>test.label</expression>";
69          String result = "This is a test";
70  
71          Properties p = new Properties();
72          p.setProperty( "test.label", "test" );
73  
74          FixedStringSearchInterpolator interpolator =
75              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
76  
77          assertEquals( result, interpolator.interpolate( src ) );
78      }
79  
80      @Test
81      public void testLongDelimitersWithNoContext()
82      {
83          String src = "<expression>test.label</expression>";
84          String result = "test";
85  
86          Properties p = new Properties();
87          p.setProperty( "test.label", "test" );
88  
89          FixedStringSearchInterpolator interpolator =
90              create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "<expression>", "</expression>" );
91  
92          assertEquals( result, interpolator.interpolate( src ) );
93      }
94  
95      @Test
96      public void testSimpleSubstitution()
97      {
98          Properties p = new Properties();
99          p.setProperty( "key", "value" );
100 
101         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
102 
103         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test ${key}." ) );
104     }
105 
106     @Test
107     public void testSimpleSubstitution_TwoExpressions()
108     {
109         Properties p = new Properties();
110         p.setProperty( "key", "value" );
111         p.setProperty( "key2", "value2" );
112 
113         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
114 
115         assertEquals( "value-value2", interpolator.interpolate( "${key}-${key2}" ) );
116     }
117 
118     @Test
119     public void testBrokenExpression_LeaveItAlone()
120     {
121         Properties p = new Properties();
122         p.setProperty( "key", "value" );
123 
124         FixedStringSearchInterpolator interpolator = create( new PropertiesBasedValueSource( p ) );
125 
126         assertEquals( "This is a test ${key.", interpolator.interpolate( "This is a test ${key." ) );
127     }
128 
129     @Test
130     public void testShouldFailOnExpressionCycle()
131     {
132         Properties props = new Properties();
133         props.setProperty( "key1", "${key2}" );
134         props.setProperty( "key2", "${key1}" );
135 
136         FixedStringSearchInterpolator rbi = create( new PropertiesBasedValueSource( props ) );
137 
138         try
139         {
140             rbi.interpolate( "${key1}" );
141 
142             fail( "Should detect expression cycle and fail." );
143         }
144         catch ( org.codehaus.plexus.interpolation.fixed.InterpolationCycleException e )
145         {
146             // expected
147         }
148     }
149 
150     @Test
151     public void testShouldResolveByMy_getVar_Method()
152         throws InterpolationException
153     {
154         FixedStringSearchInterpolator rbi = create( new ObjectBasedValueSource( this ) );
155         String result = rbi.interpolate( "this is a ${var}" );
156 
157         assertEquals( "this is a testVar", result );
158     }
159 
160     @Test
161     public void testShouldResolveByContextValue()
162         throws InterpolationException
163     {
164 
165         Map<String, String> context = new HashMap<String, String>();
166         context.put( "var", "testVar" );
167 
168         FixedStringSearchInterpolator rbi = create( new MapBasedValueSource( context ) );
169 
170         String result = rbi.interpolate( "this is a ${var}" );
171 
172         assertEquals( "this is a testVar", result );
173     }
174 
175     @Test
176     public void testShouldResolveByEnvar()
177         throws IOException, InterpolationException
178     {
179         FixedStringSearchInterpolator rbi = create( new EnvarBasedValueSource( false ) );
180 
181         String result = rbi.interpolate( "this is a ${env.HOME} ${env.PATH}" );
182 
183         assertFalse( "this is a ${HOME} ${PATH}".equals( result ) );
184         assertFalse( "this is a ${env.HOME} ${env.PATH}".equals( result ) );
185     }
186 
187     @Test
188     public void testUsePostProcessor_DoesNotChangeValue()
189         throws InterpolationException
190     {
191 
192         Map<String, String> context = new HashMap<String, String>();
193         context.put( "test.var", "testVar" );
194 
195         final InterpolationPostProcessor postProcessor = new InterpolationPostProcessor()
196         {
197             public Object execute( String expression, Object value )
198             {
199                 return null;
200             }
201         };
202         FixedStringSearchInterpolator rbi =
203             create( new MapBasedValueSource( context ) ).withPostProcessor( postProcessor );
204 
205         String result = rbi.interpolate( "this is a ${test.var}" );
206 
207         assertEquals( "this is a testVar", result );
208     }
209 
210     @Test
211     public void testUsePostProcessor_ChangesValue()
212         throws InterpolationException
213     {
214 
215         Map context = new HashMap();
216         context.put( "test.var", "testVar" );
217 
218         final InterpolationPostProcessor postProcessor = new InterpolationPostProcessor()
219         {
220             public Object execute( String expression, Object value )
221             {
222                 return value + "2";
223             }
224         };
225 
226         FixedStringSearchInterpolator rbi =
227             create( new MapBasedValueSource( context ) ).withPostProcessor( postProcessor );
228 
229         String result = rbi.interpolate( "this is a ${test.var}" );
230 
231         assertEquals( "this is a testVar2", result );
232     }
233 
234     @Test
235     public void testSimpleSubstitutionWithDefinedExpr()
236         throws InterpolationException
237     {
238         Properties p = new Properties();
239         p.setProperty( "key", "value" );
240 
241         FixedStringSearchInterpolator interpolator = create( "@{", "}", new PropertiesBasedValueSource( p ) );
242 
243         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test @{key}." ) );
244     }
245 
246     @Test
247     public void testEscape()
248         throws InterpolationException
249     {
250         Properties p = new Properties();
251         p.setProperty( "key", "value" );
252 
253         FixedStringSearchInterpolator interpolator =
254             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
255 
256         String result = interpolator.interpolate( "This is a test \\@{key}." );
257 
258         assertEquals( "This is a test @{key}.", result );
259     }
260 
261     @Test
262     public void testEscapeWithLongEscapeStr()
263         throws InterpolationException
264     {
265         Properties p = new Properties();
266         p.setProperty( "key", "value" );
267 
268         FixedStringSearchInterpolator interpolator =
269             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
270 
271         String result = interpolator.interpolate( "This is a test $$@{key}." );
272 
273         assertEquals( "This is a test @{key}.", result );
274     }
275 
276     @Test
277     public void testEscapeWithLongEscapeStrAtStart()
278         throws InterpolationException
279     {
280         Properties p = new Properties();
281         p.setProperty( "key", "value" );
282 
283         FixedStringSearchInterpolator interpolator =
284             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
285 
286         String result = interpolator.interpolate( "$$@{key} This is a test." );
287 
288         assertEquals( "@{key} This is a test.", result );
289     }
290 
291     @Test
292     public void testNotEscapeWithLongEscapeStrAtStart()
293         throws InterpolationException
294     {
295         Properties p = new Properties();
296         p.setProperty( "key", "value" );
297 
298         FixedStringSearchInterpolator interpolator =
299             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "$$" );
300 
301         String result = interpolator.interpolate( "@{key} This is a test." );
302 
303         assertEquals( "value This is a test.", result );
304     }
305 
306     @Test
307     public void testEscapeNotFailWithNullEscapeStr()
308         throws InterpolationException
309     {
310         Properties p = new Properties();
311         p.setProperty( "key", "value" );
312 
313         FixedStringSearchInterpolator interpolator =
314             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( null );
315 
316         String result = interpolator.interpolate( "This is a test @{key}." );
317 
318         assertEquals( "This is a test value.", result );
319     }
320 
321     @Test
322     public void testOnlyEscapeExprAtStart()
323         throws InterpolationException
324     {
325         Properties p = new Properties();
326         p.setProperty( "key", "value" );
327 
328         FixedStringSearchInterpolator interpolator =
329             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
330 
331         String result = interpolator.interpolate( "\\@{key} This is a test." );
332 
333         assertEquals( "@{key} This is a test.", result );
334     }
335 
336     @Test
337     public void testNotEscapeExprAtStart()
338         throws InterpolationException
339     {
340         Properties p = new Properties();
341         p.setProperty( "key", "value" );
342 
343         FixedStringSearchInterpolator interpolator =
344             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
345 
346         String result = interpolator.interpolate( "@{key} This is a test." );
347 
348         assertEquals( "value This is a test.", result );
349     }
350 
351     @Test
352     public void testEscapeExprAtStart()
353         throws InterpolationException
354     {
355         Properties p = new Properties();
356         p.setProperty( "key", "value" );
357 
358         FixedStringSearchInterpolator interpolator =
359             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@", "@" ).withEscapeString( "\\" );
360 
361         String result = interpolator.interpolate( "\\@key@ This is a test @key@." );
362 
363         assertEquals( "@key@ This is a test value.", result );
364     }
365 
366     @Test
367     public void testNPEFree()
368         throws InterpolationException
369     {
370         Properties p = new Properties();
371         p.setProperty( "key", "value" );
372 
373         FixedStringSearchInterpolator interpolator =
374             create( new PropertiesBasedValueSource( p ) ).withExpressionMarkers( "@{", "}" ).withEscapeString( "\\" );
375 
376         String result = interpolator.interpolate( null );
377 
378         assertEquals( "", result );
379     }
380 
381     @Test
382     public void testInterruptedInterpolate()
383         throws InterpolationException
384     {
385         final boolean[] error = new boolean[]{ false };
386         FixedValueSource valueSource = new FixedValueSource()
387         {
388             public Object getValue( String expression, InterpolationState errorCollector )
389             {
390                 if ( expression.equals( "key" ) )
391                 {
392                     if ( error[0] )
393                     {
394                         throw new IllegalStateException( "broken" );
395                     }
396                     return "val";
397                 }
398                 else
399                 {
400                     return null;
401                 }
402             }
403         };
404 
405         FixedStringSearchInterpolator interpolator = create( valueSource );
406 
407         assertEquals( "control case", "-val-", interpolator.interpolate( "-${key}-" ) );
408         error[0] = true;
409         try
410         {
411             interpolator.interpolate( "-${key}-" );
412             fail( "should have thrown exception" );
413         }
414         catch ( IllegalStateException x )
415         {
416             // right
417         }
418         error[0] = false;
419         assertEquals( "should not believe there is a cycle here", "-val-", interpolator.interpolate( "-${key}-" ) );
420     }
421 
422     public String getVar()
423     {
424         return "testVar";
425     }
426 
427     @Test
428     public void testLinkedInterpolators()
429     {
430         final String EXPR = "${test.label}AND${test2}";
431         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
432 
433         FixedStringSearchInterpolator interWith2Fields =
434             create( properttyBasedValueSource( "test.label", "p", "test2", "x" ) );
435         assertEquals( "pANDx", interWith2Fields.interpolate( EXPR ) );
436 
437         FixedStringSearchInterpolator joined =
438             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
439         assertEquals( "pzzANDx", joined.interpolate( EXPR2 ) );
440     }
441 
442     @Test
443     public void testDominance()
444     {
445         final String EXPR = "${test.label}AND${test2}";
446         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
447 
448         FixedStringSearchInterpolator interWith2Fields =
449             create( properttyBasedValueSource( "test.label", "p", "test2", "x", "test2.label", "dominant" ) );
450         assertEquals( "pANDx", interWith2Fields.interpolate( EXPR ) );
451 
452         FixedStringSearchInterpolator joined =
453             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
454         assertEquals( "pdominantANDx", joined.interpolate( EXPR2 ) );
455     }
456 
457     @Test
458     public void unresolable_linked()
459     {
460         final String EXPR2 = "${test.label}${test2.label}AND${test2}";
461 
462         FixedStringSearchInterpolator interWith2Fields =
463             create( properttyBasedValueSource( "test.label", "p", "test2", "x", "test2.label", "dominant" ) );
464 
465         FixedStringSearchInterpolator joined =
466             create( interWith2Fields, properttyBasedValueSource( "test2.label", "zz" ) );
467         assertEquals( "pdominantANDx", joined.interpolate( EXPR2 ) );
468     }
469 
470     @Test( expected = InterpolationCycleException.class )
471     public void testCyclesWithLinked()
472     {
473         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "${key2}" ) );
474         FixedStringSearchInterpolator second = create( first, properttyBasedValueSource( "key2", "${key1}" ) );
475         second.interpolate( "${key2}" );
476     }
477 
478     @Test( expected = InterpolationCycleException.class )
479     public void testCyclesWithLinked_betweenRootAndOther()
480     {
481         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "${key2}" ) );
482         FixedStringSearchInterpolator second = create( first, properttyBasedValueSource( "key2", "${key1}" ) );
483         second.interpolate( "${key1}" );
484     }
485 
486     @Test
487     public void fixedInjectedIntoRegular()
488         throws InterpolationException
489     {
490         FixedStringSearchInterpolator first = create( properttyBasedValueSource( "key1", "v1" ) );
491 
492         Properties p = new Properties();
493         p.setProperty( "key", "X" );
494         StringSearchInterpolator interpolator = new StringSearchInterpolator( "${", "}" );
495         interpolator.setEscapeString( "\\" );
496         interpolator.addValueSource( new org.codehaus.plexus.interpolation.PropertiesBasedValueSource( p ) );
497         interpolator.addValueSource( new FixedInterpolatorValueSource( first ) );
498         assertEquals("v1X", interpolator.interpolate( "${key1}${key}" ));
499 
500     }
501 
502     private PropertiesBasedValueSource properttyBasedValueSource( String... values )
503     {
504         Properties p = new Properties();
505         for ( int i = 0; i < values.length; i += 2 )
506         {
507             p.setProperty( values[i], values[i + 1] );
508         }
509         return new PropertiesBasedValueSource( p );
510     }
511 }
512