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 96c5873  Increase code coverage.
96c5873 is described below

commit 96c58731b1da39606f5240294d348dd9b4d2a69b
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Jul 17 17:15:10 2020 -0400

    Increase code coverage.
---
 pom.xml                                            |  6 ++--
 .../commons/text/lookup/AbstractStringLookup.java  | 14 +++++++++
 .../text/lookup/PropertiesStringLookup.java        | 16 +++++------
 .../text/lookup/ResourceBundleStringLookup.java    | 18 +++++++++---
 .../text/lookup/PropertiesStringLookupTest.java    | 16 +++++------
 .../lookup/ResourceBundleStringLookupTest.java     | 33 ++++++++++++++--------
 6 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/pom.xml b/pom.xml
index d787acc..e6ef57a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,6 +47,7 @@
     
<commons.scmPubUrl>https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-text</commons.scmPubUrl>
     
<commons.scmPubCheckoutDirectory>site-content</commons.scmPubCheckoutDirectory>
 
+    <commons.mockito.version>3.4.3</commons.mockito.version>
     <checkstyle.plugin.version>3.1.1</checkstyle.plugin.version>
     <checkstyle.version>8.34</checkstyle.version>
 
@@ -99,8 +100,9 @@
     </dependency>
     <dependency>
       <groupId>org.mockito</groupId>
