View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.StringReader;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import junit.framework.TestCase;
34  
35  /**
36   * InterpolatorFilterReaderTest, heavily based on InterpolationFilterReaderTest. Heh, even the test strings remained the
37   * same!
38   * 
39   * @author cstamas
40   * 
41   */
42  public class InterpolatorFilterReaderTest
43      extends TestCase
44  {
45      /*
46       * Added and commented by jdcasey@03-Feb-2005 because it is a bug in the InterpolationFilterReader.
47       * kenneyw@15-04-2005 fixed the bug.
48       */
49      public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken()
50          throws Exception
51      {
52          Map m = new HashMap();
53          m.put( "test", "TestValue" );
54  
55          String testStr = "This is a ${test";
56  
57          assertEquals( "This is a ${test", interpolate( testStr, m ) );
58      }
59  
60      /*
61       * kenneyw@14-04-2005 Added test to check above fix.
62       */
63      public void testShouldNotInterpolateExpressionWithMissingEndToken()
64          throws Exception
65      {
66          Map m = new HashMap();
67          m.put( "test", "TestValue" );
68  
69          String testStr = "This is a ${test, really";
70  
71          assertEquals( "This is a ${test, really", interpolate( testStr, m ) );
72      }
73  
74      public void testShouldNotInterpolateWithMalformedStartToken()
75          throws Exception
76      {
77          Map m = new HashMap();
78          m.put( "test", "testValue" );
79  
80          String foo = "This is a $!test} again";
81  
82          assertEquals( "This is a $!test} again", interpolate( foo, m ) );
83      }
84  
85      public void testShouldNotInterpolateWithMalformedEndToken()
86          throws Exception
87      {
88          Map m = new HashMap();
89          m.put( "test", "testValue" );
90  
91          String foo = "This is a ${test!} again";
92  
93          assertEquals( "This is a ${test!} again", interpolate( foo, m ) );
94      }
95  
96      public void testDefaultInterpolationWithNonInterpolatedValueAtEnd()
97          throws Exception
98      {
99          Map m = new HashMap();
100         m.put( "name", "jason" );
101         m.put( "noun", "asshole" );
102 
103         String foo = "${name} is an ${noun}. ${not.interpolated}";
104 
105         assertEquals( "jason is an asshole. ${not.interpolated}", interpolate( foo, m ) );
106     }
107 
108     public void testDefaultInterpolationWithInterpolatedValueAtEnd()
109         throws Exception
110     {
111         Map m = new HashMap();
112         m.put( "name", "jason" );
113         m.put( "noun", "asshole" );
114 
115         String foo = "${name} is an ${noun}";
116 
117         assertEquals( "jason is an asshole", interpolate( foo, m ) );
118     }
119 
120     public void testInterpolationWithInterpolatedValueAtEndWithCustomToken()
121         throws Exception
122     {
123         Map m = new HashMap();
124         m.put( "name", "jason" );
125         m.put( "noun", "asshole" );
126 
127         String foo = "@{name} is an @{noun}";
128 
129         assertEquals( "jason is an asshole", interpolate( foo, m, "@{", "}" ) );
130     }
131 
132     public void testInterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString()
133         throws Exception
134     {
135         Map m = new HashMap();
136         m.put( "name", "jason" );
137         m.put( "noun", "asshole" );
138 
139         String foo = "@name@ is an @noun@";
140 
141         assertEquals( "jason is an asshole", interpolate( foo, m, "@", "@" ) );
142     }
143 
144     public void testEscape()
145         throws Exception
146     {
147         Map m = new HashMap();
148         m.put( "name", "jason" );
149         m.put( "noun", "asshole" );
150 
151         String foo = "${name} is an \\${noun}";
152 
153         assertEquals( "jason is an ${noun}", interpolate( foo, m, "\\" ) );
154     }
155 
156     public void testEscapeAtStart()
157         throws Exception
158     {
159         Map m = new HashMap();
160         m.put( "name", "jason" );
161         m.put( "noun", "asshole" );
162 
163         String foo = "\\${name} is an \\${noun}";
164 
165         assertEquals( "${name} is an ${noun}", interpolate( foo, m, "\\" ) );
166     }
167 
168     public void testEscapeOnlyAtStart()
169         throws Exception
170     {
171         Map m = new HashMap();
172         m.put( "name", "jason" );
173         m.put( "noun", "asshole" );
174 
175         String foo = "\\@name@ is an @noun@";
176 
177         String result = interpolate( foo, m, "@", "@" );
178         assertEquals( "@name@ is an asshole", result );
179     }
180 
181     public void testEscapeOnlyAtStartDefaultToken()
182         throws Exception
183     {
184         Map m = new HashMap();
185         m.put( "name", "jason" );
186         m.put( "noun", "asshole" );
187 
188         String foo = "\\${name} is an ${noun}";
189 
190         String result = interpolate( foo, m, "${", "}" );
191         assertEquals( "${name} is an asshole", result );
192     }
193 
194     public void testShouldDetectRecursiveExpressionPassingThroughTwoPrefixes()
195         throws Exception
196     {
197         List prefixes = new ArrayList();
198 
199         prefixes.add( "prefix1" );
200         prefixes.add( "prefix2" );
201 
202         RecursionInterceptor ri = new PrefixAwareRecursionInterceptor( prefixes, false );
203 
204         Map context = new HashMap();
205         context.put( "name", "${prefix2.name}" );
206 
207         String input = "${prefix1.name}";
208 
209         StringSearchInterpolator interpolator = new StringSearchInterpolator();
210 
211         interpolator.addValueSource( new MapBasedValueSource( context ) );
212 
213         InterpolatorFilterReader r = new InterpolatorFilterReader( new StringReader( input ), interpolator, ri );
214         r.setInterpolateWithPrefixPattern( false );
215         r.setEscapeString( "\\" );
216         StringBuilder buf = new StringBuilder();
217         int read = -1;
218         char[] cbuf = new char[1024];
219         while ( ( read = r.read( cbuf ) ) > -1 )
220         {
221             buf.append( cbuf, 0, read );
222         }
223 
224         assertEquals( input, buf.toString() );
225     }
226 
227     public void testShouldDetectRecursiveExpressionWithPrefixAndWithout()
228         throws Exception
229     {
230         List prefixes = new ArrayList();
231 
232         prefixes.add( "prefix1" );
233 
234         RecursionInterceptor ri = new PrefixAwareRecursionInterceptor( prefixes, false );
235 
236         Map context = new HashMap();
237         context.put( "name", "${prefix1.name}" );
238 
239         String input = "${name}";
240 
241         StringSearchInterpolator interpolator = new StringSearchInterpolator();
242 
243         interpolator.addValueSource( new MapBasedValueSource( context ) );
244 
245         InterpolatorFilterReader r = new InterpolatorFilterReader( new StringReader( input ), interpolator, ri );
246         r.setInterpolateWithPrefixPattern( false );
247         r.setEscapeString( "\\" );
248         StringBuilder buf = new StringBuilder();
249         int read = -1;
250         char[] cbuf = new char[1024];
251         while ( ( read = r.read( cbuf ) ) > -1 )
252         {
253             buf.append( cbuf, 0, read );
254         }
255 
256         assertEquals( "${prefix1.name}", buf.toString() );
257     }
258 
259     // ----------------------------------------------------------------------
260     //
261     // ----------------------------------------------------------------------
262 
263     private String interpolate( String input, Map context )
264         throws Exception
265     {
266         return interpolate( input, context, null );
267     }
268 
269     private String interpolate( String input, Map context, String escapeStr )
270         throws Exception
271     {
272         Interpolator interpolator = new StringSearchInterpolator();
273 
274         interpolator.addValueSource( new MapBasedValueSource( context ) );
275 
276         InterpolatorFilterReader r = new InterpolatorFilterReader( new StringReader( input ), interpolator );
277         r.setInterpolateWithPrefixPattern( false );
278         if ( escapeStr != null )
279         {
280             r.setEscapeString( escapeStr );
281         }
282         StringBuilder buf = new StringBuilder();
283         int read = -1;
284         char[] cbuf = new char[1024];
285         while ( ( read = r.read( cbuf ) ) > -1 )
286         {
287             buf.append( cbuf, 0, read );
288         }
289 
290         return buf.toString();
291     }
292 
293     private String interpolate( String input, Map context, String beginToken, String endToken )
294         throws Exception
295     {
296         StringSearchInterpolator interpolator = new StringSearchInterpolator( beginToken, endToken );
297 
298         interpolator.addValueSource( new MapBasedValueSource( context ) );
299 
300         InterpolatorFilterReader r = new InterpolatorFilterReader( new StringReader( input ), interpolator, beginToken, endToken );
301         r.setInterpolateWithPrefixPattern( false );
302         r.setEscapeString( "\\" );
303         StringBuilder buf = new StringBuilder();
304         int read = -1;
305         char[] cbuf = new char[1024];
306         while ( ( read = r.read( cbuf ) ) > -1 )
307         {
308             buf.append( cbuf, 0, read );
309         }
310 
311         return buf.toString();
312     }
313 
314 }