fixed message building so the behavior matches the existing javadoc:
general arguments are the ones remaining after the specific ones have been handled git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1035004 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7662c49fff
commit
85fec9ef02
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue