You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/12/28 22:41:59 UTC
cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl TestResources.properties
craigmcc 2002/12/28 13:41:59
Modified: resources build.xml
resources/src/test/org/apache/commons/resources/impl
TestResources.properties
Added: resources/src/java/org/apache/commons/resources
Messages.java
resources/src/test/org/apache/commons/resources
MessagesTestCase.java
Log:
Add o.a.c.r.Messages to provide the message formatting and parameter
replacement services previously provided by MessageResources, but
factored so that you can use it with *any* Resources implementation.
Revision Changes Path
1.17 +3 -1 jakarta-commons-sandbox/resources/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/build.xml,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- build.xml 28 Dec 2002 03:56:29 -0000 1.16
+++ build.xml 28 Dec 2002 21:41:58 -0000 1.17
@@ -436,6 +436,8 @@
<batchtest>
<fileset dir="${build.home}/tests"
+ includes="org/apache/commons/resources/*TestCase.class"/>
+ <fileset dir="${build.home}/tests"
includes="org/apache/commons/resources/impl/*TestCase.class"/>
</batchtest>
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java
Index: Messages.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java,v 1.1 2002/12/28 21:41:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/12/28 21:41:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Locale;
/**
* <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>
*
* <p>Calls to <code>getMessage()</code> variants without a <code>Locale</code>
* argument are presumed to be requesting a message string in the default
* <code>Locale</code> for this JVM.</p>
*
* <p>When a <code>getString()</code> call to the underlying {@link Resources}
* instance fails or returns null, a suitable error message String will be
* returned.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/12/28 21:41:58 $
*/
public class Messages implements Serializable {
// ----------------------------------------------------------- Constructors
/**
* <p>Construct a {@link Messages} instance that wraps the specified
* {@link Resources} instance.</p>
*
* @param resources The {@link Resources} instance from which
* message strings are to be retrieved
*/
public Messages(Resources resources) {
this.resources = resources;
}
// ----------------------------------------------------- Instance Variables
/**
* <p>The default <code>Locale</code> for this JVM.</p>
*/
protected Locale defaultLocale = null;
/**
* <p>The {@link Resources} instance that we are wrapping.</p>
*/
protected Resources resources = null;
// ------------------------------------------------------------- Properties
/**
* <p>Return the {@link Resources} instance that we are wrapping.</p>
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
public String getMessage(Locale locale, String key, Object arg0) {
return (getMessage(resources, locale, key, arg0));
}
/**
* <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 Methods
/**
* <p>Return a text message for the specified key, for the default
* <code>Locale</code>.</p>
*
* @param resources {@link Resources} instance to retrieve the message from
* @param key Message key to retrieve
*/
public static String getMessage(Resources resources, String key) {
return (getMessage(resources, (Locale) null, key));
}
/**
* <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
*/
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);
}
/**
* <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
*/
public static String getMessage(Resources resources, String key,
Object args[]) {
return (getMessage(resources, (Locale) null, key, args));
}
/**
* <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 args Array of replacement values
*/
public static String getMessage(Resources resources, Locale locale,
String key, Object args[]) {
// FIXME - Cache MessageFormat instances?
String message = getMessage(resources, locale, key);
MessageFormat format = new MessageFormat(message);
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
*/
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
*/
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));
}
}
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java
Index: MessagesTestCase.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java,v 1.1 2002/12/28 21:41:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/12/28 21:41:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.resources.Messages;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
import org.apache.commons.resources.ResourcesKeyException;
import org.apache.commons.resources.impl.ResourceBundleResourcesFactory;
/**
* <p>Unit tests for
* <code>org.apache.commons.resources.Messages</code>.
* </p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/12/28 21:41:58 $
*/
public class MessagesTestCase
extends TestCase {
// ----------------------------------------------------- Instance Variables
// Configuration value for our resource bundles
protected static final String CONFIG = "org.apache.commons.resources.impl.TestResources";
// Logical name of the Resources instance to be created
protected static final String NAME = "name";
// The ResourcesFactory to use for acquiring the Resources
protected ResourcesFactory factory = null;
// The Resources instance containing our messages
protected Resources resources = null;
// The Messages instance to be tested
protected Messages messages = null;
// ----------------------------------------------------------- Constructors
public MessagesTestCase(String name) {
super(name);
}
// --------------------------------------------------- Overall Test Methods
// Set up instance variables required by this test case
public void setUp() throws Exception {
factory = new ResourceBundleResourcesFactory();
resources = factory.getResources(NAME, CONFIG);
messages = new Messages(resources);
}
// Return the tests included in this test suite
public static Test suite() {
return (new TestSuite(MessagesTestCase.class));
}
// Tear down the instance variables required by this test case
public void tearDown() {
messages = null;
resources = null;
factory = null;
}
// ------------------------------------------------ Individual Test Methods
// 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());
}
}
1.2 +1 -0 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.properties
Index: TestResources.properties
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.properties,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestResources.properties 28 Dec 2002 03:56:29 -0000 1.1
+++ TestResources.properties 28 Dec 2002 21:41:58 -0000 1.2
@@ -1,3 +1,4 @@
test.base=[Base] ONLY
test.specific=[Base] SPECIFIC
test.inherit=[Base] INHERIT
+test.message=[Base] REPLACE {0} WITH {1}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>