Search in sources :

Example 1 with FormatFlagsConversionMismatchException

use of java.util.FormatFlagsConversionMismatchException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalConversionException.

/**
     * java.util.Formatter#format(String, Object...) for exceptions in
     * Float/Double/BigDecimal conversion type 'e', 'E', 'g', 'G', 'f', 'a', 'A'
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalConversionException() {
    Formatter f = null;
    final char[] conversions = { 'e', 'E', 'g', 'G', 'f', 'a', 'A' };
    final Object[] illArgs = { false, (byte) 1, (short) 2, 3, (long) 4, new BigInteger("5"), new Character('c'), new Object(), new Date() };
    for (int i = 0; i < illArgs.length; i++) {
        for (int j = 0; j < conversions.length; j++) {
            try {
                f = new Formatter(Locale.UK);
                f.format("%" + conversions[j], illArgs[i]);
                fail("should throw IllegalFormatConversionException");
            } catch (IllegalFormatConversionException e) {
            // expected
            }
        }
    }
    try {
        f = new Formatter(Locale.UK);
        f.format("%a", new BigDecimal(1));
        fail("should throw IllegalFormatConversionException");
    } catch (IllegalFormatConversionException e) {
    // expected
    }
    try {
        f = new Formatter(Locale.UK);
        f.format("%A", new BigDecimal(1));
        fail("should throw IllegalFormatConversionException");
    } catch (IllegalFormatConversionException e) {
    // expected
    }
    final String[] flagsConversionMismatches = { "%,e", "%,E", "%#g", "%#G", "%,a", "%,A", "%(a", "%(A" };
    for (int i = 0; i < flagsConversionMismatches.length; i++) {
        try {
            f = new Formatter(Locale.CHINA);
            f.format(flagsConversionMismatches[i], new BigDecimal(1));
            fail("should throw FormatFlagsConversionMismatchException");
        } catch (FormatFlagsConversionMismatchException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.JAPAN);
            f.format(flagsConversionMismatches[i], (BigDecimal) null);
            fail("should throw FormatFlagsConversionMismatchException");
        } catch (FormatFlagsConversionMismatchException e) {
        // expected
        }
    }
    final String[] missingFormatWidths = { "%-0e", "%0e", "%-e", "%-0E", "%0E", "%-E", "%-0g", "%0g", "%-g", "%-0G", "%0G", "%-G", "%-0f", "%0f", "%-f", "%-0a", "%0a", "%-a", "%-0A", "%0A", "%-A" };
    for (int i = 0; i < missingFormatWidths.length; i++) {
        try {
            f = new Formatter(Locale.KOREA);
            f.format(missingFormatWidths[i], 1f);
            fail("should throw MissingFormatWidthException");
        } catch (MissingFormatWidthException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.KOREA);
            f.format(missingFormatWidths[i], (Float) null);
            fail("should throw MissingFormatWidthException");
        } catch (MissingFormatWidthException e) {
        // expected
        }
    }
    final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f", "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f", "%-03a", "%-03A" };
    for (int i = 0; i < illFlags.length; i++) {
        try {
            f = new Formatter(Locale.CANADA);
            f.format(illFlags[i], 1.23d);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException e) {
        // expected
        }
        try {
            f = new Formatter(Locale.CANADA);
            f.format(illFlags[i], (Double) null);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException e) {
        // expected
        }
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%F", 1);
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
}
Also used : FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Formatter(java.util.Formatter) IllegalFormatConversionException(java.util.IllegalFormatConversionException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UnknownFormatConversionException(java.util.UnknownFormatConversionException) BigInteger(java.math.BigInteger) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) MissingFormatWidthException(java.util.MissingFormatWidthException)

Example 2 with FormatFlagsConversionMismatchException

use of java.util.FormatFlagsConversionMismatchException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalExceptionOrder.

/**
     * java.util.Formatter#format(String, Object...) for BigDecimal
     * exception throwing order
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalExceptionOrder() {
    Formatter f = null;
    BigDecimal bd = new BigDecimal("1.0");
    /*
         * Summary: UnknownFormatConversionException >
         * MissingFormatWidthException > IllegalFormatFlagsException >
         * FormatFlagsConversionMismatchException >
         * IllegalFormatConversionException
         *
         */
    try {
        // compare FormatFlagsConversionMismatchException and
        // IllegalFormatConversionException
        f = new Formatter(Locale.US);
        f.format("%,e", (byte) 1);
        fail("should throw FormatFlagsConversionMismatchException");
    } catch (FormatFlagsConversionMismatchException e) {
    // expected
    }
    try {
        // compare IllegalFormatFlagsException and
        // FormatFlagsConversionMismatchException
        f = new Formatter(Locale.US);
        f.format("%+ ,e", bd);
        fail("should throw IllegalFormatFlagsException");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        // compare MissingFormatWidthException and
        // IllegalFormatFlagsException
        f = new Formatter(Locale.US);
        f.format("%+ -e", bd);
        fail("should throw MissingFormatWidthException");
    } catch (MissingFormatWidthException e) {
    // expected
    }
    // MissingFormatWidthException
    try {
        f = new Formatter(Locale.US);
        f.format("%-F", bd);
        fail("should throw UnknownFormatConversionException");
    } catch (UnknownFormatConversionException e) {
    // expected
    }
}
Also used : UnknownFormatConversionException(java.util.UnknownFormatConversionException) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Formatter(java.util.Formatter) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) MissingFormatWidthException(java.util.MissingFormatWidthException) BigDecimal(java.math.BigDecimal)

Example 3 with FormatFlagsConversionMismatchException

use of java.util.FormatFlagsConversionMismatchException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther.

