[ https://issues.apache.org/struts/browse/SHALE-485?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43510#action_43510 ]
Tim Kroeger commented on SHALE-485: ----------------------------------- Index: /home/tkroeger/projects/work_shale/shale-framework/shale-test/src/main/java/org/apache/shale/test/mock/MockApplication12.java =================================================================== --- /home/tkroeger/projects/work_shale/shale-framework/shale-test/src/main/java/org/apache/shale/test/mock/MockApplication12.java (revision 636687) +++ /home/tkroeger/projects/work_shale/shale-framework/shale-test/src/main/java/org/apache/shale/test/mock/MockApplication12.java (working copy) @@ -115,10 +115,10 @@ * this application.</p> * * @param name Name under which to add this resource bundle - * @param bundle ResourceBundle to add + * @param baseName base name of ResourceBundle to add */ - public void addResourceBundle(String name, ResourceBundle bundle) { - bundles.put(name, bundle); + public void addResourceBundle(String name, String baseName) { + bundles.put(name, baseName); } @@ -123,8 +123,8 @@ /** - * <p>Return a <code>Map</code> of the resource bundles configured - * for this application, keyed by name.</p> + * <p>Return a <code>Map</code> of the resource bundle base names + * configured for this application, keyed by name.</p> */ public Map getResourceBundles() { return bundles; @@ -250,6 +250,9 @@ if ((context == null) || (name == null)) { throw new NullPointerException(); } + if (!bundles.containsKey(name)) { + return null; + } Locale locale = null; UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { @@ -258,7 +261,16 @@ if (locale == null) { locale = Locale.getDefault(); } - return ResourceBundle.getBundle(name, locale); + String baseName = (String) bundles.get(name); + ResourceBundle result = null; + + if (null != baseName) { + result = ResourceBundle.getBundle(baseName, + locale, + Thread.currentThread(). + getContextClassLoader()); + } + return result; } > ResourceBundle lookup failure in MockApplication12 > -------------------------------------------------- > > Key: SHALE-485 > URL: https://issues.apache.org/struts/browse/SHALE-485 > Project: Shale > Issue Type: Bug > Components: Test > Affects Versions: 1.0.4 > Environment: Java 5, JSF 1.2, i686 GNU/Linux > Reporter: Tim Kroeger > > MockApplication12.getResourceBundle(FacesContext context, String name) is > intended to lookup the resource bundle's base name prior to requesting the > resource via ResourceBundle.getBundle(name, locale). In the current > implementation it tries to lookup the name of the resource registered with > the application, which is wrong. Consider a resource bundle added via > ResourceBundle bundle = ResourceBundle.getBundle("foo.bar.resource", > new Locale("de", "DE")); > ((MockApplication12) application).addResourceBundle("fooBarResource", > bundle); > and e.g. a validator you want to test which composes a new localized > FacesMessage by doing > context.getApplication().getResourceBundle(context, > "fooBarResource").getString("message"); > which simulates what would happen in an application where you configured your > resource bundles via faces-config.xml. > This will throw a MissingResourceException since > public ResourceBundle getResourceBundle(FacesContext context, String > name) { > if ((context == null) || (name == null)) { > throw new NullPointerException(); > } > Locale locale = null; > UIViewRoot viewRoot = context.getViewRoot(); > if (viewRoot != null) { > locale = viewRoot.getLocale(); > } > if (locale == null) { > locale = Locale.getDefault(); > } > return ResourceBundle.getBundle(name, locale); > } > tries to lookup "fooBarResource" with it's classLoader. Instead of that, one > should either do something like: > public ResourceBundle getResourceBundle(FacesContext context, String > name) { > if ((context == null) || (name == null)) { > throw new NullPointerException(); > } > if (!bundles.containsKey(name)) { > return null; > } > Locale locale = null; > UIViewRoot viewRoot = context.getViewRoot(); > if (viewRoot != null) { > locale = viewRoot.getLocale(); > } > if (locale == null) { > locale = Locale.getDefault(); > } > return bundles.get(name); > } > which completely drops any locale context but at least returns a resource > bundle, that was added with the corresponding key > OR > one could go ahead and implement a solution, where only the mapping 'name -> > baseName' is stored in a map instead of mappings to ResourceBundles, so > MockApplication12.getResourceBundle can lookup the baseName via the provided > name parameter as key to the bundles Map property and then utilize > ResourceBundle.getBundle() to lookup the bundle with the desired locale. That > at least is, what Sun does in it's > com.sun.faces.application.ApplicationAssociate which is used by > com.sun.faces.application.ApplicationImpl. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.