View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The 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.StringReader;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  
27  /**
28   * <p>InterpolationFilterReaderTest class.</p>
29   *
30   * @author herve
31   * @version $Id: $Id
32   * @since 3.4.0
33   */
34  public class InterpolationFilterReaderTest {
35      /*
36       * Added and commented by jdcasey@03-Feb-2005 because it is a bug in the InterpolationFilterReader.
37       * kenneyw@15-04-2005 fixed the bug.
38       */
39      /**
40       * <p>testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken.</p>
41       *
42       * @throws java.lang.Exception if any.
43       */
44      @Test
45      public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
46          Map<String, String> m = new HashMap<String, String>();
47          m.put("test", "TestValue");
48  
49          String testStr = "This is a ${test";
50  
51          assertEquals("This is a ${test", interpolate(testStr, m));
52      }
53  
54      /*
55       * kenneyw@14-04-2005 Added test to check above fix.
56       */
57      /**
58       * <p>testShouldNotInterpolateExpressionWithMissingEndToken.</p>
59       *
60       * @throws java.lang.Exception if any.
61       */
62      @org.junit.jupiter.api.Test
63      public void testShouldNotInterpolateExpressionWithMissingEndToken() throws Exception {
64          Map<String, String> m = new HashMap<String, String>();
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      /**
73       * <p>testShouldNotInterpolateWithMalformedStartToken.</p>
74       *
75       * @throws java.lang.Exception if any.
76       */
77      @Test
78      public void testShouldNotInterpolateWithMalformedStartToken() throws Exception {
79          Map<String, String> m = new HashMap<String, String>();
80          m.put("test", "testValue");
81  
82          String foo = "This is a $!test} again";
83  
84          assertEquals("This is a $!test} again", interpolate(foo, m));
85      }
86  
87      /**
88       * <p>testShouldNotInterpolateWithMalformedEndToken.</p>
89       *
90       * @throws java.lang.Exception if any.
91       */
92      @Test
93      public void testShouldNotInterpolateWithMalformedEndToken() throws Exception {
94          Map<String, String> m = new HashMap<String, String>();
95          m.put("test", "testValue");
96  
97          String foo = "This is a ${test!} again";
98  
99          assertEquals("This is a ${test!} again", interpolate(foo, m, "${", "$}"));
100     }
101 
102     /**
103      * <p>testInterpolationWithMulticharDelimiters.</p>
104      *
105      * @throws java.lang.Exception if any.
106      */
107     @Test
108     public void testInterpolationWithMulticharDelimiters() throws Exception {
109         Map<String, String> m = new HashMap<String, String>();
110         m.put("test", "testValue");
111 
112         String foo = "This is a ${test$} again";
113 
114         assertEquals("This is a testValue again", interpolate(foo, m, "${", "$}"));
115     }
116 
117     /**
118      * <p>testDefaultInterpolationWithNonInterpolatedValueAtEnd.</p>
119      *
120      * @throws java.lang.Exception if any.
121      */
122     @Test
123     public void testDefaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
124         Map<String, String> m = new HashMap<String, String>();
125         m.put("name", "jason");
126         m.put("noun", "asshole");
127 
128         String foo = "${name} is an ${noun}. ${not.interpolated}";
129 
130         assertEquals("jason is an asshole. ${not.interpolated}", interpolate(foo, m));
131     }
132 
133     /**
134      * <p>testDefaultInterpolationWithInterpolatedValueAtEnd.</p>
135      *
136      * @throws java.lang.Exception if any.
137      */
138     @Test
139     public void testDefaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
140         Map<String, String> m = new HashMap<String, String>();
141         m.put("name", "jason");
142         m.put("noun", "asshole");
143 
144         String foo = "${name} is an ${noun}";
145 
146         assertEquals("jason is an asshole", interpolate(foo, m));
147     }
148 
149     /**
150      * <p>testInterpolationWithSpecifiedBoundaryTokens.</p>
151      *
152      * @throws java.lang.Exception if any.
153      */
154     @Test
155     public void testInterpolationWithSpecifiedBoundaryTokens() throws Exception {
156         Map<String, String> m = new HashMap<String, String>();
157         m.put("name", "jason");
158         m.put("noun", "asshole");
159 
160         String foo = "@name@ is an @noun@. @not.interpolated@ baby @foo@. @bar@";
161 
162         assertEquals("jason is an asshole. @not.interpolated@ baby @foo@. @bar@", interpolate(foo, m, "@", "@"));
163     }
164 
165     /**
166      * <p>testInterpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd.</p>
167      *
168      * @throws java.lang.Exception if any.
169      */
170     @Test
171     public void testInterpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() throws Exception {
172         Map<String, String> m = new HashMap<String, String>();
173         m.put("name", "jason");
174         m.put("noun", "asshole");
175 
176         String foo = "@name@ is an @foobarred@";
177 
178         assertEquals("jason is an @foobarred@", interpolate(foo, m, "@", "@"));
179     }
180 
181     /**
182      * <p>testInterpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd.</p>
183      *
184      * @throws java.lang.Exception if any.
185      */
186     @Test
187     public void testInterpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws Exception {
188         Map<String, String> m = new HashMap<String, String>();
189         m.put("name", "jason");
190         m.put("noun", "asshole");
191 
192         String foo = "@name@ is an @noun@";
193 
194         assertEquals("jason is an asshole", interpolate(foo, m, "@", "@"));
195     }
196 
197     /**
198      * <p>testInterpolationWithSpecifiedBoundaryTokensAndAdditionalTokenCharacter.</p>
199      *
200      * @throws java.lang.Exception if any.
201      */
202     @Test
203     public void testInterpolationWithSpecifiedBoundaryTokensAndAdditionalTokenCharacter() throws Exception {
204         Map<String, String> m = new HashMap<String, String>();
205         m.put("name", "jason");
206         m.put("noun", "asshole");
207 
208         String foo = "@name@ (known as jason@somewhere) is an @noun@";
209 
210         assertEquals("jason (known as jason@somewhere) is an asshole", interpolate(foo, m, "@", "@"));
211     }
212 
213     // ----------------------------------------------------------------------
214     //
215     // ----------------------------------------------------------------------
216 
217     private String interpolate(String input, Map context) throws Exception {
218         return IOUtil.toString(new InterpolationFilterReader(new StringReader(input), context));
219     }
220 
221     private String interpolate(String input, Map context, String startToken, String endToken) throws Exception {
222         return IOUtil.toString(new InterpolationFilterReader(new StringReader(input), context, startToken, endToken));
223     }
224 }