Coverage Report - org.jbehave.core.i18n.LocalizedKeywords
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalizedKeywords
100%
23/23
100%
4/4
2
LocalizedKeywords$LocalizedKeywordNotFound
100%
2/2
N/A
2
LocalizedKeywords$ResourceBundleNotFound
100%
2/2
N/A
2
 
 1  
 package org.jbehave.core.i18n;
 2  
 
 3  
 import static java.util.ResourceBundle.getBundle;
 4  
 
 5  
 import java.util.HashMap;
 6  
 import java.util.Locale;
 7  
 import java.util.Map;
 8  
 import java.util.MissingResourceException;
 9  
 import java.util.ResourceBundle;
 10  
 
 11  
 import org.jbehave.core.configuration.Keywords;
 12  
 
 13  
 /**
 14  
  * Adds i18n support to Keywords, allowing to read the keywords from resource
 15  
  * bundles for a given locale.
 16  
  */
 17  
 public class LocalizedKeywords extends Keywords {
 18  
 
 19  1
     private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
 20  
     private static final String DEFAULT_BUNDLE_NAME = "i18n/keywords";
 21  1
     private static final ClassLoader DEFAULT_CLASS_LOADER = LocalizedKeywords.class.getClassLoader();
 22  
     private final Locale locale;
 23  
 
 24  
     public LocalizedKeywords() {
 25  1063
         this(DEFAULT_LOCALE);
 26  1063
     }
 27  
 
 28  
     public LocalizedKeywords(Locale locale) {
 29  1074
         this(locale, DEFAULT_BUNDLE_NAME, DEFAULT_CLASS_LOADER);
 30  1073
     }
 31  
 
 32  
     public LocalizedKeywords(Locale locale, String bundleName, ClassLoader classLoader) {
 33  1075
         super(keywords(bundleName, locale, classLoader));
 34  1073
         this.locale = locale;
 35  1073
     }
 36  
 
 37  
     public Locale getLocale(){
 38  5
         return locale;
 39  
     }
 40  
     
 41  
     private static Map<String, String> keywords(String bundleName, Locale locale,
 42  
             ClassLoader classLoader) {
 43  1075
         ResourceBundle bundle = lookupBunde(bundleName.trim(), locale, classLoader);
 44  1074
         Map<String, String> keywords = new HashMap<String, String>();
 45  1074
         for (String key : KEYWORDS) {
 46  20388
             keywords.put(key, keyword(key, bundle));
 47  
         }
 48  1073
         return keywords;
 49  
     }
 50  
 
 51  
     private static String keyword(String name, ResourceBundle bundle) {
 52  
         try {
 53  20388
             return bundle.getString(name);
 54  1
         } catch (MissingResourceException e) {
 55  1
             throw new LocalizedKeywordNotFound(name, bundle);
 56  
         }
 57  
     }
 58  
 
 59  
     private static ResourceBundle lookupBunde(String bundleName, Locale locale, ClassLoader classLoader) {
 60  
         try {            
 61  1075
             if (classLoader != null) {
 62  1074
                 return getBundle(bundleName, locale, classLoader);
 63  
             }
 64  1
             return getBundle(bundleName, locale);
 65  1
         } catch (MissingResourceException e) {
 66  1
             throw new ResourceBundleNotFound(bundleName, locale, classLoader, e);
 67  
         }
 68  
     }
 69  
 
 70  
     @SuppressWarnings("serial")
 71  
     public static class ResourceBundleNotFound extends RuntimeException {
 72  
 
 73  
         public ResourceBundleNotFound(String bundleName, Locale locale, ClassLoader classLoader,
 74  
                 MissingResourceException cause) {
 75  1
             super("Resource bundle " + bundleName + " not found for locale " + locale + " in classLoader "
 76  
                     + classLoader, cause);
 77  1
         }
 78  
 
 79  
     }
 80  
 
 81  
     @SuppressWarnings("serial")
 82  
     public static class LocalizedKeywordNotFound extends RuntimeException {
 83  
 
 84  
         public LocalizedKeywordNotFound(String name, ResourceBundle bundle) {
 85  1
             super("Keyword" + name + " not found in resource bundle " + bundle);
 86  1
         }
 87  
 
 88  
     }
 89  
 
 90  
 }