StringSubstitutorWithInterpolatorStringLookupTest.java
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.text.lookup.DefaultStringLookup;
import org.apache.commons.text.lookup.StringLookup;
import org.apache.commons.text.lookup.StringLookupFactory;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
class StringSubstitutorWithInterpolatorStringLookupTest {
private static StringLookup createInterpolatorWithLookups(final DefaultStringLookup... lookups) {
final Map<String, StringLookup> lookupMap = new HashMap<>();
for (final DefaultStringLookup lookup : lookups) {
lookupMap.put(lookup.getKey().toLowerCase(), lookup.getStringLookup());
}
return StringLookupFactory.INSTANCE.interpolatorStringLookup(lookupMap, null, false);
}
@Test
void testCustomFunctionWithDefaults() {
testCustomFunctionWithDefaults(true);
}
private void testCustomFunctionWithDefaults(final boolean addDefaultLookups) {
final String key = "key";
final String value = "value";
final Map<String, String> map = new HashMap<>();
map.put(key, value);
final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.functionStringLookup(map::get);
final Map<String, StringLookup> stringLookupMap = new HashMap<>();
stringLookupMap.put("customLookup", mapStringLookup);
final StringSubstitutor strSubst = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
if (addDefaultLookups) {
final String spKey = "user.name";
assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
assertEquals("value", strSubst.replace("${customLookup:key}"));
}
@Test
void testCustomFunctionWithoutDefaults() {
testCustomFunctionWithDefaults(false);
}
@Test
void testCustomMapWithDefaults() {
testCustomMapWithDefaults(true);
}
private void testCustomMapWithDefaults(final boolean addDefaultLookups) {
final String key = "key";
final String value = "value";
final Map<String, String> map = new HashMap<>();
map.put(key, value);
final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.mapStringLookup(map);
final Map<String, StringLookup> stringLookupMap = new HashMap<>();
stringLookupMap.put("customLookup", mapStringLookup);
final StringSubstitutor strSubst = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
if (addDefaultLookups) {
final String spKey = "user.name";
assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
assertEquals("value", strSubst.replace("${customLookup:key}"));
assertEquals("${UnknownLookup:key}", strSubst.replace("${UnknownLookup:key}"));
}
@Test
void testCustomMapWithoutDefaults() {
testCustomMapWithDefaults(false);
}
@Test
void testDefaultInterpolator() {
// Used to cut and paste into the docs.
// @formatter:off
final StringSubstitutor interpolator = StringSubstitutor.createInterpolator();
final String text = interpolator.replace(
"Base64 Decoder: ${base64Decoder:SGVsbG9Xb3JsZCE=}\n"
+ "Base64 Encoder: ${base64Encoder:HelloWorld!}\n"
+ "Java Constant: ${const:java.awt.event.KeyEvent.VK_ESCAPE}\n"
+ "Date: ${date:yyyy-MM-dd}\n"
+ "Environment Variable: ${env:USERNAME}\n"
+ "File Content: ${file:UTF-8:src/test/resources/org/apache/commons/text/document.properties}\n"
+ "Java: ${java:version}\n"
+ "Localhost: ${localhost:canonical-name}\n"
+ "Properties File: ${properties:src/test/resources/org/apache/commons/text/document.properties::mykey}\n"
+ "Resource Bundle: ${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}\n"
+ "System Property: ${sys:user.dir}\n"
+ "URL Decoder: ${urlDecoder:Hello%20World%21}\n"
+ "URL Encoder: ${urlEncoder:Hello World!}\n"
+ "XML XPath: ${xml:src/test/resources/org/apache/commons/text/document.xml:/root/path/to/node}\n"
);
// @formatter:on
assertNotNull(text);
// TEXT-171:
assertFalse(text.contains("${base64Decoder:SGVsbG9Xb3JsZCE=}"));
assertFalse(text.contains("${base64Encoder:HelloWorld!}"));
assertFalse(text.contains("${urlDecoder:Hello%20World%21}"));
assertFalse(text.contains("${urlEncoder:Hello World!}"));
assertFalse(text.contains("${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}"));
}
@Test
void testDefaultValueForMissingKeyInResourceBundle() {
final StringLookup interpolatorStringLookup = StringLookupFactory.INSTANCE.interpolatorStringLookup(
StringLookupFactory.INSTANCE.resourceBundleStringLookup("org.apache.commons.text.example.testResourceBundleLookup"));
assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.apply("keyWithMissingKey"));
assertEquals("${missingKey:-defaultValue}", interpolatorStringLookup.lookup("keyWithMissingKey"));
final StringSubstitutor stringSubstitutor = new StringSubstitutor(interpolatorStringLookup);
// The following would throw a MissingResourceException before TEXT-165.
assertEquals("defaultValue", stringSubstitutor.replace("${keyWithMissingKey}"));
}
@Test
void testDnsLookup() throws UnknownHostException {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
final String hostName = InetAddress.getLocalHost().getHostName();
assertEquals(InetAddress.getByName(hostName).getHostAddress(),
strSubst.replace("${dns:" + hostName + "}"));
}
@Test
void testDnsLookup_disabledByDefault() throws UnknownHostException {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
final String hostName = InetAddress.getLocalHost().getHostName();
final String input = "${dns:" + hostName + "}";
assertEquals(input, strSubst.replace(input));
}
@Test
void testDnsLookupAddress() throws UnknownHostException {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
assertEquals(InetAddress.getByName("apache.org").getHostAddress(),
strSubst.replace("${dns:address|apache.org}"));
}
@Test
void testDnsLookupCanonicalName() throws UnknownHostException {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
final String address = InetAddress.getLocalHost().getHostAddress();
final InetAddress inetAddress = InetAddress.getByName(address);
assertEquals(inetAddress.getCanonicalHostName(),
strSubst.replace("${dns:canonical-name|" + address + "}"));
}
@Test
void testDnsLookupName() throws UnknownHostException {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
final String address = InetAddress.getLocalHost().getHostAddress();
final InetAddress inetAddress = InetAddress.getByName(address);
assertEquals(inetAddress.getHostName(), strSubst.replace("${dns:name|" + address + "}"));
}
@Test
void testDnsLookupNameUntrimmed() throws UnknownHostException {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
final String address = InetAddress.getLocalHost().getHostAddress();
final InetAddress inetAddress = InetAddress.getByName(address);
assertEquals(inetAddress.getHostName(), strSubst.replace("${dns:name| " + address + " }"));
}
@Test
void testDnsLookupUnknown() {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.DNS));
final String unknown = "${dns: u n k n o w n}";
assertEquals(unknown, strSubst.replace(unknown));
}
@Test
void testJavaScript() {
final StringSubstitutor strSubst =
new StringSubstitutor(createInterpolatorWithLookups(DefaultStringLookup.SCRIPT));
assertEquals("Hello World!", strSubst.replace("${script:javascript:\"Hello World!\"}"));
assertEquals("7", strSubst.replace("${script:javascript:3 + 4}"));
}
@Test
void testJavaScript_disabledByDefault() {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
assertEquals("${script:javascript:3 + 4}", strSubst.replace("${script:javascript:3 + 4}"));
}
@Test
void testLocalHostLookup_Address() throws UnknownHostException {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
assertEquals(InetAddress.getLocalHost().getHostAddress(), strSubst.replace("${localhost:address}"));
}
@Test
void testLocalHostLookup_CanonicalName() throws UnknownHostException {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
assertEquals(InetAddress.getLocalHost().getCanonicalHostName(),
strSubst.replace("${localhost:canonical-name}"));
}
@Test
void testLocalHostLookup_Name() throws UnknownHostException {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
assertEquals(InetAddress.getLocalHost().getHostName(), strSubst.replace("${localhost:name}"));
}
@Test
void testMapAndSystemProperty() {
final String key = "key";
final String value = "value";
final Map<String, String> map = new HashMap<>();
map.put(key, value);
final StringSubstitutor strSubst = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
final String spKey = "user.name";
assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
assertEquals(value, strSubst.replace("${" + key + "}"));
}
@Test
void testSystemProperty() {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
final String spKey = "user.name";
assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
/**
* In our POM. the module {@code java.base} must open "opens java.lang" for the {@link SetEnvironmentVariable} annotation to work. Otherwise, the test will
* fail with an IllegalAccessException.
*/
@Disabled("Needs java.base to open 'opens java.lang' for SetEnvironmentVariable.")
@Test
@SetEnvironmentVariable(key = "testSystemPropertyDefaultDefault", value = "123456789")
void testSystemPropertyDefaultDefaultEnv() {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
assertEquals("123456789", strSubst
.replace("${env:testSystemPropertyDefaultDefault:-${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}}"));
}
@Test
void testSystemPropertyDefaultDefault() {
final StringSubstitutor strSubst = StringSubstitutor.createInterpolator();
strSubst.setEnableSubstitutionInVariables(true);
final String spKey = "user.name";
// check that "sys" works
final String spLookupStr = "${sys:" + spKey + "}";
final String actual = System.getProperty(spKey);
assertEquals(actual, strSubst.replace(spLookupStr));
// test "sys" with default value
assertEquals("foo", strSubst.replace("${sys:unknownkey1:-foo}"));
assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-foo}}"));
assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-foo}}}"));
assertEquals("foo", strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}"));
assertEquals("foo", strSubst.replace("${env:UNKNOWN_KEY:-${sys:unknownkey1:-${sys:unknownkey2:-${sys:unknownkey3:-${sys:unknownkey4:-foo}}}}}"));
assertEquals(actual, strSubst.replace("${sys:unknownkey1:-" + spLookupStr + "}"));
assertEquals(actual, strSubst.replace("${sys:unknownkey1:-${sys:unknownkey2:-" + spLookupStr + "}}"));
}
@Test
void testSystemPropertyDefaultStringLookup() {
final StringSubstitutor strSubst = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup(StringLookupFactory.INSTANCE.systemPropertyStringLookup()));
final String spKey = "user.name";
assertEquals(System.getProperty(spKey), strSubst.replace("${" + spKey + "}"));
assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
}