You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/10/02 02:24:14 UTC

cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources MessagesTestCase.java

dgraham     2003/10/01 17:24:13

  Modified:    resources/src/java/org/apache/commons/resources
                        Messages.java
               resources/src/test/org/apache/commons/resources
                        MessagesTestCase.java
  Log:
  Removed Messages methods that provided 2-4 replacement
  arguments that were carried over from Struts.  This makes
  the interface easier to understand and the implementation
  simpler.  The methods that are left provide for 0, 1, or an
  array of replacement arguments.
  
  Also added logging in empty catch block.
  
  Revision  Changes    Path
  1.6       +39 -260   jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java
  
  Index: Messages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Messages.java	28 Sep 2003 21:15:23 -0000	1.5
  +++ Messages.java	2 Oct 2003 00:24:13 -0000	1.6
  @@ -65,6 +65,8 @@
   import java.text.MessageFormat;
   import java.util.Locale;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   import org.apache.commons.resources.impl.ResourceBundleResourcesFactory;
   
   /**
  @@ -83,10 +85,16 @@
    * returned.</p>
    *
    * @author Craig R. McClanahan
  + * @author David Graham
    * @version $Revision$ $Date$
    */
   public class Messages implements Serializable {
   
  +    /**
  +     * Commons Logging instance.
  +     */
  +    private static final Log log = LogFactory.getLog(Messages.class);
  +    
   
       // ----------------------------------------------------------- Constructors
   
  @@ -211,110 +219,6 @@
       }
   
   
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     */
  -    public String getMessage(String key, Object arg0, Object arg1) {
  -
  -        return (getMessage(resources, key, arg0, arg1));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     */
  -    public String getMessage(Locale locale, String key, Object arg0,
  -                             Object arg1) {
  -
  -        return (getMessage(resources, locale, key, arg0, arg1));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     */
  -    public String getMessage(String key, Object arg0, Object arg1,
  -                             Object arg2) {
  -
  -        return (getMessage(resources, key, arg0, arg1, arg2));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     */
  -    public String getMessage(Locale locale, String key, Object arg0,
  -                             Object arg1, Object arg2) {
  -
  -        return (getMessage(resources, locale, key, arg0, arg1, arg2));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     * @param arg3 Individual parameter replacement value
  -     */
  -    public String getMessage(String key, Object arg0, Object arg1,
  -                             Object arg2, Object arg3) {
  -
  -        return (getMessage(resources, key, arg0, arg1, arg2, arg3));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     * @param arg3 Individual parameter replacement value
  -     */
  -    public String getMessage(Locale locale, String key, Object arg0,
  -                             Object arg1, Object arg2, Object arg3) {
  -
  -        return (getMessage(resources, locale, key, arg0, arg1, arg2, arg3));
  -
  -    }
  -
  -
       // ------------------------------------------------------- Static Variables
   
   
  @@ -350,21 +254,25 @@
        * @param locale <code>Locale</code> for which to retrieve the message
        * @param key Message key to retrieve
        */
  -    public static String getMessage(Resources resources, Locale locale,
  -                                    String key) {
  +	public static String getMessage(
  +		Resources resources,
  +		Locale locale,
  +		String key) {
   
  -        String message = null;
  -        try {
  -            message = resources.getString(key, locale, null);
  -        } catch (ResourcesException e) {
  -            ;
  -        }
  -        if (message == null) {
  -            message = "???" + key + "???";
  -        }
  -        return (message);
  +		String message = null;
  +		try {
  +			message = resources.getString(key, locale, null);
   
  -    }
  +		} catch (ResourcesException e) {
  +			log.debug("Failed retrieving message for key: '" + key + "'.", e);
  +		}
  +
  +		if (message == null) {
  +			message = "???" + key + "???";
  +		}
  +
  +		return message;
  +	}
   
   
       /**
  @@ -399,11 +307,10 @@
   		String key,
   		Object[] args) {
   
  -		// FIXME - Cache MessageFormat instances?
  +		// TODO - Cache MessageFormat instances?
   		String message = getMessage(resources, locale, key);
   		MessageFormat format = new MessageFormat(message);
   		return (format.format(args));
  -
   	}
   
   
  @@ -422,7 +329,6 @@
   
       }
   
  -
       /**
        * <p>Return a text message for the specified key, for the specified
        * <code>Locale</code>, with parametric replacement.</p>
  @@ -432,143 +338,14 @@
        * @param key Message key to retrieve
        * @param arg0 Individual parameter replacement value
        */
  -    public static String getMessage(Resources resources, Locale locale,
  -                                    String key, Object arg0) {
  -
  -        Object args[] = new Object[1];
  -        args[0] = arg0;
  -        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 resources {@link Resources} instance to retrieve the message from
  -     * @param key Message key to retrieve
  -     * @param arg0 Individual parameter replacement value
  -     * @param arg1 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources,
  -                                    String key, Object arg0, Object arg1) {
  -
  -        return (getMessage(resources, (Locale) null, key, arg0, arg1));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources, Locale locale,
  -                                    String key, Object arg0, Object arg1) {
  -
  -        Object args[] = new Object[2];
  -        args[0] = arg0;
  -        args[1] = arg1;
  -        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 resources {@link Resources} instance to retrieve the message from
  -     * @param key Message key to retrieve
  -     * @param arg0 Individual parameter replacement value
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources,
  -                                    String key, Object arg0, Object arg1,
  -                                    Object arg2) {
  -
  -        return (getMessage(resources, (Locale) null, key, arg0, arg1, arg2));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources, Locale locale,
  -                                    String key, Object arg0, Object arg1,
  -                                    Object arg2) {
  -
  -        Object args[] = new Object[3];
  -        args[0] = arg0;
  -        args[1] = arg1;
  -        args[2] = arg2;
  -        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 resources {@link Resources} instance to retrieve the message from
  -     * @param key Message key to retrieve
  -     * @param arg0 Individual parameter replacement value
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     * @param arg3 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources,
  -                                    String key, Object arg0, Object arg1,
  -                                    Object arg2, Object arg3) {
  -
  -        return (getMessage(resources, (Locale) null, key, arg0, arg1,
  -                           arg2, arg3));
  -
  -    }
  -
  -
  -    /**
  -     * <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
  -     * @param arg1 Individual parameter replacement value
  -     * @param arg2 Individual parameter replacement value
  -     * @param arg3 Individual parameter replacement value
  -     */
  -    public static String getMessage(Resources resources, Locale locale,
  -                                    String key, Object arg0, Object arg1,
  -                                    Object arg2, Object arg3) {
  -
  -        Object args[] = new Object[4];
  -        args[0] = arg0;
  -        args[1] = arg1;
  -        args[2] = arg2;
  -        args[3] = arg3;
  -        return (getMessage(resources, locale, key, args));
  -
  -    }
  +	public static String getMessage(
  +		Resources resources,
  +		Locale locale,
  +		String key,
  +		Object arg0) {
   
  +		return getMessage(resources, locale, key, new Object[] { arg0 });
  +	}
   
       /**
        * <p>Convenience factory method to create a {@link Messages} instance
  @@ -588,9 +365,11 @@
           if (factory == null) {
               factory = new ResourceBundleResourcesFactory();
           }
  +        
           try {
               Resources resources = factory.getResources(name);
               return (new Messages(resources));
  +            
           } catch (ResourcesException e) {
               return (null);
           }
  
  
  
  1.5       +69 -52    jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java
  
  Index: MessagesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MessagesTestCase.java	19 Mar 2003 19:02:07 -0000	1.4
  +++ MessagesTestCase.java	2 Oct 2003 00:24:13 -0000	1.5
  @@ -59,17 +59,14 @@
    *
    */
   
  -
   package org.apache.commons.resources;
   
  -
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
   import org.apache.commons.resources.impl.ResourceBundleResourcesFactory;
   
  -
   /**
    * <p>Unit tests for
    * <code>org.apache.commons.resources.Messages</code>.
  @@ -79,8 +76,7 @@
    * @version $Revision$ $Date$
    */
   
  -public class MessagesTestCase
  -    extends TestCase {
  +public class MessagesTestCase extends TestCase {
   
   
       // ----------------------------------------------------- Instance Variables
  @@ -138,51 +134,72 @@
       // ------------------------------------------------ Individual Test Methods
   
   
  -    // Test the use of the shortcut getMessages() method
  -    public void testLocal() {
  -        String message = null;
  -        Messages local =
  -            Messages.getMessages("org.apache.commons.resources.impl.LocalStrings");
  +    /**
  +     * Test the use of the shortcut getMessages() method.
  +     */
  +	public void testLocal() {
  +		String message = null;
  +        
  +		Messages local =
  +			Messages.getMessages(
  +				"org.apache.commons.resources.impl.LocalStrings");
  +		
           assertNotNull("Local messages found", local);
  -        message = local.getMessage("local.message");
  -        assertEquals("Correct individual message",
  -                     "[Local] REPLACE {0} WITH {1}",
  -                     message);
  -        message = local.getMessage("local.message", "abc", "def");
  -        assertEquals("Correct replaced message",
  -                     "[Local] REPLACE abc WITH def",
  -                     message);
  -        message = local.getMessage("local.missing");
  -        assertEquals("Correct missing message",
  -                     "???local.missing???",
  -                     message);
  -    }
  -
   
  -    // Test the ability to retrieve messages with replacements
  -    public void testMessages() {
  -        String message = null;
  -        message = messages.getMessage("test.message");
  -        assertEquals("Correct individual message",
  -                     "[Base] REPLACE {0} WITH {1}",
  -                     message);
  -        message = messages.getMessage("test.message", "abc", "def");
  -        assertEquals("Correct replaced message",
  -                     "[Base] REPLACE abc WITH def",
  -                     message);
  -        message = messages.getMessage("test.missing");
  -        assertEquals("Correct missing message",
  -                     "???test.missing???",
  -                     message);
  -    }
  -
  -
  -    // Test the characteristics of a newly created instance
  -    public void testPristine() {
  -        assertNotNull(messages);
  -        assertTrue("Correct wrapped resources",
  -                   resources == messages.getResources());
  -    }
  +		message = local.getMessage("local.message");
  +		
  +        assertEquals(
  +			"Correct individual message",
  +			"[Local] REPLACE {0} WITH {1}",
  +			message);
  +
  +		message =
  +			local.getMessage("local.message", new String[] { "abc", "def" });
  +            
  +		assertEquals(
  +			"Correct replaced message",
  +			"[Local] REPLACE abc WITH def",
  +			message);
  +
  +		message = local.getMessage("local.missing");
  +		assertEquals("Correct missing message", "???local.missing???", message);
  +	}
  +
  +
  +    /**
  +     * Test the ability to retrieve messages with replacements.
  +     */
  +	public void testMessages() {
  +		String message = null;
  +
  +		message = messages.getMessage("test.message");
  +		assertEquals(
  +			"Correct individual message",
  +			"[Base] REPLACE {0} WITH {1}",
  +			message);
  +
  +		message =
  +			messages.getMessage("test.message", new String[] { "abc", "def" });
  +		
  +        assertEquals(
  +			"Correct replaced message",
  +			"[Base] REPLACE abc WITH def",
  +			message);
  +
  +		message = messages.getMessage("test.missing");
  +		assertEquals("Correct missing message", "???test.missing???", message);
  +	}
  +
  +
  +    /**
  +     * Test the characteristics of a newly created instance.
  +     */
  +	public void testPristine() {
  +		assertNotNull(messages);
  +		assertTrue(
  +			"Correct wrapped resources",
  +			resources == messages.getResources());
  +	}
   
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org