jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1795657 - in /jmeter/trunk: src/core/org/apache/jmeter/util/JMeterUtils.java test/src/org/apache/jmeter/util/TestJMeterUtils.java
Date Sat, 20 May 2017 14:21:26 GMT
Author: fschumacher
Date: Sat May 20 14:21:26 2017
New Revision: 1795657

URL: http://svn.apache.org/viewvc?rev=1795657&view=rev
Log:
Handle uninitialized RessourceBundle more gracefully.

Bugzilla Id: 61050

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/util/JMeterUtils.java
    jmeter/trunk/test/src/org/apache/jmeter/util/TestJMeterUtils.java

Modified: jmeter/trunk/src/core/org/apache/jmeter/util/JMeterUtils.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/util/JMeterUtils.java?rev=1795657&r1=1795656&r2=1795657&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/util/JMeterUtils.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/util/JMeterUtils.java Sat May 20 14:21:26 2017
@@ -34,6 +34,9 @@ import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
@@ -495,6 +498,7 @@ public class JMeterUtils implements Unit
     private static String getResStringDefault(String key, String defaultValue) {
         return getResStringDefault(key, defaultValue, null);
     }
+
     /**
      * Helper method to do the actual work of fetching resources; allows
      * getResString(S,S) to be deprecated without affecting getResString(S);
@@ -509,9 +513,10 @@ public class JMeterUtils implements Unit
         String resString = null;
         try {
             ResourceBundle bundle = resources;
-            if(forcedLocale != null) {
-                bundle = ResourceBundle.getBundle("org.apache.jmeter.resources.messages",
forcedLocale); // $NON-NLS-1$
+            if (forcedLocale != null || bundle == null) {
+                bundle = getBundle(forcedLocale);
             }
+            
             if (bundle.containsKey(resKey)) {
                 resString = bundle.getString(resKey);
             } else {
@@ -532,6 +537,43 @@ public class JMeterUtils implements Unit
     }
 
     /**
+     * Try to get a {@link ResourceBundle} for the given {@code forcedLocale}.
+     * If none is found try to fallback to the bundle for the set {@link Locale}
+     * 
+     * @param forcedLocale the {@link Locale} which should be used first
+     * @return the resolved {@link ResourceBundle} or {@code null}, if none could be found
+     */
+    private static ResourceBundle getBundle(Locale forcedLocale) {
+        for (Locale locale: Arrays.asList(forcedLocale, getLocale())) {
+            if(locale != null) {
+                ResourceBundle bundle = ResourceBundle.getBundle("org.apache.jmeter.resources.messages",
locale); // $NON-NLS-1$
+                if (bundle == null) {
+                    log.warn("Could not resolve ResourceBundle for Locale [{}]", locale);
+                } else {
+                    return bundle;
+                }
+            }
+        }
+        return new DummyResourceBundle();
+    }
+
+    /**
+     * Simple {@link ResourceBundle}, that handles questions for every key, by giving the
key back as an answer.
+     */
+    private static class DummyResourceBundle extends ResourceBundle {
+
+        @Override
+        protected Object handleGetObject(String key) {
+            return "[" + key + "]";
+        }
+
+        @Override
+        public Enumeration<String> getKeys() {
+            return Collections.emptyEnumeration();
+        }
+    };
+
+    /**
      * To get I18N label from properties file
      * 
      * @param key

Modified: jmeter/trunk/test/src/org/apache/jmeter/util/TestJMeterUtils.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/util/TestJMeterUtils.java?rev=1795657&r1=1795656&r2=1795657&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/util/TestJMeterUtils.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/util/TestJMeterUtils.java Sat May 20 14:21:26
2017
@@ -33,9 +33,14 @@ public class TestJMeterUtils {
         String sep = System.getProperty("line.separator");
         assertEquals("line one" + sep + "line two" + sep, JMeterUtils.getResourceFileAsText("resourcefile.txt"));
     }
-    
+
     @Test
     public void testGetResourceFileAsTextWithMisingResource() throws Exception{
         assertEquals("", JMeterUtils.getResourceFileAsText("not_existant_resourcefile.txt"));
     }
+
+    @Test
+    public void testGesResStringDefaultWithNonExistantKey() throws Exception {
+        assertEquals("[res_key=noValidKey]", JMeterUtils.getResString("noValidKey"));
+    }
 }



Mime
View raw message