PatternsTest.java revision 9b1872e619bd06f33853716a4bae0214b4a7d176
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.util; 17 18import android.test.suitebuilder.annotation.SmallTest; 19import android.util.Patterns; 20 21import java.util.regex.Matcher; 22 23import junit.framework.TestCase; 24 25public class PatternsTest extends TestCase { 26 27 @SmallTest 28 public void testTldPattern() throws Exception { 29 boolean t; 30 31 t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches(); 32 assertTrue("Missed valid TLD", t); 33 34 // One of the new top level domain. 35 t = Patterns.TOP_LEVEL_DOMAIN.matcher("me").matches(); 36 assertTrue("Missed valid TLD", t); 37 38 // One of the new top level test domain. 39 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn--0zwm56d").matches(); 40 assertTrue("Missed valid TLD", t); 41 42 t = Patterns.TOP_LEVEL_DOMAIN.matcher("mem").matches(); 43 assertFalse("Matched invalid TLD!", t); 44 45 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn").matches(); 46 assertFalse("Matched invalid TLD!", t); 47 48 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches(); 49 assertFalse("Matched invalid TLD!", t); 50 } 51 52 @SmallTest 53 public void testUrlPattern() throws Exception { 54 boolean t; 55 56 t = Patterns.WEB_URL.matcher("http://www.google.com").matches(); 57 assertTrue("Valid URL", t); 58 59 // Google in one of the new top level domain. 60 t = Patterns.WEB_URL.matcher("http://www.google.me").matches(); 61 assertTrue("Valid URL", t); 62 t = Patterns.WEB_URL.matcher("google.me").matches(); 63 assertTrue("Valid URL", t); 64 65 // Test url in Chinese: http://xn--fsqu00a.xn--0zwm56d 66 t = Patterns.WEB_URL.matcher("http://xn--fsqu00a.xn--0zwm56d").matches(); 67 assertTrue("Valid URL", t); 68 t = Patterns.WEB_URL.matcher("xn--fsqu00a.xn--0zwm56d").matches(); 69 assertTrue("Valid URL", t); 70 71 // Url for testing top level Arabic country code domain in Punycode: 72 // http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx 73 t = Patterns.WEB_URL.matcher("http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches(); 74 assertTrue("Valid URL", t); 75 t = Patterns.WEB_URL.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches(); 76 assertTrue("Valid URL", t); 77 78 // Internationalized URL. 79 t = Patterns.WEB_URL.matcher("http://\uD604\uAE08\uC601\uC218\uC99D.kr").matches(); 80 assertTrue("Valid URL", t); 81 t = Patterns.WEB_URL.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches(); 82 assertTrue("Valid URL", t); 83 84 t = Patterns.WEB_URL.matcher("http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" + 85 "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/").matches(); 86 assertTrue("Valid URL", t); 87 88 t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches(); 89 assertFalse("Matched invalid protocol", t); 90 91 t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches(); 92 assertTrue("Didn't match valid URL with port", t); 93 94 t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches(); 95 assertTrue("Didn't match valid URL with port and query args", t); 96 97 t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches(); 98 assertTrue("Didn't match valid URL with ~", t); 99 } 100 101 @SmallTest 102 public void testIpPattern() throws Exception { 103 boolean t; 104 105 t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches(); 106 assertTrue("Valid IP", t); 107 108 t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches(); 109 assertFalse("Invalid IP", t); 110 } 111 112 @SmallTest 113 public void testDomainPattern() throws Exception { 114 boolean t; 115 116 t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches(); 117 assertTrue("Valid domain", t); 118 119 t = Patterns.WEB_URL.matcher("google.me").matches(); 120 assertTrue("Valid domain", t); 121 122 // Internationalized domains. 123 t = Patterns.DOMAIN_NAME.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches(); 124 assertTrue("Valid domain", t); 125 126 t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches(); 127 assertFalse("Invalid domain", t); 128 129 // Obsolete domain .yu 130 t = Patterns.DOMAIN_NAME.matcher("test.yu").matches(); 131 assertFalse("Obsolete country code top level domain", t); 132 133 // Testing top level Arabic country code domain in Punycode: 134 t = Patterns.WEB_URL.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c").matches(); 135 assertTrue("Valid domain", t); 136 } 137 138 @SmallTest 139 public void testPhonePattern() throws Exception { 140 boolean t; 141 142 t = Patterns.PHONE.matcher("(919) 555-1212").matches(); 143 assertTrue("Valid phone", t); 144 145 t = Patterns.PHONE.matcher("2334 9323/54321").matches(); 146 assertFalse("Invalid phone", t); 147 148 String[] tests = { 149 "Me: 16505551212 this\n", 150 "Me: 6505551212 this\n", 151 "Me: 5551212 this\n", 152 153 "Me: 1-650-555-1212 this\n", 154 "Me: (650) 555-1212 this\n", 155 "Me: +1 (650) 555-1212 this\n", 156 "Me: +1-650-555-1212 this\n", 157 "Me: 650-555-1212 this\n", 158 "Me: 555-1212 this\n", 159 160 "Me: 1.650.555.1212 this\n", 161 "Me: (650) 555.1212 this\n", 162 "Me: +1 (650) 555.1212 this\n", 163 "Me: +1.650.555.1212 this\n", 164 "Me: 650.555.1212 this\n", 165 "Me: 555.1212 this\n", 166 167 "Me: 1 650 555 1212 this\n", 168 "Me: (650) 555 1212 this\n", 169 "Me: +1 (650) 555 1212 this\n", 170 "Me: +1 650 555 1212 this\n", 171 "Me: 650 555 1212 this\n", 172 "Me: 555 1212 this\n", 173 }; 174 175 for (String test : tests) { 176 Matcher m = Patterns.PHONE.matcher(test); 177 178 assertTrue("Valid phone " + test, m.find()); 179 } 180 } 181} 182