harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r420829 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Formatter.java test/java/tests/api/java/util/FormatterTest.java
Date Tue, 11 Jul 2006 12:56:52 GMT
Author: gharley
Date: Tue Jul 11 05:56:51 2006
New Revision: 420829

URL: http://svn.apache.org/viewvc?rev=420829&view=rev
Log:
HARMONY 818 : [luni] Implementation: Format BigInteger for java.util.Formatter

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java?rev=420829&r1=420828&r2=420829&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Formatter.java
Tue Jul 11 05:56:51 2006
@@ -25,6 +25,7 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
 import java.security.AccessController;
@@ -539,7 +540,7 @@
                     lastArgument = argument;
                     hasLastArgumentSet = true;
                 }
-                result = transformer.transform(this, token, argument);
+                result = transformer.transform(token, argument);
                 result = (null == result ? plainText : plainText + result);
             }
             // if output is made by formattable callback
@@ -557,7 +558,7 @@
     private Object getArgument(Object[] args, int index, FormatToken token,
             Object lastArgument, boolean hasLastArgumentSet) {
         if (index == FormatToken.LAST_ARGUMENT_INDEX && !hasLastArgumentSet) {
-            throw new MissingFormatArgumentException("<");
+            throw new MissingFormatArgumentException("<"); //$NON-NLS-1$
         }
 
         if (null == args) {
@@ -810,7 +811,7 @@
          * Gets the formatted string according to the format token and the
          * argument.
          */
-        String transform(Formatter formatter, FormatToken formatToken,
+        String transform(FormatToken formatToken,
                 Object arg) {
 
             /* init data member to print */
@@ -843,8 +844,8 @@
             case 'o':
             case 'x':
             case 'X': {
-                if (null == arg) {
-                    result = transformFromNull();
+                if (null == arg || arg instanceof BigInteger) {
+                    result = transformFromBigInteger();
                 } else {
                     result = transformFromInteger();
                 }
@@ -897,7 +898,7 @@
 
             if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
                     && !formatToken.isWidthSet()) {
-                throw new MissingFormatWidthException("-"
+                throw new MissingFormatWidthException("-" //$NON-NLS-1$
                         + formatToken.getConversionType());
             }
 
@@ -908,11 +909,11 @@
             }
 
             if (null == arg) {
-                result.append("false");
+                result.append("false"); //$NON-NLS-1$
             } else if (arg instanceof Boolean) {
                 result.append(arg);
             } else {
-                result.append("true");
+                result.append("true"); //$NON-NLS-1$
             }
             return padding(result, startIndex);
         }
@@ -928,7 +929,7 @@
 
             if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
                     && !formatToken.isWidthSet()) {
-                throw new MissingFormatWidthException("-"
+                throw new MissingFormatWidthException("-" //$NON-NLS-1$
                         + formatToken.getConversionType());
             }
 
@@ -939,7 +940,7 @@
             }
 
             if (null == arg) {
-                result.append("null");
+                result.append("null"); //$NON-NLS-1$
             } else {
                 result.append(Integer.toHexString(arg.hashCode()));
             }
@@ -956,7 +957,7 @@
 
             if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
                     && !formatToken.isWidthSet()) {
-                throw new MissingFormatWidthException("-"
+                throw new MissingFormatWidthException("-" //$NON-NLS-1$
                         + formatToken.getConversionType());
             }
 
@@ -1008,7 +1009,7 @@
 
             if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
                     && !formatToken.isWidthSet()) {
-                throw new MissingFormatWidthException("-"
+                throw new MissingFormatWidthException("-" //$NON-NLS-1$
                         + formatToken.getConversionType());
             }
 
@@ -1025,7 +1026,7 @@
             }
 
             if (null == arg) {
-                result.append("null");
+                result.append("null"); //$NON-NLS-1$
             } else {
                 if (arg instanceof Character) {
                     result.append(arg);
@@ -1062,14 +1063,14 @@
          * Precision is illegal.
          */
         private String transformFromPercent() {
-            StringBuilder result = new StringBuilder("%");
+            StringBuilder result = new StringBuilder("%"); //$NON-NLS-1$
 
             int startIndex = 0;
             int flags = formatToken.getFlags();
 
             if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
                     && !formatToken.isWidthSet()) {
-                throw new MissingFormatWidthException("-"
+                throw new MissingFormatWidthException("-" //$NON-NLS-1$
                         + formatToken.getConversionType());
             }
 
@@ -1109,7 +1110,7 @@
                         .doPrivileged(new PrivilegedAction<String>() {
 
                             public String run() {
-                                return System.getProperty("line.separator");
+                                return System.getProperty("line.separator"); //$NON-NLS-1$
                             }
                         });
             }
@@ -1187,6 +1188,18 @@
                 throw new IllegalFormatPrecisionException(formatToken
                         .getPrecision());
             }
+            if (arg instanceof Long) {
+                value = ((Long) arg).longValue();
+            } else if (arg instanceof Integer) {
+                value = ((Integer) arg).longValue();
+            } else if (arg instanceof Short) {
+                value = ((Short) arg).longValue();
+            } else if (arg instanceof Byte) {
+                value = ((Byte) arg).longValue();
+            } else {
+                throw new IllegalFormatConversionException(formatToken
+                        .getConversionType(), arg.getClass());
+            }
             if ('d' != formatToken.getConversionType()) {
                 if (formatToken.isFlagSet(FormatToken.FLAG_ADD)
                         | formatToken.isFlagSet(FormatToken.FLAG_SPACE)
@@ -1204,11 +1217,11 @@
                             formatToken.getStrFlags(), formatToken
                                     .getConversionType());
                 } else if ('o' == formatToken.getConversionType()) {
-                    result.append("0");
-                    startIndex += "0".length();
+                    result.append("0"); //$NON-NLS-1$
+                    startIndex += 1;
                 } else {
-                    result.append("0x");
-                    startIndex += "0x".length();
+                    result.append("0x"); //$NON-NLS-1$
+                    startIndex += 2;
                 }
             }
 
@@ -1217,18 +1230,6 @@
                 throw new IllegalFormatFlagsException(formatToken.getStrFlags());
             }
 
-            if (arg instanceof Long) {
-                value = ((Long) arg).longValue();
-            } else if (arg instanceof Integer) {
-                value = ((Integer) arg).longValue();
-            } else if (arg instanceof Short) {
-                value = ((Short) arg).longValue();
-            } else if (arg instanceof Byte) {
-                value = ((Byte) arg).longValue();
-            } else {
-                throw new IllegalFormatConversionException(formatToken
-                        .getConversionType(), arg.getClass());
-            }
 
             if (value < 0) {
                 isNegative = true;
@@ -1266,11 +1267,11 @@
             if (!isNegative) {
                 if (formatToken.isFlagSet(FormatToken.FLAG_ADD)) {
                     result.insert(0, '+');
-                    startIndex += "+".length();
+                    startIndex += 1;
                 }
                 if (formatToken.isFlagSet(FormatToken.FLAG_SPACE)) {
                     result.insert(0, ' ');
-                    startIndex += " ".length();
+                    startIndex += 1;
                 }
             }
 
@@ -1304,9 +1305,124 @@
         private String transformFromNull() {
             formatToken.setFlags(formatToken.getFlags()
                     & (~FormatToken.FLAG_ZERO));
-            return padding(new StringBuilder("null"), 0);
+            return padding(new StringBuilder("null"), 0); //$NON-NLS-1$
         }
 
+        /*
+         * Transforms a BigInteger to a formatted string.
+         */
+        private String transformFromBigInteger() {
+            int startIndex = 0;
+            boolean isNegative = false;
+            StringBuilder result = new StringBuilder();
+            BigInteger bigInt = (BigInteger) arg;
+            char currentConversionType = formatToken.getConversionType();
+
+            if (formatToken.isFlagSet(FormatToken.FLAG_MINUS)
+                    || formatToken.isFlagSet(FormatToken.FLAG_ZERO)) {
+                if (!formatToken.isWidthSet()) {
+                    throw new MissingFormatWidthException(formatToken
+                            .getStrFlags());
+                }
+            }
+
+            // Combination of '+' & ' ' is illegal.
+            if (formatToken.isFlagSet(FormatToken.FLAG_ADD)
+                    && formatToken.isFlagSet(FormatToken.FLAG_SPACE)) {
+                throw new IllegalFormatFlagsException(formatToken.getStrFlags());
+            }
+            
+            // Combination of '-' & '0' is illegal.
+            if(formatToken.isFlagSet(FormatToken.FLAG_ZERO)
+                    && formatToken.isFlagSet(FormatToken.FLAG_MINUS)) {
+                throw new IllegalFormatFlagsException(formatToken.getStrFlags());
+            }
+
+            if (formatToken.isPrecisionSet()) {
+                throw new IllegalFormatPrecisionException(formatToken
+                        .getPrecision());
+            }
+
+            if ('d' != currentConversionType
+                    && formatToken.isFlagSet(FormatToken.FLAG_COMMA)) {
+                throw new FormatFlagsConversionMismatchException(formatToken
+                        .getStrFlags(), currentConversionType);
+            }
+
+            if (formatToken.isFlagSet(FormatToken.FLAG_SHARP)
+                    && 'd' == currentConversionType) {
+                throw new FormatFlagsConversionMismatchException(formatToken
+                        .getStrFlags(), currentConversionType);
+            }
+
+            if (null == bigInt) {
+                return transformFromNull();
+            }
+
+            isNegative = (bigInt.compareTo(BigInteger.ZERO) < 0);
+
+            if ('d' == currentConversionType) {
+                NumberFormat numberFormat = getNumberFormat();
+                boolean readableName = formatToken.isFlagSet(FormatToken.FLAG_COMMA); 
+                numberFormat.setGroupingUsed(readableName);
+                result.append(numberFormat.format(bigInt));
+            } else if ('o' == currentConversionType) {
+                // convert BigInteger to a string presentation using radix 8
+                result.append(bigInt.toString(8));
+            } else {
+                // convert BigInteger to a string presentation using radix 16
+                result.append(bigInt.toString(16));
+            }
+            if (formatToken.isFlagSet(FormatToken.FLAG_SHARP)) {
+                startIndex = isNegative ? 1 : 0;
+                if ('o' == currentConversionType) {
+                    result.insert(startIndex, "0"); //$NON-NLS-1$
+                    startIndex += 1;
+                } else if( 'x' == currentConversionType ||
+                        'X' == currentConversionType){
+                    result.insert(startIndex, "0x"); //$NON-NLS-1$
+                    startIndex += 2;
+                }
+            }
+
+            if (!isNegative) {
+                if (formatToken.isFlagSet(FormatToken.FLAG_ADD)) {
+                    result.insert(0, '+');
+                    startIndex += 1;
+                }
+                if (formatToken.isFlagSet(FormatToken.FLAG_SPACE)) {
+                    result.insert(0, ' ');
+                    startIndex += 1;
+                }
+            }
+
+            /* pad paddingChar to the output */
+            if (isNegative
+                    && formatToken.isFlagSet(FormatToken.FLAG_PARENTHESIS)) {
+                /*
+                 * add () to the output,if the value is negative and
+                 * formatToken.FLAG_PARENTTHESIS is set.
+                 */
+                StringBuilder buffer = new StringBuilder();
+                result.deleteCharAt(0); // delete the '-'
+                result.insert(0, '(');
+                if (formatToken.isFlagSet(FormatToken.FLAG_ZERO)) {
+                    formatToken.setWidth(formatToken.getWidth() - 1);
+                    buffer.append(padding(result, 1));
+                    buffer.append(')');
+                } else {
+                    result.append(')');
+                    buffer.append(padding(result, 0));
+                }
+                return buffer.toString();
+
+            }
+            if (isNegative && formatToken.isFlagSet(FormatToken.FLAG_ZERO)) {
+                startIndex++;
+            }
+            return padding(result, startIndex);
+        }
+        
         /*
          * Transforms a Float,Double or BigDecimal to a formatted string.
          */

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java?rev=420829&r1=420828&r2=420829&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/FormatterTest.java
Tue Jul 11 05:56:51 2006
@@ -2500,7 +2500,283 @@
         f.format("%(+,-7d%<( o%<+(x %<( 06X", (BigInteger) null);
         assertEquals("null   nullnull   NULL", f.toString());
     }
+    
+    /**
+     * @tests java.util.Formatter#format(String, Object...) for legal
+     *        BigInteger conversion type 'd'
+     */
+    public void test_formatLjava_lang_String$LBigInteger() {
+        final Object[][] tripleD = {
+                {new BigInteger("123456789012345678901234567890"),          "%d",       "123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%10d",     "123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-1d",     "123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%+d",      "+123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "% d",      "
123456789012345678901234567890"}, //$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%,d",      "123.456.789.012.345.678.901.234.567.890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%(d",      "123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%08d",     "123456789012345678901234567890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-+,(11d", "+123.456.789.012.345.678.901.234.567.890"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%0 ,(11d", "
123.456.789.012.345.678.901.234.567.890"}, //$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%d",       "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%10d",     "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-1d",     "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%+d",      "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "% d",      "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%,d",      "-9.876.543.210.987.654.321.098.765.432.100.000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%(d",      "(9876543210987654321098765432100000)"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%08d",     "-9876543210987654321098765432100000"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-+,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%0 ,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)"},
//$NON-NLS-2$
+        };
+        
+        final int input = 0;
+        final int pattern = 1;
+        final int output = 2;
+        Formatter f;
+        for (int i = 0; i < tripleD.length; i++) {
+            f = new Formatter(Locale.GERMAN);
+            f.format((String) tripleD[i][pattern],
+                    tripleD[i][input]);
+            assertEquals("triple[" + i + "]:" + tripleD[i][input] + ",pattern["
+                    + i + "]:" + tripleD[i][pattern], tripleD[i][output], f
+                    .toString());
+            
+        }
+        
+        final Object[][] tripleO = {
+                {new BigInteger("123456789012345678901234567890"),          "%o",       "143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-6o",     "143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%08o",     "143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%#o",      "0143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%0#11o",   "0143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-#9o",    "0143564417755415637016711617605322"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%o",       "-36336340043453651353467270113157312240"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-6o",     "-36336340043453651353467270113157312240"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%08o",     "-36336340043453651353467270113157312240"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%#o",      "-036336340043453651353467270113157312240"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%0#11o",   "-036336340043453651353467270113157312240"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-#9o",    "-036336340043453651353467270113157312240"},
//$NON-NLS-2$
+        };
+        for (int i = 0; i < tripleO.length; i++) {
+            f = new Formatter(Locale.ITALY);
+            f.format((String) tripleO[i][pattern],
+                    tripleO[i][input]);
+            assertEquals("triple[" + i + "]:" + tripleO[i][input] + ",pattern["
+                    + i + "]:" + tripleO[i][pattern], tripleO[i][output], f
+                    .toString());
+            
+        }
+        
+        final Object[][] tripleX = {
+                {new BigInteger("123456789012345678901234567890"),          "%x",       "18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-8x",     "18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%06x",     "18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%#x",      "0x18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%0#12x",   "0x18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("123456789012345678901234567890"),          "%-#9x",    "0x18ee90ff6c373e0ee4e3f0ad2"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%x",       "-1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-8x",     "-1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%06x",     "-1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%#x",      "-0x1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%0#12x",   "-0x1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+                {new BigInteger("-9876543210987654321098765432100000"),     "%-#9x",    "-0x1e6f380472bd4bae6eb8259bd94a0"},
//$NON-NLS-2$
+        };
+        
+        for (int i = 0; i < tripleX.length; i++) {
+            f = new Formatter(Locale.FRANCE);
+            f.format((String) tripleX[i][pattern],
+                    tripleX[i][input]);
+            assertEquals("triple[" + i + "]:" + tripleX[i][input] + ",pattern["
+                    + i + "]:" + tripleX[i][pattern], tripleX[i][output], f
+                    .toString());
+            
+        }
+        
+        f = new Formatter(Locale.GERMAN);
+        f.format("%(+,-7d%<( o%<+(x %<( 06X", (BigInteger) null);
+        assertEquals("null   nullnull   NULL", f.toString());
+    }
 
+    /**
+     * @tests java.util.Formatter#format(String, Object...) for padding of
+     *        BigInteger conversion
+     */
+    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerPaddingConversion()
{
+        Formatter f = null;
+
+        BigInteger bigInt = new BigInteger("123456789012345678901234567890");
+        f = new Formatter(Locale.GERMAN);
+        f.format("%32d", bigInt);
+        assertEquals("  123456789012345678901234567890", f.toString());
+
+        f = new Formatter(Locale.GERMAN);
+        f.format("%+32x", bigInt);
+        assertEquals("      +18ee90ff6c373e0ee4e3f0ad2", f.toString());
+
+        f = new Formatter(Locale.GERMAN);
+        f.format("% 32o", bigInt);
+        assertEquals(" 143564417755415637016711617605322", f.toString());
+
+        BigInteger negBigInt = new BigInteger(
+                "-1234567890123456789012345678901234567890");
+        f = new Formatter(Locale.GERMAN);
+        f.format("%( 040X", negBigInt);
+        assertEquals("(000003A0C92075C0DBF3B8ACBC5F96CE3F0AD2)", f.toString());
+
+        f = new Formatter(Locale.GERMAN);
+        f.format("%+(045d", negBigInt);
+        assertEquals("(0001234567890123456789012345678901234567890)", f
+                .toString());
+
+        f = new Formatter(Locale.GERMAN);
+        f.format("%+,-(60d", negBigInt);
+        assertEquals(
+                "(1.234.567.890.123.456.789.012.345.678.901.234.567.890)     ",
+                f.toString());
+    }
+    
+    /**
+     * @tests java.util.Formatter#format(String, Object...) for BigInteger
+     *        conversion exception
+     */
+    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerConversionException()
{
+        Formatter f = null;
+
+        final String[] flagsConversionMismatches = { "%#d", "%,o", "%,x", "%,X" };
+        for (int i = 0; i < flagsConversionMismatches.length; i++) {
+            try {
+                f = new Formatter(Locale.CHINA);
+                f.format(flagsConversionMismatches[i], new BigInteger("1"));
+                fail("should throw FormatFlagsConversionMismatchException");
+            } catch (FormatFlagsConversionMismatchException e) {
+                // expected
+            }
+        }
+
+        final String[] missingFormatWidths = { "%-0d", "%0d", "%-d", "%-0o",
+                "%0o", "%-o", "%-0x", "%0x", "%-x", "%-0X", "%0X", "%-X" };
+        for (int i = 0; i < missingFormatWidths.length; i++) {
+            try {
+                f = new Formatter(Locale.KOREA);
+                f.format(missingFormatWidths[i], new BigInteger("1"));
+                fail("should throw MissingFormatWidthException");
+            } catch (MissingFormatWidthException e) {
+                // expected
+            }
+        }
+
+        final String[] illFlags = { "%+ d", "%-08d", "%+ o", "%-08o", "%+ x",
+                "%-08x", "%+ X", "%-08X" };
+        for (int i = 0; i < illFlags.length; i++) {
+            try {
+                f = new Formatter(Locale.CANADA);
+                f.format(illFlags[i], new BigInteger("1"));
+                fail("should throw IllegalFormatFlagsException");
+            } catch (IllegalFormatFlagsException e) {
+                // expected
+            }
+        }
+
+        final String[] precisionExceptions = { "%.4d", "%2.5o", "%8.6x",
+                "%11.17X" };
+        for (int i = 0; i < precisionExceptions.length; i++) {
+            try {
+                f = new Formatter(Locale.US);
+                f.format(precisionExceptions[i], new BigInteger("1"));
+                fail("should throw IllegalFormatPrecisionException");
+            } catch (IllegalFormatPrecisionException e) {
+                // expected
+            }
+        }
+
+        f = new Formatter(Locale.US);
+        try {
+            f.format("%D", new BigInteger("1"));
+            fail("should throw UnknownFormatConversionException");
+        } catch (UnknownFormatConversionException e) {
+            // expected
+        }
+
+        f = new Formatter(Locale.US);
+        try {
+            f.format("%O", new BigInteger("1"));
+            fail("should throw UnknownFormatConversionException");
+        } catch (UnknownFormatConversionException e) {
+            // expected
+        }
+
+        try {
+            f = new Formatter();
+            f.format("%010000000000000000000000000000000001d", new BigInteger(
+                    "1"));
+            fail("should throw MissingFormatWidthException");
+        } catch (MissingFormatWidthException e) {
+            // expected
+        }
+    }
+    
+    /**
+     * @tests java.util.Formatter#format(String, Object...) for BigInteger
+     *        exception throwing order
+     */
+    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerExceptionOrder()
{
+        Formatter f = null;
+        BigInteger big = new BigInteger("100");
+
+        /*
+         * Order summary: UnknownFormatConversionException >
+         * MissingFormatWidthException > IllegalFormatFlagsException >
+         * IllegalFormatPrecisionException > IllegalFormatConversionException >
+         * FormatFlagsConversionMismatchException
+         * 
+         */
+        f = new Formatter(Locale.US);
+        try {
+            // compare IllegalFormatConversionException and
+            // FormatFlagsConversionMismatchException
+            f.format("%(o", false);
+            fail("should throw IllegalFormatConversionException");
+        } catch (IllegalFormatConversionException e) {
+            // expected
+        }
+
+        try {
+            // compare IllegalFormatPrecisionException and
+            // IllegalFormatConversionException
+            f.format("%.4o", false);
+            fail("should throw IllegalFormatPrecisionException");
+        } catch (IllegalFormatPrecisionException e) {
+            // expected
+        }
+
+        try {
+            // compare IllegalFormatFlagsException and
+            // IllegalFormatPrecisionException
+            f.format("%+ .4o", big);
+            fail("should throw IllegalFormatFlagsException");
+        } catch (IllegalFormatFlagsException e) {
+            // expected
+        }
+
+        try {
+            // compare MissingFormatWidthException and
+            // IllegalFormatFlagsException
+            f.format("%+ -o", big);
+            fail("should throw MissingFormatWidthException");
+        } catch (MissingFormatWidthException e) {
+            // expected
+        }
+
+        try {
+            // compare UnknownFormatConversionException and
+            // MissingFormatWidthException
+            f.format("%-O", big);
+            fail("should throw UnknownFormatConversionException");
+        } catch (UnknownFormatConversionException e) {
+            // expected
+        }
+    }
+    
     /**
      * Setup resource files for testing
      */



Mime
View raw message