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 org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  
37  /**
38   * InterpolatorFilterReaderTest, heavily based on InterpolationFilterReaderTest. Heh, even the test strings remained the
39   * same!
40   *
41   * @author cstamas
42   *
43   */
44  public class InterpolatorFilterReaderTest {
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      @Test
50      public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
51          Map m = new HashMap();
52          m.put("test", "TestValue");
53  
54          String testStr = "This is a ${test";
55  
56          assertEquals("This is a ${test", interpolate(testStr, m));
57      }
58  
59      /*
60       * kenneyw@14-04-2005 Added test to check above fix.
61       */
62      @Test
63      public void testShouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
64          Map m = new HashMap();
65          m.put("test", "TestValue");
66  
67          String testStr = "This is a ${test, really";
68  
69          assertEquals("This is a ${test, really", interpolate(testStr, m));
70      }
71  
72      @Test
73      public void testShouldNotInterpolateWithMalformedStartToken() throws Exception {
74          Map m = new HashMap();
75          m.put("test", "testValue");
76  
77          String foo = "This is a $!test} again";
78  
79          assertEquals("This is a $!test} again", interpolate(foo, m));
80      }
81  
82      @Test
83      public void testShouldNotInterpolateWithMalformedEndToken() throws Exception {
84          Map m = new HashMap();
85          m.put("test", "testValue");
86  
87          String foo = "This is a ${test!} again";
88  
89          assertEquals("This is a ${test!} again", interpolate(foo, m));
90      }
91  
92      @Test
93      public void testDefaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
94          Map m = new HashMap();
95          m.put("name", "jason");
96          m.put("noun", "asshole");
97  
98          String foo = "${name} is an ${noun}. ${not.interpolated}";
99  
100         assertEquals("jason is an asshole. ${not.interpolated}", interpolate(foo, m));
101     }
102 
103     @Test
104     public void testDefaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
105         Map m = new HashMap();
106         m.put("name", "jason");
107         m.put("noun", "asshole");
108 
109         String foo = "${name} is an ${noun}";
110 
111         assertEquals("jason is an asshole", interpolate(foo, m));
112     }
113 
114     @Test
115     public void testInterpolationWithInterpolatedValueAtEndWithCustomToken() throws Exception {
116         Map m = new HashMap();
117         m.put("name", "jason");
118         m.put("noun", "asshole");
119 
120         String foo = "@{name} is an @{noun}";
121 
122         assertEquals("jason is an asshole", interpolate(foo, m, "@{", "}"));
123     }
124 
125     @Test
126     public void testInterpolationWithInterpolatedValueAtEndWithCustomTokenAndCustomString() throws Exception {
127         Map m = new HashMap();
128         m.put("name", "jason");
129         m.put("noun", "asshole");
130 
131         String foo = "@name@ is an @noun@";
132 
133         assertEquals("jason is an asshole", interpolate(foo, m, "@", "@"));
134     }
135 
136     @Test
137     public void testEscape() throws Exception {
138         Map m = new HashMap();
139         m.put("name", "jason");
140         m.put("noun", "asshole");
141 
142         String foo = "${name} is an \\${noun}";
143 
144         assertEquals("jason is an ${noun}", interpolate(foo, m, "\\"));
145     }
146 
147     @Test
148     public void testEscapeAtStart() throws Exception {
149         Map m = new HashMap();
150         m.put("name", "jason");
151         m.put("noun", "asshole");
152 
153         String foo = "\\${name} is an \\${noun}";
154 
155         assertEquals("${name} is an ${noun}", interpolate(foo, m, "\\"));
156     }
157 
158     @Test
159     public void testEscapeOnlyAtStart() throws Exception {
160         Map m = new HashMap();
161         m.put("name", "jason");
162         m.put("noun", "asshole");
163 
164         String foo = "\\@name@ is an @noun@";
165 
166         String result = interpolate(foo, m, "@", "@");
167         assertEquals("@name@ is an asshole", result);
168     }
169 
170     @Test
171     public void testEscapeOnlyAtStartDefaultToken() throws Exception {
172         Map m = new HashMap();
173         m.put("name", "jason");
174         m.put("noun", "asshole");
175 
176         String foo = "\\${name} is an ${noun}";
177 
178         String result = interpolate(foo, m, "${", "}");
179         assertEquals("${name} is an asshole", result);
180     }
181 
182     @Test
183     public void testShouldDetectRecursiveExpressionPassingThroughTwoPrefixes() throws Exception {
184         List prefixes = new ArrayList();
185 
186         prefixes.add("prefix1");
187         prefixes.add("prefix2");
188 
189         RecursionInterceptor ri = new PrefixAwareRecursionInterceptor(prefixes, false);
190 
191         Map context = new HashMap();
192         context.put("name", "${prefix2.name}");
193 
194         String input = "${prefix1.name}";
195 
196         StringSearchInterpolator interpolator = new StringSearchInterpolator();
197 
198         interpolator.addValueSource(new MapBasedValueSource(context));
199 
200         InterpolatorFilterReader r = new InterpolatorFilterReader(new StringReader(input), interpolator, ri);
201         r.setInterpolateWithPrefixPattern(false);
202         r.setEscapeString("\\");
203         StringBuilder buf = new StringBuilder();
204         int read = -1;
205         char[] cbuf = new char[1024];
206         while ((read = r.read(cbuf)) > -1) {
207             buf.append(cbuf, 0, read);
208         }
209 
210         assertEquals(input, buf.toString());
211     }
212 
213     @Test
214     public void testShouldDetectRecursiveExpressionWithPrefixAndWithout() throws Exception {
215         List prefixes = new ArrayList();
216 
217         prefixes.add("prefix1");
218 
219         RecursionInterceptor ri = new PrefixAwareRecursionInterceptor(prefixes, false);
220 
221         Map context = new HashMap();
222         context.put("name", "${prefix1.name}");
223 
224         String input = "${name}";
225 
226         StringSearchInterpolator interpolator = new StringSearchInterpolator();
227 
228         interpolator.addValueSource(new MapBasedValueSource(context));
229 
230         InterpolatorFilterReader r = new InterpolatorFilterReader(new StringReader(input), interpolator, ri);
231         r.setInterpolateWithPrefixPattern(false);
232         r.setEscapeString("\\");
233         StringBuilder buf = new StringBuilder();
234         int read = -1;
235         char[] cbuf = new char[1024];
236         while ((read = r.read(cbuf)) > -1) {
237             buf.append(cbuf, 0, read);
238         }
239 
240         assertEquals("${prefix1.name}", buf.toString());
241     }
242 
243     // ----------------------------------------------------------------------
244     //
245     // ----------------------------------------------------------------------
246 
247     private String interpolate(String input, Map context) throws Exception {
248         return interpolate(input, context, null);
249     }
250 
251     private String interpolate(String input, Map context, String escapeStr) throws Exception {
252         Interpolator interpolator = new StringSearchInterpolator();
253 
254         interpolator.addValueSource(new MapBasedValueSource(context));
255 
256         InterpolatorFilterReader r = new InterpolatorFilterReader(new StringReader(input), interpolator);
257         r.setInterpolateWithPrefixPattern(false);
258         if (escapeStr != null) {
259             r.setEscapeString(escapeStr);
260         }
261         StringBuilder buf = new StringBuilder();
262         int read = -1;
263         char[] cbuf = new char[1024];
264         while ((read = r.read(cbuf)) > -1) {
265             buf.append(cbuf, 0, read);
266         }
267 
268         return buf.toString();
269     }
270 
271     private String interpolate(String input, Map context, String beginToken, String endToken) throws Exception {
272         StringSearchInterpolator interpolator = new StringSearchInterpolator(beginToken, endToken);
273 
274         interpolator.addValueSource(new MapBasedValueSource(context));
275 
276         InterpolatorFilterReader r =
277                 new InterpolatorFilterReader(new StringReader(input), interpolator, beginToken, endToken);
278         r.setInterpolateWithPrefixPattern(false);
279         r.setEscapeString("\\");
280         StringBuilder buf = new StringBuilder();
281         int read = -1;
282         char[] cbuf = new char[1024];
283         while ((read = r.read(cbuf)) > -1) {
284             buf.append(cbuf, 0, read);
285         }
286 
287         return buf.toString();
288     }
289 }