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 d0a615d Increase code coverage. d0a615d is described below commit d0a615d27fb80df173d6e22b40087f6acc75772a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Jul 16 23:03:58 2020 -0400 Increase code coverage. --- .../commons/text/lookup/AbstractStringLookup.java | 20 +++---- .../text/lookup/BiFunctionStringLookup.java | 15 ++---- .../commons/text/lookup/ConstantStringLookup.java | 34 ++++++------ .../commons/text/lookup/DateStringLookup.java | 2 +- .../commons/text/lookup/DefaultStringLookup.java | 6 +-- .../commons/text/lookup/FileStringLookup.java | 4 +- .../commons/text/lookup/FunctionStringLookup.java | 9 ---- .../text/lookup/IllegalArgumentExceptions.java | 15 ++---- .../commons/text/lookup/InetAddressKeys.java | 3 ++ .../text/lookup/InterpolatorStringLookup.java | 63 ++++++++++------------ .../text/lookup/JavaPlatformStringLookup.java | 30 +++++------ .../text/lookup/PropertiesStringLookup.java | 7 ++- .../text/lookup/ResourceBundleStringLookup.java | 21 ++++---- .../commons/text/lookup/ScriptStringLookup.java | 12 ++--- .../text/lookup/UrlDecoderStringLookup.java | 1 + .../text/lookup/UrlEncoderStringLookup.java | 1 + .../commons/text/lookup/UrlStringLookup.java | 9 ++-- .../text/matcher/AbstractStringMatcher.java | 2 +- .../text/lookup/AbstractStringLookupTest.java | 57 ++++++++++++++++++++ .../text/lookup/Base64DecoderStringLookupTest.java | 6 +++ .../text/lookup/Base64EncoderStringLookupTest.java | 6 +++ .../text/lookup/BiFunctionStringLookupTest.java | 24 ++++++++- .../text/lookup/ConstantStringLookupBasicTest.java | 32 ++++++++++- .../text/lookup/ConstantStringLookupTest.java | 11 ++-- .../commons/text/lookup/DateStringLookupTest.java | 22 ++++++-- .../text/lookup/DefaultStringLookupTest.java | 18 +++---- .../commons/text/lookup/DnsStringLookupTest.java | 8 ++- .../EnvironmentVariableStringLookupTest.java | 6 +++ .../commons/text/lookup/FileStringLookupTest.java | 15 +++++- .../text/lookup/FunctionStringLookupTest.java | 12 +++++ .../text/lookup/InterpolatorStringLookupTest.java | 46 ++++++++++------ .../text/lookup/JavaPlatformStringLookupTest.java | 6 +++ .../text/lookup/LocalHostStringLookupTest.java | 17 ++++-- .../commons/text/lookup/NullStringLookupTest.java | 6 +++ .../text/lookup/PropertiesStringLookupTest.java | 46 +++++++++++----- .../lookup/ResourceBundleStringLookupTest.java | 10 +++- .../text/lookup/ScriptEngineFactoryHelper.java | 4 +- .../text/lookup/ScriptStringLookupTest.java | 6 +++ .../lookup/SystemPropertyStringLookupTest.java | 6 +++ .../text/lookup/UrlDecoderStringLookupTest.java | 6 +++ .../text/lookup/UrlEncoderStringLookupTest.java | 5 ++ .../commons/text/lookup/UrlStringLookupTest.java | 20 +++++++ .../commons/text/lookup/XmlStringLookupTest.java | 23 +++++++- 43 files changed, 467 insertions(+), 205 deletions(-) diff --git a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java index 35a8107..3229c03 100644 --- a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java @@ -50,29 +50,29 @@ abstract class AbstractStringLookup implements StringLookup { } /** - * Returns the substring after the first occurrence of {@code ch} in {@code value}. + * Returns the substring after the first occurrence of {@code str} in {@code value}. * * @param value The source string. - * @param ch The character to search. + * @param str The string to search. * @return a new string. - * @deprecated Use {@link StringUtils#substringAfterLast(String, int)}. + * @deprecated Use {@link StringUtils#substringAfter(String, String)}. */ @Deprecated - protected String substringAfterLast(final String value, final char ch) { - return StringUtils.substringAfterLast(value, ch); + protected String substringAfter(final String value, final String str) { + return StringUtils.substringAfter(value, str); } /** - * Returns the substring after the first occurrence of {@code str} in {@code value}. + * Returns the substring after the first occurrence of {@code ch} in {@code value}. * * @param value The source string. - * @param str The string to search. + * @param ch The character to search. * @return a new string. - * @deprecated Use {@link StringUtils#substringAfter(String, String)}. + * @deprecated Use {@link StringUtils#substringAfterLast(String, int)}. */ @Deprecated - protected String substringAfter(final String value, final String str) { - return StringUtils.substringAfter(value, str); + protected String substringAfterLast(final String value, final char ch) { + return StringUtils.substringAfterLast(value, ch); } } diff --git a/src/main/java/org/apache/commons/text/lookup/BiFunctionStringLookup.java b/src/main/java/org/apache/commons/text/lookup/BiFunctionStringLookup.java index 0bd49a5..4eba940 100644 --- a/src/main/java/org/apache/commons/text/lookup/BiFunctionStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/BiFunctionStringLookup.java @@ -66,13 +66,9 @@ final class BiFunctionStringLookup<P, R> implements BiStringLookup<P> { this.biFunction = biFunction; } - /** - * Gets the function used in lookups. - * - * @return The function used in lookups. - */ - BiFunction<String, P, R> getBiFunction() { - return biFunction; + @Override + public String lookup(final String key) { + return lookup(key, null); } /** @@ -106,9 +102,4 @@ final class BiFunctionStringLookup<P, R> implements BiStringLookup<P> { return super.toString() + " [function=" + biFunction + "]"; } - @Override - public String lookup(final String key) { - return lookup(key, null); - } - } diff --git a/src/main/java/org/apache/commons/text/lookup/ConstantStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ConstantStringLookup.java index e3fc737..05d211a 100644 --- a/src/main/java/org/apache/commons/text/lookup/ConstantStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ConstantStringLookup.java @@ -63,6 +63,9 @@ import org.apache.commons.text.StringSubstitutor; */ class ConstantStringLookup extends AbstractStringLookup { + /** An internally used cache for already retrieved values. */ + private static ConcurrentHashMap<String, String> constantCache = new ConcurrentHashMap<>(); + /** Constant for the field separator. */ private static final char FIELD_SEPRATOR = '.'; @@ -71,9 +74,6 @@ class ConstantStringLookup extends AbstractStringLookup { */ static final ConstantStringLookup INSTANCE = new ConstantStringLookup(); - /** An internally used cache for already retrieved values. */ - private static ConcurrentHashMap<String, String> constantCache = new ConcurrentHashMap<>(); - /** * Clears the shared cache with the so far resolved constants. */ @@ -82,6 +82,20 @@ class ConstantStringLookup extends AbstractStringLookup { } /** + * Loads the class with the specified name. If an application has special needs regarding the class loaders to be + * used, it can hook in here. This implementation delegates to the {@code getClass()} method of Commons Lang's + * <code><a href="https://commons.apache.org/lang/api-release/org/apache/commons/lang/ClassUtils.html"> + * ClassUtils</a></code>. + * + * @param className the name of the class to be loaded + * @return The corresponding class object + * @throws ClassNotFoundException if the class cannot be loaded + */ + protected Class<?> fetchClass(final String className) throws ClassNotFoundException { + return ClassUtils.getClass(className); + } + + /** * Tries to resolve the specified variable. The passed in variable name is interpreted as the name of a <b>static * final</b> member field of a class. If the value has already been obtained, it can be retrieved from an internal * cache. Otherwise this method will invoke the {@code resolveField()} method and pass in the name of the class and @@ -135,18 +149,4 @@ class ConstantStringLookup extends AbstractStringLookup { } return clazz.getField(fieldName).get(null); } - - /** - * Loads the class with the specified name. If an application has special needs regarding the class loaders to be - * used, it can hook in here. This implementation delegates to the {@code getClass()} method of Commons Lang's - * <code><a href="https://commons.apache.org/lang/api-release/org/apache/commons/lang/ClassUtils.html"> - * ClassUtils</a></code>. - * - * @param className the name of the class to be loaded - * @return The corresponding class object - * @throws ClassNotFoundException if the class cannot be loaded - */ - protected Class<?> fetchClass(final String className) throws ClassNotFoundException { - return ClassUtils.getClass(className); - } } diff --git a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java index 676387d..e7e192e 100644 --- a/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/DateStringLookup.java @@ -61,7 +61,7 @@ final class DateStringLookup extends AbstractStringLookup { /** * Formats the given {@code date} long with the given {@code format}. * - * @param date the date to format + * @param date the date to format * @param format the format string for {@link SimpleDateFormat}. * @return The formatted date */ diff --git a/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java index 6f75937..628ec49 100644 --- a/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java @@ -115,12 +115,12 @@ public enum DefaultStringLookup { */ XML(StringLookupFactory.KEY_XML, StringLookupFactory.INSTANCE.xmlStringLookup()); - /** The associated lookup instance. */ - private final StringLookup lookup; - /** The prefix under which the associated lookup object is registered. */ private final String key; + /** The associated lookup instance. */ + private final StringLookup lookup; + /** * Creates a new instance of {@link DefaultStringLookup} and sets the key and the associated lookup instance. * diff --git a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java index a04b6d5..de15027 100644 --- a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java @@ -77,7 +77,7 @@ final class FileStringLookup extends AbstractStringLookup { final int keyLen = keys.length; if (keyLen < 2) { throw IllegalArgumentExceptions - .format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key); + .format("Bad file key format [%s], expected format is CharsetName:DocumentPath.", key); } final String charsetName = keys[0]; final String fileName = StringUtils.substringAfter(key, SPLIT_CH); @@ -85,7 +85,7 @@ final class FileStringLookup extends AbstractStringLookup { return new String(Files.readAllBytes(Paths.get(fileName)), charsetName); } catch (final Exception e) { throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName, - charsetName); + charsetName); } } diff --git a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java index 56e2c28..ba158fc 100644 --- a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java @@ -66,15 +66,6 @@ final class FunctionStringLookup<V> extends AbstractStringLookup { } /** - * Gets the function used in lookups. - * - * @return The function used in lookups. - */ - Function<String, V> getFunction() { - return function; - } - - /** * Looks up a String key by applying the function. * <p> * If the function is null, then null is returned. The function result object is converted to a string using diff --git a/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java b/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java index e5e2445..6ebb0e3 100644 --- a/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java +++ b/src/main/java/org/apache/commons/text/lookup/IllegalArgumentExceptions.java @@ -27,10 +27,8 @@ final class IllegalArgumentExceptions { /** * Creates an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)}. * - * @param format - * See {@link String#format(String,Object...)} - * @param args - * See {@link String#format(String,Object...)} + * @param format See {@link String#format(String,Object...)} + * @param args See {@link String#format(String,Object...)} * @return an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)} */ static IllegalArgumentException format(final String format, final Object... args) { @@ -40,12 +38,9 @@ final class IllegalArgumentExceptions { /** * Creates an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)}. * - * @param t - * the throwable cause - * @param format - * See {@link String#format(String,Object...)} - * @param args - * See {@link String#format(String,Object...)} + * @param t the throwable cause + * @param format See {@link String#format(String,Object...)} + * @param args See {@link String#format(String,Object...)} * @return an {@link IllegalArgumentException} with a message formated with {@link String#format(String,Object...)} */ static IllegalArgumentException format(final Throwable t, final String format, final Object... args) { 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 2465614..2766fd4 100644 --- a/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java +++ b/src/main/java/org/apache/commons/text/lookup/InetAddressKeys.java @@ -41,4 +41,7 @@ class InetAddressKeys { */ static final String KEY_NAME = "name"; + private InetAddressKeys() { + // noop + } } diff --git a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java index b00148c..0a9bda5 100644 --- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java @@ -39,6 +39,10 @@ class InterpolatorStringLookup extends AbstractStringLookup { /** Constant for the prefix separator. */ private static final char PREFIX_SEPARATOR = ':'; + static String toKey(final String key) { + return key.toLowerCase(Locale.ROOT); + } + /** The default string lookup. */ private final StringLookup defaultStringLookup; @@ -56,15 +60,33 @@ class InterpolatorStringLookup extends AbstractStringLookup { } /** + * Creates a fully customized instance. + * + * @param stringLookupMap the map of string lookups. + * @param defaultStringLookup the default string lookup. + * @param addDefaultLookups whether the default lookups should be used. + */ + InterpolatorStringLookup(final Map<String, StringLookup> stringLookupMap, final StringLookup defaultStringLookup, + final boolean addDefaultLookups) { + super(); + this.defaultStringLookup = defaultStringLookup; + this.stringLookupMap = new HashMap<>(stringLookupMap.size()); + for (final Entry<String, StringLookup> entry : stringLookupMap.entrySet()) { + this.stringLookupMap.put(toKey(entry.getKey()), entry.getValue()); + } + if (addDefaultLookups) { + StringLookupFactory.INSTANCE.addDefaultStringLookups(this.stringLookupMap); + } + } + + /** * Creates an instance using only lookups that work without initial properties and are stateless. * <p> * Uses the {@link StringLookupFactory default lookups}. * </p> * - * @param <V> - * the map's value type. - * @param defaultMap - * the default map for string lookups. + * @param <V> the map's value type. + * @param defaultMap the default map for string lookups. */ <V> InterpolatorStringLookup(final Map<String, V> defaultMap) { this(StringLookupFactory.INSTANCE.mapStringLookup(defaultMap == null ? new HashMap<String, V>() : defaultMap)); @@ -73,37 +95,13 @@ class InterpolatorStringLookup extends AbstractStringLookup { /** * Creates an instance with the given lookup. * - * @param defaultStringLookup - * the default lookup. + * @param defaultStringLookup the default lookup. */ InterpolatorStringLookup(final StringLookup defaultStringLookup) { this(new HashMap<>(), defaultStringLookup, true); } /** - * Creates a fully customized instance. - * - * @param stringLookupMap - * the map of string lookups. - * @param defaultStringLookup - * the default string lookup. - * @param addDefaultLookups - * whether the default lookups should be used. - */ - InterpolatorStringLookup(final Map<String, StringLookup> stringLookupMap, final StringLookup defaultStringLookup, - final boolean addDefaultLookups) { - super(); - this.defaultStringLookup = defaultStringLookup; - this.stringLookupMap = new HashMap<>(stringLookupMap.size()); - for (final Entry<String, StringLookup> entry : stringLookupMap.entrySet()) { - this.stringLookupMap.put(toKey(entry.getKey()), entry.getValue()); - } - if (addDefaultLookups) { - StringLookupFactory.INSTANCE.addDefaultStringLookups(this.stringLookupMap); - } - } - - /** * Gets the lookup map. * * @return The lookup map. @@ -112,18 +110,13 @@ class InterpolatorStringLookup extends AbstractStringLookup { return stringLookupMap; } - static String toKey(final String key) { - return key.toLowerCase(Locale.ROOT); - } - /** * Resolves the specified variable. This implementation will try to extract a variable prefix from the given * variable name (the first colon (':') is used as prefix separator). It then passes the name of the variable with * the prefix stripped to the lookup object registered for this prefix. If no prefix can be found or if the * associated lookup object cannot resolve this variable, the default lookup object will be used. * - * @param var - * the name of the variable whose value is to be looked up + * @param var the name of the variable whose value is to be looked up * @return The value of this variable or <b>null</b> if it cannot be resolved */ @Override diff --git a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java index 2f8da70..5339c83 100644 --- a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java @@ -58,23 +58,23 @@ import org.apache.commons.text.StringSubstitutor; */ final class JavaPlatformStringLookup extends AbstractStringLookup { - /** {@code locale} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ - private static final String KEY_LOCALE = "locale"; + /** + * Defines the singleton for this class. + */ + static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup(); /** {@code hardware} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ private static final String KEY_HARDWARE = "hardware"; + /** {@code locale} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ + private static final String KEY_LOCALE = "locale"; /** {@code os} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ private static final String KEY_OS = "os"; - /** {@code vm} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ - private static final String KEY_VM = "vm"; /** {@code runtime} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ private static final String KEY_RUNTIME = "runtime"; /** {@code version} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ private static final String KEY_VERSION = "version"; - /** - * Defines the singleton for this class. - */ - static final JavaPlatformStringLookup INSTANCE = new JavaPlatformStringLookup(); + /** {@code vm} key for driving {@link JavaPlatformStringLookup#lookup(String)}. */ + private static final String KEY_VM = "vm"; /** * The main method for running the JavaPlatformStringLookup. @@ -105,8 +105,8 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { */ String getHardware() { return "processors: " + Runtime.getRuntime().availableProcessors() + ", architecture: " - + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model") - + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist"); + + getSystemProperty("os.arch") + this.getSystemProperty("-", "sun.arch.data.model") + + this.getSystemProperty(", instruction sets: ", "sun.cpu.isalist"); } /** @@ -125,8 +125,8 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { */ String getOperatingSystem() { return getSystemProperty("os.name") + " " + getSystemProperty("os.version") - + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch") - + getSystemProperty("-", "sun.arch.data.model"); + + getSystemProperty(" ", "sun.os.patch.level") + ", architecture: " + getSystemProperty("os.arch") + + getSystemProperty("-", "sun.arch.data.model"); } /** @@ -136,7 +136,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { */ String getRuntime() { return getSystemProperty("java.runtime.name") + " (build " + getSystemProperty("java.runtime.version") - + ") from " + getSystemProperty("java.vendor"); + + ") from " + getSystemProperty("java.vendor"); } /** @@ -153,7 +153,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { * Gets the given system property. * * @param prefix the prefix to use for the result string - * @param name a system property name. + * @param name a system property name. * @return The prefix + a system property value. */ private String getSystemProperty(final String prefix, final String name) { @@ -171,7 +171,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup { */ String getVirtualMachine() { return getSystemProperty("java.vm.name") + " (build " + getSystemProperty("java.vm.version") + ", " - + getSystemProperty("java.vm.info") + ")"; + + getSystemProperty("java.vm.info") + ")"; } /** 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 e5ed3a1..28a1a4f 100644 --- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java @@ -61,8 +61,7 @@ final class PropertiesStringLookup extends AbstractStringLookup { * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths. * </p> * - * @param key - * the key to be looked up, may be null + * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override @@ -74,7 +73,7 @@ final class PropertiesStringLookup extends AbstractStringLookup { final int keyLen = keys.length; if (keyLen < 2) { throw IllegalArgumentExceptions - .format("Bad properties key format [%s]; expected format is DocumentPath::Key.", key); + .format("Bad properties key format [%s]; expected format is DocumentPath::Key.", key); } final String documentPath = keys[0]; final String propertyKey = StringUtils.substringAfter(key, "::"); @@ -86,7 +85,7 @@ final class PropertiesStringLookup extends AbstractStringLookup { return properties.getProperty(propertyKey); } catch (final Exception e) { throw IllegalArgumentExceptions.format(e, "Error looking up properties [%s] and key [%s].", documentPath, - propertyKey); + propertyKey); } } 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 f530636..fbbc7d6 100644 --- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java @@ -35,14 +35,14 @@ import java.util.ResourceBundle; final class ResourceBundleStringLookup extends AbstractStringLookup { /** - * The name of the resource bundle from which to look something up. + * Defines the singleton for this class. */ - private final String bundleName; + static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup(); /** - * Defines the singleton for this class. + * The name of the resource bundle from which to look something up. */ - static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup(); + private final String bundleName; /** * No need to build instances for now. @@ -54,8 +54,7 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { /** * Constructs an instance that only works for the given bundle. * - * @param bundleName - * the name of the resource bundle from which we will look keys up. + * @param bundleName the name of the resource bundle from which we will look keys up. * @since 1.5 */ ResourceBundleStringLookup(final String bundleName) { @@ -67,8 +66,7 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { * * For example: "com.domain.messages:MyKey". * - * @param key - * the key to be looked up, may be null + * @param key the key to be looked up, may be null * @return The value associated with the key. * @see ResourceBundle * @see ResourceBundle#getBundle(String) @@ -84,10 +82,10 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { final boolean anyBundle = bundleName == null; if (anyBundle && keyLen != 2) { throw IllegalArgumentExceptions - .format("Bad resource bundle key format [%s]; expected format is BundleName:KeyName.", key); + .format("Bad resource bundle key format [%s]; expected format is BundleName:KeyName.", key); } else if (bundleName != null && keyLen != 1) { throw IllegalArgumentExceptions.format("Bad resource bundle key format [%s]; expected format is KeyName.", - key); + key); } final String keyBundleName = anyBundle ? keys[0] : bundleName; final String bundleKey = anyBundle ? keys[1] : keys[0]; @@ -99,7 +97,7 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { return null; } catch (final Exception e) { throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].", - keyBundleName, bundleKey); + keyBundleName, bundleKey); } } @@ -108,5 +106,4 @@ final class ResourceBundleStringLookup extends AbstractStringLookup { return super.toString() + " [bundleName=" + bundleName + "]"; } - } 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 3029d73..bd32201 100644 --- a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java @@ -35,9 +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 { @@ -55,14 +57,12 @@ final class ScriptStringLookup extends AbstractStringLookup { } /** - * Execute the script with the engine name in the format "EngineName:Script". - * Extra colons will be ignored. + * Execute the script with the engine name in the format "EngineName:Script". Extra colons will be ignored. * <p> * For example: {@code "javascript:3 + 4"}. * </p> * - * @param key - * the engine:script to execute, may be null + * @param key the engine:script to execute, may be null * @return The value returned by the execution. */ @Override @@ -74,7 +74,7 @@ final class ScriptStringLookup extends AbstractStringLookup { final int keyLen = keys.length; if (keyLen != 2) { throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is EngineName:Script.", - key); + key); } final String engineName = keys[0]; final String script = keys[1]; @@ -86,7 +86,7 @@ final class ScriptStringLookup extends AbstractStringLookup { return Objects.toString(scriptEngine.eval(script), null); } catch (final Exception e) { throw IllegalArgumentExceptions.format(e, "Error in script engine [%s] evaluating script [%s].", engineName, - script); + script); } } 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 67e7a1a..763dc8b 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java @@ -51,6 +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. 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 6b7db6c..1925c5a 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java @@ -50,6 +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. throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc); } } diff --git a/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java index 4daaa58..1f0ef38 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java @@ -55,8 +55,7 @@ final class UrlStringLookup extends AbstractStringLookup { * For example: "com/domain/document.xml:/path/to/node". * </p> * - * @param key - * the key to be looked up, may be null + * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override @@ -68,7 +67,7 @@ final class UrlStringLookup extends AbstractStringLookup { final int keyLen = keys.length; if (keyLen < 2) { throw IllegalArgumentExceptions.format("Bad URL key format [%s]; expected format is DocumentPath:Key.", - key); + key); } final String charsetName = keys[0]; final String urlStr = StringUtils.substringAfter(key, SPLIT_CH); @@ -78,7 +77,7 @@ final class UrlStringLookup extends AbstractStringLookup { final StringWriter writer = new StringWriter(size); final char[] buffer = new char[size]; try (BufferedInputStream bis = new BufferedInputStream(url.openStream()); - InputStreamReader reader = new InputStreamReader(bis, charsetName)) { + InputStreamReader reader = new InputStreamReader(bis, charsetName)) { int n; while (-1 != (n = reader.read(buffer))) { writer.write(buffer, 0, n); @@ -87,7 +86,7 @@ final class UrlStringLookup extends AbstractStringLookup { return writer.toString(); } catch (final Exception e) { throw IllegalArgumentExceptions.format(e, "Error looking up URL [%s] with Charset [%s].", urlStr, - charsetName); + charsetName); } } diff --git a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java index 8100c6f..f102063 100644 --- a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java +++ b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java @@ -48,7 +48,7 @@ abstract class AbstractStringMatcher implements StringMatcher { * {@link NoneMatcher} instead. */ AndStringMatcher(final StringMatcher... stringMatchers) { - this.stringMatchers = stringMatchers; + this.stringMatchers = stringMatchers.clone(); } @Override diff --git a/src/test/java/org/apache/commons/text/lookup/AbstractStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/AbstractStringLookupTest.java new file mode 100644 index 0000000..3f4a435 --- /dev/null +++ b/src/test/java/org/apache/commons/text/lookup/AbstractStringLookupTest.java @@ -0,0 +1,57 @@ +/* + * 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 + * + * http://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.lookup; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +/** + * Tests forwarding methods provided by {@link AbstractStringLookup}. + */ +public class AbstractStringLookupTest { + + private static class TestStringLookup extends AbstractStringLookup { + + @Override + public String lookup(final String key) { + // noop + return null; + } + + } + + @SuppressWarnings("deprecation") + @Test + public void testForwarding_substringAfter() { + assertEquals(StringUtils.substringAfterLast("abc", 'a'), new TestStringLookup().substringAfterLast("abc", 'a')); + } + + @SuppressWarnings("deprecation") + @Test + public void testForwarding_substringAfterChar() { + assertEquals(StringUtils.substringAfter("abc", 'a'), new TestStringLookup().substringAfter("abc", 'a')); + } + + @SuppressWarnings("deprecation") + @Test + public void testForwarding_substringAfterString() { + assertEquals(StringUtils.substringAfter("abc", "a"), new TestStringLookup().substringAfter("abc", "a")); + } +} diff --git a/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java index f95ee47..bf8c40f 100644 --- a/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java @@ -35,4 +35,10 @@ public class Base64DecoderStringLookupTest { Assertions.assertNull(StringLookupFactory.INSTANCE_BASE64_DECODER.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(StringLookupFactory.INSTANCE_BASE64_DECODER.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java index 4d05b81..b31e9b9 100644 --- a/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java @@ -35,4 +35,10 @@ public class Base64EncoderStringLookupTest { Assertions.assertNull(StringLookupFactory.INSTANCE_BASE64_ENCODER.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(StringLookupFactory.INSTANCE_BASE64_ENCODER.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/BiFunctionStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/BiFunctionStringLookupTest.java index c8e2588..bff1341 100644 --- a/src/test/java/org/apache/commons/text/lookup/BiFunctionStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/BiFunctionStringLookupTest.java @@ -39,7 +39,7 @@ public class BiFunctionStringLookupTest { if (keyCandidate.isEmpty()) { return m.get(k); } - Object value = m.get(keyCandidate); + final Object value = m.get(keyCandidate); if (value instanceof Map) { return this.nestedMapBiFunction.apply(StringUtils.substringAfter(k, SEPARATOR), (Map<String, Object>) value); @@ -83,11 +83,27 @@ public class BiFunctionStringLookupTest { } @Test + public void testDefaultMethod() { + Assertions.assertNull(BiFunctionStringLookup.on(new BiFunction<String, Object, Object>() { + + @Override + public Object apply(final String t, final Object u) { + return null; + } + }).lookup(null, null)); + } + + @Test public void testHashMapNull() { Assertions.assertNull(BiFunctionStringLookup.on(new HashMap<>()).lookup(null)); } @Test + public void testNullBiFunction() { + Assertions.assertNull(BiFunctionStringLookup.on((BiFunction<String, Object, Object>) null).lookup(null)); + } + + @Test public void testOne() { final String key = "key"; final String value = "value"; @@ -96,4 +112,10 @@ public class BiFunctionStringLookupTest { Assertions.assertEquals(value, FunctionStringLookup.on(map).lookup(key)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(BiFunctionStringLookup.on(new HashMap<>()).toString().isEmpty()); + } + } 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 cdbd807..a5fff89 100644 --- a/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupBasicTest.java @@ -30,6 +30,11 @@ public class ConstantStringLookupBasicTest { /** * Test fixture. */ + public static final String NULL_STRING_FIXTURE = null; + + /** + * Test fixture. + */ public static final String STRING_FIXTURE = "Hello World!"; /** @@ -54,8 +59,31 @@ public class ConstantStringLookupBasicTest { } @Test + public void testNullClassFetch() { + Assertions.assertNull(new ConstantStringLookup() { + @Override + protected Class<?> fetchClass(final String className) throws ClassNotFoundException { + return null; + } + }.lookup("foo")); + } + + @Test + public void testNullValue() { + Assertions.assertEquals(NULL_STRING_FIXTURE, ConstantStringLookup.INSTANCE + .lookup(ConstantStringLookupBasicTest.class.getName() + ".NULL_STRING_FIXTURE")); + } + + @Test public void testOne() { - Assertions.assertEquals(STRING_FIXTURE, ConstantStringLookup.INSTANCE - .lookup(ConstantStringLookupBasicTest.class.getName() + ".STRING_FIXTURE")); + Assertions.assertEquals(STRING_FIXTURE, + ConstantStringLookup.INSTANCE.lookup(ConstantStringLookupBasicTest.class.getName() + ".STRING_FIXTURE")); } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(ConstantStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupTest.java index fb1c4ca..aa25946 100644 --- a/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ConstantStringLookupTest.java @@ -80,7 +80,7 @@ public class ConstantStringLookupTest { @Test public void testLookupInvalidSyntax() { Assertions.assertNull(stringLookup.lookup("InvalidVariableName"), - "Non null return value for invalid variable name"); + "Non null return value for invalid variable name"); } /** @@ -89,7 +89,7 @@ public class ConstantStringLookupTest { @Test public void testLookupNonExisting() { Assertions.assertNull(stringLookup.lookup(variable("NO_FIELD")), - "Non null return value for non existing constant"); + "Non null return value for non existing constant"); } /** @@ -117,7 +117,7 @@ public class ConstantStringLookupTest { @Test public void testLookupPrivate() { Assertions.assertNull(stringLookup.lookup(variable("PRIVATE_FIELD")), - "Non null return value for non accessible field"); + "Non null return value for non accessible field"); } /** @@ -126,14 +126,13 @@ public class ConstantStringLookupTest { @Test public void testLookupUnknownClass() { Assertions.assertNull(stringLookup.lookup("org.apache.commons.configuration.NonExistingConfig." + FIELD), - "Non null return value for unknown class"); + "Non null return value for unknown class"); } /** * Generates the name of a variable for a lookup operation based on the given field name of this class. * - * @param field - * the field name + * @param field the field name * @return the variable for looking up this field */ private String variable(final String field) { diff --git a/src/test/java/org/apache/commons/text/lookup/DateStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/DateStringLookupTest.java index 2371942..6550883 100644 --- a/src/test/java/org/apache/commons/text/lookup/DateStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/DateStringLookupTest.java @@ -19,12 +19,14 @@ package org.apache.commons.text.lookup; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** @@ -33,6 +35,12 @@ import org.junit.jupiter.api.Test; public class DateStringLookupTest { @Test + public void testBadFormat() { + assertThrows(IllegalArgumentException.class, + () -> DateStringLookup.INSTANCE.lookup("this-is-a-bad-format-dontcha-know")); + } + + @Test public void testDefault() throws ParseException { final String formatted = DateStringLookup.INSTANCE.lookup(null); DateFormat.getInstance().parse(formatted); // throws ParseException @@ -41,14 +49,20 @@ public class DateStringLookupTest { @Test public void testFormat() { - final String fomat = "yyyy-MM-dd"; - final String value = DateStringLookup.INSTANCE.lookup(fomat); + final String format = "yyyy-MM-dd"; + final String value = DateStringLookup.INSTANCE.lookup(format); // System.out.println(value); assertNotNull(value, "No Date"); - final SimpleDateFormat format = new SimpleDateFormat(fomat); - final String today = format.format(new Date()); + final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); + final String today = simpleDateFormat.format(new Date()); assertEquals(value, today); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(DateStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java index 0fa967a..24621a6 100644 --- a/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java @@ -41,30 +41,30 @@ public class DefaultStringLookupTest { @Test public void testIndividualEnums() { assertSame(DefaultStringLookup.BASE64_DECODER.getStringLookup(), - StringLookupFactory.INSTANCE.base64DecoderStringLookup()); + StringLookupFactory.INSTANCE.base64DecoderStringLookup()); assertSame(DefaultStringLookup.BASE64_ENCODER.getStringLookup(), - StringLookupFactory.INSTANCE.base64EncoderStringLookup()); + StringLookupFactory.INSTANCE.base64EncoderStringLookup()); assertSame(DefaultStringLookup.CONST.getStringLookup(), StringLookupFactory.INSTANCE.constantStringLookup()); assertSame(DefaultStringLookup.DATE.getStringLookup(), StringLookupFactory.INSTANCE.dateStringLookup()); assertSame(DefaultStringLookup.DNS.getStringLookup(), StringLookupFactory.INSTANCE.dnsStringLookup()); assertSame(DefaultStringLookup.ENVIRONMENT.getStringLookup(), - StringLookupFactory.INSTANCE.environmentVariableStringLookup()); + StringLookupFactory.INSTANCE.environmentVariableStringLookup()); assertSame(DefaultStringLookup.FILE.getStringLookup(), StringLookupFactory.INSTANCE.fileStringLookup()); assertSame(DefaultStringLookup.JAVA.getStringLookup(), StringLookupFactory.INSTANCE.javaPlatformStringLookup()); assertSame(DefaultStringLookup.LOCAL_HOST.getStringLookup(), - StringLookupFactory.INSTANCE.localHostStringLookup()); + StringLookupFactory.INSTANCE.localHostStringLookup()); assertSame(DefaultStringLookup.PROPERTIES.getStringLookup(), - StringLookupFactory.INSTANCE.propertiesStringLookup()); + StringLookupFactory.INSTANCE.propertiesStringLookup()); assertSame(DefaultStringLookup.RESOURCE_BUNDLE.getStringLookup(), - StringLookupFactory.INSTANCE.resourceBundleStringLookup()); + StringLookupFactory.INSTANCE.resourceBundleStringLookup()); assertSame(DefaultStringLookup.SCRIPT.getStringLookup(), StringLookupFactory.INSTANCE.scriptStringLookup()); assertSame(DefaultStringLookup.SYSTEM_PROPERTIES.getStringLookup(), - StringLookupFactory.INSTANCE.systemPropertyStringLookup()); + StringLookupFactory.INSTANCE.systemPropertyStringLookup()); assertSame(DefaultStringLookup.URL.getStringLookup(), StringLookupFactory.INSTANCE.urlStringLookup()); assertSame(DefaultStringLookup.URL_DECODER.getStringLookup(), - StringLookupFactory.INSTANCE.urlDecoderStringLookup()); + StringLookupFactory.INSTANCE.urlDecoderStringLookup()); assertSame(DefaultStringLookup.URL_ENCODER.getStringLookup(), - StringLookupFactory.INSTANCE.urlEncoderStringLookup()); + StringLookupFactory.INSTANCE.urlEncoderStringLookup()); assertSame(DefaultStringLookup.XML.getStringLookup(), StringLookupFactory.INSTANCE.xmlStringLookup()); } diff --git a/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java index e3af1ab..f72e55d 100644 --- a/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/DnsStringLookupTest.java @@ -61,7 +61,7 @@ public class DnsStringLookupTest { final String address = InetAddress.getLocalHost().getHostAddress(); final InetAddress[] localHostAll = InetAddress.getAllByName(address); boolean matched = false; - for (InetAddress localHost : localHostAll) { + for (final InetAddress localHost : localHostAll) { if (localHost.getHostName().equals(DnsStringLookup.INSTANCE.lookup("name|" + address + ""))) { matched = true; } @@ -74,4 +74,10 @@ public class DnsStringLookupTest { Assertions.assertNull(DnsStringLookup.INSTANCE.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(DnsStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java index 4b398a6..aa5c51d 100644 --- a/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java @@ -42,4 +42,10 @@ public class EnvironmentVariableStringLookupTest { } } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java index 5b51b4b..611781d 100644 --- a/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java @@ -46,6 +46,13 @@ public class FileStringLookupTest { } @Test + public void testMissingFilePart() { + assertThrows(IllegalArgumentException.class, () -> { + FileStringLookup.INSTANCE.lookup(StandardCharsets.UTF_8.name()); + }); + } + + @Test public void testNull() { Assertions.assertNull(FileStringLookup.INSTANCE.lookup(null)); } @@ -55,6 +62,12 @@ public class FileStringLookupTest { final byte[] expectedBytes = Files.readAllBytes(Paths.get("src/test/resources/document.properties")); final String expectedString = new String(expectedBytes, StandardCharsets.UTF_8); Assertions.assertEquals(expectedString, - FileStringLookup.INSTANCE.lookup("UTF-8:src/test/resources/document.properties")); + FileStringLookup.INSTANCE.lookup("UTF-8:src/test/resources/document.properties")); + } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(FileStringLookup.INSTANCE.toString().isEmpty()); } } diff --git a/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java index 1ae122e..1a8f75c 100644 --- a/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java @@ -20,6 +20,7 @@ package org.apache.commons.text.lookup; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -40,6 +41,11 @@ public class FunctionStringLookupTest { } @Test + public void testNullFunction() { + Assertions.assertNull(FunctionStringLookup.on((Function<String, Object>) null).lookup(null)); + } + + @Test public void testOne() { final String key = "key"; final String value = "value"; @@ -48,4 +54,10 @@ public class FunctionStringLookupTest { Assertions.assertEquals(value, FunctionStringLookup.on(map).lookup(key)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(FunctionStringLookup.on(new HashMap<>()).toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java index 78f97e1..800aaab 100644 --- a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java @@ -61,6 +61,24 @@ public class InterpolatorStringLookupTest { // System.out.println(key + " = " + value); } + private void check(final StringLookup lookup) { + String value = lookup.lookup("sys:" + TESTKEY); + assertEquals(TESTVAL, value); + value = lookup.lookup("env:PATH"); + assertNotNull(value); + value = lookup.lookup("date:yyyy-MM-dd"); + assertNotNull(value, "No Date"); + final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + final String today = format.format(new Date()); + assertEquals(value, today); + assertLookupNotEmpty(lookup, "java:version"); + assertLookupNotEmpty(lookup, "java:runtime"); + assertLookupNotEmpty(lookup, "java:vm"); + assertLookupNotEmpty(lookup, "java:os"); + assertLookupNotEmpty(lookup, "java:locale"); + assertLookupNotEmpty(lookup, "java:hardware"); + } + @Test public void testLookup() { final Map<String, String> map = new HashMap<>(); @@ -87,26 +105,22 @@ public class InterpolatorStringLookupTest { @Test public void testLookupWithDefaultInterpolator() { - final StringLookup lookup = new InterpolatorStringLookup(); - String value = lookup.lookup("sys:" + TESTKEY); - assertEquals(TESTVAL, value); - value = lookup.lookup("env:PATH"); - assertNotNull(value); - value = lookup.lookup("date:yyyy-MM-dd"); - assertNotNull(value, "No Date"); - final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - final String today = format.format(new Date()); - assertEquals(value, today); - assertLookupNotEmpty(lookup, "java:version"); - assertLookupNotEmpty(lookup, "java:runtime"); - assertLookupNotEmpty(lookup, "java:vm"); - assertLookupNotEmpty(lookup, "java:os"); - assertLookupNotEmpty(lookup, "java:locale"); - assertLookupNotEmpty(lookup, "java:hardware"); + check(new InterpolatorStringLookup()); + } + + @Test + public void testLookupWithNullDefaultInterpolator() { + check(new InterpolatorStringLookup((StringLookup) null)); } @Test public void testNull() { Assertions.assertNull(InterpolatorStringLookup.INSTANCE.lookup(null)); } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(new InterpolatorStringLookup().toString().isEmpty()); + } } diff --git a/src/test/java/org/apache/commons/text/lookup/JavaPlatformStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/JavaPlatformStringLookupTest.java index 8cbb54c..704bf70 100644 --- a/src/test/java/org/apache/commons/text/lookup/JavaPlatformStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/JavaPlatformStringLookupTest.java @@ -47,6 +47,12 @@ public class JavaPlatformStringLookupTest { } @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(JavaPlatformStringLookup.INSTANCE.toString().isEmpty()); + } + + @Test public void testVm() { final String key = "vm"; assertTrue(JavaPlatformStringLookup.INSTANCE.lookup(key).contains(System.getProperty("java.vm.name"))); diff --git a/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java index 0d8ade7..032891d 100644 --- a/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/LocalHostStringLookupTest.java @@ -31,19 +31,24 @@ public class LocalHostStringLookupTest { @Test public void testAddress() throws UnknownHostException { Assertions.assertEquals(InetAddress.getLocalHost().getHostAddress(), - LocalHostStringLookup.INSTANCE.lookup("address")); + LocalHostStringLookup.INSTANCE.lookup("address")); + } + + @Test + public void testBadKey() throws UnknownHostException { + Assertions.assertThrows(IllegalArgumentException.class, () -> LocalHostStringLookup.INSTANCE.lookup("FOO")); } @Test public void testCanonicalName() throws UnknownHostException { Assertions.assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), - LocalHostStringLookup.INSTANCE.lookup("canonical-name")); + LocalHostStringLookup.INSTANCE.lookup("canonical-name")); } @Test public void testName() throws UnknownHostException { Assertions.assertEquals(InetAddress.getLocalHost().getHostName(), - LocalHostStringLookup.INSTANCE.lookup("name")); + LocalHostStringLookup.INSTANCE.lookup("name")); } @Test @@ -51,4 +56,10 @@ public class LocalHostStringLookupTest { Assertions.assertNull(LocalHostStringLookup.INSTANCE.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(LocalHostStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java index cb2feb5..1cb3677 100644 --- a/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java @@ -31,4 +31,10 @@ public class NullStringLookupTest { Assertions.assertEquals(null, StringLookupFactory.INSTANCE_NULL.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(StringLookupFactory.INSTANCE_NULL.toString().isEmpty()); + } + } 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 c57348b..b16ea97 100644 --- a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java @@ -17,6 +17,9 @@ package org.apache.commons.text.lookup; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.util.HashMap; import java.util.Map; @@ -36,7 +39,7 @@ public class PropertiesStringLookupTest { @Test public void testInterpolator() { final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator(); - Assertions.assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_PATH + "}")); + assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_PATH + "}")); } @Test @@ -44,10 +47,10 @@ public class PropertiesStringLookupTest { final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator(); // Need to handle "C:" in the sys prop user.dir. final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_PATH + "}"); - Assertions.assertEquals( - "${properties:" + System.getProperty("user.dir") + "/src/test/resources/document.properties::mykey}", - replaced); - Assertions.assertEquals("Hello World!", stringSubstitutor.replace(replaced)); + assertEquals( + "${properties:" + System.getProperty("user.dir") + "/src/test/resources/document.properties::mykey}", + replaced); + assertEquals("Hello World!", stringSubstitutor.replace(replaced)); } @Test @@ -55,10 +58,10 @@ public class PropertiesStringLookupTest { final Map<String, String> map = new HashMap<>(); map.put("KeyIsHere", KEY); final StringSubstitutor stringSubstitutor = new StringSubstitutor( - StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); + StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); final String replaced = stringSubstitutor.replace("$${properties:" + DOC_PATH + "::${KeyIsHere}}"); - Assertions.assertEquals("${properties:" + DOC_PATH + "::mykey}", replaced); - Assertions.assertEquals("Hello World!", stringSubstitutor.replace(replaced)); + assertEquals("${properties:" + DOC_PATH + "::mykey}", replaced); + assertEquals("Hello World!", stringSubstitutor.replace(replaced)); } @Test @@ -66,12 +69,21 @@ public class PropertiesStringLookupTest { final Map<String, String> map = new HashMap<>(); map.put("KeyIsHere", KEY); final StringSubstitutor stringSubstitutor = new StringSubstitutor( - StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); + StringLookupFactory.INSTANCE.interpolatorStringLookup(map)); final String replaced = stringSubstitutor - .replace("$${properties:${sys:user.dir}/" + DOC_PATH + "::${KeyIsHere}}"); - Assertions.assertEquals("${properties:" + System.getProperty("user.dir") + "/" + DOC_PATH + "::mykey}", - replaced); - Assertions.assertEquals("Hello World!", stringSubstitutor.replace(replaced)); + .replace("$${properties:${sys:user.dir}/" + DOC_PATH + "::${KeyIsHere}}"); + assertEquals("${properties:" + System.getProperty("user.dir") + "/" + DOC_PATH + "::mykey}", replaced); + assertEquals("Hello World!", stringSubstitutor.replace(replaced)); + } + + @Test + public void testMissingFile() { + assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.lookup("MissingFile")); + } + + @Test + public void testMissingKey() { + assertThrows(IllegalArgumentException.class, () -> PropertiesStringLookup.INSTANCE.lookup(DOC_PATH)); } @Test @@ -81,7 +93,13 @@ public class PropertiesStringLookupTest { @Test public void testOne() { - Assertions.assertEquals("Hello World!", PropertiesStringLookup.INSTANCE.lookup(KEY_PATH)); + assertEquals("Hello World!", PropertiesStringLookup.INSTANCE.lookup(KEY_PATH)); + } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(PropertiesStringLookup.INSTANCE.toString().isEmpty()); } } 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 3c2d02c..d362d6d 100644 --- a/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java @@ -37,7 +37,7 @@ public class ResourceBundleStringLookupTest { final String bundleName = TEST_RESOURCE_BUNDLE; final String bundleKey = "key"; Assertions.assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey), - ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" + bundleKey)); + ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" + bundleKey)); } @Test @@ -71,7 +71,13 @@ public class ResourceBundleStringLookupTest { final String bundleName = TEST_RESOURCE_BUNDLE; final String bundleKey = "key"; Assertions.assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey), - new ResourceBundleStringLookup(bundleName).lookup(bundleKey)); + new ResourceBundleStringLookup(bundleName).lookup(bundleKey)); + } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(ResourceBundleStringLookup.INSTANCE.toString().isEmpty()); } } diff --git a/src/test/java/org/apache/commons/text/lookup/ScriptEngineFactoryHelper.java b/src/test/java/org/apache/commons/text/lookup/ScriptEngineFactoryHelper.java index 1a1600d..edd4465 100644 --- a/src/test/java/org/apache/commons/text/lookup/ScriptEngineFactoryHelper.java +++ b/src/test/java/org/apache/commons/text/lookup/ScriptEngineFactoryHelper.java @@ -23,9 +23,9 @@ import javax.script.ScriptEngineManager; /** Helper to see what ScriptEngineFactory are the classpath. */ public class ScriptEngineFactoryHelper { - public static void main(String[] args) { + public static void main(final String[] args) { final String indent = " "; - for (ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories()) { + for (final ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories()) { System.out.println(factory); System.out.println(indent + factory.getEngineName()); System.out.println(indent + factory.getEngineVersion()); diff --git a/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java index 19d4410..88f9c40 100644 --- a/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java @@ -80,4 +80,10 @@ public class ScriptStringLookupTest { ScriptStringLookup.INSTANCE.lookup(JS_NAME + ":true ? \"It Works\" : \"It Does Not Work\" ")); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(ScriptStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java index 57ba0b6..483c695 100644 --- a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java @@ -31,6 +31,12 @@ public class SystemPropertyStringLookupTest { } @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.toString().isEmpty()); + } + + @Test public void testUserName() { final String key = "user.name"; Assertions.assertEquals(System.getProperty(key), StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(key)); diff --git a/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java index 49c27dc..48518b7 100644 --- a/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java @@ -45,4 +45,10 @@ public class UrlDecoderStringLookupTest { Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello+World!")); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(UrlDecoderStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java index 801fe59..75fbb62 100644 --- a/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java @@ -35,5 +35,10 @@ public class UrlEncoderStringLookupTest { Assertions.assertNull(UrlEncoderStringLookup.INSTANCE.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(UrlEncoderStringLookup.INSTANCE.toString().isEmpty()); + } } diff --git a/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java index 095fea4..ced97c3 100644 --- a/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java @@ -41,6 +41,13 @@ public class UrlStringLookupTest { } @Test + public void testBadEncoding() { + assertThrows(IllegalArgumentException.class, () -> { + UrlStringLookup.INSTANCE.lookup("FOO:https://www.google.com"); + }); + } + + @Test public void testBadUrl() { assertThrows(IllegalArgumentException.class, () -> { UrlStringLookup.INSTANCE.lookup("UTF-8:BAD_URL"); @@ -64,8 +71,21 @@ public class UrlStringLookupTest { } @Test + public void testMissingUrl() { + assertThrows(IllegalArgumentException.class, () -> { + UrlStringLookup.INSTANCE.lookup("UTF-8"); + }); + } + + @Test public void testNull() { Assertions.assertNull(UrlStringLookup.INSTANCE.lookup(null)); } + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(UrlStringLookup.INSTANCE.toString().isEmpty()); + } + } diff --git a/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java index 41be0f9..1ca60ea 100644 --- a/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java @@ -17,6 +17,8 @@ package org.apache.commons.text.lookup; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,6 +27,18 @@ import org.junit.jupiter.api.Test; */ public class XmlStringLookupTest { + private static final String DOC_PATH = "src/test/resources/document.xml"; + + @Test + public void testBadXPath() { + assertThrows(IllegalArgumentException.class, () -> XmlStringLookup.INSTANCE.lookup("docName")); + } + + @Test + public void testMissingXPath() { + assertThrows(IllegalArgumentException.class, () -> XmlStringLookup.INSTANCE.lookup(DOC_PATH + ":" + "!JUNK!")); + } + @Test public void testNull() { Assertions.assertNull(XmlStringLookup.INSTANCE.lookup(null)); @@ -32,9 +46,14 @@ public class XmlStringLookupTest { @Test public void testOne() { - final String docName = "src/test/resources/document.xml"; final String xpath = "/root/path/to/node"; - Assertions.assertEquals("Hello World!", XmlStringLookup.INSTANCE.lookup(docName + ":" + xpath)); + Assertions.assertEquals("Hello World!", XmlStringLookup.INSTANCE.lookup(DOC_PATH + ":" + xpath)); + } + + @Test + public void testToString() { + // does not blow up and gives some kind of string. + Assertions.assertFalse(XmlStringLookup.INSTANCE.toString().isEmpty()); } }