/**
     * java.util.Formatter#format(String, Object...) for general
     * conversion other cases
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther() {
    /*
         * In Turkish locale, the upper case of 'i' is 'İ'. The
         * following test indicate that 'i' is coverted to upper case
         * without using the turkish locale.
         */
    Formatter f = new Formatter(new Locale("tr"));
    f.format("%S", "i");
    //assertEquals("I", f.toString());  J2ObjC changed.
    assertEquals("İ", f.toString());
    final Object[] input = { Boolean.FALSE, Boolean.TRUE, new Character('c'), new Byte((byte) 0x01), new Short((short) 0x0001), new Integer(1), new Float(1.1f), new Double(1.1d), "", "string content", new MockFormattable(), (Object) null };
    f = new Formatter(Locale.GERMAN);
    for (int i = 0; i < input.length; i++) {
        if (!(input[i] instanceof Formattable)) {
            try {
                f.format("%#s", input[i]);
                /*
                     * fail on RI, spec says if the '#' flag is present and the
                     * argument is not a Formattable , then a
                     * FormatFlagsConversionMismatchException will be thrown.
                     */
                fail("should throw FormatFlagsConversionMismatchException");
            } catch (FormatFlagsConversionMismatchException e) {
            // expected
            }
        } else {
            f.format("%#s%<-#8s", input[i]);
            assertEquals("customized format function width: -1 precision: -1customized format function width: 8 precision: -1", f.toString());
        }
    }
}
Also used : Locale(java.util.Locale) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) Formatter(java.util.Formatter) Formattable(java.util.Formattable) BigInteger(java.math.BigInteger)

Example 4 with FormatFlagsConversionMismatchException

use of java.util.FormatFlagsConversionMismatchException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerConversionException.

/**
     * 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
    }
}
Also used : UnknownFormatConversionException(java.util.UnknownFormatConversionException) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Formatter(java.util.Formatter) BigInteger(java.math.BigInteger) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) MissingFormatWidthException(java.util.MissingFormatWidthException)

Example 5 with FormatFlagsConversionMismatchException

use of java.util.FormatFlagsConversionMismatchException in project j2objc by google.

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_CharacterConversion.

/**
     * java.util.Formatter#format(String, Object...) for Character
     * conversion
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_CharacterConversion() {
    Formatter f = new Formatter(Locale.US);
    final Object[] illArgs = { Boolean.TRUE, new Float(1.1f), new Double(1.1d), "string content", new Float(1.1f), new Date() };
    for (int i = 0; i < illArgs.length; i++) {
        try {
            f.format("%c", illArgs[i]);
            fail("should throw IllegalFormatConversionException");
        } catch (IllegalFormatConversionException e) {
        // expected
        }
    }
    try {
        f.format("%c", Integer.MAX_VALUE);
        fail("should throw IllegalFormatCodePointException");
    } catch (IllegalFormatCodePointException e) {
    // expected
    }
    try {
        f.format("%#c", 'c');
        fail("should throw FormatFlagsConversionMismatchException");
    } catch (FormatFlagsConversionMismatchException e) {
    // expected
    }
    final Object[][] triple = { { 'c', "%c", "c" }, { 'c', "%-2c", "c " }, { 'ģ', "%c", "ģ" }, { 'ģ', "%-2c", "ģ " }, { (byte) 0x11, "%c", "" }, { (byte) 0x11, "%-2c", " " }, { (short) 0x1111, "%c", "ᄑ" }, { (short) 0x1111, "%-2c", "ᄑ " }, { 0x11, "%c", "" }, { 0x11, "%-2c", " " } };
    final int input = 0;
    final int pattern = 1;
    final int output = 2;
    for (int i = 0; i < triple.length; i++) {
        f = new Formatter(Locale.US);
        f.format((String) triple[i][pattern], triple[i][input]);
        assertEquals(triple[i][output], f.toString());
    }
    f = new Formatter(Locale.US);
    f.format("%c", 0x10000);
    assertEquals(0x10000, f.toString().codePointAt(0));
    try {
        f.format("%2.2c", 'c');
        fail("should throw IllegalFormatPrecisionException");
    } catch (IllegalFormatPrecisionException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    f.format("%C", 'w');
    // error on RI, throw UnknownFormatConversionException
    // RI do not support converter 'C'
    assertEquals("W", f.toString());
    f = new Formatter(Locale.JAPAN);
    f.format("%Ced", 0x1111);
    // error on RI, throw UnknownFormatConversionException
    // RI do not support converter 'C'
    assertEquals("ᄑed", f.toString());
}
Also used : IllegalFormatCodePointException(java.util.IllegalFormatCodePointException) FormatFlagsConversionMismatchException(java.util.FormatFlagsConversionMismatchException) IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Formatter(java.util.Formatter) IllegalFormatConversionException(java.util.IllegalFormatConversionException) Date(java.util.Date)

Aggregations

FormatFlagsConversionMismatchException (java.util.FormatFlagsConversionMismatchException)8 Formatter (java.util.Formatter)8 MissingFormatWidthException (java.util.MissingFormatWidthException)6 IllegalFormatFlagsException (java.util.IllegalFormatFlagsException)5 UnknownFormatConversionException (java.util.UnknownFormatConversionException)5 BigInteger (java.math.BigInteger)4 IllegalFormatConversionException (java.util.IllegalFormatConversionException)3 IllegalFormatPrecisionException (java.util.IllegalFormatPrecisionException)3 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 IllegalFormatCodePointException (java.util.IllegalFormatCodePointException)2 Formattable (java.util.Formattable)1 Locale (java.util.Locale)1