FileDocCategorySizeDatePackage
StringTest.javaAPI DocAndroid 1.5 API5419Wed May 06 22:42:02 BST 2009android.core

StringTest

public class StringTest extends TestCase
Some String tests.

Fields Summary
private String
germanSpelloutRule
Constructors Summary
Methods Summary
public voidtestString()


    
         
        String test = "0123456789";
        String test1 = new String("0123456789");    // different object
        String test2 = new String("0123456780");    // different value
        String offset = new String("xxx0123456789yyy");
        String sub = offset.substring(3, 13);

        assertEquals(test, test);
        assertEquals(test, test1);
        assertFalse(test.equals(test2));

        assertEquals(0, test.compareTo(test1));
        assertTrue(test1.compareTo(test2) > 0);
        assertTrue(test2.compareTo(test1) < 0);

        /* compare string with a nonzero offset, in left/right side */
        assertEquals(0, test.compareTo(sub));
        assertEquals(0, sub.compareTo(test));
        assertEquals(test, sub);
        assertEquals(sub, test);
        /* same base, one is a substring */
        assertFalse(offset.equals(sub));
        assertFalse(sub.equals(offset));
        /* wrong class */
        assertFalse(test.equals(this));

        /* null ptr - throw */
        try {
            test.compareTo(null);
            fail("didn't get expected npe");
        } catch (NullPointerException npe) {
            // expected
        }
        /* null ptr - ok */
        assertFalse(test.equals(null));

        test = test.substring(1);
        assertEquals("123456789", test);
        assertFalse(test.equals(test1));

        test = test.substring(1);
        assertEquals("23456789", test);

        test = test.substring(1);
        assertEquals("3456789", test);

        test = test.substring(1);
        assertEquals("456789", test);

        test = test.substring(3, 5);
        assertEquals("78", test);

        test = "this/is/a/path";
        String[] strings = test.split("/");
        assertEquals(4, strings.length);

        assertEquals("this is a path", test.replaceAll("/", " "));
        assertEquals("this is a path", test.replace("/", " "));

        assertEquals(0, "abc".compareToIgnoreCase("ABC"));
        assertTrue("abc".compareToIgnoreCase("DEF") < 0);
        assertTrue("ABC".compareToIgnoreCase("def") < 0);
        assertTrue("Now".compareTo("Mow") > 0);
        assertTrue("Now".compareToIgnoreCase("Mow") > 0);

        // RuleBasedNumberFormnat tests
        RuleBasedNumberFormat format = new RuleBasedNumberFormat();
        format.open(RBNFType.SPELLOUT);
        String result = format.format(15);
        assertEquals("Expected spellout format: 'fifteen' but was "
                + result, "fifteen", result);
        format.close();

        format.open(RBNFType.DURATION);
        result = format.format(15);
        assertEquals("Expected spellout format: '15 sec.' but was "
                + result, "15 sec.", result);
        format.close();

        format.open(RBNFType.ORDINAL);
        result = format.format(15);
        assertEquals("Expected spellout format: '15th' but was "
                + result, "15th", result);
        format.close();
        format.open(germanSpelloutRule);

        result = format.format(1323);
        assertEquals("Expected spellout format: 'tausenddrei" +
                "hundertdreiundzwanzig' but was " + result, "tausend" +
                        "dreihundertdreiundzwanzig", result);
            int res = format.parse("tausenddreihundertdreiundzwanzig")
                    .intValue();
            assertEquals("Expected spellout format: 'tausend" +
                    "dreihundertdreiundzwanzig' but was " + res , 1323, res);
        format.close();