Author: mbenson
Date: Fri Mar 7 16:30:04 2008
New Revision: 634874
URL: http://svn.apache.org/viewvc?rev=634874&view=rev
Log:
get ExtendedMessageFormat working on JDK 1.3, sort of
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/ExtendedMessageFormat.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/ExtendedMessageFormatTest.java
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/ExtendedMessageFormat.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/ExtendedMessageFormat.java?rev=634874&r1=634873&r2=634874&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/ExtendedMessageFormat.java
(original)
+++
commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/ExtendedMessageFormat.java
Fri Mar 7 16:30:04 2008
@@ -192,8 +192,10 @@
toPattern = insertFormats(super.toPattern(), foundDescriptions);
if (containsElements(foundFormats)) {
Format[] origFormats = getFormats();
- for (int i = 0; i < origFormats.length; i++) {
- Format f = (Format) foundFormats.get(i);
+ //only loop over what we know we have, as MessageFormat on Java
1.3 seems to provide an extra format element:
+ int i = 0;
+ for (Iterator it = foundFormats.iterator(); it.hasNext(); i++) {
+ Format f = (Format) it.next();
if (f != null) {
origFormats[i] = f;
}
Modified:
commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/ExtendedMessageFormatTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/ExtendedMessageFormatTest.java?rev=634874&r1=634873&r2=634874&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/ExtendedMessageFormatTest.java
(original)
+++
commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/ExtendedMessageFormatTest.java
Fri Mar 7 16:30:04 2008
@@ -27,6 +27,8 @@
import java.util.Locale;
import java.util.Map;
+import org.apache.commons.lang.SystemUtils;
+
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -83,7 +85,7 @@
public void testExtendedFormats() {
String pattern = "Lower: {0,lower} Upper: {1,upper}";
ExtendedMessageFormat emf = new ExtendedMessageFormat(pattern,
registry);
- assertEquals("TOPATTERN", pattern, emf.toPattern());
+ assertPatternsEqual("TOPATTERN", pattern, emf.toPattern());
assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"foo",
"bar"}));
assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"Foo",
"Bar"}));
assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"FOO",
"BAR"}));
@@ -120,7 +122,7 @@
expected.append(df.format(args[1]));
expected.append(" Salary: ");
expected.append(nf.format(args[2]));
- assertEquals(pattern, emf.toPattern());
+ assertPatternsEqual(null, pattern, emf.toPattern());
assertEquals("" + testLocales[i], expected.toString(),
emf.format(args));
}
}
@@ -133,7 +135,7 @@
// public void testExtendedWithChoiceFormat() {
// String pattern = "Choice: {0,choice,1.0#{1,lower}|2.0#{1,upper}}";
// ExtendedMessageFormat emf = new ExtendedMessageFormat(pattern,
registry);
-// assertEquals(pattern, emf.toPattern());
+// assertPatterns(null, pattern, emf.toPattern());
// try {
// assertEquals("one", emf.format(new Object[] {new Integer(1),
"ONE"}));
// assertEquals("TWO", emf.format(new Object[] {new Integer(2),
"two"}));
@@ -165,7 +167,7 @@
// cf = NumberFormat.getCurrencyInstance(testLocales[i]);
// emf = new ExtendedMessageFormat(pattern, testLocales[i],
registry);
// }
-// assertEquals(pattern, emf.toPattern());
+// assertPatterns(null, pattern, emf.toPattern());
// try {
// String lowExpected = lowArgs[0] + " low " +
nf.format(lowArgs[2]);
// String highExpected = highArgs[0] + " HIGH " +
cf.format(highArgs[2]);
@@ -261,7 +263,14 @@
emf = new ExtendedMessageFormat(pattern, locale);
}
assertEquals("format " + buffer.toString(), mf.format(args),
emf.format(args));
- assertEquals("toPattern " + buffer.toString(), mf.toPattern(),
emf.toPattern());
+ assertPatternsEqual("toPattern " + buffer.toString(), mf.toPattern(),
emf.toPattern());
+ }
+
+ //can't trust what MessageFormat does with toPattern() pre 1.4:
+ private void assertPatternsEqual(String message, String expected, String
actual) {
+ if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
+ assertEquals(message, expected, actual);
+ }
}
// ------------------------ Test Formats ------------------------