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 24d4397 Increase code coverage. Checkstyle. 24d4397 is described below commit 24d4397a59b1e8e11cbf50487132818b7a67df7d Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jul 17 10:41:18 2020 -0400 Increase code coverage. Checkstyle. --- .../apache/commons/text/lookup/InetAddressKeys.java | 2 +- .../commons/text/lookup/PropertiesStringLookup.java | 18 ++++++++++++++---- .../text/lookup/ResourceBundleStringLookup.java | 1 + .../apache/commons/text/lookup/ScriptStringLookup.java | 4 ++-- .../commons/text/lookup/UrlDecoderStringLookup.java | 2 +- .../commons/text/lookup/UrlEncoderStringLookup.java | 2 +- .../text/lookup/ConstantStringLookupBasicTest.java | 2 +- .../text/lookup/PropertiesStringLookupTest.java | 18 +++++++++++++----- .../text/lookup/ResourceBundleStringLookupTest.java | 13 ++++++++++--- 9 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java b/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java index 2766fd4..139dfe6 100644 --- a/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java +++ b/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java @@ -24,7 +24,7 @@ import java.net.InetAddress; * * @since 1.8 */ -class InetAddressKeys { +final class InetAddressKeys { /** * Constants for referring to {@link InetAddress#getAddress()}. diff --git a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java index 28a1a4f..fd177d0 100644 --- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java @@ -40,6 +40,16 @@ import org.apache.commons.lang3.StringUtils; */ final class PropertiesStringLookup extends AbstractStringLookup { + /** Separates file and key. */ + static final String SEPARATOR = "::"; + + /** + * Creates a lookup key for a given file and key. + */ + static String toLookupKey(final String file, final String key) { + return file + SEPARATOR + key; + } + /** * Defines the singleton for this class. */ @@ -69,14 +79,14 @@ final class PropertiesStringLookup extends AbstractStringLookup { if (key == null) { return null; } - final String[] keys = key.split("::"); + final String[] keys = key.split(SEPARATOR); final int keyLen = keys.length; if (keyLen < 2) { - throw IllegalArgumentExceptions - .format("Bad properties key format [%s]; expected format is DocumentPath::Key.", key); + throw IllegalArgumentExceptions.format("Bad properties key format [%s]; expected format is %s.", key, + toLookupKey("DocumentPath", "Key")); } final String documentPath = keys[0]; - final String propertyKey = StringUtils.substringAfter(key, "::"); + final String propertyKey = StringUtils.substringAfter(key, SEPARATOR); try { final Properties properties = new Properties(); try (InputStream inputStream = Files.newInputStream(Paths.get(documentPath))) { 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 fbbc7d6..fd30fed 100644 --- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java @@ -96,6 +96,7 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { // The key is missing, return null such that an interpolator can supply a default value. return null; } catch (final Exception e) { + // Should not happen throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].", keyBundleName, bundleKey); } diff --git a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java index bd32201..3397c9b 100644 --- a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java @@ -35,11 +35,11 @@ import org.apache.commons.text.StringSubstitutor; * <p> * Using a {@link StringSubstitutor}: * </p> - * + * * <pre> * StringSubstitutor.createInterpolator().replace("${script:javascript:3 + 4}")); * </pre> - * + * * @since 1.5 */ final class ScriptStringLookup extends AbstractStringLookup { diff --git a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java index 763dc8b..78ffa8f 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java @@ -51,7 +51,7 @@ final class UrlDecoderStringLookup extends AbstractStringLookup { try { return URLDecoder.decode(key, enc); } catch (final UnsupportedEncodingException e) { - // Can't happen since UTF_8 is required by the Java specification. + // Can't happen since UTF-8 is required by the Java specification. throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc); } } diff --git a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java index 1925c5a..dd6d5ab 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java @@ -50,7 +50,7 @@ final class UrlEncoderStringLookup extends AbstractStringLookup { try { return URLEncoder.encode(key, enc); } catch (final UnsupportedEncodingException e) { - // Can't happen since UTF_8 is required by the Java specification. + // Can't happen since UTF-8 is required by the Java specification. throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc); } } diff --git a/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java b/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java index a5fff89..ce40d72 100644 --- a/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java @@ -65,7 +65,7 @@ public class ConstantStringLookupBasicTest { protected Class<?> fetchClass(final String className) throws ClassNotFoundException { return null; } - }.lookup("foo")); + }.lookup(ConstantStringLookupBasicTest.class.getName() + ".STRING_FIXTURE")); } @Test diff --git a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java index b16ea97..40af31b 100644 --- a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java @@ -34,7 +34,7 @@ public class PropertiesStringLookupTest { private static final String DOC_PATH = "src/test/resources/document.properties"; private static final String KEY = "mykey"; - private static final String KEY_PATH = DOC_PATH + "::" + KEY; + private static final String KEY_PATH = PropertiesStringLookup.toLookupKey(DOC_PATH, KEY); @Test public void testInterpolator() { @@ -59,8 +59,9 @@ public class PropertiesStringLookupTest { map.put("KeyIsHere", KEY); final StringSubstitutor stringSubstitutor = new StringSubstitutor( StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); - final String replaced = stringSubstitutor.replace("$${properties:" + DOC_PATH + "::${KeyIsHere}}"); - assertEquals("${properties:" + DOC_PATH + "::mykey}", replaced); + final String replaced = stringSubstitutor + .replace("$${properties:" + PropertiesStringLookup.toLookupKey(DOC_PATH, "${KeyIsHere}}")); + assertEquals("${properties:" + PropertiesStringLookup.toLookupKey(DOC_PATH, "mykey}"), replaced); assertEquals("Hello World!", stringSubstitutor.replace(replaced)); } @@ -71,8 +72,9 @@ public class PropertiesStringLookupTest { final StringSubstitutor stringSubstitutor = new StringSubstitutor( StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); final String replaced = stringSubstitutor - .replace("$${properties:${sys:user.dir}/" + DOC_PATH + "::${KeyIsHere}}"); - assertEquals("${properties:" + System.getProperty("user.dir") + "/" + DOC_PATH + "::mykey}", replaced); + .replace("$${properties:${sys:user.dir}/" + PropertiesStringLookup.toLookupKey(DOC_PATH, "${KeyIsHere}}")); + assertEquals("${properties:" + System.getProperty("user.dir") + "/" + + PropertiesStringLookup.toLookupKey(DOC_PATH, "mykey}"), replaced); assertEquals("Hello World!", stringSubstitutor.replace(replaced)); } @@ -82,6 +84,12 @@ public class PropertiesStringLookupTest { } @Test + public void testMissingFileWithKey() { + assertThrows(IllegalArgumentException.class, + () -> PropertiesStringLookup.INSTANCE.lookup(PropertiesStringLookup.toLookupKey("MissingFile", "AnyKey"))); + } + + @Test public void testMissingKey() { assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.lookup(DOC_PATH)); } 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 d362d6d..142d4d8 100644 --- a/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java @@ -49,15 +49,22 @@ public class ResourceBundleStringLookupTest { } @Test - public void testBadName() { + public void testBadNames() { assertNull(ResourceBundleStringLookup.INSTANCE.lookup("BAD_RESOURCE_BUNDLE_NAME:KEY")); } @Test - public void testMissingKeyInSpec() { + public void testDoubleBundle() { final String bundleName = TEST_RESOURCE_BUNDLE; + final String bundleKey = "key"; + assertThrows(IllegalArgumentException.class, + () -> new ResourceBundleStringLookup(bundleName).lookup("OtherBundle:" + bundleKey)); + } + + @Test + public void testMissingKeyInSpec() { assertThrows(IllegalArgumentException.class, () -> { - ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":"); + ResourceBundleStringLookup.INSTANCE.lookup(TEST_RESOURCE_BUNDLE + ":"); }); }