Author: luc
Date: Sun Nov 14 15:06:35 2010
New Revision: 1035004

URL: http://svn.apache.org/viewvc?rev=1035004&view=rev
Log:
fixed message building so the behavior matches the existing javadoc:
general arguments are the ones remaining after the specific ones have been 
handled

Added:
    
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
   (with props)
Modified:
    
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java

Modified: 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java?rev=1035004&r1=1035003&r2=1035004&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
 Sun Nov 14 15:06:35 2010
@@ -61,15 +61,30 @@ public class MessageFactory {
                                       Localizable specific,
                                       Localizable general,
                                       Object ... arguments) {
+
         final StringBuilder sb = new StringBuilder();
-        MessageFormat fmt = null;
+        final MessageFormat generalFmt  = new 
MessageFormat(general.getLocalizedString(locale), locale);
+        Object[] generalArgs = arguments;
+
         if (specific != null) {
-            fmt = new MessageFormat(specific.getLocalizedString(locale), 
locale);
-            sb.append(fmt.format(arguments));
+
+            final MessageFormat specificFmt = new 
MessageFormat(specific.getLocalizedString(locale), locale);
+
+            // split the arguments: first specific ones then general ones
+            final int nbSpecific = Math.min(arguments.length, 
specificFmt.getFormatsByArgumentIndex().length);
+            final int nbGeneral  = arguments.length - nbSpecific;
+            Object[] specificArgs = new Object[nbSpecific];
+            System.arraycopy(arguments, 0, specificArgs, 0, nbSpecific);
+            generalArgs = new Object[nbGeneral];
+            System.arraycopy(arguments, nbSpecific, generalArgs, 0, nbGeneral);
+
+            // build the message
+            sb.append(specificFmt.format(specificArgs));
             sb.append(": ");
+
         }
-        fmt = new MessageFormat(general.getLocalizedString(locale), locale);
-        sb.append(fmt.format(arguments));
+
+        sb.append(generalFmt.format(generalArgs));
 
         return sb.toString();
     }

Added: 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java?rev=1035004&view=auto
==============================================================================
--- 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
 (added)
+++ 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
 Sun Nov 14 15:06:35 2010
@@ -0,0 +1,35 @@
+/*
+ * 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.commons.math.exception.util;
+
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MessageFactoryTest {
+
+    @Test
+    public void testSpecificGeneric() {
+        Localizable specific = new DummyLocalizable("specific {0} - {1} - 
{2}");
+        Localizable general  = new DummyLocalizable("general  {0} / {1}");
+        String message = MessageFactory.buildMessage(Locale.FRENCH, specific, 
general,
+                                                     0, 1, 2, 'a', 'b');
+        Assert.assertEquals("specific 0 - 1 - 2: general  a / b", message);
+    }
+
+}

Propchange: 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision


Reply via email to