##### Coverage Report - org.jbehave.core.i18n.LocalizedKeywords

Classes in this File | Line Coverage | Branch Coverage | Complexity
LocalizedKeywords |
100% | 23/23
100% | 4/4
| 2.0;2
LocalizedKeywords$LocalizedKeywordNotFound |
100% | 2/2
N/A | N/A
| 2.0;2
LocalizedKeywords$ResourceBundleNotFound |
100% | 2/2
N/A | N/A
| 2.0;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 | 3975 |
             this(DEFAULT_LOCALE);
26 | 3975 |
         }
27 |  |
28 |  |
         public LocalizedKeywords(Locale locale) {
29 | 3998 |
             this(locale, DEFAULT_BUNDLE_NAME, DEFAULT_CLASS_LOADER);
30 | 3997 |
         }
31 |  |
32 |  |
         public LocalizedKeywords(Locale locale, String bundleName, ClassLoader classLoader) {
33 | 3999 |
             super(keywords(bundleName, locale, classLoader));
34 | 3997 |
             this.locale = locale;
35 | 3997 |
         }
36 |  |
37 |  |
         public Locale getLocale(){
38 | 28 |
             return locale;
39 |  |
         }
40 |  |
41 |  |
         private static Map<String, String> keywords(String bundleName, Locale locale,
42 |  |
                 ClassLoader classLoader) {
43 | 3999 |
             ResourceBundle bundle = lookupBunde(bundleName.trim(), locale, classLoader);
44 | 3998 |
             Map<String, String> keywords = new HashMap<String, String>();
45 | 3998 |
             for (String key : KEYWORDS) {
46 |  |
                 try {
47 | 119911 |
                     keywords.put(key, bundle.getString(key));
48 | 1 |
                 } catch (MissingResourceException e) {
49 | 1 |
                     throw new LocalizedKeywordNotFound(key, bundleName, locale);
50 | 119910 |
                 }
51 |  |
             }
52 | 3997 |
             return keywords;
53 |  |
         }
54 |  |
55 |  |
         private static ResourceBundle lookupBunde(String bundleName, Locale locale, ClassLoader classLoader) {
56 |  |
             try {
57 | 3999 |
                 if (classLoader != null) {
58 | 3998 |
                     return getBundle(bundleName, locale, classLoader);
59 |  |
                 }
60 | 1 |
                 return getBundle(bundleName, locale);
61 | 1 |
             } catch (MissingResourceException e) {
62 | 1 |
                 throw new ResourceBundleNotFound(bundleName, locale, classLoader, e);
63 |  |
             }
64 |  |
         }
65 |  |
66 |  |
         @SuppressWarnings("serial")
67 |  |
         public static class ResourceBundleNotFound extends RuntimeException {
68 |  |
69 |  |
             public ResourceBundleNotFound(String bundleName, Locale locale, ClassLoader classLoader,
70 |  |
                     MissingResourceException cause) {
71 | 1 |
                 super("Resource bundle " + bundleName + " not found for locale " + locale + " in classLoader "
72 |  |
                         + classLoader, cause);
73 | 1 |
             }
74 |  |
75 |  |
         }
76 |  |
77 |  |
         @SuppressWarnings("serial")
78 |  |
         public static class LocalizedKeywordNotFound extends RuntimeException {
79 |  |
80 |  |
             public LocalizedKeywordNotFound(String key, String bundleName, Locale locale) {
81 | 1 |
                 super("Keyword " + key + " not found for locale " + locale + " in bundle "+bundleName);
82 | 1 |
             }
83 |  |
84 |  |
         }
85 |  |
86 |  |
     }