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 */ 16 17 package android.util; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import androidx.test.ext.junit.runners.AndroidJUnit4; 24 import androidx.test.filters.SmallTest; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.regex.Matcher; 30 import java.util.regex.Pattern; 31 32 @RunWith(AndroidJUnit4.class) 33 public class PatternsTest { 34 35 // Tests for Patterns.TOP_LEVEL_DOMAIN 36 37 @Test 38 @SmallTest testTldPattern()39 public void testTldPattern() throws Exception { 40 boolean t; 41 42 t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches(); 43 assertTrue("Missed valid TLD", t); 44 45 // One of the new top level domain. 46 t = Patterns.TOP_LEVEL_DOMAIN.matcher("me").matches(); 47 assertTrue("Missed valid TLD", t); 48 49 // One of the new top level test domain. 50 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn--0zwm56d").matches(); 51 assertTrue("Missed valid TLD", t); 52 53 // One of the new top level unicode domain. 54 t = Patterns.TOP_LEVEL_DOMAIN.matcher("\uD55C\uAD6D").matches(); 55 assertTrue("Missed valid TLD", t); 56 57 t = Patterns.TOP_LEVEL_DOMAIN.matcher("mem").matches(); 58 assertFalse("Matched invalid TLD!", t); 59 60 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn").matches(); 61 assertFalse("Matched invalid TLD!", t); 62 63 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches(); 64 assertFalse("Matched invalid TLD!", t); 65 } 66 67 // Tests for Patterns.IANA_TOP_LEVEL_DOMAINS 68 69 @Test 70 @SmallTest testIanaTopLevelDomains_matchesValidTld()71 public void testIanaTopLevelDomains_matchesValidTld() throws Exception { 72 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 73 assertTrue("Should match 'com'", pattern.matcher("com").matches()); 74 } 75 76 @Test 77 @SmallTest testIanaTopLevelDomains_matchesValidNewTld()78 public void testIanaTopLevelDomains_matchesValidNewTld() throws Exception { 79 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 80 assertTrue("Should match 'me'", pattern.matcher("me").matches()); 81 } 82 83 @Test 84 @SmallTest testIanaTopLevelDomains_matchesPunycodeTld()85 public void testIanaTopLevelDomains_matchesPunycodeTld() throws Exception { 86 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 87 assertTrue("Should match Punycode TLD", pattern.matcher("xn--qxam").matches()); 88 } 89 90 @Test 91 @SmallTest testIanaTopLevelDomains_matchesIriTLD()92 public void testIanaTopLevelDomains_matchesIriTLD() throws Exception { 93 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 94 assertTrue("Should match IRI TLD", pattern.matcher("\uD55C\uAD6D").matches()); 95 } 96 97 @Test 98 @SmallTest testIanaTopLevelDomains_doesNotMatchWrongTld()99 public void testIanaTopLevelDomains_doesNotMatchWrongTld() throws Exception { 100 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 101 assertFalse("Should not match 'mem'", pattern.matcher("mem").matches()); 102 } 103 104 @Test 105 @SmallTest testIanaTopLevelDomains_doesNotMatchWrongPunycodeTld()106 public void testIanaTopLevelDomains_doesNotMatchWrongPunycodeTld() throws Exception { 107 Pattern pattern = Pattern.compile(Patterns.IANA_TOP_LEVEL_DOMAINS); 108 assertFalse("Should not match invalid Punycode TLD", pattern.matcher("xn").matches()); 109 } 110 111 // Tests for Patterns.WEB_URL 112 113 @Test 114 @SmallTest testWebUrl_matchesValidUrlWithSchemeAndHostname()115 public void testWebUrl_matchesValidUrlWithSchemeAndHostname() throws Exception { 116 String url = "http://www.android.com"; 117 assertTrue("Should match URL with scheme and hostname", 118 Patterns.WEB_URL.matcher(url).matches()); 119 } 120 121 @Test 122 @SmallTest testWebUrl_matchesValidUrlWithSchemeHostnameAndNewTld()123 public void testWebUrl_matchesValidUrlWithSchemeHostnameAndNewTld() throws Exception { 124 String url = "http://www.android.me"; 125 assertTrue("Should match URL with scheme, hostname and new TLD", 126 Patterns.WEB_URL.matcher(url).matches()); 127 } 128 129 @Test 130 @SmallTest testWebUrl_matchesValidUrlWithHostnameAndNewTld()131 public void testWebUrl_matchesValidUrlWithHostnameAndNewTld() throws Exception { 132 String url = "android.me"; 133 assertTrue("Should match URL with hostname and new TLD", 134 Patterns.WEB_URL.matcher(url).matches()); 135 } 136 137 @Test 138 @SmallTest testWebUrl_matchesChinesePunycodeUrlWithProtocol()139 public void testWebUrl_matchesChinesePunycodeUrlWithProtocol() throws Exception { 140 String url = "http://xn--fsqu00a.xn--0zwm56d"; 141 assertTrue("Should match Chinese Punycode URL with protocol", 142 Patterns.WEB_URL.matcher(url).matches()); 143 } 144 145 @Test 146 @SmallTest testWebUrl_matchesChinesePunycodeUrlWithoutProtocol()147 public void testWebUrl_matchesChinesePunycodeUrlWithoutProtocol() throws Exception { 148 String url = "xn--fsqu00a.xn--0zwm56d"; 149 assertTrue("Should match Chinese Punycode URL without protocol", 150 Patterns.WEB_URL.matcher(url).matches()); 151 } 152 153 @Test 154 @SmallTest testWebUrl_matchesArabicPunycodeUrlWithProtocol()155 public void testWebUrl_matchesArabicPunycodeUrlWithProtocol() throws Exception { 156 String url = "http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx"; 157 assertTrue("Should match arabic Punycode URL with protocol", 158 Patterns.WEB_URL.matcher(url).matches()); 159 } 160 161 @Test 162 @SmallTest testWebUrl_matchesArabicPunycodeUrlWithoutProtocol()163 public void testWebUrl_matchesArabicPunycodeUrlWithoutProtocol() throws Exception { 164 String url = "xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx"; 165 assertTrue("Should match Arabic Punycode URL without protocol", 166 Patterns.WEB_URL.matcher(url).matches()); 167 } 168 169 @Test 170 @SmallTest testWebUrl_matchesUrlWithUnicodeDomainNameWithProtocol()171 public void testWebUrl_matchesUrlWithUnicodeDomainNameWithProtocol() throws Exception { 172 String url = "http://\uD604\uAE08\uC601\uC218\uC99D.kr"; 173 assertTrue("Should match URL with Unicode domain name", 174 Patterns.WEB_URL.matcher(url).matches()); 175 } 176 177 @Test 178 @SmallTest testWebUrl_matchesUrlWithUnicodeDomainNameWithoutProtocol()179 public void testWebUrl_matchesUrlWithUnicodeDomainNameWithoutProtocol() throws Exception { 180 String url = "\uD604\uAE08\uC601\uC218\uC99D.kr"; 181 assertTrue("Should match URL without protocol and with Unicode domain name", 182 Patterns.WEB_URL.matcher(url).matches()); 183 } 184 185 @Test 186 @SmallTest testWebUrl_matchesUrlWithUnicodeTld()187 public void testWebUrl_matchesUrlWithUnicodeTld() throws Exception { 188 String url = "\uB3C4\uBA54\uC778.\uD55C\uAD6D"; 189 assertTrue("Should match URL with Unicode TLD", 190 Patterns.WEB_URL.matcher(url).matches()); 191 } 192 193 @Test 194 @SmallTest testWebUrl_matchesUrlWithUnicodePath()195 public void testWebUrl_matchesUrlWithUnicodePath() throws Exception { 196 String url = "http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" + 197 "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/"; 198 assertTrue("Should match URL with Unicode path", 199 Patterns.WEB_URL.matcher(url).matches()); 200 } 201 202 @Test 203 @SmallTest testWebUrl_doesNotMatchValidUrlWithInvalidProtocol()204 public void testWebUrl_doesNotMatchValidUrlWithInvalidProtocol() throws Exception { 205 String url = "invalid://www.example.com"; 206 assertFalse("Should not match URL with invalid protocol", 207 Patterns.WEB_URL.matcher(url).matches()); 208 } 209 210 @Test 211 @SmallTest testWebUrl_matchesValidUrlWithPort()212 public void testWebUrl_matchesValidUrlWithPort() throws Exception { 213 String url = "http://www.example.com:8080"; 214 assertTrue("Should match URL with port", Patterns.WEB_URL.matcher(url).matches()); 215 } 216 217 @Test 218 @SmallTest testWebUrl_matchesUrlWithPortAndQuery()219 public void testWebUrl_matchesUrlWithPortAndQuery() throws Exception { 220 String url = "http://www.example.com:8080/?foo=bar"; 221 assertTrue("Should match URL with port and query", 222 Patterns.WEB_URL.matcher(url).matches()); 223 } 224 225 @Test 226 @SmallTest testWebUrl_matchesUrlWithTilde()227 public void testWebUrl_matchesUrlWithTilde() throws Exception { 228 String url = "http://www.example.com:8080/~user/?foo=bar"; 229 assertTrue("Should match URL with tilde", Patterns.WEB_URL.matcher(url).matches()); 230 } 231 232 @Test 233 @SmallTest testWebUrl_matchesProtocolCaseInsensitive()234 public void testWebUrl_matchesProtocolCaseInsensitive() throws Exception { 235 String url = "hTtP://android.com"; 236 assertTrue("Protocol matching should be case insensitive", 237 Patterns.WEB_URL.matcher(url).matches()); 238 } 239 240 @Test 241 @SmallTest testWebUrl_matchesDomainNameWithDash()242 public void testWebUrl_matchesDomainNameWithDash() throws Exception { 243 String url = "http://a-nd.r-oid.com"; 244 assertTrue("Should match dash in domain name", 245 Patterns.WEB_URL.matcher(url).matches()); 246 247 url = "a-nd.r-oid.com"; 248 assertTrue("Should match dash in domain name", 249 Patterns.WEB_URL.matcher(url).matches()); 250 } 251 252 @Test 253 @SmallTest testWebUrl_matchesDomainNameWithUnderscore()254 public void testWebUrl_matchesDomainNameWithUnderscore() throws Exception { 255 String url = "http://a_nd.r_oid.com"; 256 assertTrue("Should match underscore in domain name", 257 Patterns.WEB_URL.matcher(url).matches()); 258 259 url = "a_nd.r_oid.com"; 260 assertTrue("Should match underscore in domain name", 261 Patterns.WEB_URL.matcher(url).matches()); 262 } 263 264 @Test 265 @SmallTest testWebUrl_matchesPathAndQueryWithDollarSign()266 public void testWebUrl_matchesPathAndQueryWithDollarSign() throws Exception { 267 String url = "http://android.com/path$?v=$val"; 268 assertTrue("Should match dollar sign in path/query", 269 Patterns.WEB_URL.matcher(url).matches()); 270 271 url = "android.com/path$?v=$val"; 272 assertTrue("Should match dollar sign in path/query", 273 Patterns.WEB_URL.matcher(url).matches()); 274 } 275 276 @Test 277 @SmallTest testWebUrl_matchesEmptyPathWithQueryParams()278 public void testWebUrl_matchesEmptyPathWithQueryParams() throws Exception { 279 String url = "http://android.com?q=v"; 280 assertTrue("Should match empty path with query param", 281 Patterns.WEB_URL.matcher(url).matches()); 282 283 url = "android.com?q=v"; 284 assertTrue("Should match empty path with query param", 285 Patterns.WEB_URL.matcher(url).matches()); 286 287 url = "http://android.com/?q=v"; 288 assertTrue("Should match empty path with query param", 289 Patterns.WEB_URL.matcher(url).matches()); 290 291 url = "android.com/?q=v"; 292 assertTrue("Should match empty path with query param", 293 Patterns.WEB_URL.matcher(url).matches()); 294 } 295 296 // Tests for Patterns.AUTOLINK_WEB_URL 297 298 @Test 299 @SmallTest testAutoLinkWebUrl_matchesValidUrlWithSchemeAndHostname()300 public void testAutoLinkWebUrl_matchesValidUrlWithSchemeAndHostname() throws Exception { 301 String url = "http://www.android.com"; 302 assertTrue("Should match URL with scheme and hostname", 303 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 304 } 305 306 @Test 307 @SmallTest testAutoLinkWebUrl_matchesValidUrlWithSchemeHostnameAndNewTld()308 public void testAutoLinkWebUrl_matchesValidUrlWithSchemeHostnameAndNewTld() throws Exception { 309 String url = "http://www.android.me"; 310 assertTrue("Should match URL with scheme, hostname and new TLD", 311 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 312 } 313 314 @Test 315 @SmallTest testAutoLinkWebUrl_matchesValidUrlWithHostnameAndNewTld()316 public void testAutoLinkWebUrl_matchesValidUrlWithHostnameAndNewTld() throws Exception { 317 String url = "android.me"; 318 assertTrue("Should match URL with hostname and new TLD", 319 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 320 321 url = "android.camera"; 322 assertTrue("Should match URL with hostname and new TLD", 323 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 324 } 325 326 @Test 327 @SmallTest testAutoLinkWebUrl_matchesChinesePunycodeUrlWithProtocol()328 public void testAutoLinkWebUrl_matchesChinesePunycodeUrlWithProtocol() throws Exception { 329 String url = "http://xn--fsqu00a.xn--0zwm56d"; 330 assertTrue("Should match Chinese Punycode URL with protocol", 331 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 332 } 333 334 @Test 335 @SmallTest testAutoLinkWebUrl_matchesChinesePunycodeUrlWithoutProtocol()336 public void testAutoLinkWebUrl_matchesChinesePunycodeUrlWithoutProtocol() throws Exception { 337 String url = "xn--fsqu00a.xn--0zwm56d"; 338 assertTrue("Should match Chinese Punycode URL without protocol", 339 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 340 } 341 342 @Test 343 @SmallTest testAutoLinkWebUrl_matchesArabicPunycodeUrlWithProtocol()344 public void testAutoLinkWebUrl_matchesArabicPunycodeUrlWithProtocol() throws Exception { 345 String url = "http://xn--4gbrim.xn--rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx"; 346 assertTrue("Should match Arabic Punycode URL with protocol", 347 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 348 } 349 350 @Test 351 @SmallTest testAutoLinkWebUrl_matchesArabicPunycodeUrlWithoutProtocol()352 public void testAutoLinkWebUrl_matchesArabicPunycodeUrlWithoutProtocol() throws Exception { 353 String url = "xn--4gbrim.xn--rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx"; 354 assertTrue("Should match Arabic Punycode URL without protocol", 355 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 356 } 357 358 @Test 359 @SmallTest testAutoLinkWebUrl_doesNotMatchPunycodeTldThatStartsWithDash()360 public void testAutoLinkWebUrl_doesNotMatchPunycodeTldThatStartsWithDash() throws Exception { 361 String url = "http://xn--fsqu00a.-xn--0zwm56d"; 362 assertFalse("Should not match Punycode TLD that starts with dash", 363 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 364 } 365 366 @Test 367 @SmallTest testAutoLinkWebUrl_doesNotMatchPunycodeTldThatEndsWithDash()368 public void testAutoLinkWebUrl_doesNotMatchPunycodeTldThatEndsWithDash() throws Exception { 369 String url = "http://xn--fsqu00a.xn--0zwm56d-"; 370 assertFalse("Should not match Punycode TLD that ends with dash", 371 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 372 } 373 374 @Test 375 @SmallTest testAutoLinkWebUrl_matchesUrlWithUnicodeDomainName()376 public void testAutoLinkWebUrl_matchesUrlWithUnicodeDomainName() throws Exception { 377 String url = "http://\uD604\uAE08\uC601\uC218\uC99D.kr"; 378 assertTrue("Should match URL with Unicode domain name", 379 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 380 381 url = "\uD604\uAE08\uC601\uC218\uC99D.kr"; 382 assertTrue("hould match URL without protocol and with Unicode domain name", 383 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 384 } 385 386 @Test 387 @SmallTest testAutoLinkWebUrl_matchesUrlWithUnicodeTld()388 public void testAutoLinkWebUrl_matchesUrlWithUnicodeTld() throws Exception { 389 String url = "\uB3C4\uBA54\uC778.\uD55C\uAD6D"; 390 assertTrue("Should match URL with Unicode TLD", 391 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 392 } 393 394 @Test 395 @SmallTest testAutoLinkWebUrl_matchesUrlWithUnicodePath()396 public void testAutoLinkWebUrl_matchesUrlWithUnicodePath() throws Exception { 397 String url = "http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" + 398 "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/"; 399 assertTrue("Should match URL with Unicode path", 400 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 401 } 402 403 @Test 404 @SmallTest testAutoLinkWebUrl_doesNotMatchValidUrlWithInvalidProtocol()405 public void testAutoLinkWebUrl_doesNotMatchValidUrlWithInvalidProtocol() throws Exception { 406 String url = "invalid://www.example.com"; 407 assertFalse("Should not match URL with invalid protocol", 408 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 409 } 410 411 @Test 412 @SmallTest testAutoLinkWebUrl_matchesValidUrlWithPort()413 public void testAutoLinkWebUrl_matchesValidUrlWithPort() throws Exception { 414 String url = "http://www.example.com:8080"; 415 assertTrue("Should match URL with port", 416 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 417 } 418 419 @Test 420 @SmallTest testAutoLinkWebUrl_matchesUrlWithPortAndQuery()421 public void testAutoLinkWebUrl_matchesUrlWithPortAndQuery() throws Exception { 422 String url = "http://www.example.com:8080/?foo=bar"; 423 assertTrue("Should match URL with port and query", 424 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 425 } 426 427 @Test 428 @SmallTest testAutoLinkWebUrl_matchesUrlWithTilde()429 public void testAutoLinkWebUrl_matchesUrlWithTilde() throws Exception { 430 String url = "http://www.example.com:8080/~user/?foo=bar"; 431 assertTrue("Should match URL with tilde", 432 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 433 } 434 435 @Test 436 @SmallTest testAutoLinkWebUrl_matchesProtocolCaseInsensitive()437 public void testAutoLinkWebUrl_matchesProtocolCaseInsensitive() throws Exception { 438 String url = "hTtP://android.com"; 439 assertTrue("Protocol matching should be case insensitive", 440 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 441 } 442 443 @Test 444 @SmallTest testAutoLinkWebUrl_matchesUrlStartingWithHttpAndDoesNotHaveTld()445 public void testAutoLinkWebUrl_matchesUrlStartingWithHttpAndDoesNotHaveTld() throws Exception { 446 String url = "http://android/#notld///a/n/d/r/o/i/d&p1=1&p2=2"; 447 assertTrue("Should match URL without a TLD and starting with http ", 448 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 449 } 450 451 @Test 452 @SmallTest testAutoLinkWebUrl_doesNotMatchUrlsWithoutProtocolAndWithUnknownTld()453 public void testAutoLinkWebUrl_doesNotMatchUrlsWithoutProtocolAndWithUnknownTld() 454 throws Exception { 455 String url = "thank.unknowntld"; 456 assertFalse("Should not match URL that does not start with a protocol and " + 457 "does not contain a known TLD", 458 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 459 } 460 461 @Test 462 @SmallTest testAutoLinkWebUrl_doesNotPartiallyMatchUnknownProtocol()463 public void testAutoLinkWebUrl_doesNotPartiallyMatchUnknownProtocol() throws Exception { 464 String url = "invalid://foo.bar/baz"; 465 assertFalse("Should not partially match URL with unknown protocol", 466 Patterns.AUTOLINK_WEB_URL.matcher(url).find()); 467 } 468 469 @Test 470 @SmallTest testAutoLinkWebUrl_matchesValidUrlWithEmoji()471 public void testAutoLinkWebUrl_matchesValidUrlWithEmoji() throws Exception { 472 String url = "Thank\u263A.com"; 473 assertTrue("Should match URL with emoji", 474 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 475 } 476 477 @Test 478 @SmallTest testAutoLinkWebUrl_doesNotMatchUrlsWithEmojiWithoutProtocolAndWithoutKnownTld()479 public void testAutoLinkWebUrl_doesNotMatchUrlsWithEmojiWithoutProtocolAndWithoutKnownTld() 480 throws Exception { 481 String url = "Thank\u263A.unknowntld"; 482 assertFalse("Should not match URLs containing emoji and with unknown TLD", 483 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 484 } 485 486 @Test 487 @SmallTest testAutoLinkWebUrl_doesNotMatchEmailAddress()488 public void testAutoLinkWebUrl_doesNotMatchEmailAddress() 489 throws Exception { 490 String url = "[email protected]"; 491 assertFalse("Should not match email address", 492 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 493 } 494 495 @Test 496 @SmallTest testAutoLinkWebUrl_matchesDomainNameWithSurrogatePairs()497 public void testAutoLinkWebUrl_matchesDomainNameWithSurrogatePairs() throws Exception { 498 String url = "android\uD83C\uDF38.com"; 499 assertTrue("Should match domain name with Unicode surrogate pairs", 500 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 501 } 502 503 @Test 504 @SmallTest testAutoLinkWebUrl_matchesTldWithSurrogatePairs()505 public void testAutoLinkWebUrl_matchesTldWithSurrogatePairs() throws Exception { 506 String url = "http://android.\uD83C\uDF38com"; 507 assertTrue("Should match TLD with Unicode surrogate pairs", 508 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 509 } 510 511 @Test 512 @SmallTest testAutoLinkWebUrl_matchesPathWithSurrogatePairs()513 public void testAutoLinkWebUrl_matchesPathWithSurrogatePairs() throws Exception { 514 String url = "http://android.com/path-with-\uD83C\uDF38?v=\uD83C\uDF38"; 515 assertTrue("Should match path and query with Unicode surrogate pairs", 516 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 517 } 518 519 @Test 520 @SmallTest testAutoLinkWebUrl_doesNotMatchUrlWithExcludedSurrogate()521 public void testAutoLinkWebUrl_doesNotMatchUrlWithExcludedSurrogate() throws Exception { 522 String url = "http://android\uD83F\uDFFE.com"; 523 assertFalse("Should not match URL with excluded Unicode surrogate pair", 524 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 525 } 526 527 @Test 528 @SmallTest testAutoLinkWebUrl_doesNotMatchUnicodeSpaces()529 public void testAutoLinkWebUrl_doesNotMatchUnicodeSpaces() throws Exception { 530 String part1 = "http://and"; 531 String part2 = "roid"; 532 String[] emptySpaces = new String[]{ 533 "\u00A0", // no-break space 534 "\u2000", // en quad 535 "\u2001", // em quad 536 "\u2002", // en space 537 "\u2003", // em space 538 "\u2004", // three-per-em space 539 "\u2005", // four-per-em space 540 "\u2006", // six-per-em space 541 "\u2007", // figure space 542 "\u2008", // punctuation space 543 "\u2009", // thin space 544 "\u200A", // hair space 545 "\u2028", // line separator 546 "\u2029", // paragraph separator 547 "\u202F", // narrow no-break space 548 "\u3000" // ideographic space 549 }; 550 551 for (String emptySpace : emptySpaces) { 552 String url = part1 + emptySpace + part2; 553 assertFalse("Should not match empty space - code:" + emptySpace.codePointAt(0), 554 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 555 } 556 } 557 558 @Test 559 @SmallTest testAutoLinkWebUrl_matchesDomainNameWithDash()560 public void testAutoLinkWebUrl_matchesDomainNameWithDash() throws Exception { 561 String url = "http://a-nd.r-oid.com"; 562 assertTrue("Should match dash in domain name", 563 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 564 565 url = "a-nd.r-oid.com"; 566 assertTrue("Should match dash in domain name", 567 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 568 } 569 570 @Test 571 @SmallTest testAutoLinkWebUrl_matchesDomainNameWithUnderscore()572 public void testAutoLinkWebUrl_matchesDomainNameWithUnderscore() throws Exception { 573 String url = "http://a_nd.r_oid.com"; 574 assertTrue("Should match underscore in domain name", 575 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 576 577 url = "a_nd.r_oid.com"; 578 assertTrue("Should match underscore in domain name", 579 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 580 } 581 582 @Test 583 @SmallTest testAutoLinkWebUrl_matchesPathAndQueryWithDollarSign()584 public void testAutoLinkWebUrl_matchesPathAndQueryWithDollarSign() throws Exception { 585 String url = "http://android.com/path$?v=$val"; 586 assertTrue("Should match dollar sign in path/query", 587 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 588 589 url = "android.com/path$?v=$val"; 590 assertTrue("Should match dollar sign in path/query", 591 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 592 } 593 594 @Test 595 @SmallTest testAutoLinkWebUrl_matchesEmptyPathWithQueryParams()596 public void testAutoLinkWebUrl_matchesEmptyPathWithQueryParams() throws Exception { 597 String url = "http://android.com?q=v"; 598 assertTrue("Should match empty path with query param", 599 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 600 601 url = "android.com?q=v"; 602 assertTrue("Should match empty path with query param", 603 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 604 605 url = "http://android.com/?q=v"; 606 assertTrue("Should match empty path with query param", 607 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 608 609 url = "android.com/?q=v"; 610 assertTrue("Should match empty path with query param", 611 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 612 } 613 614 // Tests for Patterns.IP_ADDRESS 615 616 @Test 617 @SmallTest testIpPattern()618 public void testIpPattern() throws Exception { 619 boolean t; 620 621 t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches(); 622 assertTrue("Valid IP", t); 623 624 t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches(); 625 assertFalse("Invalid IP", t); 626 } 627 628 // Tests for Patterns.DOMAIN_NAME 629 630 @Test 631 @SmallTest testDomain_matchesPunycodeTld()632 public void testDomain_matchesPunycodeTld() throws Exception { 633 String domain = "xn--fsqu00a.xn--0zwm56d"; 634 assertTrue("Should match domain name in Punycode", 635 Patterns.DOMAIN_NAME.matcher(domain).matches()); 636 } 637 638 @Test 639 @SmallTest testDomain_doesNotMatchPunycodeThatStartsWithDash()640 public void testDomain_doesNotMatchPunycodeThatStartsWithDash() throws Exception { 641 String domain = "xn--fsqu00a.-xn--0zwm56d"; 642 assertFalse("Should not match Punycode TLD that starts with a dash", 643 Patterns.DOMAIN_NAME.matcher(domain).matches()); 644 } 645 646 @Test 647 @SmallTest testDomain_doesNotMatchPunycodeThatEndsWithDash()648 public void testDomain_doesNotMatchPunycodeThatEndsWithDash() throws Exception { 649 String domain = "xn--fsqu00a.xn--0zwm56d-"; 650 assertFalse("Should not match Punycode TLD that ends with a dash", 651 Patterns.DOMAIN_NAME.matcher(domain).matches()); 652 } 653 654 @Test 655 @SmallTest testDomain_doesNotMatchPunycodeLongerThanAllowed()656 public void testDomain_doesNotMatchPunycodeLongerThanAllowed() throws Exception { 657 String tld = "xn--"; 658 for(int i=0; i<=6; i++) { 659 tld += "0123456789"; 660 } 661 String domain = "xn--fsqu00a." + tld; 662 assertFalse("Should not match Punycode TLD that is longer than 63 chars", 663 Patterns.DOMAIN_NAME.matcher(domain).matches()); 664 } 665 666 @Test 667 @SmallTest testDomain_matchesObsoleteTld()668 public void testDomain_matchesObsoleteTld() throws Exception { 669 String domain = "test.yu"; 670 assertTrue("Should match domain names with obsolete TLD", 671 Patterns.DOMAIN_NAME.matcher(domain).matches()); 672 } 673 674 @Test 675 @SmallTest testDomain_matchesWithSubDomain()676 public void testDomain_matchesWithSubDomain() throws Exception { 677 String domain = "mail.example.com"; 678 assertTrue("Should match domain names with subdomains", 679 Patterns.DOMAIN_NAME.matcher(domain).matches()); 680 } 681 682 @Test 683 @SmallTest testDomain_matchesWithoutSubDomain()684 public void testDomain_matchesWithoutSubDomain() throws Exception { 685 String domain = "android.me"; 686 assertTrue("Should match domain names without subdomains", 687 Patterns.DOMAIN_NAME.matcher(domain).matches()); 688 } 689 690 @Test 691 @SmallTest testDomain_matchesUnicodeDomainNames()692 public void testDomain_matchesUnicodeDomainNames() throws Exception { 693 String domain = "\uD604\uAE08\uC601\uC218\uC99D.kr"; 694 assertTrue("Should match unicodedomain names", 695 Patterns.DOMAIN_NAME.matcher(domain).matches()); 696 } 697 698 @Test 699 @SmallTest testDomain_doesNotMatchInvalidDomain()700 public void testDomain_doesNotMatchInvalidDomain() throws Exception { 701 String domain = "__+&42.xer"; 702 assertFalse("Should not match invalid domain name", 703 Patterns.DOMAIN_NAME.matcher(domain).matches()); 704 } 705 706 @Test 707 @SmallTest testDomain_matchesPunycodeArabicDomainName()708 public void testDomain_matchesPunycodeArabicDomainName() throws Exception { 709 String domain = "xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c"; 710 assertTrue("Should match Punycode Arabic domain name", 711 Patterns.DOMAIN_NAME.matcher(domain).matches()); 712 } 713 714 @Test 715 @SmallTest testDomain_matchesDomainNameWithDash()716 public void testDomain_matchesDomainNameWithDash() throws Exception { 717 String url = "http://a-nd.r-oid.com"; 718 assertTrue("Should match dash in domain name", 719 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 720 721 url = "a-nd.r-oid.com"; 722 assertTrue("Should match dash in domain name", 723 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 724 } 725 726 @Test 727 @SmallTest testDomain_matchesDomainNameWithUnderscore()728 public void testDomain_matchesDomainNameWithUnderscore() throws Exception { 729 String url = "http://a_nd.r_oid.com"; 730 assertTrue("Should match underscore in domain name", 731 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 732 733 url = "a_nd.r_oid.com"; 734 assertTrue("Should match underscore in domain name", 735 Patterns.AUTOLINK_WEB_URL.matcher(url).matches()); 736 } 737 738 // Tests for Patterns.AUTOLINK_EMAIL_ADDRESS 739 740 @Test 741 @SmallTest testAutoLinkEmailAddress_matchesShortValidEmail()742 public void testAutoLinkEmailAddress_matchesShortValidEmail() throws Exception { 743 String email = "[email protected]"; 744 assertTrue("Should match short valid email: " + email, 745 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 746 } 747 748 @Test 749 @SmallTest testAutoLinkEmailAddress_matchesRegularEmail()750 public void testAutoLinkEmailAddress_matchesRegularEmail() throws Exception { 751 String email = "[email protected]"; 752 assertTrue("Should match email: " + email, 753 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 754 } 755 756 @Test 757 @SmallTest testAutoLinkEmailAddress_matchesEmailWithMultipleSubdomains()758 public void testAutoLinkEmailAddress_matchesEmailWithMultipleSubdomains() throws Exception { 759 String email = "[email protected]"; 760 assertTrue("Should match email: " + email, 761 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 762 } 763 764 @Test 765 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithDot()766 public void testAutoLinkEmailAddress_matchesLocalPartWithDot() throws Exception { 767 String email = "[email protected]"; 768 assertTrue("Should match email: " + email, 769 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 770 } 771 772 @Test 773 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithPlus()774 public void testAutoLinkEmailAddress_matchesLocalPartWithPlus() throws Exception { 775 String email = "[email protected]"; 776 assertTrue("Should match email: " + email, 777 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 778 } 779 780 @Test 781 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithUnderscore()782 public void testAutoLinkEmailAddress_matchesLocalPartWithUnderscore() throws Exception { 783 String email = "[email protected]"; 784 assertTrue("Should match email: " + email, 785 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 786 } 787 788 @Test 789 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithDash()790 public void testAutoLinkEmailAddress_matchesLocalPartWithDash() throws Exception { 791 String email = "[email protected]"; 792 assertTrue("Should match email: " + email, 793 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 794 } 795 796 @Test 797 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithApostrophe()798 public void testAutoLinkEmailAddress_matchesLocalPartWithApostrophe() throws Exception { 799 String email = "e'[email protected]"; 800 assertTrue("Should match email: " + email, 801 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 802 } 803 804 @Test 805 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithDigits()806 public void testAutoLinkEmailAddress_matchesLocalPartWithDigits() throws Exception { 807 String email = "[email protected]"; 808 assertTrue("Should match email: " + email, 809 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 810 } 811 812 @Test 813 @SmallTest testAutoLinkEmailAddress_matchesUnicodeLocalPart()814 public void testAutoLinkEmailAddress_matchesUnicodeLocalPart() throws Exception { 815 String email = "\uD604\uAE08\uC601\uC218\[email protected]"; 816 assertTrue("Should match email: " + email, 817 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 818 } 819 820 @Test 821 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithEmoji()822 public void testAutoLinkEmailAddress_matchesLocalPartWithEmoji() throws Exception { 823 String email = "smiley\[email protected]"; 824 assertTrue("Should match email: " + email, 825 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 826 } 827 828 @Test 829 @SmallTest testAutoLinkEmailAddress_matchesLocalPartWithSurrogatePairs()830 public void testAutoLinkEmailAddress_matchesLocalPartWithSurrogatePairs() throws Exception { 831 String email = "\uD83C\[email protected]"; 832 assertTrue("Should match email: " + email, 833 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 834 } 835 836 @Test 837 @SmallTest testAutoLinkEmailAddress_matchesDomainWithDash()838 public void testAutoLinkEmailAddress_matchesDomainWithDash() throws Exception { 839 String email = "[email protected]"; 840 assertTrue("Should match email: " + email, 841 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 842 } 843 844 @Test 845 @SmallTest testAutoLinkEmailAddress_matchesUnicodeDomain()846 public void testAutoLinkEmailAddress_matchesUnicodeDomain() throws Exception { 847 String email = "email@\uD604\uAE08\uC601\uC218\uC99D.kr"; 848 assertTrue("Should match email: " + email, 849 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 850 } 851 852 @Test 853 @SmallTest testAutoLinkEmailAddress_matchesUnicodeLocalPartAndDomain()854 public void testAutoLinkEmailAddress_matchesUnicodeLocalPartAndDomain() throws Exception { 855 String email = "\uD604\uAE08\uC601\uC218\uC99D@\uD604\uAE08\uC601\uC218\uC99D.kr"; 856 assertTrue("Should match email: " + email, 857 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 858 } 859 860 @Test 861 @SmallTest testAutoLinkEmailAddress_matchesDomainWithEmoji()862 public void testAutoLinkEmailAddress_matchesDomainWithEmoji() throws Exception { 863 String email = "smiley@\u263Aandroid.com"; 864 assertTrue("Should match email: " + email, 865 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 866 } 867 868 @Test 869 @SmallTest testAutoLinkEmailAddress_matchesDomainWithSurrogatePairs()870 public void testAutoLinkEmailAddress_matchesDomainWithSurrogatePairs() throws Exception { 871 String email = "email@\uD83C\uDF38android.com"; 872 assertTrue("Should match email: " + email, 873 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 874 } 875 876 @Test 877 @SmallTest testAutoLinkEmailAddress_matchesLocalPartAndDomainWithSurrogatePairs()878 public void testAutoLinkEmailAddress_matchesLocalPartAndDomainWithSurrogatePairs() 879 throws Exception { 880 String email = "\uD83C\uDF38@\uD83C\uDF38android.com"; 881 assertTrue("Should match email: " + email, 882 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 883 } 884 885 @Test 886 @SmallTest testAutoLinkEmailAddress_doesNotMatchStringWithoutAtSign()887 public void testAutoLinkEmailAddress_doesNotMatchStringWithoutAtSign() throws Exception { 888 String email = "android.com"; 889 assertFalse("Should not match email: " + email, 890 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 891 } 892 893 @Test 894 @SmallTest testAutoLinkEmailAddress_doesNotMatchPlainString()895 public void testAutoLinkEmailAddress_doesNotMatchPlainString() throws Exception { 896 String email = "email"; 897 assertFalse("Should not match email: " + email, 898 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 899 } 900 901 @Test 902 @SmallTest testAutoLinkEmailAddress_doesNotMatchStringWithMultipleAtSigns()903 public void testAutoLinkEmailAddress_doesNotMatchStringWithMultipleAtSigns() throws Exception { 904 String email = "email@[email protected]"; 905 assertFalse("Should not match email: " + email, 906 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 907 } 908 909 @Test 910 @SmallTest testAutoLinkEmailAddress_doesNotMatchEmailWithoutTld()911 public void testAutoLinkEmailAddress_doesNotMatchEmailWithoutTld() throws Exception { 912 String email = "email@android"; 913 assertFalse("Should not match email: " + email, 914 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 915 } 916 917 @Test 918 @SmallTest testAutoLinkEmailAddress_doesNotMatchLocalPartEndingWithDot()919 public void testAutoLinkEmailAddress_doesNotMatchLocalPartEndingWithDot() throws Exception { 920 String email = "[email protected]"; 921 assertFalse("Should not match email: " + email, 922 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 923 } 924 925 @Test 926 @SmallTest testAutoLinkEmailAddress_doesNotMatchLocalPartStartingWithDot()927 public void testAutoLinkEmailAddress_doesNotMatchLocalPartStartingWithDot() throws Exception { 928 String email = "[email protected]"; 929 assertFalse("Should not match email: " + email, 930 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 931 } 932 933 @Test 934 @SmallTest testAutoLinkEmailAddress_doesNotMatchDomainStartingWithDash()935 public void testAutoLinkEmailAddress_doesNotMatchDomainStartingWithDash() throws Exception { 936 String email = "[email protected]"; 937 assertFalse("Should not match email: " + email, 938 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 939 } 940 941 @Test 942 @SmallTest testAutoLinkEmailAddress_doesNotMatchDomainWithConsecutiveDots()943 public void testAutoLinkEmailAddress_doesNotMatchDomainWithConsecutiveDots() throws Exception { 944 String email = "[email protected]"; 945 assertFalse("Should not match email: " + email, 946 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 947 } 948 949 @Test 950 @SmallTest testAutoLinkEmailAddress_doesNotMatchEmailWithIpAsDomain()951 public void testAutoLinkEmailAddress_doesNotMatchEmailWithIpAsDomain() throws Exception { 952 String email = "[email protected]"; 953 assertFalse("Should not match email: " + email, 954 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 955 } 956 957 @Test 958 @SmallTest testAutoLinkEmailAddress_doesNotMatchEmailWithInvalidTld()959 public void testAutoLinkEmailAddress_doesNotMatchEmailWithInvalidTld() throws Exception { 960 String email = "[email protected]"; 961 assertFalse("Should not match email: " + email, 962 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 963 } 964 965 @Test 966 @SmallTest testAutoLinkEmailAddress_matchesLocalPartUpTo64Chars()967 public void testAutoLinkEmailAddress_matchesLocalPartUpTo64Chars() throws Exception { 968 String localPart = ""; 969 for (int i = 0; i < 64; i++) { 970 localPart += "a"; 971 } 972 String email = localPart + "@android.com"; 973 974 assertTrue("Should match local part of length: " + localPart.length(), 975 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 976 977 email = localPart + "[email protected]"; 978 assertFalse("Should not match local part of length: " + localPart.length(), 979 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 980 } 981 982 @Test 983 @SmallTest testAutoLinkEmailAddress_matchesSubdomainUpTo63Chars()984 public void testAutoLinkEmailAddress_matchesSubdomainUpTo63Chars() throws Exception { 985 String subdomain = ""; 986 for (int i = 0; i < 63; i++) { 987 subdomain += "a"; 988 } 989 String email = "email@" + subdomain + ".com"; 990 991 assertTrue("Should match subdomain of length: " + subdomain.length(), 992 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 993 994 subdomain += "a"; 995 email = "email@" + subdomain + ".com"; 996 assertFalse("Should not match local part of length: " + subdomain.length(), 997 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 998 } 999 1000 @Test 1001 @SmallTest testAutoLinkEmailAddress_matchesDomainUpTo255Chars()1002 public void testAutoLinkEmailAddress_matchesDomainUpTo255Chars() throws Exception { 1003 String longDomain = ""; 1004 while (longDomain.length() <= 250) { 1005 longDomain += "d."; 1006 } 1007 longDomain += "com"; 1008 assertEquals(255, longDomain.length()); 1009 String email = "a@" + longDomain; 1010 1011 assertTrue("Should match domain of length: " + longDomain.length(), 1012 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 1013 1014 email = email + "m"; 1015 assertEquals(258, email.length()); 1016 assertFalse("Should not match domain of length: " + longDomain.length(), 1017 Patterns.AUTOLINK_EMAIL_ADDRESS.matcher(email).matches()); 1018 } 1019 1020 // Tests for Patterns.PHONE 1021 1022 @Test 1023 @SmallTest testPhonePattern()1024 public void testPhonePattern() throws Exception { 1025 boolean t; 1026 1027 t = Patterns.PHONE.matcher("(919) 555-1212").matches(); 1028 assertTrue("Valid phone", t); 1029 1030 t = Patterns.PHONE.matcher("2334 9323/54321").matches(); 1031 assertFalse("Invalid phone", t); 1032 1033 String[] tests = { 1034 "Me: 16505551212 this\n", 1035 "Me: 6505551212 this\n", 1036 "Me: 5551212 this\n", 1037 "Me: 2211 this\n", 1038 "Me: 112 this\n", 1039 1040 "Me: 1-650-555-1212 this\n", 1041 "Me: (650) 555-1212 this\n", 1042 "Me: +1 (650) 555-1212 this\n", 1043 "Me: +1-650-555-1212 this\n", 1044 "Me: 650-555-1212 this\n", 1045 "Me: 555-1212 this\n", 1046 1047 "Me: 1.650.555.1212 this\n", 1048 "Me: (650) 555.1212 this\n", 1049 "Me: +1 (650) 555.1212 this\n", 1050 "Me: +1.650.555.1212 this\n", 1051 "Me: 650.555.1212 this\n", 1052 "Me: 555.1212 this\n", 1053 1054 "Me: 1 650 555 1212 this\n", 1055 "Me: (650) 555 1212 this\n", 1056 "Me: +1 (650) 555 1212 this\n", 1057 "Me: +1 650 555 1212 this\n", 1058 "Me: 650 555 1212 this\n", 1059 "Me: 555 1212 this\n", 1060 }; 1061 1062 for (String test : tests) { 1063 Matcher m = Patterns.PHONE.matcher(test); 1064 1065 assertTrue("Valid phone " + test, m.find()); 1066 } 1067 } 1068 } 1069