commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmei...@apache.org
Subject svn commit: r781182 - in /commons/sandbox/resources/trunk/src: main/java/org/apache/commons/resources/Messages.java test/java/org/apache/commons/resources/MessagesTestCase.java
Date Tue, 02 Jun 2009 21:48:30 GMT
Author: dmeikle
Date: Tue Jun  2 21:48:30 2009
New Revision: 781182

URL: http://svn.apache.org/viewvc?rev=781182&view=rev
Log:
RESOURCES-17 - Messages API Refactoring

Modified:
    commons/sandbox/resources/trunk/src/main/java/org/apache/commons/resources/Messages.java
    commons/sandbox/resources/trunk/src/test/java/org/apache/commons/resources/MessagesTestCase.java

Modified: commons/sandbox/resources/trunk/src/main/java/org/apache/commons/resources/Messages.java
URL: http://svn.apache.org/viewvc/commons/sandbox/resources/trunk/src/main/java/org/apache/commons/resources/Messages.java?rev=781182&r1=781181&r2=781182&view=diff
==============================================================================
--- commons/sandbox/resources/trunk/src/main/java/org/apache/commons/resources/Messages.java
(original)
+++ commons/sandbox/resources/trunk/src/main/java/org/apache/commons/resources/Messages.java
Tue Jun  2 21:48:30 2009
@@ -28,9 +28,7 @@
 /**
  * <p>Wrapper around any {@link Resources} object that performs message
  * string lookups from the {@link Resources} instance, and parameter
- * replacement via <code>java.text.MessageFormat</code>.  For convenience,
- * the same functionality is also available via static methods that accept
- * a {@link Resources} parameter.</p>
+ * replacement via <code>java.text.MessageFormat</code>.
  *
  * <p>Calls to <code>getMessage()</code> variants without a <code>Locale</code>
  * argument are presumed to be requesting a message string in the default
@@ -54,148 +52,29 @@
      *  message strings are to be retrieved
      */
     public Messages(Resources resources) {
-
         this.resources = resources;
-
     }
 
-
     // ----------------------------------------------------- Instance Variables
 
-
     /**
      * <p>The {@link Resources} instance that we are wrapping.</p>
      */
     private Resources resources = null;
 
-
     // ------------------------------------------------------------- Properties
 
-
     /**
      * <p>Return the {@link Resources} instance that we are wrapping.</p>
      *
      * @return The wrapped resources.
      */
     public Resources getResources() {
-
         return (this.resources);
-
     }
 
-
     // --------------------------------------------------------- Public Methods
 
-
-    /**
-     * <p>Return a text message for the specified key, for the default
-     * <code>Locale</code>.</p>
-     *
-     * @param key Message key to retrieve
-     * @return The text message for the specified key.
-     */
-    public String getMessage(String key) {
-
-        return (getMessage(resources, key));
-
-    }
-
-
-    /**
-     * <p>Return a text message for the specified key, for the specified
-     * <code>Locale</code>.</p>
-     *
-     * @param locale <code>Locale</code> for which to retrieve the message
-     * @param key Message key to retrieve
-     * @return The text message for the specified key and locale.
-     */
-    public String getMessage(Locale locale, String key) {
-
-        return (getMessage(resources, locale, key));
-
-    }
-
-
-    /**
-     * <p>Return a text message for the specified key, for the default
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param key Message key to retrieve
-     * @param args Array of replacement values
-     * @return The text message with parametric replacement.
-     */
-    public String getMessage(String key, Object[] args) {
-        return getMessage(resources, key, args);
-    }
-
-
-    /**
-     * <p>Return a text message for the specified key, for the specified
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param locale <code>Locale</code> for which to retrieve the message
-     * @param key Message key to retrieve
-     * @param args Array of replacement values
-     * @return The text message for a spcified locale with parametric replacement.
-     */
-    public String getMessage(Locale locale, String key, Object[] args) {
-        return getMessage(resources, locale, key, args);
-    }
-
-
-    /**
-     * <p>Return a text message for the specified key, for the default
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param key Message key to retrieve
-     * @param arg0 Individual parameter replacement value
-     * @return The text message with parametric replacement.
-     */
-    public String getMessage(String key, Object arg0) {
-
-        return (getMessage(resources, key, arg0));
-
-    }
-
-
-    /**
-     * <p>Return a text message for the specified key, for the specified
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param locale <code>Locale</code> for which to retrieve the message
-     * @param key Message key to retrieve
-     * @param arg0 Individual parameter replacement value
-     * @return The text message for a spcified locale with parametric replacement.
-     */
-    public String getMessage(Locale locale, String key, Object arg0) {
-
-        return (getMessage(resources, locale, key, arg0));
-
-    }
-
-
-    // ------------------------------------------------------- Static Variables
-
-
-    /**
-     * <p>The {@link org.apache.commons.resources.ResourcesFactory} that will be used
by the
-     * <code>getMessages()</code> method.</p>
-     */
-    private static ResourcesFactory factory = null;
-
-
-    // --------------------------------------------------------- Static Methods
-
-    /**
-     * <p>Set the {@link org.apache.commons.resources.ResourcesFactory} that
-     *  will be used by the <code>getMessages()</code> method.</p>
-     *
-     * @param factory ResourcesFactory instance to set.
-     */
-    public static void setFactory(ResourcesFactory factory) {
-        Messages.factory = factory;
-    }
-
     /**
      * <p>Return a text message for the specified key, for the default
      * <code>Locale</code>.</p>
@@ -204,27 +83,19 @@
      * @param key Message key to retrieve
      * @return The text message.
      */
