View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.text;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.net.InetAddress;
25  import java.net.UnknownHostException;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.text.lookup.DefaultStringLookup;
30  import org.apache.commons.text.lookup.StringLookup;
31  import org.apache.commons.text.lookup.StringLookupFactory;
32  import org.junit.jupiter.api.Disabled;
33  import org.junit.jupiter.api.Test;
34  import org.junitpioneer.jupiter.SetEnvironmentVariable;
35  
36  class StringSubstitutorWithInterpolatorStringLookupTest {
37  
38      private static StringLookup createInterpolatorWithLookups(final DefaultStringLookup... lookups) {
39          final Map<String, StringLookup> lookupMap = new HashMap<>();
40          for (final DefaultStringLookup lookup : lookups) {
41              lookupMap.put(lookup.getKey().toLowerCase(), lookup.getStringLookup());
42          }
43  
44          return StringLookupFactory.INSTANCE.interpolatorStringLookup(lookupMap, null, false);
45      }
46  
47      @Test
48      void testCustomFunctionWithDefaults() {
49          testCustomFunctionWithDefaults(true);
50      }
51  
52      private void testCustomFunctionWithDefaults(final boolean addDefaultLookups) {
53          final String key = "key";
54          final String value = "value";
55          final Map<String, String> map = new HashMap<>();
56          map.put(key, value);
57          final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.functionStringLookup(map::get);
58          final Map<String, StringLookup> stringLookupMap = new HashMap<>();
59          stringLookupMap.put("customLookup", mapStringLookup);
60          final StringSubstitutor strSubst = new StringSubstitutor(
61              StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
62          if (addDefaultLookups) {
63              final String spKey = "user.name";
64              assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
65          }
66          assertEquals("value", strSubst.replace("${customLookup:key}"));
67      }
68  
69      @Test
70      void testCustomFunctionWithoutDefaults() {
71          testCustomFunctionWithDefaults(false);
72      }
73  
74      @Test
75      void testCustomMapWithDefaults() {
76          testCustomMapWithDefaults(true);
77      }
78  
79      private void testCustomMapWithDefaults(final boolean addDefaultLookups) {
80          final String key = "key";
81          final String value = "value";
82          final Map<String, String> map = new HashMap<>();
83          map.put(key, value);
84          final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.mapStringLookup(map);
85          final Map<String, StringLookup> stringLookupMap = new HashMap<>();
86          stringLookupMap.put("customLookup", mapStringLookup);
87          final StringSubstitutor strSubst = new StringSubstitutor(
88              StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
89          if (addDefaultLookups) {
90              final String spKey = "user.name";
91              assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
92          }
93          assertEquals("value", strSubst.replace("${customLookup:key}"));
94          assertEquals("${UnknownLookup:key}", strSubst.replace("${UnknownLookup:key}"));
95      }
96  
97      @Test
98      void testCustomMapWithoutDefaults() {
99          testCustomMapWithDefaults(false);
100     }
101     @Test
102     void testDefaultInterpolator() {
103         // Used to cut and paste into the docs.
104         // @formatter:off
105         final StringSubstitutor interpolator = StringSubstitutor.createInterpolator();
106         final String text = interpolator.replace(
107                 "Base64 Decoder:        ${base64Decoder:SGVsbG9Xb3JsZCE=}\n"
108               + "Base64 Encoder:        ${base64Encoder:HelloWorld!}\n"
109               + "Java Constant:         ${const:java.awt.event.KeyEvent.VK_ESCAPE}\n"
110               + "Date:                  ${date:yyyy-MM-dd}\n"
111               + "Environment Variable:  ${env:USERNAME}\n"
112               + "File Content:          ${file:UTF-8:src/test/resources/org/apache/commons/text/document.properties}\n"
113               + "Java:                  ${java:version}\n"
114               + "Localhost:             ${localhost:canonical-name}\n"
115               + "Properties File:       ${properties:src/test/resources/org/apache/commons/text/document.properties::mykey}\n"
116               + "Resource Bundle:       ${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}\n"
117               + "System Property:       ${sys:user.dir}\n"
118               + "URL Decoder:           ${urlDecoder:Hello%20World%21}\n"
119               + "URL Encoder:           ${urlEncoder:Hello World!}\n"
120               + "XML XPath:             ${xml:src/test/resources/org/apache/commons/text/document.xml:/root/path/to/node}\n"
121         );
122         // @formatter:on
123         assertNotNull(text);
124         // TEXT-171:
125         assertFalse(text.contains("${base64Decoder:SGVsbG9Xb3JsZCE=}"));
126         assertFalse(text.contains("${base64Encoder:HelloWorld!}"));
127         assertFalse(text.contains("${urlDecoder:Hello%20World%21}"));
128         assertFalse(text.contains("${urlEncoder:Hello World!}"));
129         assertFalse(text.contains("${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}"));
130     }
131 
132     @Test
133     void testDefaultValueForMissingKeyInResourceBundle() {
134         final StringLookup interpolatorStringLookup = StringLookupFactory.INSTANCE.interpolatorStringLookup(
135             StringLookupFactory.INSTANCE.resourceBundleStringLookup("org.apache.commons.text.example.testResourceBundleLookup"));
136         assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.apply("keyWithMissingKey"));
137         assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.lookup("keyWithMissingKey"));
138         final StringSubstitutor stringSubstitutor = new StringSubstitutor(interpolatorStringLookup);
139         // The following would throw a MissingResourceException before TEXT-165.
140         assertEquals("defaultValue", stringSubstitutor.replace("${keyWithMissingKey}"));
141     }
142 
143     @Test
144     void testDnsLookup() throws UnknownHostException {
145         final StringSubstitutor strSubst =
146                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
147         final String hostName = InetAddress.getLocalHost().getHostName();
148         assertEquals(InetAddress.getByName(hostName).getHostAddress(),
149             strSubst.replace("${dns:" + hostName + "}"));
150     }
151 
152     @Test
153     void testDnsLookup_disabledByDefault() throws UnknownHostException {
154         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
155         final String hostName = InetAddress.getLocalHost().getHostName();
156         final String input = "${dns:" + hostName + "}";
157         assertEquals(input, strSubst.replace(input));
158     }
159 
160     @Test
161     void testDnsLookupAddress() throws UnknownHostException {
162         final StringSubstitutor strSubst =
163                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
164         assertEquals(InetAddress.getByName("apache.org").getHostAddress(),
165             strSubst.replace("${dns:address|apache.org}"));
166     }
167 
168     @Test
169     void testDnsLookupCanonicalName() throws UnknownHostException {
170         final StringSubstitutor strSubst =
171                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
172         final String address = InetAddress.getLocalHost().getHostAddress();
173         final InetAddress inetAddress = InetAddress.getByName(address);
174         assertEquals(inetAddress.getCanonicalHostName(),
175             strSubst.replace("${dns:canonical-name|" + address + "}"));
176     }
177 
178     @Test
179     void testDnsLookupName() throws UnknownHostException {
180         final StringSubstitutor strSubst =
181                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
182         final String address = InetAddress.getLocalHost().getHostAddress();
183         final InetAddress inetAddress = InetAddress.getByName(address);
184         assertEquals(inetAddress.getHostName(), strSubst.replace("${dns:name|" + address + "}"));
185     }
186 
187     @Test
188     void testDnsLookupNameUntrimmed() throws UnknownHostException {
189         final StringSubstitutor strSubst =
190                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
191         final String address = InetAddress.getLocalHost().getHostAddress();
192         final InetAddress inetAddress = InetAddress.getByName(address);
193         assertEquals(inetAddress.getHostName(), strSubst.replace("${dns:name| " + address + " }"));
194     }
195 
196     @Test
197     void testDnsLookupUnknown() {
198         final StringSubstitutor strSubst =
199                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
200         final String unknown = "${dns: u n k n o w n}";
201         assertEquals(unknown, strSubst.replace(unknown));
202     }
203 
204     @Test
205     void testJavaScript() {
206         final StringSubstitutor strSubst =
207                 new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.SCRIPT));
208 
209         assertEquals("Hello World!", strSubst.replace("${script:javascript:\"Hello World!\"}"));
210         assertEquals("7", strSubst.replace("${script:javascript:3 + 4}"));
211     }
212 
213     @Test
214     void testJavaScript_disabledByDefault() {
215         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
216 
217         assertEquals("${script:javascript:3 + 4}", strSubst.replace("${script:javascript:3 + 4}"));
218     }
219 
220     @Test
221     void testLocalHostLookup_Address() throws UnknownHostException {
222         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
223         assertEquals(InetAddress.getLocalHost().getHostAddress(), strSubst.replace("${localhost:address}"));
224     }
225 
226     @Test
227     void testLocalHostLookup_CanonicalName() throws UnknownHostException {
228         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
229         assertEquals(InetAddress.getLocalHost().getCanonicalHostName(),
230             strSubst.replace("${localhost:canonical-name}"));
231     }
232 
233     @Test
234     void testLocalHostLookup_Name() throws UnknownHostException {
235         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
236         assertEquals(InetAddress.getLocalHost().getHostName(), strSubst.replace("${localhost:name}"));
237     }
238 
239     @Test
240     void testMapAndSystemProperty() {
241         final String key = "key";
242         final String value = "value";
243         final Map<String, String> map = new HashMap<>();
244         map.put(key, value);
245         final StringSubstitutor strSubst = new StringSubstitutor(
246             StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
247         final String spKey = "user.name";
248         assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
249         assertEquals(value, strSubst.replace("${" + key + "}"));
250     }
251 
252     @Test
253     void testSystemProperty() {
254         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
255         final String spKey = "user.name";
256         assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
257     }
258 
259     @Test
260     void testSystemPropertyDefaultDefault() {
261         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
262         strSubst.setEnableSubstitutionInVariables(true);
263         final String spKey = "user.name";
264         // check that "sys" works
265         final String spLookupStr = "${sys:" + spKey + "}";
266         final String actual = System.getProperty(spKey);
267         assertEquals(actual, strSubst.replace(spLookupStr));
268         // test "sys" with default value
269         assertEquals("foo", strSubst.replace("${sys:unknownkey1:-foo}"));
270         assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-foo}}"));
271         assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-foo}}}"));
272         assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}"));
273         assertEquals("foo", strSubst.replace("${env:UNKNOWN_KEY:-${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}}"));
274         assertEquals(actual, strSubst.replace("${sys:unknownkey1:-" + spLookupStr + "}"));
275         assertEquals(actual, strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-" + spLookupStr + "}}"));
276     }
277 
278     /**
279      * In our POM. the module {@code java.base} must open "opens java.lang" for the {@link SetEnvironmentVariable} annotation to work. Otherwise, the test will
280      * fail with an IllegalAccessException.
281      */
282     @Disabled("Needs java.base to open 'opens java.lang' for SetEnvironmentVariable.")
283     @Test
284     @SetEnvironmentVariable(key = "testSystemPropertyDefaultDefault", value = "123456789")
285     void testSystemPropertyDefaultDefaultEnv() {
286         final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
287         assertEquals("123456789", strSubst
288                 .replace("${env:testSystemPropertyDefaultDefault:-${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}}"));
289     }
290 
291     @Test
292     void testSystemPropertyDefaultStringLookup() {
293         final StringSubstitutor strSubst = new StringSubstitutor(
294             StringLookupFactory.INSTANCE.interpolatorStringLookup(StringLookupFactory.INSTANCE.systemPropertyStringLookup()));
295         final String spKey = "user.name";
296         assertEquals(System.getProperty(spKey), strSubst.replace("${" + spKey + "}"));
297         assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
298     }
299 }