Package java.text

Examples of java.text.ChoiceFormat


        Format[] formats = format1.getFormats();
        Format[] correctFormats = new Format[] {
                NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(),
                NumberFormat.getPercentInstance(), null,
                new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance(), };

        assertEquals("Test1:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1:wrong format for pattern index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        // test with max argument index > max offset
        formats = format2.getFormats();
        correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(),
                NumberFormat.getPercentInstance(), null,
                new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance() };

        assertEquals("Test2:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test2:wrong format for pattern index " + i + ":",
View Full Code Here


        // Test for method java.text.Format [] test_getFormatsByArgumentIndex()

        // test with repeating formats and max argument index < max offset
        Format[] formats = format1.getFormatsByArgumentIndex();
        Format[] correctFormats = new Format[] { DateFormat.getDateInstance(),
                new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
                NumberFormat.getCurrencyInstance(), null };

        assertEquals("Test1:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1:wrong format for argument index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        // test with max argument index > max offset
        formats = format2.getFormatsByArgumentIndex();
        correctFormats = new Format[] { DateFormat.getDateInstance(),
                new ChoiceFormat("0#off|1#on"), null,
                NumberFormat.getCurrencyInstance(), null, null, null, null,
                DateFormat.getTimeInstance() };

        assertEquals("Test2:Returned wrong number of formats:",
                correctFormats.length, formats.length);
View Full Code Here

     */
    public void test_setFormatByArgumentIndexILjava_text_Format() {
        // test for method setFormatByArgumentIndex(int, Format)
        MessageFormat f1 = (MessageFormat) format1.clone();
        f1.setFormatByArgumentIndex(0, DateFormat.getTimeInstance());
        f1.setFormatByArgumentIndex(4, new ChoiceFormat("1#few|2#ok|3#a lot"));

        // test with repeating formats and max argument index < max offset
        // compare getFormatsByArgumentIndex() results after calls to
        // setFormatByArgumentIndex()
        Format[] formats = f1.getFormatsByArgumentIndex();

        Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
                new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
                NumberFormat.getCurrencyInstance(),
                new ChoiceFormat("1#few|2#ok|3#a lot") };

        assertEquals("Test1A:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1B:wrong format for argument index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        // compare getFormats() results after calls to
        // setFormatByArgumentIndex()
        formats = f1.getFormats();

        correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
                new ChoiceFormat("1#few|2#ok|3#a lot"),
                new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };

        assertEquals("Test1C:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1D:wrong format for pattern index " + i + ":",
View Full Code Here

        // test with repeating formats and max argument index < max offset
        // compare getFormatsByArgumentIndex() results after calls to
        // setFormatsByArgumentIndex(Format[])
        Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
                new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
                NumberFormat.getCurrencyInstance(),
                new ChoiceFormat("1#few|2#ok|3#a lot") };

        f1.setFormatsByArgumentIndex(correctFormats);
        Format[] formats = f1.getFormatsByArgumentIndex();

        assertEquals("Test1A:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1B:wrong format for argument index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        // compare getFormats() results after calls to
        // setFormatByArgumentIndex()
        formats = f1.getFormats();
        correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
                new ChoiceFormat("1#few|2#ok|3#a lot"),
                new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };

        assertEquals("Test1C:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1D:wrong format for pattern index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        // test setting argumentIndexes that are not used
        MessageFormat f2 = (MessageFormat) format2.clone();
        Format[] inputFormats = new Format[] { DateFormat.getDateInstance(),
                new ChoiceFormat("0#off|1#on"),
                NumberFormat.getPercentInstance(),
                NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(), null, null, null,
                DateFormat.getTimeInstance() };
        f2.setFormatsByArgumentIndex(inputFormats);

        formats = f2.getFormatsByArgumentIndex();
        correctFormats = format2.getFormatsByArgumentIndex();

        assertEquals("Test2A:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test2B:wrong format for argument index " + i + ":",
                    correctFormats[i], formats[i]);
        }

        formats = f2.getFormats();
        correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
                DateFormat.getTimeInstance(), DateFormat.getDateInstance(),
                null, new ChoiceFormat("0#off|1#on"),
                DateFormat.getDateInstance() };

        assertEquals("Test2C:Returned wrong number of formats:",
                correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
View Full Code Here

     * @tests java.text.ChoiceFormat#applyPattern(java.lang.String)
     */
    public void test_applyPatternLjava_lang_String() {
        // Test for method void
        // java.text.ChoiceFormat.applyPattern(java.lang.String)
        ChoiceFormat f = (ChoiceFormat) f1.clone();
        f.applyPattern("0#0|1#1");
        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
                new double[] { 0, 1 }));
        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
                new String[] { "0", "1" }));
       
        //Regression for Harmony 540
        double[] choiceLimits = { -1, 0, 1, ChoiceFormat.nextDouble(1) };
        String[] choiceFormats = { "is negative", "is zero or fraction",
                "is one", "is more than 1" };
       
        f = new ChoiceFormat("");
        f.applyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1");
        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
                choiceLimits));
        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
                choiceFormats));
       
        f = new ChoiceFormat("");
        try {
            f.applyPattern("-1#is negative|0#is zero or fraction|-1#is one|1<is more than 1");
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // Expected
        }
       
        f = new ChoiceFormat("");
        try {
            f.applyPattern("-1is negative|0#is zero or fraction|1#is one|1<is more than 1");
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // Expected
        }
       
        f = new ChoiceFormat("");
        f.applyPattern("-1<is negative|0#is zero or fraction|1#is one|1<is more than 1");
        choiceLimits[0] = ChoiceFormat.nextDouble(-1);
        assertTrue("Incorrect limits", java.util.Arrays.equals(f.getLimits(),
                choiceLimits));
        assertTrue("Incorrect formats", java.util.Arrays.equals(f.getFormats(),
                choiceFormats));
       
        f = new ChoiceFormat("");
        f.applyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1");
        String str = "org.apache.harmony.tests.java.text.ChoiceFormat";
        f.applyPattern(str);
        String ptrn = f.toPattern();
        assertEquals("Return value should be empty string for invalid pattern",
                0, ptrn.length());
    }
