FileDocCategorySizeDatePackage
RegexTest.javaAPI DocAndroid 1.5 API4093Wed May 06 22:42:02 BST 2009com.android.unit_tests

RegexTest

public class RegexTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
public voidtestDomainPattern()

        boolean t;

        t = Regex.DOMAIN_NAME_PATTERN.matcher("mail.example.com").matches();
        assertTrue("Valid domain", t);

        t = Regex.DOMAIN_NAME_PATTERN.matcher("__+&42.xer").matches();
        assertFalse("Invalid domain", t);
    
public voidtestIpPattern()

        boolean t;

        t = Regex.IP_ADDRESS_PATTERN.matcher("172.29.86.3").matches();
        assertTrue("Valid IP", t);

        t = Regex.IP_ADDRESS_PATTERN.matcher("1234.4321.9.9").matches();
        assertFalse("Invalid IP", t);
    
public voidtestPhonePattern()

        boolean t;

        t = Regex.PHONE_PATTERN.matcher("(919) 555-1212").matches();
        assertTrue("Valid phone", t);

        t = Regex.PHONE_PATTERN.matcher("2334 9323/54321").matches();
        assertFalse("Invalid phone", t);

        String[] tests = {
                "Me: 16505551212 this\n",
                "Me: 6505551212 this\n",
                "Me: 5551212 this\n",

                "Me: 1-650-555-1212 this\n",
                "Me: (650) 555-1212 this\n",
                "Me: +1 (650) 555-1212 this\n",
                "Me: +1-650-555-1212 this\n",
                "Me: 650-555-1212 this\n",
                "Me: 555-1212 this\n",

                "Me: 1.650.555.1212 this\n",
                "Me: (650) 555.1212 this\n",
                "Me: +1 (650) 555.1212 this\n",
                "Me: +1.650.555.1212 this\n",
                "Me: 650.555.1212 this\n",
                "Me: 555.1212 this\n",

                "Me: 1 650 555 1212 this\n",
                "Me: (650) 555 1212 this\n",
                "Me: +1 (650) 555 1212 this\n",
                "Me: +1 650 555 1212 this\n",
                "Me: 650 555 1212 this\n",
                "Me: 555 1212 this\n",
        };

        for (String test : tests) {
            Matcher m = Regex.PHONE_PATTERN.matcher(test);

            assertTrue("Valid phone " + test, m.find());
        }
    
public voidtestTldPattern()

        boolean t;

        t = Regex.TOP_LEVEL_DOMAIN_PATTERN.matcher("com").matches();
        assertTrue("Missed valid TLD", t);

        t = Regex.TOP_LEVEL_DOMAIN_PATTERN.matcher("xer").matches();
        assertFalse("Matched invalid TLD!", t);
    
public voidtestUrlPattern()

        boolean t;

        t = Regex.WEB_URL_PATTERN.matcher("http://www.google.com").matches();
        assertTrue("Valid URL", t);

        t = Regex.WEB_URL_PATTERN.matcher("ftp://www.example.com").matches();
        assertFalse("Matched invalid protocol", t);

        t = Regex.WEB_URL_PATTERN.matcher("http://www.example.com:8080").matches();
        assertTrue("Didn't match valid URL with port", t);

        t = Regex.WEB_URL_PATTERN.matcher("http://www.example.com:8080/?foo=bar").matches();
        assertTrue("Didn't match valid URL with port and query args", t);

        t = Regex.WEB_URL_PATTERN.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
        assertTrue("Didn't match valid URL with ~", t);