You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/28 20:47:48 UTC
[commons-text] branch master updated: [TEXT-165]
ResourceBundleStringLookup.lookup(String) throws MissingResourceException
instead of returning null
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new 23427f2 [TEXT-165] ResourceBundleStringLookup.lookup(String) throws MissingResourceException instead of returning null
23427f2 is described below
commit 23427f24425db4601bf069db93ee1f873edf07d4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 28 16:47:45 2019 -0400
[TEXT-165] ResourceBundleStringLookup.lookup(String) throws
MissingResourceException instead of returning null
---
src/changes/changes.xml | 1 +
.../commons/text/lookup/ResourceBundleStringLookup.java | 4 ++++
...StringSubstitutorWithInterpolatorStringLookupTest.java | 15 ++++++++++++++-
.../text/lookup/ResourceBundleStringLookupTest.java | 13 ++++---------
src/test/resources/testResourceBundleLookup.properties | 1 +
5 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d524ad7..5151bc3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-151" type="fix" dev="aherbert">Fix the JaroWinklerSimilarity to use StringUtils.equals to test for CharSequence equality</action>
<action issue="TEXT-155" type="add" dev="aherbert">Add a generic IntersectionSimilarity measure</action>
<action issue="TEXT-162" type="update" dev="ggregory">Update Apache Commons Lang from 3.8.1 to 3.9.</action>
+ <action issue="TEXT-165" type="fix" dev="ggregory">ResourceBundleStringLookup.lookup(String) throws MissingResourceException instead of returning null.</action>
</release>
<release version="1.6" date="2018-10-12" description="Release 1.6">
diff --git a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
index a5380bf..7ca28b1 100644
--- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
@@ -17,6 +17,7 @@
package org.apache.commons.text.lookup;
+import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
@@ -93,6 +94,9 @@ final class ResourceBundleStringLookup extends AbstractStringLookup {
try {
// The ResourceBundle class caches bundles, no need to cache here.
return ResourceBundle.getBundle(keyBundleName).getString(bundleKey);
+ } catch (final MissingResourceException e) {
+ // The key is missing, return null such that an interpolator can supply a default value.
+ return null;
} catch (final Exception e) {
throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].",
keyBundleName, bundleKey);
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index 33157b7..9213e51 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -17,6 +17,8 @@
package org.apache.commons.text;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
@@ -57,6 +59,16 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
}
@Test
+ public void testDefaultValueForMissingKeyInResourceBundle() {
+ final StringLookup interpolatorStringLookup = StringLookupFactory.INSTANCE.interpolatorStringLookup(
+ StringLookupFactory.INSTANCE.resourceBundleStringLookup("testResourceBundleLookup"));
+ 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
public void testLocalHostLookup_Address() throws UnknownHostException {
final StringSubstitutor strSubst = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup());
@@ -90,7 +102,7 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
Assertions.assertEquals(value, strSubst.replace("${" + key + "}"));
}
-
+
@Test
public void testSystemProperty() {
final StringSubstitutor strSubst = new StringSubstitutor(
@@ -98,4 +110,5 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
final String spKey = "user.name";
Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
}
+
}
diff --git a/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java
index 7c30aa7..c6dbd13 100644
--- a/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java
@@ -18,6 +18,7 @@
package org.apache.commons.text.lookup;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.ResourceBundle;
@@ -43,19 +44,13 @@ public class ResourceBundleStringLookupTest {
public void testBadKey() {
final String bundleName = TEST_RESOURCE_BUNDLE;
final String bundleKey = "bad_key";
- assertThrows(IllegalArgumentException.class, () -> {
- new ResourceBundleStringLookup(bundleName).lookup(bundleKey);
- });
- assertThrows(IllegalArgumentException.class, () -> {
- ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" + bundleKey);
- });
+ assertNull(new ResourceBundleStringLookup(bundleName).lookup(bundleKey));
+ assertNull(ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" + bundleKey));
}
@Test
public void testBadName() {
- assertThrows(IllegalArgumentException.class, () -> {
- ResourceBundleStringLookup.INSTANCE.lookup("BAD_RESOURCE_BUNDLE_NAME:KEY");
- });
+ assertNull(ResourceBundleStringLookup.INSTANCE.lookup("BAD_RESOURCE_BUNDLE_NAME:KEY"));
}
@Test
diff --git a/src/test/resources/testResourceBundleLookup.properties b/src/test/resources/testResourceBundleLookup.properties
index ea39746..ffc5e13 100644
--- a/src/test/resources/testResourceBundleLookup.properties
+++ b/src/test/resources/testResourceBundleLookup.properties
@@ -15,3 +15,4 @@
key = value
number = 2
+keyWithMissingKey = ${missingKey:-defaultValue}