Search in sources :

Example 1 with IllegalFormatPrecisionException

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

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_LineSeparator.

/**
     * java.util.Formatter#format(String, Object...) for line sperator
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_LineSeparator() {
    Formatter f = null;
    /* J2ObjC: Setting line.separator has no effect, see System.lineSeparator().
        String oldSeparator = System.getProperty("line.separator");
        try {
            System.setProperty("line.separator", "!\n");

            f = new Formatter(Locale.US);
            f.format("%1$n", 1);
            assertEquals("!\n", f.toString());

            f = new Formatter(Locale.KOREAN);
            f.format("head%1$n%2$n", 1, new Date());
            assertEquals("head!\n!\n", f.toString());

            f = new Formatter(Locale.US);
            f.format("%n%s", "hello");
            assertEquals("!\nhello", f.toString());
        } finally {
            System.setProperty("line.separator", oldSeparator);
        }*/
    f = new Formatter(Locale.US);
    try {
        f.format("%-n");
        fail("should throw IllegalFormatFlagsException: %-n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("%+n");
        fail("should throw IllegalFormatFlagsException: %+n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("%#n");
        fail("should throw IllegalFormatFlagsException: %#n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("% n");
        fail("should throw IllegalFormatFlagsException: % n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("%0n");
        fail("should throw IllegalFormatFlagsException: %0n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("%,n");
        fail("should throw IllegalFormatFlagsException: %,n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    try {
        f.format("%(n");
        fail("should throw IllegalFormatFlagsException: %(n");
    } catch (IllegalFormatFlagsException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%4n");
        fail("should throw IllegalFormatWidthException");
    } catch (IllegalFormatWidthException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%-4n");
        fail("should throw IllegalFormatWidthException");
    } catch (IllegalFormatWidthException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%.9n");
        fail("should throw IllegalFormatPrecisionException");
    } catch (IllegalFormatPrecisionException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%5.9n");
        fail("should throw IllegalFormatPrecisionException");
    } catch (IllegalFormatPrecisionException e) {
    // expected
    }
//System.setProperty("line.separator", oldSeparator);
}
Also used : IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Formatter(java.util.Formatter) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException) IllegalFormatWidthException(java.util.IllegalFormatWidthException)

Example 2 with IllegalFormatPrecisionException

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

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_Precision.

/**
     * java.util.Formatter#format(String, Object...) for precision
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_Precision() {
    Formatter f = new Formatter(Locale.US);
    f.format("%.5s", "123456");
    assertEquals("12345", f.toString());
    f = new Formatter(Locale.US);
    // 2147483648 is the value of Integer.MAX_VALUE + 1
    f.format("%.2147483648s", "...");
    assertEquals("...", f.toString());
    // the value of Integer.MAX_VALUE will allocate about 4G bytes of
    // memory.
    // It may cause OutOfMemoryError, so this value is not tested
    f = new Formatter(Locale.US);
    f.format("%10.0b", Boolean.TRUE);
    assertEquals("          ", f.toString());
    f = new Formatter(Locale.US);
    f.format("%10.01s", "hello");
    assertEquals("         h", f.toString());
    try {
        f = new Formatter(Locale.US);
        f.format("%.s", "hello", "2");
        fail("should throw Exception");
    } catch (UnknownFormatConversionException | IllegalFormatPrecisionException expected) {
    // expected
    }
    try {
        f = new Formatter(Locale.US);
        f.format("%.-5s", "123456");
        fail("should throw Exception");
    } catch (UnknownFormatConversionException | IllegalFormatPrecisionException expected) {
    // expected
    }
    try {
        f = new Formatter(Locale.US);
        f.format("%1.s", "hello", "2");
        fail("should throw Exception");
    } catch (UnknownFormatConversionException | IllegalFormatPrecisionException expected) {
    // expected
    }
    f = new Formatter(Locale.US);
    f.format("%5.1s", "hello");
    assertEquals("    h", f.toString());
    f = new Formatter(Locale.FRANCE);
    f.format("%.0s", "hello", "2");
    assertEquals("", f.toString());
}
Also used : UnknownFormatConversionException(java.util.UnknownFormatConversionException) IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Formatter(java.util.Formatter)

Example 3 with IllegalFormatPrecisionException

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

the class FormatterTest method test_formatLjava_lang_String$Ljava_lang_Object_Percent.

/**
     * java.util.Formatter#format(String, Object...) for percent
     */
public void test_formatLjava_lang_String$Ljava_lang_Object_Percent() {
    Formatter f = null;
    f = new Formatter(Locale.ENGLISH);
    f.format("%1$%", 100);
    assertEquals("%", f.toString());
    f = new Formatter(Locale.CHINA);
    f.format("%1$%%%", "hello", new Object());
    assertEquals("%%", f.toString());
    f = new Formatter(Locale.CHINA);
    f.format("%%%s", "hello");
    assertEquals("%hello", f.toString());
    f = new Formatter(Locale.US);
    try {
        f.format("%.9%");
        fail("should throw IllegalFormatPrecisionException");
    } catch (IllegalFormatPrecisionException e) {
    // expected
    }
    f = new Formatter(Locale.US);
    try {
        f.format("%5.9%");
        fail("should throw IllegalFormatPrecisionException");
    } catch (IllegalFormatPrecisionException e) {
    // expected
    }
/* J2ObjC: Throws IllegalFormatFlagsException.
        f = new Formatter(Locale.US);
        assertFormatFlagsConversionMismatchException(f, "%+%");
        assertFormatFlagsConversionMismatchException(f, "%#%");
        assertFormatFlagsConversionMismatchException(f, "% %");
        assertFormatFlagsConversionMismatchException(f, "%0%");
        assertFormatFlagsConversionMismatchException(f, "%,%");
        assertFormatFlagsConversionMismatchException(f, "%(%");*/
// J2ObjC: Seems to be more compatible with the RI than Android, strangely.
// f = new Formatter(Locale.KOREAN);
// f.format("%4%", 1);
// /*
//  * fail on RI the output string should be right justified by appending
//  * spaces till the whole string is 4 chars width.
//  */
// assertEquals("   %", f.toString());
// f = new Formatter(Locale.US);
// f.format("%-4%", 100);
// /*
//  * fail on RI, throw UnknownFormatConversionException the output string
//  * should be left justified by appending spaces till the whole string is
//  * 4 chars width.
//  */
// assertEquals("%   ", f.toString());
}
Also used : IllegalFormatPrecisionException(java.util.IllegalFormatPrecisionException) Formatter(java.util.Formatter)

Example 4 with IllegalFormatPrecisionException

use of java.util.IllegalFormatPrecisionException 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 IllegalFormatPrecisionException

use of java.util.IllegalFormatPrecisionException 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

Formatter (java.util.Formatter)7 IllegalFormatPrecisionException (java.util.IllegalFormatPrecisionException)7 UnknownFormatConversionException (java.util.UnknownFormatConversionException)4 FormatFlagsConversionMismatchException (java.util.FormatFlagsConversionMismatchException)3 IllegalFormatFlagsException (java.util.IllegalFormatFlagsException)3 BigInteger (java.math.BigInteger)2 IllegalFormatConversionException (java.util.IllegalFormatConversionException)2 MissingFormatWidthException (java.util.MissingFormatWidthException)2 Date (java.util.Date)1 DuplicateFormatFlagsException (java.util.DuplicateFormatFlagsException)1 IllegalFormatCodePointException (java.util.IllegalFormatCodePointException)1 IllegalFormatWidthException (java.util.IllegalFormatWidthException)1