Skip to content

Commit a713ac5

Browse files
committed
StringLookupFactory.DefaultStringLookupsHolder.createDefaultStringLookups()
maps DefaultStringLookup.LOCAL_HOST twice instead of once for LOCAL_HOST and LOOPBACK_ADDRESS
1 parent 0cd5f73 commit a713ac5

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
4949
<!-- FIX -->
5050
<action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action>
5151
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate EntityArrays.EntityArrays().</action>
52+
<action type="fix" dev="ggregory" due-to="Gary Gregory">StringLookupFactory.DefaultStringLookupsHolder.createDefaultStringLookups() maps DefaultStringLookup.LOCAL_HOST twice instead of once for LOCAL_HOST and LOOPBACK_ADDRESS.</action>
5253
<!-- ADD -->
5354
<!-- UPDATE -->
5455
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 79 to 81.</action>

src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ private static void addLookup(final DefaultStringLookup lookup, final Map<String
295295
*/
296296
private static Map<String, StringLookup> createDefaultStringLookups() {
297297
final Map<String, StringLookup> lookupMap = new HashMap<>();
298-
299298
addLookup(DefaultStringLookup.BASE64_DECODER, lookupMap);
300299
addLookup(DefaultStringLookup.BASE64_ENCODER, lookupMap);
301300
addLookup(DefaultStringLookup.CONST, lookupMap);
@@ -304,7 +303,7 @@ private static Map<String, StringLookup> createDefaultStringLookups() {
304303
addLookup(DefaultStringLookup.FILE, lookupMap);
305304
addLookup(DefaultStringLookup.JAVA, lookupMap);
306305
addLookup(DefaultStringLookup.LOCAL_HOST, lookupMap);
307-
addLookup(DefaultStringLookup.LOCAL_HOST, lookupMap);
306+
addLookup(DefaultStringLookup.LOOPBACK_ADDRESS, lookupMap);
308307
addLookup(DefaultStringLookup.PROPERTIES, lookupMap);
309308
addLookup(DefaultStringLookup.RESOURCE_BUNDLE, lookupMap);
310309
addLookup(DefaultStringLookup.SYSTEM_PROPERTIES, lookupMap);
@@ -313,7 +312,6 @@ private static Map<String, StringLookup> createDefaultStringLookups() {
313312
addLookup(DefaultStringLookup.XML, lookupMap);
314313
addLookup(DefaultStringLookup.XML_DECODER, lookupMap);
315314
addLookup(DefaultStringLookup.XML_ENCODER, lookupMap);
316-
317315
return lookupMap;
318316
}
319317

src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
*/
1717
package org.apache.commons.text.lookup;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertSame;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
1925
import java.util.HashMap;
2026
import java.util.HashSet;
27+
import java.util.Locale;
2128
import java.util.Map;
2229
import java.util.Properties;
2330
import java.util.Set;
2431

2532
import javax.xml.XMLConstants;
2633

27-
import org.junit.jupiter.api.Assertions;
2834
import org.junit.jupiter.api.Test;
2935

3036
/**
@@ -60,12 +66,12 @@ private static void assertMappedLookups(final Map<String, StringLookup> lookupMa
6066

6167
for (final String key : keys) {
6268
final String normalizedKey = StringLookupFactory.toKey(key);
63-
Assertions.assertNotNull(normalizedKey, () -> "Expected map to contain string lookup for key " + key);
69+
assertNotNull(normalizedKey, () -> "Expected map to contain string lookup for key " + key);
6470

6571
remainingKeys.remove(normalizedKey);
6672
}
6773

68-
Assertions.assertTrue(remainingKeys.isEmpty(), () -> "Unexpected keys in lookup map: " + remainingKeys);
74+
assertTrue(remainingKeys.isEmpty(), () -> "Unexpected keys in lookup map: " + remainingKeys);
6975
}
7076

7177
private static void checkDefaultStringLookupsHolder(final Properties props, final String... keys) {
@@ -157,9 +163,9 @@ public void testDefaultStringLookupsHolder_invalidLookupsDefinition() {
157163
final Properties props = new Properties();
158164
props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "base64_encoder nope");
159165

160-
final Exception exc = Assertions.assertThrows(IllegalArgumentException.class,
166+
final Exception exc = assertThrows(IllegalArgumentException.class,
161167
() -> new StringLookupFactory.DefaultStringLookupsHolder(props));
162-
Assertions.assertEquals("Invalid default string lookups definition: base64_encoder nope", exc.getMessage());
168+
assertEquals("Invalid default string lookups definition: base64_encoder nope", exc.getMessage());
163169
}
164170

165171
@Test
@@ -215,30 +221,57 @@ public void testDefaultStringLookupsHolder_multipleLookups() {
215221
@Test
216222
public void testSingletons() {
217223
final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
218-
Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER,
224+
assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER,
219225
stringLookupFactory.base64DecoderStringLookup());
220-
Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_ENCODER,
226+
assertSame(StringLookupFactory.INSTANCE_BASE64_ENCODER,
221227
stringLookupFactory.base64EncoderStringLookup());
222-
Assertions.assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup());
223-
Assertions.assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup());
224-
Assertions.assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());
225-
Assertions.assertSame(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES,
228+
assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup());
229+
assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup());
230+
assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());
231+
assertSame(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES,
226232
stringLookupFactory.environmentVariableStringLookup());
227-
Assertions.assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
228-
Assertions.assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
229-
Assertions.assertSame(InetAddressStringLookup.LOCAL_HOST, stringLookupFactory.localHostStringLookup());
230-
Assertions.assertSame(InetAddressStringLookup.LOOPACK_ADDRESS, stringLookupFactory.loopbackAddressStringLookup());
231-
Assertions.assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
232-
Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
233-
Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
234-
Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES,
233+
assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
234+
assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
235+
assertSame(InetAddressStringLookup.LOCAL_HOST, stringLookupFactory.localHostStringLookup());
236+
assertSame(InetAddressStringLookup.LOOPACK_ADDRESS, stringLookupFactory.loopbackAddressStringLookup());
237+
assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
238+
assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
239+
assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
240+
assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES,
235241
stringLookupFactory.systemPropertyStringLookup());
236-
Assertions.assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
237-
Assertions.assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
238-
Assertions.assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
239-
Assertions.assertSame(XmlStringLookup.INSTANCE, stringLookupFactory.xmlStringLookup());
240-
Assertions.assertSame(XmlDecoderStringLookup.INSTANCE, stringLookupFactory.xmlDecoderStringLookup());
241-
Assertions.assertSame(XmlEncoderStringLookup.INSTANCE, stringLookupFactory.xmlEncoderStringLookup());
242+
assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
243+
assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
244+
assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
245+
assertSame(XmlStringLookup.INSTANCE, stringLookupFactory.xmlStringLookup());
246+
assertSame(XmlDecoderStringLookup.INSTANCE, stringLookupFactory.xmlDecoderStringLookup());
247+
assertSame(XmlEncoderStringLookup.INSTANCE, stringLookupFactory.xmlEncoderStringLookup());
248+
}
249+
250+
/**
251+
* Tests that we return the singleton.
252+
*/
253+
@Test
254+
public void testDefault() {
255+
final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE;
256+
final Map<String, StringLookup> stringLookupMap = new HashMap<>();
257+
stringLookupFactory.addDefaultStringLookups(stringLookupMap);
258+
assertTrue(stringLookupMap.containsKey("base64"));
259+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_BASE64_ENCODER.toLowerCase(Locale.ROOT)));
260+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_CONST.toLowerCase(Locale.ROOT)));
261+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_DATE));
262+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_ENV.toLowerCase(Locale.ROOT)));
263+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_FILE.toLowerCase(Locale.ROOT)));
264+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_JAVA.toLowerCase(Locale.ROOT)));
265+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_LOCALHOST.toLowerCase(Locale.ROOT)));
266+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_LOOPBACK_ADDRESS.toLowerCase(Locale.ROOT)));
267+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_PROPERTIES.toLowerCase(Locale.ROOT)));
268+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_RESOURCE_BUNDLE.toLowerCase(Locale.ROOT)));
269+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_SYS.toLowerCase(Locale.ROOT)));
270+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_URL_DECODER.toLowerCase(Locale.ROOT)));
271+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_URL_ENCODER.toLowerCase(Locale.ROOT)));
272+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_XML.toLowerCase(Locale.ROOT)));
273+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_XML_DECODER.toLowerCase(Locale.ROOT)));
274+
assertTrue(stringLookupMap.containsKey(StringLookupFactory.KEY_XML_ENCODER.toLowerCase(Locale.ROOT)));
242275
}
243276

244277
@Test

0 commit comments

Comments
 (0)