-    public static String getMessage(Resources resources, String key) {
-
-        return (getMessage(resources, (Locale) null, key));
-
+    public String getMessage(String key) {
+        return (getMessage(key, (Locale) null));
     }
 
-
     /**
      * <p>Return a text message for the specified key, for the specified
      * <code>Locale</code>.</p>
      *
-     * @param resources {@link Resources} instance to retrieve the message from
-     * @param locale <code>Locale</code> for which to retrieve the message
      * @param key Message key to retrieve
+     * @param locale <code>Locale</code> for which to retrieve the message
      * @return The text message.
      */
-    public static String getMessage(
-        Resources resources,
-        Locale locale,
-        String key) {
-
+    public String getMessage(String key, Locale locale) {
         String message = null;
         try {
             message = resources.getString(key, locale);
@@ -242,22 +113,16 @@
         return message;
     }
 
-
     /**
      * <p>Return a text message for the specified key, for the default
      * <code>Locale</code>, with parametric replacement.</p>
      *
-     * @param resources {@link Resources} instance to retrieve the message from
      * @param key Message key to retrieve
      * @param args Array of replacement values
      * @return The text message.
      */
-    public static String getMessage(
-        Resources resources,
-        String key,
-        Object[] args) {
-
-        return getMessage(resources, (Locale) null, key, args);
+    public String getMessage(String key, Object... args) {
+        return getMessage(key, (Locale) null, args);
     }
 
 
@@ -265,59 +130,20 @@
      * <p>Return a text message for the specified key, for the specified
      * <code>Locale</code>, with parametric replacement.</p>
      *
-     * @param resources {@link Resources} instance to retrieve the message from
-     * @param locale <code>Locale</code> for which to retrieve the message
      * @param key Message key to retrieve
+     * @param locale <code>Locale</code> for which to retrieve the message
      * @param args Array of replacement values
      * @return The text message.
      */
-    public static String getMessage(
-        Resources resources,
-        Locale locale,
-        String key,
-        Object[] args) {
-
+    public String getMessage(String key, Locale locale, Object... args) {
         // TODO - Cache MessageFormat instances?
-        String message = getMessage(resources, locale, key);
+        String message = getMessage(key, locale);
         MessageFormat format = new MessageFormat(message, locale);
         return (format.format(args));
     }
 
-
-    /**
-     * <p>Return a text message for the specified key, for the default
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param resources {@link Resources} instance to retrieve the message from
-     * @param key Message key to retrieve
-     * @param arg0 Individual parameter replacement value
-     * @return The text message.
-     */
-    public static String getMessage(Resources resources,
-                                    String key, Object arg0) {
-
-        return (getMessage(resources, (Locale) null, key, arg0));
-
-    }
-
-    /**
-     * <p>Return a text message for the specified key, for the specified
-     * <code>Locale</code>, with parametric replacement.</p>
-     *
-     * @param resources {@link Resources} instance to retrieve the message from
-     * @param locale <code>Locale</code> for which to retrieve the message
-     * @param key Message key to retrieve
-     * @param arg0 Individual parameter replacement value
-     * @return The text message.
-     */
-    public static String getMessage(
-        Resources resources,
-        Locale locale,
-        String key,
-        Object arg0) {
-
-        return getMessage(resources, locale, key, new Object[] { arg0 });
-    }
+   
+    // --------------------------------------------------------- Static Methods
 
     /**
      * <p>Convenience factory method to create a {@link Messages} instance
@@ -329,12 +155,13 @@
      * <code>LocalStrings_en_US.properties</code> for messages localized to
      * a particular <code>Locale</code>.</p>
      *
+     * @param factory the <code>ResourcesFactory</code> instance to use. 
+     *  If null is passed a <code>ResourceBundleResourcesFactory</code>.
      * @param name Package + file name of the properties file for which
      *  local message resources are desired (ie. org.apache.commons.resources.LocalStrings).
      * @return The text messages.
      */
-    public static Messages getMessages(String name) {
-
+    public static Messages getMessages(ResourcesFactory factory, String name) {
         if (factory == null) {
             factory = new ResourceBundleResourcesFactory();
         }
@@ -346,8 +173,5 @@
         } catch (ResourcesException e) {
             return (null);
         }
-
     }
-
-
 }

Modified: commons/sandbox/resources/trunk/src/test/java/org/apache/commons/resources/MessagesTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/resources/trunk/src/test/java/org/apache/commons/resources/MessagesTestCase.java?rev=781182&r1=781181&r2=781182&view=diff
==============================================================================
--- commons/sandbox/resources/trunk/src/test/java/org/apache/commons/resources/MessagesTestCase.java
(original)
+++ commons/sandbox/resources/trunk/src/test/java/org/apache/commons/resources/MessagesTestCase.java
Tue Jun  2 21:48:30 2009
@@ -92,8 +92,7 @@
         String message = null;
 
         Messages local =
-            Messages.getMessages(
-                "org.apache.commons.resources.impl.LocalStrings");
+            Messages.getMessages(null, "org.apache.commons.resources.impl.LocalStrings");
 
         assertNotNull("Local messages found", local);
 
@@ -116,15 +115,32 @@
         message = local.getMessage("local.missing");
         assertEquals("Correct missing message", "???local.missing???", message);
 
-
-        Messages messages2 = new MyMessages(null);
-        Messages messages3 = messages2.getMessages(null);
+        ResourcesFactory factory = new ResourcesFactory() {
+            public Resources getResources(String name)
+                             throws ResourcesException {
+                throw new ResourcesException("Bad Implementation");
+            }
+
+            public boolean isReturnNull() {
+                return false;
+            }
+
+            public void setReturnNull(boolean returnNull) {}
+
+            public Resources getResources(String name, String config) throws ResourcesException
{
+                return null;
+            }
+
+            public void release() throws ResourcesException {
+            }
+        };
+        Messages messages3 = Messages.getMessages(factory, null);
         assertNull("Invalid Messages", messages3);
 
         // Test for RESOURCES-7
-        message = local.getMessage(Locale.US, "local.localetest", new Object[]{new Integer(100)});
+        message = local.getMessage("local.localetest", Locale.US, new Object[]{new Integer(100)});
         assertEquals("Jane has $100.00", message);
-        message = local.getMessage(Locale.UK, "local.localetest", new Object[]{new Integer(100)});
+        message = local.getMessage("local.localetest", Locale.UK, new Object[]{new Integer(100)});
         assertEquals("Jane has \u00a3100.00", message); // u00a3 is pound sign for UTF-8
build
     }
 
@@ -139,7 +155,7 @@
         message = messages.getMessage("test.missing");
         assertEquals("Correct missing message", "???test.missing???", message);
 
-        message = messages.getMessage(Locale.ENGLISH, "test.message");
+        message = messages.getMessage("test.message", Locale.ENGLISH);
         assertEquals(
             "Correct individual message",
             "[Base] REPLACE {0} WITH {1}",
@@ -154,7 +170,7 @@
             message);
 
         message =
-            messages.getMessage(Locale.ENGLISH, "test.message", new String[] { "abc", "def"
});
+            messages.getMessage("test.message", Locale.ENGLISH, new String[] { "abc", "def"
});
 
         assertEquals(
             "Correct replaced message",
@@ -162,7 +178,7 @@
             message);
 
         message =
-            messages.getMessage(Locale.ENGLISH, "test.message.single", "abc");
+            messages.getMessage("test.message.single", Locale.ENGLISH, "abc");
 
         assertEquals(
             "Correct replaced message",
@@ -236,32 +252,4 @@
             resources == messages.getResources());
     }
 
-
-    class MyMessages extends Messages{
-
-        public MyMessages(Resources resources) {
-            super(resources);
-            ResourcesFactory factory = new ResourcesFactory() {
-                public Resources getResources(String name)
-                                 throws ResourcesException {
-                    throw new ResourcesException("Bad Implementation");
-                }
-
-                public boolean isReturnNull() {
-                    return false;
-                }
-
-                public void setReturnNull(boolean returnNull) {}
-
-                public Resources getResources(String name, String config) throws ResourcesException
{
-                    return null;
-                }
-
-                public void release() throws ResourcesException {
-                }
-            };
-            Messages.setFactory(factory);
-        }
-
-    }
 }



Mime
View raw message