View Full Code Here

    /**
     * @tests java.text.ChoiceFormat#clone()
     */
    public void test_clone() {
        // Test for method java.lang.Object java.text.ChoiceFormat.clone()
        ChoiceFormat f = (ChoiceFormat) f1.clone();
        assertTrue("Not equal", f.equals(f1));
        f.setChoices(new double[] { 0, 1, 2 }, new String[] { "0", "1", "2" });
        assertTrue("Equal", !f.equals(f1));
    }
View Full Code Here

                "Large Apple", "Huge Apple" };
        double[] orangeLimits = { -2, 0, ChoiceFormat.nextDouble(0), 1, 2 };
        String[] orangeFormats = { "Inverted Orange", "No Orange",
                "Almost No Orange", "Normal Orange", "Expensive Orange" };

        ChoiceFormat appleChoiceFormat = new ChoiceFormat(appleLimits,
                appleFormats);
        ChoiceFormat orangeChoiceFormat = new ChoiceFormat(orangeLimits,
                orangeFormats);
        ChoiceFormat orangeChoiceFormat2 = new ChoiceFormat(patternString);
        ChoiceFormat hybridChoiceFormat = new ChoiceFormat(appleLimits,
                orangeFormats);

        assertTrue("Apples should not equal oranges", !appleChoiceFormat
                .equals(orangeChoiceFormat));
        assertTrue("Different limit list--should not appear as equal",
                !orangeChoiceFormat.equals(hybridChoiceFormat));
        assertTrue("Different format list--should not appear as equal",
                !appleChoiceFormat.equals(hybridChoiceFormat));
        assertTrue("Should be equal--identical format", appleChoiceFormat
                .equals(appleChoiceFormat));
        assertTrue("Should be equals--same limits, same formats",
                orangeChoiceFormat.equals(orangeChoiceFormat2));

        ChoiceFormat f2 = new ChoiceFormat(
                "0#Less than one|1#one|1<Between one and two|2<Greater than two");
        assertTrue("Not equal", f1.equals(f2));
    }
View Full Code Here

        buf.setLength(0);
        r = f1.format(3, buf, field).toString();
    assertEquals("Wrong choice for 3", "Greater than two", r);

        // Regression test for HARMONY-1081
        assertEquals(0, new ChoiceFormat("|").format(Double.NaN, new StringBuffer(), new FieldPosition(6)).length());
        assertEquals(0, new ChoiceFormat("|").format(1, new StringBuffer(), new FieldPosition(6)).length());
        assertEquals("Less than one", f1.format(Double.NaN, new StringBuffer(), field).toString());
    }
View Full Code Here

    /**
     * @tests java.text.ChoiceFormat#hashCode()
     */
    public void test_hashCode() {
        // Test for method int java.text.ChoiceFormat.hashCode()
        ChoiceFormat f2 = new ChoiceFormat(
                "0#Less than one|1#one|1<Between one and two|2<Greater than two");
        assertTrue("Different hash", f1.hashCode() == f2.hashCode());
    }
View Full Code Here

     */
    public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
        // Test for method java.lang.Number
        // java.text.ChoiceFormat.parse(java.lang.String,
        // java.text.ParsePosition)
        ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three");
    assertEquals("Case insensitive", 0, format
        .parse("One", new ParsePosition(0)).intValue());

        ParsePosition pos = new ParsePosition(0);
        Number result = f1.parse("Greater than two", pos);
        assertTrue("Not a Double1", result instanceof Double);
View Full Code Here

TOP

Related Classes of java.text.ChoiceFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.