1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29
30
31
32
33
34 public class InterpolationFilterReaderTest {
35
36
37
38
39
40
41
42
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
56
57
58
59
60
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
74
75
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
89
90
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
104
105
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
119
120
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
135
136
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
151
152
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
167
168
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
183
184
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
199
200
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 }