-      <artifactId>mockito-core</artifactId>
-      <version>3.4.3</version>
+      <!--  Use mockito-inline instead of mockito-core to mock and spy on 
final classes. -->
+      <artifactId>mockito-inline</artifactId>
+      <version>${commons.mockito.version}</version>
       <scope>test</scope>
     </dependency>
   </dependencies>
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 3229c03..6858ce3 100644
--- a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
@@ -37,6 +37,20 @@ abstract class AbstractStringLookup implements StringLookup {
     protected static final String SPLIT_STR = String.valueOf(SPLIT_CH);
 
     /**
+     * Creates a lookup key for a given file and key.
+     */
+    static String toLookupKey(final String left, final String right) {
+        return toLookupKey(left, SPLIT_STR, right);
+    }
+
+    /**
+     * Creates a lookup key for a given file and key.
+     */
+    static String toLookupKey(final String left, final String separator, final 
String right) {
+        return left + separator + right;
+    }
+
+    /**
      * Returns the substring after the first occurrence of {@code ch} in 
{@code value}.
      *
      * @param value The source string.
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 fd177d0..2f5d9a2 100644
--- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
@@ -40,22 +40,22 @@ import org.apache.commons.lang3.StringUtils;
  */
 final class PropertiesStringLookup extends AbstractStringLookup {
 
+    /**
+     * Defines the singleton for this class.
+     */
+    static final PropertiesStringLookup INSTANCE = new 
PropertiesStringLookup();
+
     /** 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;
+    static String toPropertyKey(final String file, final String key) {
+        return AbstractStringLookup.toLookupKey(file, SEPARATOR, key);
     }
 
     /**
-     * Defines the singleton for this class.
-     */
-    static final PropertiesStringLookup INSTANCE = new 
PropertiesStringLookup();
-
-    /**
      * No need to build instances for now.
      */
     private PropertiesStringLookup() {
@@ -83,7 +83,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 %s.", key,
-                toLookupKey("DocumentPath", "Key"));
+                toPropertyKey("DocumentPath", "Key"));
         }
         final String documentPath = keys[0];
         final String propertyKey = StringUtils.substringAfter(key, SEPARATOR);
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 b31c489..cd27168 100644
--- 
a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
+++ 
b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
@@ -45,9 +45,11 @@ final class ResourceBundleStringLookup extends 
AbstractStringLookup {
     private final String bundleName;
 
     /**
-     * No need to build instances for now.
+     * Constructs a blank instance.
+     *
+     * This ctor is not private to allow Mockito spying.
      */
-    private ResourceBundleStringLookup() {
+    ResourceBundleStringLookup() {
         this(null);
     }
 
@@ -61,6 +63,15 @@ final class ResourceBundleStringLookup extends 
AbstractStringLookup {
         this.bundleName = bundleName;
     }
 
+    ResourceBundle getBundle(final String keyBundleName) {
+        // The ResourceBundle class caches bundles, no need to cache here.
+        return ResourceBundle.getBundle(keyBundleName);
+    }
+
+    String getString(final String keyBundleName, final String bundleKey) {
+        return getBundle(keyBundleName).getString(bundleKey);
+    }
+
     /**
      * Looks up the value for the key in the format "BundleName:BundleKey".
      *
@@ -90,8 +101,7 @@ final class ResourceBundleStringLookup extends 
AbstractStringLookup {
         final String keyBundleName = anyBundle ? keys[0] : bundleName;
         final String bundleKey = anyBundle ? keys[1] : keys[0];
         try {
-            // The ResourceBundle class caches bundles, no need to cache here.
-            return 
ResourceBundle.getBundle(keyBundleName).getString(bundleKey);
+            return getString(keyBundleName, bundleKey);
         } catch (final MissingResourceException e) {
             // The key is missing, return null such that an interpolator can 
supply a default value.
             return null;
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 40af31b..4c076bf 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 = 
PropertiesStringLookup.toLookupKey(DOC_PATH, KEY);
+    private static final String KEY_PATH = 
PropertiesStringLookup.toPropertyKey(DOC_PATH, KEY);
 
     @Test
     public void testInterpolator() {
@@ -60,8 +60,8 @@ public class PropertiesStringLookupTest {
         final StringSubstitutor stringSubstitutor = new StringSubstitutor(
             StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
         final String replaced = stringSubstitutor
-            .replace("$${properties:" + 
PropertiesStringLookup.toLookupKey(DOC_PATH, "${KeyIsHere}}"));
-        assertEquals("${properties:" + 
PropertiesStringLookup.toLookupKey(DOC_PATH, "mykey}"), replaced);
+            .replace("$${properties:" + 
PropertiesStringLookup.toPropertyKey(DOC_PATH, "${KeyIsHere}}"));
+        assertEquals("${properties:" + 
PropertiesStringLookup.toPropertyKey(DOC_PATH, "mykey}"), replaced);
         assertEquals("Hello World!", stringSubstitutor.replace(replaced));
     }
 
@@ -71,10 +71,10 @@ public class PropertiesStringLookupTest {
         map.put("KeyIsHere", KEY);
         final StringSubstitutor stringSubstitutor = new StringSubstitutor(
             StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
-        final String replaced = stringSubstitutor
-            .replace("$${properties:${sys:user.dir}/" + 
PropertiesStringLookup.toLookupKey(DOC_PATH, "${KeyIsHere}}"));
+        final String replaced = stringSubstitutor.replace(
+            "$${properties:${sys:user.dir}/" + 
PropertiesStringLookup.toPropertyKey(DOC_PATH, "${KeyIsHere}}"));
         assertEquals("${properties:" + System.getProperty("user.dir") + "/"
-            + PropertiesStringLookup.toLookupKey(DOC_PATH, "mykey}"), 
replaced);
+            + PropertiesStringLookup.toPropertyKey(DOC_PATH, "mykey}"), 
replaced);
         assertEquals("Hello World!", stringSubstitutor.replace(replaced));
     }
 
@@ -85,8 +85,8 @@ public class PropertiesStringLookupTest {
 
     @Test
     public void testMissingFileWithKey() {
-        assertThrows(IllegalArgumentException.class,
-            () -> 
PropertiesStringLookup.INSTANCE.lookup(PropertiesStringLookup.toLookupKey("MissingFile",
 "AnyKey")));
+        assertThrows(IllegalArgumentException.class, () -> 
PropertiesStringLookup.INSTANCE
+            .lookup(PropertiesStringLookup.toPropertyKey("MissingFile", 
"AnyKey")));
     }
 
     @Test
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 142d4d8..536a1fe 100644
--- 
a/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java
+++ 
b/src/test/java/org/apache/commons/text/lookup/ResourceBundleStringLookupTest.java
@@ -19,6 +19,8 @@ package org.apache.commons.text.lookup;
 
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
 
 import java.util.ResourceBundle;
 
@@ -30,14 +32,15 @@ import org.junit.jupiter.api.Test;
  */
 public class ResourceBundleStringLookupTest {
 
+    private static final String KEY = "key";
     private static final String TEST_RESOURCE_BUNDLE = 
"org.example.testResourceBundleLookup";
 
     @Test
     public void testAny() {
         final String bundleName = TEST_RESOURCE_BUNDLE;
-        final String bundleKey = "key";
+        final String bundleKey = KEY;
         
Assertions.assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey),
-            ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" + 
bundleKey));
+            
ResourceBundleStringLookup.INSTANCE.lookup(AbstractStringLookup.toLookupKey(bundleName,
 bundleKey)));
     }
 
     @Test
