Author: markt Date: Wed Apr 8 11:08:17 2009 New Revision: 763183 URL: http://svn.apache.org/viewvc?rev=763183&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=46933 Simplify StringManager using Java 5 features. Based on a patch by Jens Kapitza. Include a test case to ensure simplification is OK for null input
Added: tomcat/trunk/test/org/apache/tomcat/util/ tomcat/trunk/test/org/apache/tomcat/util/res/ tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java (with props) Modified: tomcat/trunk/java/org/apache/tomcat/util/res/StringManager.java Modified: tomcat/trunk/java/org/apache/tomcat/util/res/StringManager.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/res/StringManager.java?rev=763183&r1=763182&r2=763183&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/res/StringManager.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/res/StringManager.java Wed Apr 8 11:08:17 2009 @@ -131,107 +131,17 @@ * @param args */ - public String getString(String key, Object[] args) { - String iString = null; + public String getString(final String key, final Object... args) { String value = getString(key); - - // this check for the runtime exception is some pre 1.1.6 - // VM's don't do an automatic toString() on the passed in - // objects and barf out - - try { - // ensure the arguments are not null so pre 1.2 VM's don't barf - if(args==null){ - args = new Object[1]; - } - - Object[] nonNullArgs = args; - for (int i=0; i<args.length; i++) { - if (args[i] == null) { - if (nonNullArgs==args){ - nonNullArgs=args.clone(); - } - nonNullArgs[i] = "null"; - } - } - if( value==null ) value=key; - MessageFormat mf = new MessageFormat(value); - mf.setLocale(locale); - iString = mf.format(nonNullArgs, new StringBuffer(), null).toString(); - } catch (IllegalArgumentException iae) { - StringBuffer buf = new StringBuffer(); - buf.append(value); - for (int i = 0; i < args.length; i++) { - buf.append(" arg[" + i + "]=" + args[i]); - } - iString = buf.toString(); + if (value == null) { + value = key; } - return iString; - } - - /** - * Get a string from the underlying resource bundle and format it - * with the given object argument. This argument can of course be - * a String object. - * - * @param key - * @param arg - */ - - public String getString(String key, Object arg) { - Object[] args = new Object[] {arg}; - return getString(key, args); - } - - /** - * Get a string from the underlying resource bundle and format it - * with the given object arguments. These arguments can of course - * be String objects. - * - * @param key - * @param arg1 - * @param arg2 - */ - - public String getString(String key, Object arg1, Object arg2) { - Object[] args = new Object[] {arg1, arg2}; - return getString(key, args); - } - - /** - * Get a string from the underlying resource bundle and format it - * with the given object arguments. These arguments can of course - * be String objects. - * - * @param key - * @param arg1 - * @param arg2 - * @param arg3 - */ - public String getString(String key, Object arg1, Object arg2, - Object arg3) { - Object[] args = new Object[] {arg1, arg2, arg3}; - return getString(key, args); + MessageFormat mf = new MessageFormat(value); + mf.setLocale(locale); + return mf.format(args, new StringBuffer(), null).toString(); } - /** - * Get a string from the underlying resource bundle and format it - * with the given object arguments. These arguments can of course - * be String objects. - * - * @param key - * @param arg1 - * @param arg2 - * @param arg3 - * @param arg4 - */ - - public String getString(String key, Object arg1, Object arg2, - Object arg3, Object arg4) { - Object[] args = new Object[] {arg1, arg2, arg3, arg4}; - return getString(key, args); - } // -------------------------------------------------------------- // STATIC SUPPORT METHODS // -------------------------------------------------------------- Added: tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java?rev=763183&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java (added) +++ tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java Wed Apr 8 11:08:17 2009 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tomcat.util.res; + +import junit.framework.TestCase; + +public class TestStringManager extends TestCase { + + private static final StringManager sm = + StringManager.getManager("org.apache.naming"); + + public void testNullKey() { + boolean iaeThrown = false; + + try { + sm.getString(null); + } catch (IllegalArgumentException iae) { + iaeThrown = true; + } + assertEquals("IAE not thrown on null key", true, iaeThrown); + } + + public void testBug46933() { + // Check null args are OK + sm.getString("namingContext.nameNotBound"); + sm.getString("namingContext.nameNotBound", (Object[]) null); + sm.getString("namingContext.nameNotBound", new Object[1]); + } +} Propchange: tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/trunk/test/org/apache/tomcat/util/res/TestStringManager.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org