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.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import junit.framework.TestCase;
27  
28  public class StringSearchInterpolatorTest
29      extends TestCase
30  {
31  
32      public void testLongDelimitersInContext()
33          throws InterpolationException
34      {
35          String src = "This is a <expression>test.label</expression> for long delimiters in context.";
36          String result = "This is a test for long delimiters in context.";
37  
38          Properties p = new Properties();
39          p.setProperty( "test.label", "test" );
40  
41          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
42          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
43  
44          assertEquals( result, interpolator.interpolate( src ) );
45      }
46  
47      public void testLongDelimitersWithNoStartContext()
48          throws InterpolationException
49      {
50          String src = "<expression>test.label</expression> for long delimiters in context.";
51          String result = "test for long delimiters in context.";
52  
53          Properties p = new Properties();
54          p.setProperty( "test.label", "test" );
55  
56          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
57          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
58  
59          assertEquals( result, interpolator.interpolate( src ) );
60      }
61  
62      public void testLongDelimitersWithNoEndContext()
63          throws InterpolationException
64      {
65          String src = "This is a <expression>test.label</expression>";
66          String result = "This is a test";
67  
68          Properties p = new Properties();
69          p.setProperty( "test.label", "test" );
70  
71          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
72          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
73  
74          assertEquals( result, interpolator.interpolate( src ) );
75      }
76  
77      public void testLongDelimitersWithNoContext()
78          throws InterpolationException
79      {
80          String src = "<expression>test.label</expression>";
81          String result = "test";
82  
83          Properties p = new Properties();
84          p.setProperty( "test.label", "test" );
85  
86          StringSearchInterpolator interpolator = new StringSearchInterpolator( "<expression>", "</expression>" );
87          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
88  
89          assertEquals( result, interpolator.interpolate( src ) );
90      }
91  
92      public void testSimpleSubstitution()
93          throws InterpolationException
94      {
95          Properties p = new Properties();
96          p.setProperty( "key", "value" );
97  
98          StringSearchInterpolator interpolator = new StringSearchInterpolator();
99          interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
100 
101         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test ${key}." ) );
102     }
103 
104     public void testSimpleSubstitution_TwoExpressions()
105         throws InterpolationException
106     {
107         Properties p = new Properties();
108         p.setProperty( "key", "value" );
109         p.setProperty( "key2", "value2" );
110 
111         StringSearchInterpolator interpolator = new StringSearchInterpolator();
112         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
113 
114         assertEquals( "value-value2", interpolator.interpolate( "${key}-${key2}" ) );
115     }
116 
117     public void testBrokenExpression_LeaveItAlone()
118         throws InterpolationException
119     {
120         Properties p = new Properties();
121         p.setProperty( "key", "value" );
122 
123         StringSearchInterpolator interpolator = new StringSearchInterpolator();
124         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
125 
126         assertEquals( "This is a test ${key.", interpolator.interpolate( "This is a test ${key." ) );
127     }
128 
129     public void testShouldFailOnExpressionCycle()
130     {
131         Properties props = new Properties();
132         props.setProperty( "key1", "${key2}" );
133         props.setProperty( "key2", "${key1}" );
134 
135         StringSearchInterpolator rbi = new StringSearchInterpolator();
136         rbi.addValueSource( new PropertiesBasedValueSource( props ) );
137 
138         try
139         {
140             rbi.interpolate( "${key1}", new SimpleRecursionInterceptor() );
141 
142             fail( "Should detect expression cycle and fail." );
143         }
144         catch ( InterpolationException e )
145         {
146             // expected
147         }
148     }
149 
150     public void testShouldResolveByMy_getVar_Method()
151         throws InterpolationException
152     {
153         StringSearchInterpolator rbi = new StringSearchInterpolator();
154         rbi.addValueSource( new ObjectBasedValueSource( this ) );
155         String result = rbi.interpolate( "this is a ${var}" );
156 
157         assertEquals( "this is a testVar", result );
158     }
159 
160     public void testShouldResolveByContextValue()
161         throws InterpolationException
162     {
163         StringSearchInterpolator rbi = new StringSearchInterpolator();
164 
165         Map context = new HashMap();
166         context.put( "var", "testVar" );
167 
168         rbi.addValueSource( new MapBasedValueSource( context ) );
169 
170         String result = rbi.interpolate( "this is a ${var}" );
171 
172         assertEquals( "this is a testVar", result );
173     }
174 
175     public void testShouldResolveByEnvar()
176         throws IOException, InterpolationException
177     {
178         StringSearchInterpolator rbi = new StringSearchInterpolator();
179 
180         rbi.addValueSource( new EnvarBasedValueSource( false ) );
181 
182         String result = rbi.interpolate( "this is a ${env.HOME} ${env.PATH}" );
183 
184         assertFalse( "this is a ${HOME} ${PATH}".equals( result ) );
185         assertFalse( "this is a ${env.HOME} ${env.PATH}".equals( result ) );
186     }
187 
188     public void testUsePostProcessor_DoesNotChangeValue()
189         throws InterpolationException
190     {
191         StringSearchInterpolator rbi = new StringSearchInterpolator();
192 
193         Map context = new HashMap();
194         context.put( "test.var", "testVar" );
195 
196         rbi.addValueSource( new MapBasedValueSource( context ) );
197 
198         rbi.addPostProcessor( new InterpolationPostProcessor()
199         {
200             public Object execute( String expression, Object value )
201             {
202                 return null;
203             }
204         } );
205 
206         String result = rbi.interpolate( "this is a ${test.var}" );
207 
208         assertEquals( "this is a testVar", result );
209     }
210 
211     public void testUsePostProcessor_ChangesValue()
212         throws InterpolationException
213     {
214 
215         StringSearchInterpolator rbi = new StringSearchInterpolator();
216 
217         Map context = new HashMap();
218         context.put( "test.var", "testVar" );
219 
220         rbi.addValueSource( new MapBasedValueSource( context ) );
221 
222         rbi.addPostProcessor( new InterpolationPostProcessor()
223         {
224             public Object execute( String expression, Object value )
225             {
226                 return value + "2";
227             }
228         } );
229 
230         String result = rbi.interpolate( "this is a ${test.var}" );
231 
232         assertEquals( "this is a testVar2", result );
233     }
234 
235     public void testSimpleSubstitutionWithDefinedExpr()
236         throws InterpolationException
237     {
238         Properties p = new Properties();
239         p.setProperty( "key", "value" );
240 
241         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
242         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
243 
244         assertEquals( "This is a test value.", interpolator.interpolate( "This is a test @{key}." ) );
245     }
246 
247     public void testEscape()
248         throws InterpolationException
249     {
250         Properties p = new Properties();
251         p.setProperty( "key", "value" );
252 
253         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
254         interpolator.setEscapeString( "\\" );
255         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
256 
257         String result = interpolator.interpolate( "This is a test \\@{key}." );
258 
259         assertEquals( "This is a test @{key}.", result );
260     }
261 
262     public void testEscapeWithLongEscapeStr()
263         throws InterpolationException
264     {
265         Properties p = new Properties();
266         p.setProperty( "key", "value" );
267 
268         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
269         interpolator.setEscapeString( "$$" );
270         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
271 
272         String result = interpolator.interpolate( "This is a test $$@{key}." );
273 
274         assertEquals( "This is a test @{key}.", result );
275     }
276 
277     public void testEscapeWithLongEscapeStrAtStart()
278         throws InterpolationException
279     {
280         Properties p = new Properties();
281         p.setProperty( "key", "value" );
282 
283         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
284         interpolator.setEscapeString( "$$" );
285         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
286 
287         String result = interpolator.interpolate( "$$@{key} This is a test." );
288 
289         assertEquals( "@{key} This is a test.", result );
290     }
291 
292     public void testNotEscapeWithLongEscapeStrAtStart()
293         throws InterpolationException
294     {
295         Properties p = new Properties();
296         p.setProperty( "key", "value" );
297 
298         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
299         interpolator.setEscapeString( "$$" );
300         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
301 
302         String result = interpolator.interpolate( "@{key} This is a test." );
303 
304         assertEquals( "value This is a test.", result );
305     }
306 
307     public void testEscapeNotFailWithNullEscapeStr()
308         throws InterpolationException
309     {
310         Properties p = new Properties();
311         p.setProperty( "key", "value" );
312 
313         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
314         interpolator.setEscapeString( null );
315         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
316 
317         String result = interpolator.interpolate( "This is a test @{key}." );
318 
319         assertEquals( "This is a test value.", result );
320     }
321 
322     public void testOnlyEscapeExprAtStart()
323         throws InterpolationException
324     {
325         Properties p = new Properties();
326         p.setProperty( "key", "value" );
327 
328         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
329         interpolator.setEscapeString( "\\" );
330         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
331 
332         String result = interpolator.interpolate( "\\@{key} This is a test." );
333 
334         assertEquals( "@{key} This is a test.", result );
335     }
336 
337     public void testNotEscapeExprAtStart()
338         throws InterpolationException
339     {
340         Properties p = new Properties();
341         p.setProperty( "key", "value" );
342 
343         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
344         interpolator.setEscapeString( "\\" );
345         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
346 
347         String result = interpolator.interpolate( "@{key} This is a test." );
348 
349         assertEquals( "value This is a test.", result );
350     }
351 
352     public void testEscapeExprAtStart()
353         throws InterpolationException
354     {
355         Properties p = new Properties();
356         p.setProperty( "key", "value" );
357 
358         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@", "@" );
359         interpolator.setEscapeString( "\\" );
360         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
361 
362         String result = interpolator.interpolate( "\\@key@ This is a test @key@." );
363 
364         assertEquals( "@key@ This is a test value.", result );
365     }
366 
367     public void testNPEFree()
368         throws InterpolationException
369     {
370         Properties p = new Properties();
371         p.setProperty( "key", "value" );
372 
373         StringSearchInterpolator interpolator = new StringSearchInterpolator( "@{", "}" );
374         interpolator.setEscapeString( "\\" );
375         interpolator.addValueSource( new PropertiesBasedValueSource( p ) );
376 
377         String result = interpolator.interpolate( null );
378 
379         assertEquals( "", result );
380     }
381 
382     public void testInterruptedInterpolate()
383         throws InterpolationException
384     {
385         Interpolator interpolator = new StringSearchInterpolator();
386         RecursionInterceptor recursionInterceptor = new SimpleRecursionInterceptor();
387         final boolean[] error = new boolean[] { false };
388         interpolator.addValueSource( new ValueSource()
389         {
390             public Object getValue( String expression ) {
391                 if ( expression.equals( "key" ) )
392                 {
393                     if ( error[ 0 ] )
394                     {
395                         throw new IllegalStateException( "broken" );
396                     }
397                     return "val";
398                 }
399                 else
400                 {
401                     return null;
402                 }
403             }
404             public List getFeedback()
405             {
406                 return Collections.EMPTY_LIST;
407             }
408             public void clearFeedback()
409             {
410             }
411         } );
412         assertEquals( "control case", "-val-" , interpolator.interpolate( "-${key}-", recursionInterceptor ) );
413         error[ 0 ] = true;
414         try
415         {
416             interpolator.interpolate( "-${key}-", recursionInterceptor );
417             fail( "should have thrown exception" );
418         }
419         catch ( IllegalStateException x )
420         {
421             // right
422         }
423         error[ 0 ] = false;
424         assertEquals( "should not believe there is a cycle here", "-val-", interpolator.interpolate( "-${key}-", recursionInterceptor ) );
425     }
426 
427     public String getVar()
428     {
429         return "testVar";
430     }
431 
432 }