@@ -45,20 +48,28 @@ public class ResourceBundleStringLookupTest {
         final String bundleName = TEST_RESOURCE_BUNDLE;
         final String bundleKey = "bad_key";
         assertNull(new 
ResourceBundleStringLookup(bundleName).lookup(bundleKey));
-        assertNull(ResourceBundleStringLookup.INSTANCE.lookup(bundleName + ":" 
+ bundleKey));
+        
assertNull(ResourceBundleStringLookup.INSTANCE.lookup(AbstractStringLookup.toLookupKey(bundleName,
 bundleKey)));
     }
 
     @Test
     public void testBadNames() {
-        
assertNull(ResourceBundleStringLookup.INSTANCE.lookup("BAD_RESOURCE_BUNDLE_NAME:KEY"));
+        assertNull(ResourceBundleStringLookup.INSTANCE
+            
.lookup(AbstractStringLookup.toLookupKey("BAD_RESOURCE_BUNDLE_NAME", "KEY")));
     }
 
     @Test
     public void testDoubleBundle() {
-        final String bundleName = TEST_RESOURCE_BUNDLE;
-        final String bundleKey = "key";
-        assertThrows(IllegalArgumentException.class,
-            () -> new 
ResourceBundleStringLookup(bundleName).lookup("OtherBundle:" + bundleKey));
+        assertThrows(IllegalArgumentException.class, () -> new 
ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE)
+            .lookup(AbstractStringLookup.toLookupKey("OtherBundle", KEY)));
+    }
+
+    @Test
+    public void testExceptionGettingString() {
+        final ResourceBundleStringLookup mockLookup = 
spy(ResourceBundleStringLookup.class);
+        when(mockLookup.getString(TEST_RESOURCE_BUNDLE, 
KEY)).thenThrow(ClassCastException.class);
+        assertThrows(IllegalArgumentException.class, () -> {
+            
mockLookup.lookup(AbstractStringLookup.toLookupKey(TEST_RESOURCE_BUNDLE, KEY));
+        });
     }
 
     @Test
@@ -75,10 +86,8 @@ public class ResourceBundleStringLookupTest {
 
     @Test
     public void testOne() {
-        final String bundleName = TEST_RESOURCE_BUNDLE;
-        final String bundleKey = "key";
-        
Assertions.assertEquals(ResourceBundle.getBundle(bundleName).getString(bundleKey),
-            new ResourceBundleStringLookup(bundleName).lookup(bundleKey));
+        
Assertions.assertEquals(ResourceBundle.getBundle(TEST_RESOURCE_BUNDLE).getString(KEY),
+            new ResourceBundleStringLookup(TEST_RESOURCE_BUNDLE).lookup(KEY));
     }
 
     @Test

Reply via email to