1 /* 2 * Copyright (C) 2009 The Libphonenumber Authors 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 com.android.i18n.phonenumbers; 18 19 import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; 20 import com.android.i18n.phonenumbers.Phonemetadata.NumberFormat; 21 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 22 import com.android.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; 23 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; 24 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * Unit tests for PhoneNumberUtil.java 31 * 32 * Note that these tests use the test metadata, not the normal metadata file, so should not be used 33 * for regression test purposes - these tests are illustrative only and test functionality. 34 * 35 * @author Shaopeng Jia 36 * @author Lara Rennie 37 */ 38 public class PhoneNumberUtilTest extends TestMetadataTestCase { 39 // Set up some test numbers to re-use. 40 // TODO: Rewrite this as static functions that return new numbers each time to avoid 41 // any risk of accidental changes to mutable static state affecting many tests. 42 private static final PhoneNumber ALPHA_NUMERIC_NUMBER = 43 new PhoneNumber().setCountryCode(1).setNationalNumber(80074935247L); 44 private static final PhoneNumber AE_UAN = 45 new PhoneNumber().setCountryCode(971).setNationalNumber(600123456L); 46 private static final PhoneNumber AR_MOBILE = 47 new PhoneNumber().setCountryCode(54).setNationalNumber(91187654321L); 48 private static final PhoneNumber AR_NUMBER = 49 new PhoneNumber().setCountryCode(54).setNationalNumber(1187654321); 50 private static final PhoneNumber AU_NUMBER = 51 new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L); 52 private static final PhoneNumber BS_MOBILE = 53 new PhoneNumber().setCountryCode(1).setNationalNumber(2423570000L); 54 private static final PhoneNumber BS_NUMBER = 55 new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L); 56 // Note that this is the same as the example number for DE in the metadata. 57 private static final PhoneNumber DE_NUMBER = 58 new PhoneNumber().setCountryCode(49).setNationalNumber(30123456L); 59 private static final PhoneNumber DE_SHORT_NUMBER = 60 new PhoneNumber().setCountryCode(49).setNationalNumber(1234L); 61 private static final PhoneNumber GB_MOBILE = 62 new PhoneNumber().setCountryCode(44).setNationalNumber(7912345678L); 63 private static final PhoneNumber GB_NUMBER = 64 new PhoneNumber().setCountryCode(44).setNationalNumber(2070313000L); 65 private static final PhoneNumber IT_MOBILE = 66 new PhoneNumber().setCountryCode(39).setNationalNumber(345678901L); 67 private static final PhoneNumber IT_NUMBER = 68 new PhoneNumber().setCountryCode(39).setNationalNumber(236618300L). 69 setItalianLeadingZero(true); 70 private static final PhoneNumber JP_STAR_NUMBER = 71 new PhoneNumber().setCountryCode(81).setNationalNumber(2345); 72 // Numbers to test the formatting rules from Mexico. 73 private static final PhoneNumber MX_MOBILE1 = 74 new PhoneNumber().setCountryCode(52).setNationalNumber(12345678900L); 75 private static final PhoneNumber MX_MOBILE2 = 76 new PhoneNumber().setCountryCode(52).setNationalNumber(15512345678L); 77 private static final PhoneNumber MX_NUMBER1 = 78 new PhoneNumber().setCountryCode(52).setNationalNumber(3312345678L); 79 private static final PhoneNumber MX_NUMBER2 = 80 new PhoneNumber().setCountryCode(52).setNationalNumber(8211234567L); 81 private static final PhoneNumber NZ_NUMBER = 82 new PhoneNumber().setCountryCode(64).setNationalNumber(33316005L); 83 private static final PhoneNumber SG_NUMBER = 84 new PhoneNumber().setCountryCode(65).setNationalNumber(65218000L); 85 // A too-long and hence invalid US number. 86 private static final PhoneNumber US_LONG_NUMBER = 87 new PhoneNumber().setCountryCode(1).setNationalNumber(65025300001L); 88 private static final PhoneNumber US_NUMBER = 89 new PhoneNumber().setCountryCode(1).setNationalNumber(6502530000L); 90 private static final PhoneNumber US_PREMIUM = 91 new PhoneNumber().setCountryCode(1).setNationalNumber(9002530000L); 92 // Too short, but still possible US numbers. 93 private static final PhoneNumber US_LOCAL_NUMBER = 94 new PhoneNumber().setCountryCode(1).setNationalNumber(2530000L); 95 private static final PhoneNumber US_SHORT_BY_ONE_NUMBER = 96 new PhoneNumber().setCountryCode(1).setNationalNumber(650253000L); 97 private static final PhoneNumber US_TOLLFREE = 98 new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L); 99 private static final PhoneNumber US_SPOOF = 100 new PhoneNumber().setCountryCode(1).setNationalNumber(0L); 101 private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT = 102 new PhoneNumber().setCountryCode(1).setNationalNumber(0L) 103 .setRawInput("000-000-0000"); 104 private static final PhoneNumber INTERNATIONAL_TOLL_FREE = 105 new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L); 106 // We set this to be the same length as numbers for the other non-geographical country prefix that 107 // we have in our test metadata. However, this is not considered valid because they differ in 108 // their country calling code. 109 private static final PhoneNumber INTERNATIONAL_TOLL_FREE_TOO_LONG = 110 new PhoneNumber().setCountryCode(800).setNationalNumber(123456789L); 111 private static final PhoneNumber UNIVERSAL_PREMIUM_RATE = 112 new PhoneNumber().setCountryCode(979).setNationalNumber(123456789L); 113 private static final PhoneNumber UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT = 114 new PhoneNumber().setCountryCode(2).setNationalNumber(12345L); 115 116 public void testGetSupportedRegions() { 117 assertTrue(phoneUtil.getSupportedRegions().size() > 0); 118 } 119 120 public void testGetInstanceLoadBadMetadata() { 121 assertNull(phoneUtil.getMetadataForRegion("No Such Region")); 122 assertNull(phoneUtil.getMetadataForNonGeographicalRegion(-1)); 123 } 124 125 public void testMissingMetadataFileThrowsRuntimeException() { 126 // In normal usage we should never get a state where we are asking to load metadata that doesn't 127 // exist. However if the library is packaged incorrectly in the jar, this could happen and the 128 // best we can do is make sure the exception has the file name in it. 129 try { 130 phoneUtil.loadMetadataFromFile("no/such/file", "XX", -1); 131 fail("expected exception"); 132 } catch (RuntimeException e) { 133 assertTrue("Unexpected error: " + e, e.toString().contains("no/such/file_XX")); 134 } 135 try { 136 phoneUtil.loadMetadataFromFile( 137 "no/such/file", PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY, 123); 138 fail("expected exception"); 139 } catch (RuntimeException e) { 140 assertTrue("Unexpected error: " + e, e.getMessage().contains("no/such/file_123")); 141 } 142 } 143 144 public void testGetInstanceLoadUSMetadata() { 145 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US); 146 assertEquals("US", metadata.getId()); 147 assertEquals(1, metadata.getCountryCode()); 148 assertEquals("011", metadata.getInternationalPrefix()); 149 assertTrue(metadata.hasNationalPrefix()); 150 assertEquals(2, metadata.numberFormatSize()); 151 assertEquals("(\\d{3})(\\d{3})(\\d{4})", 152 metadata.getNumberFormat(1).getPattern()); 153 assertEquals("$1 $2 $3", metadata.getNumberFormat(1).getFormat()); 154 assertEquals("[13-689]\\d{9}|2[0-35-9]\\d{8}", 155 metadata.getGeneralDesc().getNationalNumberPattern()); 156 assertEquals("\\d{7}(?:\\d{3})?", metadata.getGeneralDesc().getPossibleNumberPattern()); 157 assertTrue(metadata.getGeneralDesc().exactlySameAs(metadata.getFixedLine())); 158 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern()); 159 assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern()); 160 // No shared-cost data is available, so it should be initialised to "NA". 161 assertEquals("NA", metadata.getSharedCost().getNationalNumberPattern()); 162 assertEquals("NA", metadata.getSharedCost().getPossibleNumberPattern()); 163 } 164 165 public void testGetInstanceLoadDEMetadata() { 166 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.DE); 167 assertEquals("DE", metadata.getId()); 168 assertEquals(49, metadata.getCountryCode()); 169 assertEquals("00", metadata.getInternationalPrefix()); 170 assertEquals("0", metadata.getNationalPrefix()); 171 assertEquals(6, metadata.numberFormatSize()); 172 assertEquals(1, metadata.getNumberFormat(5).leadingDigitsPatternSize()); 173 assertEquals("900", metadata.getNumberFormat(5).getLeadingDigitsPattern(0)); 174 assertEquals("(\\d{3})(\\d{3,4})(\\d{4})", 175 metadata.getNumberFormat(5).getPattern()); 176 assertEquals("$1 $2 $3", metadata.getNumberFormat(5).getFormat()); 177 assertEquals("(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{1,8}", 178 metadata.getFixedLine().getNationalNumberPattern()); 179 assertEquals("\\d{2,14}", metadata.getFixedLine().getPossibleNumberPattern()); 180 assertEquals("30123456", metadata.getFixedLine().getExampleNumber()); 181 assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern()); 182 assertEquals("900([135]\\d{6}|9\\d{7})", metadata.getPremiumRate().getNationalNumberPattern()); 183 } 184 185 public void testGetInstanceLoadARMetadata() { 186 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.AR); 187 assertEquals("AR", metadata.getId()); 188 assertEquals(54, metadata.getCountryCode()); 189 assertEquals("00", metadata.getInternationalPrefix()); 190 assertEquals("0", metadata.getNationalPrefix()); 191 assertEquals("0(?:(11|343|3715)15)?", metadata.getNationalPrefixForParsing()); 192 assertEquals("9$1", metadata.getNationalPrefixTransformRule()); 193 assertEquals("$2 15 $3-$4", metadata.getNumberFormat(2).getFormat()); 194 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})", 195 metadata.getNumberFormat(3).getPattern()); 196 assertEquals("(9)(\\d{4})(\\d{2})(\\d{4})", 197 metadata.getIntlNumberFormat(3).getPattern()); 198 assertEquals("$1 $2 $3 $4", metadata.getIntlNumberFormat(3).getFormat()); 199 } 200 201 public void testGetInstanceLoadInternationalTollFreeMetadata() { 202 PhoneMetadata metadata = phoneUtil.getMetadataForNonGeographicalRegion(800); 203 assertEquals("001", metadata.getId()); 204 assertEquals(800, metadata.getCountryCode()); 205 assertEquals("$1 $2", metadata.getNumberFormat(0).getFormat()); 206 assertEquals("(\\d{4})(\\d{4})", metadata.getNumberFormat(0).getPattern()); 207 assertEquals("12345678", metadata.getGeneralDesc().getExampleNumber()); 208 assertEquals("12345678", metadata.getTollFree().getExampleNumber()); 209 } 210 211 public void testIsNumberGeographical() { 212 assertFalse(phoneUtil.isNumberGeographical(BS_MOBILE)); // Bahamas, mobile phone number. 213 assertTrue(phoneUtil.isNumberGeographical(AU_NUMBER)); // Australian fixed line number. 214 assertFalse(phoneUtil.isNumberGeographical(INTERNATIONAL_TOLL_FREE)); // International toll 215 // free number. 216 } 217 218 public void testIsLeadingZeroPossible() { 219 assertTrue(phoneUtil.isLeadingZeroPossible(39)); // Italy 220 assertFalse(phoneUtil.isLeadingZeroPossible(1)); // USA 221 assertTrue(phoneUtil.isLeadingZeroPossible(800)); // International toll free 222 assertFalse(phoneUtil.isLeadingZeroPossible(979)); // International premium-rate 223 assertFalse(phoneUtil.isLeadingZeroPossible(888)); // Not in metadata file, just default to 224 // false. 225 } 226 227 public void testGetLengthOfGeographicalAreaCode() { 228 // Google MTV, which has area code "650". 229 assertEquals(3, phoneUtil.getLengthOfGeographicalAreaCode(US_NUMBER)); 230 231 // A North America toll-free number, which has no area code. 232 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_TOLLFREE)); 233 234 // Google London, which has area code "20". 235 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(GB_NUMBER)); 236 237 // A UK mobile phone, which has no area code. 238 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(GB_MOBILE)); 239 240 // Google Buenos Aires, which has area code "11". 241 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(AR_NUMBER)); 242 243 // Google Sydney, which has area code "2". 244 assertEquals(1, phoneUtil.getLengthOfGeographicalAreaCode(AU_NUMBER)); 245 246 // Italian numbers - there is no national prefix, but it still has an area code. 247 assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(IT_NUMBER)); 248 249 // Google Singapore. Singapore has no area code and no national prefix. 250 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(SG_NUMBER)); 251 252 // An invalid US number (1 digit shorter), which has no area code. 253 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_SHORT_BY_ONE_NUMBER)); 254 255 // An international toll free number, which has no area code. 256 assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(INTERNATIONAL_TOLL_FREE)); 257 } 258 259 public void testGetLengthOfNationalDestinationCode() { 260 // Google MTV, which has national destination code (NDC) "650". 261 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_NUMBER)); 262 263 // A North America toll-free number, which has NDC "800". 264 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_TOLLFREE)); 265 266 // Google London, which has NDC "20". 267 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(GB_NUMBER)); 268 269 // A UK mobile phone, which has NDC "7912". 270 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(GB_MOBILE)); 271 272 // Google Buenos Aires, which has NDC "11". 273 assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(AR_NUMBER)); 274 275 // An Argentinian mobile which has NDC "911". 276 assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(AR_MOBILE)); 277 278 // Google Sydney, which has NDC "2". 279 assertEquals(1, phoneUtil.getLengthOfNationalDestinationCode(AU_NUMBER)); 280 281 // Google Singapore, which has NDC "6521". 282 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(SG_NUMBER)); 283 284 // An invalid US number (1 digit shorter), which has no NDC. 285 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(US_SHORT_BY_ONE_NUMBER)); 286 287 // A number containing an invalid country calling code, which shouldn't have any NDC. 288 PhoneNumber number = new PhoneNumber().setCountryCode(123).setNationalNumber(6502530000L); 289 assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(number)); 290 291 // An international toll free number, which has NDC "1234". 292 assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(INTERNATIONAL_TOLL_FREE)); 293 } 294 295 public void testGetNationalSignificantNumber() { 296 assertEquals("6502530000", phoneUtil.getNationalSignificantNumber(US_NUMBER)); 297 298 // An Italian mobile number. 299 assertEquals("345678901", phoneUtil.getNationalSignificantNumber(IT_MOBILE)); 300 301 // An Italian fixed line number. 302 assertEquals("0236618300", phoneUtil.getNationalSignificantNumber(IT_NUMBER)); 303 304 assertEquals("12345678", phoneUtil.getNationalSignificantNumber(INTERNATIONAL_TOLL_FREE)); 305 } 306 307 public void testGetExampleNumber() { 308 assertEquals(DE_NUMBER, phoneUtil.getExampleNumber(RegionCode.DE)); 309 310 assertEquals(DE_NUMBER, 311 phoneUtil.getExampleNumberForType(RegionCode.DE, 312 PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); 313 assertEquals(null, 314 phoneUtil.getExampleNumberForType(RegionCode.DE, 315 PhoneNumberUtil.PhoneNumberType.MOBILE)); 316 // For the US, the example number is placed under general description, and hence should be used 317 // for both fixed line and mobile, so neither of these should return null. 318 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, 319 PhoneNumberUtil.PhoneNumberType.FIXED_LINE)); 320 assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, 321 PhoneNumberUtil.PhoneNumberType.MOBILE)); 322 // CS is an invalid region, so we have no data for it. 323 assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS, 324 PhoneNumberUtil.PhoneNumberType.MOBILE)); 325 // RegionCode 001 is reserved for supporting non-geographical country calling code. We don't 326 // support getting an example number for it with this method. 327 assertEquals(null, phoneUtil.getExampleNumber(RegionCode.UN001)); 328 } 329 330 public void testGetExampleNumberForNonGeoEntity() { 331 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.getExampleNumberForNonGeoEntity(800)); 332 assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.getExampleNumberForNonGeoEntity(979)); 333 } 334 335 public void testConvertAlphaCharactersInNumber() { 336 String input = "1800-ABC-DEF"; 337 // Alpha chars are converted to digits; everything else is left untouched. 338 String expectedOutput = "1800-222-333"; 339 assertEquals(expectedOutput, PhoneNumberUtil.convertAlphaCharactersInNumber(input)); 340 } 341 342 public void testNormaliseRemovePunctuation() { 343 String inputNumber = "034-56&+#2\u00AD34"; 344 String expectedOutput = "03456234"; 345 assertEquals("Conversion did not correctly remove punctuation", 346 expectedOutput, 347 PhoneNumberUtil.normalize(inputNumber)); 348 } 349 350 public void testNormaliseReplaceAlphaCharacters() { 351 String inputNumber = "034-I-am-HUNGRY"; 352 String expectedOutput = "034426486479"; 353 assertEquals("Conversion did not correctly replace alpha characters", 354 expectedOutput, 355 PhoneNumberUtil.normalize(inputNumber)); 356 } 357 358 public void testNormaliseOtherDigits() { 359 String inputNumber = "\uFF125\u0665"; 360 String expectedOutput = "255"; 361 assertEquals("Conversion did not correctly replace non-latin digits", 362 expectedOutput, 363 PhoneNumberUtil.normalize(inputNumber)); 364 // Eastern-Arabic digits. 365 inputNumber = "\u06F52\u06F0"; 366 expectedOutput = "520"; 367 assertEquals("Conversion did not correctly replace non-latin digits", 368 expectedOutput, 369 PhoneNumberUtil.normalize(inputNumber)); 370 } 371 372 public void testNormaliseStripAlphaCharacters() { 373 String inputNumber = "034-56&+a#234"; 374 String expectedOutput = "03456234"; 375 assertEquals("Conversion did not correctly remove alpha character", 376 expectedOutput, 377 PhoneNumberUtil.normalizeDigitsOnly(inputNumber)); 378 } 379 380 public void testFormatUSNumber() { 381 assertEquals("650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.NATIONAL)); 382 assertEquals("+1 650 253 0000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 383 384 assertEquals("800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.NATIONAL)); 385 assertEquals("+1 800 253 0000", phoneUtil.format(US_TOLLFREE, PhoneNumberFormat.INTERNATIONAL)); 386 387 assertEquals("900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.NATIONAL)); 388 assertEquals("+1 900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL)); 389 assertEquals("tel:+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966)); 390 // Numbers with all zeros in the national number part will be formatted by using the raw_input 391 // if that is available no matter which format is specified. 392 assertEquals("000-000-0000", 393 phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL)); 394 assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL)); 395 } 396 397 public void testFormatBSNumber() { 398 assertEquals("242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.NATIONAL)); 399 assertEquals("+1 242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 400 } 401 402 public void testFormatGBNumber() { 403 assertEquals("(020) 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.NATIONAL)); 404 assertEquals("+44 20 7031 3000", phoneUtil.format(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 405 406 assertEquals("(07912) 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.NATIONAL)); 407 assertEquals("+44 7912 345 678", phoneUtil.format(GB_MOBILE, PhoneNumberFormat.INTERNATIONAL)); 408 } 409 410 public void testFormatDENumber() { 411 PhoneNumber deNumber = new PhoneNumber(); 412 deNumber.setCountryCode(49).setNationalNumber(301234L); 413 assertEquals("030/1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 414 assertEquals("+49 30/1234", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 415 assertEquals("tel:+49-30-1234", phoneUtil.format(deNumber, PhoneNumberFormat.RFC3966)); 416 417 deNumber.clear(); 418 deNumber.setCountryCode(49).setNationalNumber(291123L); 419 assertEquals("0291 123", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 420 assertEquals("+49 291 123", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 421 422 deNumber.clear(); 423 deNumber.setCountryCode(49).setNationalNumber(29112345678L); 424 assertEquals("0291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 425 assertEquals("+49 291 12345678", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 426 427 deNumber.clear(); 428 deNumber.setCountryCode(49).setNationalNumber(912312345L); 429 assertEquals("09123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 430 assertEquals("+49 9123 12345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 431 deNumber.clear(); 432 deNumber.setCountryCode(49).setNationalNumber(80212345L); 433 assertEquals("08021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 434 assertEquals("+49 8021 2345", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL)); 435 // Note this number is correctly formatted without national prefix. Most of the numbers that 436 // are treated as invalid numbers by the library are short numbers, and they are usually not 437 // dialed with national prefix. 438 assertEquals("1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.NATIONAL)); 439 assertEquals("+49 1234", phoneUtil.format(DE_SHORT_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 440 441 deNumber.clear(); 442 deNumber.setCountryCode(49).setNationalNumber(41341234); 443 assertEquals("04134 1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL)); 444 } 445 446 public void testFormatITNumber() { 447 assertEquals("02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.NATIONAL)); 448 assertEquals("+39 02 3661 8300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 449 assertEquals("+390236618300", phoneUtil.format(IT_NUMBER, PhoneNumberFormat.E164)); 450 451 assertEquals("345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.NATIONAL)); 452 assertEquals("+39 345 678 901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.INTERNATIONAL)); 453 assertEquals("+39345678901", phoneUtil.format(IT_MOBILE, PhoneNumberFormat.E164)); 454 } 455 456 public void testFormatAUNumber() { 457 assertEquals("02 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.NATIONAL)); 458 assertEquals("+61 2 3661 8300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 459 assertEquals("+61236618300", phoneUtil.format(AU_NUMBER, PhoneNumberFormat.E164)); 460 461 PhoneNumber auNumber = new PhoneNumber().setCountryCode(61).setNationalNumber(1800123456L); 462 assertEquals("1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.NATIONAL)); 463 assertEquals("+61 1800 123 456", phoneUtil.format(auNumber, PhoneNumberFormat.INTERNATIONAL)); 464 assertEquals("+611800123456", phoneUtil.format(auNumber, PhoneNumberFormat.E164)); 465 } 466 467 public void testFormatARNumber() { 468 assertEquals("011 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.NATIONAL)); 469 assertEquals("+54 11 8765-4321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.INTERNATIONAL)); 470 assertEquals("+541187654321", phoneUtil.format(AR_NUMBER, PhoneNumberFormat.E164)); 471 472 assertEquals("011 15 8765-4321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.NATIONAL)); 473 assertEquals("+54 9 11 8765 4321", phoneUtil.format(AR_MOBILE, 474 PhoneNumberFormat.INTERNATIONAL)); 475 assertEquals("+5491187654321", phoneUtil.format(AR_MOBILE, PhoneNumberFormat.E164)); 476 } 477 478 public void testFormatMXNumber() { 479 assertEquals("045 234 567 8900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.NATIONAL)); 480 assertEquals("+52 1 234 567 8900", phoneUtil.format( 481 MX_MOBILE1, PhoneNumberFormat.INTERNATIONAL)); 482 assertEquals("+5212345678900", phoneUtil.format(MX_MOBILE1, PhoneNumberFormat.E164)); 483 484 assertEquals("045 55 1234 5678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.NATIONAL)); 485 assertEquals("+52 1 55 1234 5678", phoneUtil.format( 486 MX_MOBILE2, PhoneNumberFormat.INTERNATIONAL)); 487 assertEquals("+5215512345678", phoneUtil.format(MX_MOBILE2, PhoneNumberFormat.E164)); 488 489 assertEquals("01 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.NATIONAL)); 490 assertEquals("+52 33 1234 5678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.INTERNATIONAL)); 491 assertEquals("+523312345678", phoneUtil.format(MX_NUMBER1, PhoneNumberFormat.E164)); 492 493 assertEquals("01 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.NATIONAL)); 494 assertEquals("+52 821 123 4567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.INTERNATIONAL)); 495 assertEquals("+528211234567", phoneUtil.format(MX_NUMBER2, PhoneNumberFormat.E164)); 496 } 497 498 public void testFormatOutOfCountryCallingNumber() { 499 assertEquals("00 1 900 253 0000", 500 phoneUtil.formatOutOfCountryCallingNumber(US_PREMIUM, RegionCode.DE)); 501 502 assertEquals("1 650 253 0000", 503 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.BS)); 504 505 assertEquals("00 1 650 253 0000", 506 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.PL)); 507 508 assertEquals("011 44 7912 345 678", 509 phoneUtil.formatOutOfCountryCallingNumber(GB_MOBILE, RegionCode.US)); 510 511 assertEquals("00 49 1234", 512 phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.GB)); 513 // Note this number is correctly formatted without national prefix. Most of the numbers that 514 // are treated as invalid numbers by the library are short numbers, and they are usually not 515 // dialed with national prefix. 516 assertEquals("1234", phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER, RegionCode.DE)); 517 518 assertEquals("011 39 02 3661 8300", 519 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.US)); 520 assertEquals("02 3661 8300", 521 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.IT)); 522 assertEquals("+39 02 3661 8300", 523 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.SG)); 524 525 assertEquals("6521 8000", 526 phoneUtil.formatOutOfCountryCallingNumber(SG_NUMBER, RegionCode.SG)); 527 528 assertEquals("011 54 9 11 8765 4321", 529 phoneUtil.formatOutOfCountryCallingNumber(AR_MOBILE, RegionCode.US)); 530 assertEquals("011 800 1234 5678", 531 phoneUtil.formatOutOfCountryCallingNumber(INTERNATIONAL_TOLL_FREE, RegionCode.US)); 532 533 PhoneNumber arNumberWithExtn = new PhoneNumber().mergeFrom(AR_MOBILE).setExtension("1234"); 534 assertEquals("011 54 9 11 8765 4321 ext. 1234", 535 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.US)); 536 assertEquals("0011 54 9 11 8765 4321 ext. 1234", 537 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AU)); 538 assertEquals("011 15 8765-4321 ext. 1234", 539 phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn, RegionCode.AR)); 540 } 541 542 public void testFormatOutOfCountryWithInvalidRegion() { 543 // AQ/Antarctica isn't a valid region code for phone number formatting, 544 // so this falls back to intl formatting. 545 assertEquals("+1 650 253 0000", 546 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AQ)); 547 // For region code 001, the out-of-country format always turns into the international format. 548 assertEquals("+1 650 253 0000", 549 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.UN001)); 550 } 551 552 public void testFormatOutOfCountryWithPreferredIntlPrefix() { 553 // This should use 0011, since that is the preferred international prefix (both 0011 and 0012 554 // are accepted as possible international prefixes in our test metadta.) 555 assertEquals("0011 39 02 3661 8300", 556 phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.AU)); 557 } 558 559 public void testFormatOutOfCountryKeepingAlphaChars() { 560 PhoneNumber alphaNumericNumber = new PhoneNumber(); 561 alphaNumericNumber.setCountryCode(1).setNationalNumber(8007493524L) 562 .setRawInput("1800 six-flag"); 563 assertEquals("0011 1 800 SIX-FLAG", 564 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 565 566 alphaNumericNumber.setRawInput("1-800-SIX-flag"); 567 assertEquals("0011 1 800-SIX-FLAG", 568 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 569 570 alphaNumericNumber.setRawInput("Call us from UK: 00 1 800 SIX-flag"); 571 assertEquals("0011 1 800 SIX-FLAG", 572 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 573 574 alphaNumericNumber.setRawInput("800 SIX-flag"); 575 assertEquals("0011 1 800 SIX-FLAG", 576 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 577 578 // Formatting from within the NANPA region. 579 assertEquals("1 800 SIX-FLAG", 580 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.US)); 581 582 assertEquals("1 800 SIX-FLAG", 583 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.BS)); 584 585 // Testing that if the raw input doesn't exist, it is formatted using 586 // formatOutOfCountryCallingNumber. 587 alphaNumericNumber.clearRawInput(); 588 assertEquals("00 1 800 749 3524", 589 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 590 591 // Testing AU alpha number formatted from Australia. 592 alphaNumericNumber.setCountryCode(61).setNationalNumber(827493524L) 593 .setRawInput("+61 82749-FLAG"); 594 // This number should have the national prefix fixed. 595 assertEquals("082749-FLAG", 596 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 597 598 alphaNumericNumber.setRawInput("082749-FLAG"); 599 assertEquals("082749-FLAG", 600 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 601 602 alphaNumericNumber.setNationalNumber(18007493524L).setRawInput("1-800-SIX-flag"); 603 // This number should not have the national prefix prefixed, in accordance with the override for 604 // this specific formatting rule. 605 assertEquals("1-800-SIX-FLAG", 606 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AU)); 607 608 // The metadata should not be permanently changed, since we copied it before modifying patterns. 609 // Here we check this. 610 alphaNumericNumber.setNationalNumber(1800749352L); 611 assertEquals("1800 749 352", 612 phoneUtil.formatOutOfCountryCallingNumber(alphaNumericNumber, RegionCode.AU)); 613 614 // Testing a region with multiple international prefixes. 615 assertEquals("+61 1-800-SIX-FLAG", 616 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.SG)); 617 // Testing the case of calling from a non-supported region. 618 assertEquals("+61 1-800-SIX-FLAG", 619 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ)); 620 621 // Testing the case with an invalid country calling code. 622 alphaNumericNumber.setCountryCode(0).setNationalNumber(18007493524L) 623 .setRawInput("1-800-SIX-flag"); 624 // Uses the raw input only. 625 assertEquals("1-800-SIX-flag", 626 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 627 628 // Testing the case of an invalid alpha number. 629 alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX"); 630 // No country-code stripping can be done. 631 assertEquals("00 1 180-SIX", 632 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.DE)); 633 634 // Testing the case of calling from a non-supported region. 635 alphaNumericNumber.setCountryCode(1).setNationalNumber(80749L).setRawInput("180-SIX"); 636 // No country-code stripping can be done since the number is invalid. 637 assertEquals("+1 180-SIX", 638 phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber, RegionCode.AQ)); 639 } 640 641 public void testFormatWithCarrierCode() { 642 // We only support this for AR in our test metadata, and only for mobile numbers starting with 643 // certain values. 644 PhoneNumber arMobile = new PhoneNumber().setCountryCode(54).setNationalNumber(92234654321L); 645 assertEquals("02234 65-4321", phoneUtil.format(arMobile, PhoneNumberFormat.NATIONAL)); 646 // Here we force 14 as the carrier code. 647 assertEquals("02234 14 65-4321", 648 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, "14")); 649 // Here we force the number to be shown with no carrier code. 650 assertEquals("02234 65-4321", 651 phoneUtil.formatNationalNumberWithCarrierCode(arMobile, "")); 652 // Here the international rule is used, so no carrier code should be present. 653 assertEquals("+5492234654321", phoneUtil.format(arMobile, PhoneNumberFormat.E164)); 654 // We don't support this for the US so there should be no change. 655 assertEquals("650 253 0000", phoneUtil.formatNationalNumberWithCarrierCode(US_NUMBER, "15")); 656 657 // Invalid country code should just get the NSN. 658 assertEquals("12345", 659 phoneUtil.formatNationalNumberWithCarrierCode(UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT, "89")); 660 } 661 662 public void testFormatWithPreferredCarrierCode() { 663 // We only support this for AR in our test metadata. 664 PhoneNumber arNumber = new PhoneNumber(); 665 arNumber.setCountryCode(54).setNationalNumber(91234125678L); 666 // Test formatting with no preferred carrier code stored in the number itself. 667 assertEquals("01234 15 12-5678", 668 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 669 assertEquals("01234 12-5678", 670 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "")); 671 // Test formatting with preferred carrier code present. 672 arNumber.setPreferredDomesticCarrierCode("19"); 673 assertEquals("01234 12-5678", phoneUtil.format(arNumber, PhoneNumberFormat.NATIONAL)); 674 assertEquals("01234 19 12-5678", 675 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 676 assertEquals("01234 19 12-5678", 677 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "")); 678 // When the preferred_domestic_carrier_code is present (even when it contains an empty string), 679 // use it instead of the default carrier code passed in. 680 arNumber.setPreferredDomesticCarrierCode(""); 681 assertEquals("01234 12-5678", 682 phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, "15")); 683 // We don't support this for the US so there should be no change. 684 PhoneNumber usNumber = new PhoneNumber(); 685 usNumber.setCountryCode(1).setNationalNumber(4241231234L).setPreferredDomesticCarrierCode("99"); 686 assertEquals("424 123 1234", phoneUtil.format(usNumber, PhoneNumberFormat.NATIONAL)); 687 assertEquals("424 123 1234", 688 phoneUtil.formatNationalNumberWithPreferredCarrierCode(usNumber, "15")); 689 } 690 691 public void testFormatNumberForMobileDialing() { 692 // US toll free numbers are marked as noInternationalDialling in the test metadata for testing 693 // purposes. 694 assertEquals("800 253 0000", 695 phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, 696 true /* keep formatting */)); 697 assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true)); 698 assertEquals("+1 650 253 0000", 699 phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true)); 700 PhoneNumber usNumberWithExtn = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("1234"); 701 assertEquals("+1 650 253 0000", 702 phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, true)); 703 704 assertEquals("8002530000", 705 phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, 706 false /* remove formatting */)); 707 assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, false)); 708 assertEquals("+16502530000", 709 phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false)); 710 assertEquals("+16502530000", 711 phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, false)); 712 713 // An invalid US number, which is one digit too long. 714 assertEquals("+165025300001", 715 phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, false)); 716 assertEquals("+1 65025300001", 717 phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, true)); 718 719 // Star numbers. In real life they appear in Israel, but we have them in JP in our test 720 // metadata. 721 assertEquals("*2345", 722 phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, false)); 723 assertEquals("*2345", 724 phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, true)); 725 726 assertEquals("+80012345678", 727 phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, false)); 728 assertEquals("+800 1234 5678", 729 phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE, RegionCode.JP, true)); 730 731 // UAE numbers beginning with 600 (classified as UAN) need to be dialled without +971 locally. 732 assertEquals("+971600123456", 733 phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.JP, false)); 734 assertEquals("600123456", 735 phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.AE, false)); 736 } 737 738 public void testFormatByPattern() { 739 NumberFormat newNumFormat = new NumberFormat(); 740 newNumFormat.setPattern("(\\d{3})(\\d{3})(\\d{4})"); 741 newNumFormat.setFormat("($1) $2-$3"); 742 List<NumberFormat> newNumberFormats = new ArrayList<NumberFormat>(); 743 newNumberFormats.add(newNumFormat); 744 745 assertEquals("(650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.NATIONAL, 746 newNumberFormats)); 747 assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, 748 PhoneNumberFormat.INTERNATIONAL, 749 newNumberFormats)); 750 assertEquals("tel:+1-650-253-0000", phoneUtil.formatByPattern(US_NUMBER, 751 PhoneNumberFormat.RFC3966, 752 newNumberFormats)); 753 754 // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are 755 // followed. 756 newNumFormat.setNationalPrefixFormattingRule("$NP ($FG)"); 757 newNumFormat.setFormat("$1 $2-$3"); 758 assertEquals("1 (242) 365-1234", 759 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.NATIONAL, 760 newNumberFormats)); 761 assertEquals("+1 242 365-1234", 762 phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL, 763 newNumberFormats)); 764 765 newNumFormat.setPattern("(\\d{2})(\\d{5})(\\d{3})"); 766 newNumFormat.setFormat("$1-$2 $3"); 767 newNumberFormats.set(0, newNumFormat); 768 769 assertEquals("02-36618 300", 770 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.NATIONAL, 771 newNumberFormats)); 772 assertEquals("+39 02-36618 300", 773 phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL, 774 newNumberFormats)); 775 776 newNumFormat.setNationalPrefixFormattingRule("$NP$FG"); 777 newNumFormat.setPattern("(\\d{2})(\\d{4})(\\d{4})"); 778 newNumFormat.setFormat("$1 $2 $3"); 779 newNumberFormats.set(0, newNumFormat); 780 assertEquals("020 7031 3000", 781 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 782 newNumberFormats)); 783 784 newNumFormat.setNationalPrefixFormattingRule("($NP$FG)"); 785 assertEquals("(020) 7031 3000", 786 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 787 newNumberFormats)); 788 789 newNumFormat.setNationalPrefixFormattingRule(""); 790 assertEquals("20 7031 3000", 791 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, 792 newNumberFormats)); 793 794 assertEquals("+44 20 7031 3000", 795 phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL, 796 newNumberFormats)); 797 } 798 799 public void testFormatE164Number() { 800 assertEquals("+16502530000", phoneUtil.format(US_NUMBER, PhoneNumberFormat.E164)); 801 assertEquals("+4930123456", phoneUtil.format(DE_NUMBER, PhoneNumberFormat.E164)); 802 assertEquals("+80012345678", phoneUtil.format(INTERNATIONAL_TOLL_FREE, PhoneNumberFormat.E164)); 803 } 804 805 public void testFormatNumberWithExtension() { 806 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("1234"); 807 // Uses default extension prefix: 808 assertEquals("03-331 6005 ext. 1234", phoneUtil.format(nzNumber, PhoneNumberFormat.NATIONAL)); 809 // Uses RFC 3966 syntax. 810 assertEquals("tel:+64-3-331-6005;ext=1234", 811 phoneUtil.format(nzNumber, PhoneNumberFormat.RFC3966)); 812 // Extension prefix overridden in the territory information for the US: 813 PhoneNumber usNumberWithExtension = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("4567"); 814 assertEquals("650 253 0000 extn. 4567", phoneUtil.format(usNumberWithExtension, 815 PhoneNumberFormat.NATIONAL)); 816 } 817 818 public void testFormatInOriginalFormat() throws Exception { 819 PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", RegionCode.GB); 820 assertEquals("+44 20 8765 4321", phoneUtil.formatInOriginalFormat(number1, RegionCode.GB)); 821 822 PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", RegionCode.GB); 823 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number2, RegionCode.GB)); 824 825 PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", RegionCode.US); 826 assertEquals("011 44 20 8765 4321", phoneUtil.formatInOriginalFormat(number3, RegionCode.US)); 827 828 PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", RegionCode.GB); 829 assertEquals("44 20 8765 4321", phoneUtil.formatInOriginalFormat(number4, RegionCode.GB)); 830 831 PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB); 832 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); 833 834 // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area 835 // codes starting with 7 are intentionally excluded in the test metadata for testing purposes. 836 PhoneNumber number6 = phoneUtil.parseAndKeepRawInput("7345678901", RegionCode.US); 837 assertEquals("734 567 8901", phoneUtil.formatInOriginalFormat(number6, RegionCode.US)); 838 839 // US is not a leading zero country, and the presence of the leading zero leads us to format the 840 // number using raw_input. 841 PhoneNumber number7 = phoneUtil.parseAndKeepRawInput("0734567 8901", RegionCode.US); 842 assertEquals("0734567 8901", phoneUtil.formatInOriginalFormat(number7, RegionCode.US)); 843 844 // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw 845 // input. 846 PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR); 847 assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR)); 848 849 PhoneNumber number9 = phoneUtil.parseAndKeepRawInput("01180012345678", RegionCode.US); 850 assertEquals("011 800 1234 5678", phoneUtil.formatInOriginalFormat(number9, RegionCode.US)); 851 852 PhoneNumber number10 = phoneUtil.parseAndKeepRawInput("+80012345678", RegionCode.KR); 853 assertEquals("+800 1234 5678", phoneUtil.formatInOriginalFormat(number10, RegionCode.KR)); 854 855 // US local numbers are formatted correctly, as we have formatting patterns for them. 856 PhoneNumber localNumberUS = phoneUtil.parseAndKeepRawInput("2530000", RegionCode.US); 857 assertEquals("253 0000", phoneUtil.formatInOriginalFormat(localNumberUS, RegionCode.US)); 858 859 PhoneNumber numberWithNationalPrefixUS = 860 phoneUtil.parseAndKeepRawInput("18003456789", RegionCode.US); 861 assertEquals("1 800 345 6789", 862 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixUS, RegionCode.US)); 863 864 PhoneNumber numberWithoutNationalPrefixGB = 865 phoneUtil.parseAndKeepRawInput("2087654321", RegionCode.GB); 866 assertEquals("20 8765 4321", 867 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixGB, RegionCode.GB)); 868 // Make sure no metadata is modified as a result of the previous function call. 869 assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB)); 870 871 PhoneNumber numberWithNationalPrefixMX = 872 phoneUtil.parseAndKeepRawInput("013312345678", RegionCode.MX); 873 assertEquals("01 33 1234 5678", 874 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX, RegionCode.MX)); 875 876 PhoneNumber numberWithoutNationalPrefixMX = 877 phoneUtil.parseAndKeepRawInput("3312345678", RegionCode.MX); 878 assertEquals("33 1234 5678", 879 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixMX, RegionCode.MX)); 880 881 PhoneNumber italianFixedLineNumber = 882 phoneUtil.parseAndKeepRawInput("0212345678", RegionCode.IT); 883 assertEquals("02 1234 5678", 884 phoneUtil.formatInOriginalFormat(italianFixedLineNumber, RegionCode.IT)); 885 886 PhoneNumber numberWithNationalPrefixJP = 887 phoneUtil.parseAndKeepRawInput("00777012", RegionCode.JP); 888 assertEquals("0077-7012", 889 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixJP, RegionCode.JP)); 890 891 PhoneNumber numberWithoutNationalPrefixJP = 892 phoneUtil.parseAndKeepRawInput("0777012", RegionCode.JP); 893 assertEquals("0777012", 894 phoneUtil.formatInOriginalFormat(numberWithoutNationalPrefixJP, RegionCode.JP)); 895 896 PhoneNumber numberWithCarrierCodeBR = 897 phoneUtil.parseAndKeepRawInput("012 3121286979", RegionCode.BR); 898 assertEquals("012 3121286979", 899 phoneUtil.formatInOriginalFormat(numberWithCarrierCodeBR, RegionCode.BR)); 900 901 // The default national prefix used in this case is 045. When a number with national prefix 044 902 // is entered, we return the raw input as we don't want to change the number entered. 903 PhoneNumber numberWithNationalPrefixMX1 = 904 phoneUtil.parseAndKeepRawInput("044(33)1234-5678", RegionCode.MX); 905 assertEquals("044(33)1234-5678", 906 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX1, RegionCode.MX)); 907 908 PhoneNumber numberWithNationalPrefixMX2 = 909 phoneUtil.parseAndKeepRawInput("045(33)1234-5678", RegionCode.MX); 910 assertEquals("045 33 1234 5678", 911 phoneUtil.formatInOriginalFormat(numberWithNationalPrefixMX2, RegionCode.MX)); 912 913 // The default international prefix used in this case is 0011. When a number with international 914 // prefix 0012 is entered, we return the raw input as we don't want to change the number 915 // entered. 916 PhoneNumber outOfCountryNumberFromAU1 = 917 phoneUtil.parseAndKeepRawInput("0012 16502530000", RegionCode.AU); 918 assertEquals("0012 16502530000", 919 phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU1, RegionCode.AU)); 920 921 PhoneNumber outOfCountryNumberFromAU2 = 922 phoneUtil.parseAndKeepRawInput("0011 16502530000", RegionCode.AU); 923 assertEquals("0011 1 650 253 0000", 924 phoneUtil.formatInOriginalFormat(outOfCountryNumberFromAU2, RegionCode.AU)); 925 926 // Test the star sign is not removed from or added to the original input by this method. 927 PhoneNumber starNumber = phoneUtil.parseAndKeepRawInput("*1234", RegionCode.JP); 928 assertEquals("*1234", phoneUtil.formatInOriginalFormat(starNumber, RegionCode.JP)); 929 PhoneNumber numberWithoutStar = phoneUtil.parseAndKeepRawInput("1234", RegionCode.JP); 930 assertEquals("1234", phoneUtil.formatInOriginalFormat(numberWithoutStar, RegionCode.JP)); 931 932 // Test an invalid national number without raw input is just formatted as the national number. 933 assertEquals("650253000", 934 phoneUtil.formatInOriginalFormat(US_SHORT_BY_ONE_NUMBER, RegionCode.US)); 935 } 936 937 public void testIsPremiumRate() { 938 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, phoneUtil.getNumberType(US_PREMIUM)); 939 940 PhoneNumber premiumRateNumber = new PhoneNumber(); 941 premiumRateNumber.setCountryCode(39).setNationalNumber(892123L); 942 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 943 phoneUtil.getNumberType(premiumRateNumber)); 944 945 premiumRateNumber.clear(); 946 premiumRateNumber.setCountryCode(44).setNationalNumber(9187654321L); 947 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 948 phoneUtil.getNumberType(premiumRateNumber)); 949 950 premiumRateNumber.clear(); 951 premiumRateNumber.setCountryCode(49).setNationalNumber(9001654321L); 952 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 953 phoneUtil.getNumberType(premiumRateNumber)); 954 955 premiumRateNumber.clear(); 956 premiumRateNumber.setCountryCode(49).setNationalNumber(90091234567L); 957 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 958 phoneUtil.getNumberType(premiumRateNumber)); 959 960 assertEquals(PhoneNumberUtil.PhoneNumberType.PREMIUM_RATE, 961 phoneUtil.getNumberType(UNIVERSAL_PREMIUM_RATE)); 962 } 963 964 public void testIsTollFree() { 965 PhoneNumber tollFreeNumber = new PhoneNumber(); 966 967 tollFreeNumber.setCountryCode(1).setNationalNumber(8881234567L); 968 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 969 phoneUtil.getNumberType(tollFreeNumber)); 970 971 tollFreeNumber.clear(); 972 tollFreeNumber.setCountryCode(39).setNationalNumber(803123L); 973 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 974 phoneUtil.getNumberType(tollFreeNumber)); 975 976 tollFreeNumber.clear(); 977 tollFreeNumber.setCountryCode(44).setNationalNumber(8012345678L); 978 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 979 phoneUtil.getNumberType(tollFreeNumber)); 980 981 tollFreeNumber.clear(); 982 tollFreeNumber.setCountryCode(49).setNationalNumber(8001234567L); 983 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 984 phoneUtil.getNumberType(tollFreeNumber)); 985 986 assertEquals(PhoneNumberUtil.PhoneNumberType.TOLL_FREE, 987 phoneUtil.getNumberType(INTERNATIONAL_TOLL_FREE)); 988 } 989 990 public void testIsMobile() { 991 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(BS_MOBILE)); 992 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(GB_MOBILE)); 993 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(IT_MOBILE)); 994 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(AR_MOBILE)); 995 996 PhoneNumber mobileNumber = new PhoneNumber(); 997 mobileNumber.setCountryCode(49).setNationalNumber(15123456789L); 998 assertEquals(PhoneNumberUtil.PhoneNumberType.MOBILE, phoneUtil.getNumberType(mobileNumber)); 999 } 1000 1001 public void testIsFixedLine() { 1002 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(BS_NUMBER)); 1003 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(IT_NUMBER)); 1004 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(GB_NUMBER)); 1005 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE, phoneUtil.getNumberType(DE_NUMBER)); 1006 } 1007 1008 public void testIsFixedLineAndMobile() { 1009 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, 1010 phoneUtil.getNumberType(US_NUMBER)); 1011 1012 PhoneNumber fixedLineAndMobileNumber = new PhoneNumber(). 1013 setCountryCode(54).setNationalNumber(1987654321L); 1014 assertEquals(PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE, 1015 phoneUtil.getNumberType(fixedLineAndMobileNumber)); 1016 } 1017 1018 public void testIsSharedCost() { 1019 PhoneNumber gbNumber = new PhoneNumber(); 1020 gbNumber.setCountryCode(44).setNationalNumber(8431231234L); 1021 assertEquals(PhoneNumberUtil.PhoneNumberType.SHARED_COST, phoneUtil.getNumberType(gbNumber)); 1022 } 1023 1024 public void testIsVoip() { 1025 PhoneNumber gbNumber = new PhoneNumber(); 1026 gbNumber.setCountryCode(44).setNationalNumber(5631231234L); 1027 assertEquals(PhoneNumberUtil.PhoneNumberType.VOIP, phoneUtil.getNumberType(gbNumber)); 1028 } 1029 1030 public void testIsPersonalNumber() { 1031 PhoneNumber gbNumber = new PhoneNumber(); 1032 gbNumber.setCountryCode(44).setNationalNumber(7031231234L); 1033 assertEquals(PhoneNumberUtil.PhoneNumberType.PERSONAL_NUMBER, 1034 phoneUtil.getNumberType(gbNumber)); 1035 } 1036 1037 public void testIsUnknown() { 1038 // Invalid numbers should be of type UNKNOWN. 1039 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(US_LOCAL_NUMBER)); 1040 } 1041 1042 public void testIsValidNumber() { 1043 assertTrue(phoneUtil.isValidNumber(US_NUMBER)); 1044 assertTrue(phoneUtil.isValidNumber(IT_NUMBER)); 1045 assertTrue(phoneUtil.isValidNumber(GB_MOBILE)); 1046 assertTrue(phoneUtil.isValidNumber(INTERNATIONAL_TOLL_FREE)); 1047 assertTrue(phoneUtil.isValidNumber(UNIVERSAL_PREMIUM_RATE)); 1048 1049 PhoneNumber nzNumber = new PhoneNumber().setCountryCode(64).setNationalNumber(21387835L); 1050 assertTrue(phoneUtil.isValidNumber(nzNumber)); 1051 } 1052 1053 public void testIsValidForRegion() { 1054 // This number is valid for the Bahamas, but is not a valid US number. 1055 assertTrue(phoneUtil.isValidNumber(BS_NUMBER)); 1056 assertTrue(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.BS)); 1057 assertFalse(phoneUtil.isValidNumberForRegion(BS_NUMBER, RegionCode.US)); 1058 PhoneNumber bsInvalidNumber = 1059 new PhoneNumber().setCountryCode(1).setNationalNumber(2421232345L); 1060 // This number is no longer valid. 1061 assertFalse(phoneUtil.isValidNumber(bsInvalidNumber)); 1062 1063 // La Mayotte and Reunion use 'leadingDigits' to differentiate them. 1064 PhoneNumber reNumber = new PhoneNumber(); 1065 reNumber.setCountryCode(262).setNationalNumber(262123456L); 1066 assertTrue(phoneUtil.isValidNumber(reNumber)); 1067 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 1068 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 1069 // Now change the number to be a number for La Mayotte. 1070 reNumber.setNationalNumber(269601234L); 1071 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 1072 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 1073 // This number is no longer valid for La Reunion. 1074 reNumber.setNationalNumber(269123456L); 1075 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 1076 assertFalse(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 1077 assertFalse(phoneUtil.isValidNumber(reNumber)); 1078 // However, it should be recognised as from La Mayotte, since it is valid for this region. 1079 assertEquals(RegionCode.YT, phoneUtil.getRegionCodeForNumber(reNumber)); 1080 // This number is valid in both places. 1081 reNumber.setNationalNumber(800123456L); 1082 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.YT)); 1083 assertTrue(phoneUtil.isValidNumberForRegion(reNumber, RegionCode.RE)); 1084 assertTrue(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.UN001)); 1085 assertFalse(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.US)); 1086 assertFalse(phoneUtil.isValidNumberForRegion(INTERNATIONAL_TOLL_FREE, RegionCode.ZZ)); 1087 1088 PhoneNumber invalidNumber = new PhoneNumber(); 1089 // Invalid country calling codes. 1090 invalidNumber.setCountryCode(3923).setNationalNumber(2366L); 1091 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ)); 1092 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001)); 1093 invalidNumber.setCountryCode(0); 1094 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001)); 1095 assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ)); 1096 } 1097 1098 public void testIsNotValidNumber() { 1099 assertFalse(phoneUtil.isValidNumber(US_LOCAL_NUMBER)); 1100 1101 PhoneNumber invalidNumber = new PhoneNumber(); 1102 invalidNumber.setCountryCode(39).setNationalNumber(23661830000L).setItalianLeadingZero(true); 1103 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1104 1105 invalidNumber.clear(); 1106 invalidNumber.setCountryCode(44).setNationalNumber(791234567L); 1107 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1108 1109 invalidNumber.clear(); 1110 invalidNumber.setCountryCode(49).setNationalNumber(1234L); 1111 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1112 1113 invalidNumber.clear(); 1114 invalidNumber.setCountryCode(64).setNationalNumber(3316005L); 1115 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1116 1117 invalidNumber.clear(); 1118 // Invalid country calling codes. 1119 invalidNumber.setCountryCode(3923).setNationalNumber(2366L); 1120 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1121 invalidNumber.setCountryCode(0); 1122 assertFalse(phoneUtil.isValidNumber(invalidNumber)); 1123 1124 assertFalse(phoneUtil.isValidNumber(INTERNATIONAL_TOLL_FREE_TOO_LONG)); 1125 } 1126 1127 public void testGetRegionCodeForCountryCode() { 1128 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForCountryCode(1)); 1129 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForCountryCode(44)); 1130 assertEquals(RegionCode.DE, phoneUtil.getRegionCodeForCountryCode(49)); 1131 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForCountryCode(800)); 1132 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForCountryCode(979)); 1133 } 1134 1135 public void testGetRegionCodeForNumber() { 1136 assertEquals(RegionCode.BS, phoneUtil.getRegionCodeForNumber(BS_NUMBER)); 1137 assertEquals(RegionCode.US, phoneUtil.getRegionCodeForNumber(US_NUMBER)); 1138 assertEquals(RegionCode.GB, phoneUtil.getRegionCodeForNumber(GB_MOBILE)); 1139 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForNumber(INTERNATIONAL_TOLL_FREE)); 1140 assertEquals(RegionCode.UN001, phoneUtil.getRegionCodeForNumber(UNIVERSAL_PREMIUM_RATE)); 1141 } 1142 1143 public void testGetRegionCodesForCountryCode() { 1144 List<String> regionCodesForNANPA = phoneUtil.getRegionCodesForCountryCode(1); 1145 assertTrue(regionCodesForNANPA.contains(RegionCode.US)); 1146 assertTrue(regionCodesForNANPA.contains(RegionCode.BS)); 1147 assertTrue(phoneUtil.getRegionCodesForCountryCode(44).contains(RegionCode.GB)); 1148 assertTrue(phoneUtil.getRegionCodesForCountryCode(49).contains(RegionCode.DE)); 1149 assertTrue(phoneUtil.getRegionCodesForCountryCode(800).contains(RegionCode.UN001)); 1150 // Test with invalid country calling code. 1151 assertTrue(phoneUtil.getRegionCodesForCountryCode(-1).isEmpty()); 1152 } 1153 1154 public void testGetCountryCodeForRegion() { 1155 assertEquals(1, phoneUtil.getCountryCodeForRegion(RegionCode.US)); 1156 assertEquals(64, phoneUtil.getCountryCodeForRegion(RegionCode.NZ)); 1157 assertEquals(0, phoneUtil.getCountryCodeForRegion(null)); 1158 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.ZZ)); 1159 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.UN001)); 1160 // CS is already deprecated so the library doesn't support it. 1161 assertEquals(0, phoneUtil.getCountryCodeForRegion(RegionCode.CS)); 1162 } 1163 1164 public void testGetNationalDiallingPrefixForRegion() { 1165 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.US, false)); 1166 // Test non-main country to see it gets the national dialling prefix for the main country with 1167 // that country calling code. 1168 assertEquals("1", phoneUtil.getNddPrefixForRegion(RegionCode.BS, false)); 1169 assertEquals("0", phoneUtil.getNddPrefixForRegion(RegionCode.NZ, false)); 1170 // Test case with non digit in the national prefix. 1171 assertEquals("0~0", phoneUtil.getNddPrefixForRegion(RegionCode.AO, false)); 1172 assertEquals("00", phoneUtil.getNddPrefixForRegion(RegionCode.AO, true)); 1173 // Test cases with invalid regions. 1174 assertEquals(null, phoneUtil.getNddPrefixForRegion(null, false)); 1175 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.ZZ, false)); 1176 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.UN001, false)); 1177 // CS is already deprecated so the library doesn't support it. 1178 assertEquals(null, phoneUtil.getNddPrefixForRegion(RegionCode.CS, false)); 1179 } 1180 1181 public void testIsNANPACountry() { 1182 assertTrue(phoneUtil.isNANPACountry(RegionCode.US)); 1183 assertTrue(phoneUtil.isNANPACountry(RegionCode.BS)); 1184 assertFalse(phoneUtil.isNANPACountry(RegionCode.DE)); 1185 assertFalse(phoneUtil.isNANPACountry(RegionCode.ZZ)); 1186 assertFalse(phoneUtil.isNANPACountry(RegionCode.UN001)); 1187 assertFalse(phoneUtil.isNANPACountry(null)); 1188 } 1189 1190 public void testIsPossibleNumber() { 1191 assertTrue(phoneUtil.isPossibleNumber(US_NUMBER)); 1192 assertTrue(phoneUtil.isPossibleNumber(US_LOCAL_NUMBER)); 1193 assertTrue(phoneUtil.isPossibleNumber(GB_NUMBER)); 1194 assertTrue(phoneUtil.isPossibleNumber(INTERNATIONAL_TOLL_FREE)); 1195 1196 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.US)); 1197 assertTrue(phoneUtil.isPossibleNumber("+1 650 GOO OGLE", RegionCode.US)); 1198 assertTrue(phoneUtil.isPossibleNumber("(650) 253-0000", RegionCode.US)); 1199 assertTrue(phoneUtil.isPossibleNumber("253-0000", RegionCode.US)); 1200 assertTrue(phoneUtil.isPossibleNumber("+1 650 253 0000", RegionCode.GB)); 1201 assertTrue(phoneUtil.isPossibleNumber("+44 20 7031 3000", RegionCode.GB)); 1202 assertTrue(phoneUtil.isPossibleNumber("(020) 7031 3000", RegionCode.GB)); 1203 assertTrue(phoneUtil.isPossibleNumber("7031 3000", RegionCode.GB)); 1204 assertTrue(phoneUtil.isPossibleNumber("3331 6005", RegionCode.NZ)); 1205 assertTrue(phoneUtil.isPossibleNumber("+800 1234 5678", RegionCode.UN001)); 1206 } 1207 1208 public void testIsPossibleNumberWithReason() { 1209 // National numbers for country calling code +1 that are within 7 to 10 digits are possible. 1210 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 1211 phoneUtil.isPossibleNumberWithReason(US_NUMBER)); 1212 1213 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 1214 phoneUtil.isPossibleNumberWithReason(US_LOCAL_NUMBER)); 1215 1216 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, 1217 phoneUtil.isPossibleNumberWithReason(US_LONG_NUMBER)); 1218 1219 PhoneNumber number = new PhoneNumber(); 1220 number.setCountryCode(0).setNationalNumber(2530000L); 1221 assertEquals(PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE, 1222 phoneUtil.isPossibleNumberWithReason(number)); 1223 1224 number.clear(); 1225 number.setCountryCode(1).setNationalNumber(253000L); 1226 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, 1227 phoneUtil.isPossibleNumberWithReason(number)); 1228 1229 number.clear(); 1230 number.setCountryCode(65).setNationalNumber(1234567890L); 1231 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 1232 phoneUtil.isPossibleNumberWithReason(number)); 1233 1234 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, 1235 phoneUtil.isPossibleNumberWithReason(INTERNATIONAL_TOLL_FREE_TOO_LONG)); 1236 1237 // Try with number that we don't have metadata for. 1238 PhoneNumber adNumber = new PhoneNumber(); 1239 adNumber.setCountryCode(376).setNationalNumber(12345L); 1240 assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE, 1241 phoneUtil.isPossibleNumberWithReason(adNumber)); 1242 adNumber.setCountryCode(376).setNationalNumber(1L); 1243 assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT, 1244 phoneUtil.isPossibleNumberWithReason(adNumber)); 1245 adNumber.setCountryCode(376).setNationalNumber(12345678901234567L); 1246 assertEquals(PhoneNumberUtil.ValidationResult.TOO_LONG, 1247 phoneUtil.isPossibleNumberWithReason(adNumber)); 1248 } 1249 1250 public void testIsNotPossibleNumber() { 1251 assertFalse(phoneUtil.isPossibleNumber(US_LONG_NUMBER)); 1252 assertFalse(phoneUtil.isPossibleNumber(INTERNATIONAL_TOLL_FREE_TOO_LONG)); 1253 1254 PhoneNumber number = new PhoneNumber(); 1255 number.setCountryCode(1).setNationalNumber(253000L); 1256 assertFalse(phoneUtil.isPossibleNumber(number)); 1257 1258 number.clear(); 1259 number.setCountryCode(44).setNationalNumber(300L); 1260 assertFalse(phoneUtil.isPossibleNumber(number)); 1261 assertFalse(phoneUtil.isPossibleNumber("+1 650 253 00000", RegionCode.US)); 1262 assertFalse(phoneUtil.isPossibleNumber("(650) 253-00000", RegionCode.US)); 1263 assertFalse(phoneUtil.isPossibleNumber("I want a Pizza", RegionCode.US)); 1264 assertFalse(phoneUtil.isPossibleNumber("253-000", RegionCode.US)); 1265 assertFalse(phoneUtil.isPossibleNumber("1 3000", RegionCode.GB)); 1266 assertFalse(phoneUtil.isPossibleNumber("+44 300", RegionCode.GB)); 1267 assertFalse(phoneUtil.isPossibleNumber("+800 1234 5678 9", RegionCode.UN001)); 1268 } 1269 1270 public void testTruncateTooLongNumber() { 1271 // GB number 080 1234 5678, but entered with 4 extra digits at the end. 1272 PhoneNumber tooLongNumber = new PhoneNumber(); 1273 tooLongNumber.setCountryCode(44).setNationalNumber(80123456780123L); 1274 PhoneNumber validNumber = new PhoneNumber(); 1275 validNumber.setCountryCode(44).setNationalNumber(8012345678L); 1276 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1277 assertEquals(validNumber, tooLongNumber); 1278 1279 // IT number 022 3456 7890, but entered with 3 extra digits at the end. 1280 tooLongNumber.clear(); 1281 tooLongNumber.setCountryCode(39).setNationalNumber(2234567890123L).setItalianLeadingZero(true); 1282 validNumber.clear(); 1283 validNumber.setCountryCode(39).setNationalNumber(2234567890L).setItalianLeadingZero(true); 1284 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1285 assertEquals(validNumber, tooLongNumber); 1286 1287 // US number 650-253-0000, but entered with one additional digit at the end. 1288 tooLongNumber.clear(); 1289 tooLongNumber.mergeFrom(US_LONG_NUMBER); 1290 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1291 assertEquals(US_NUMBER, tooLongNumber); 1292 1293 tooLongNumber.clear(); 1294 tooLongNumber.mergeFrom(INTERNATIONAL_TOLL_FREE_TOO_LONG); 1295 assertTrue(phoneUtil.truncateTooLongNumber(tooLongNumber)); 1296 assertEquals(INTERNATIONAL_TOLL_FREE, tooLongNumber); 1297 1298 // Tests what happens when a valid number is passed in. 1299 PhoneNumber validNumberCopy = new PhoneNumber().mergeFrom(validNumber); 1300 assertTrue(phoneUtil.truncateTooLongNumber(validNumber)); 1301 // Tests the number is not modified. 1302 assertEquals(validNumberCopy, validNumber); 1303 1304 // Tests what happens when a number with invalid prefix is passed in. 1305 PhoneNumber numberWithInvalidPrefix = new PhoneNumber(); 1306 // The test metadata says US numbers cannot have prefix 240. 1307 numberWithInvalidPrefix.setCountryCode(1).setNationalNumber(2401234567L); 1308 PhoneNumber invalidNumberCopy = new PhoneNumber().mergeFrom(numberWithInvalidPrefix); 1309 assertFalse(phoneUtil.truncateTooLongNumber(numberWithInvalidPrefix)); 1310 // Tests the number is not modified. 1311 assertEquals(invalidNumberCopy, numberWithInvalidPrefix); 1312 1313 // Tests what happens when a too short number is passed in. 1314 PhoneNumber tooShortNumber = new PhoneNumber().setCountryCode(1).setNationalNumber(1234L); 1315 PhoneNumber tooShortNumberCopy = new PhoneNumber().mergeFrom(tooShortNumber); 1316 assertFalse(phoneUtil.truncateTooLongNumber(tooShortNumber)); 1317 // Tests the number is not modified. 1318 assertEquals(tooShortNumberCopy, tooShortNumber); 1319 } 1320 1321 public void testIsViablePhoneNumber() { 1322 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1")); 1323 // Only one or two digits before strange non-possible punctuation. 1324 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1+1+1")); 1325 assertFalse(PhoneNumberUtil.isViablePhoneNumber("80+0")); 1326 // Two digits is viable. 1327 assertTrue(PhoneNumberUtil.isViablePhoneNumber("00")); 1328 assertTrue(PhoneNumberUtil.isViablePhoneNumber("111")); 1329 // Alpha numbers. 1330 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-pizza")); 1331 assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-PIZZA")); 1332 // We need at least three digits before any alpha characters. 1333 assertFalse(PhoneNumberUtil.isViablePhoneNumber("08-PIZZA")); 1334 assertFalse(PhoneNumberUtil.isViablePhoneNumber("8-PIZZA")); 1335 assertFalse(PhoneNumberUtil.isViablePhoneNumber("12. March")); 1336 } 1337 1338 public void testIsViablePhoneNumberNonAscii() { 1339 // Only one or two digits before possible punctuation followed by more digits. 1340 assertTrue(PhoneNumberUtil.isViablePhoneNumber("1\u300034")); 1341 assertFalse(PhoneNumberUtil.isViablePhoneNumber("1\u30003+4")); 1342 // Unicode variants of possible starting character and other allowed punctuation/digits. 1343 assertTrue(PhoneNumberUtil.isViablePhoneNumber("\uFF081\uFF09\u30003456789")); 1344 // Testing a leading + is okay. 1345 assertTrue(PhoneNumberUtil.isViablePhoneNumber("+1\uFF09\u30003456789")); 1346 } 1347 1348 public void testExtractPossibleNumber() { 1349 // Removes preceding funky punctuation and letters but leaves the rest untouched. 1350 assertEquals("0800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:0800-345-600")); 1351 assertEquals("0800 FOR PIZZA", PhoneNumberUtil.extractPossibleNumber("Tel:0800 FOR PIZZA")); 1352 // Should not remove plus sign 1353 assertEquals("+800-345-600", PhoneNumberUtil.extractPossibleNumber("Tel:+800-345-600")); 1354 // Should recognise wide digits as possible start values. 1355 assertEquals("\uFF10\uFF12\uFF13", 1356 PhoneNumberUtil.extractPossibleNumber("\uFF10\uFF12\uFF13")); 1357 // Dashes are not possible start values and should be removed. 1358 assertEquals("\uFF11\uFF12\uFF13", 1359 PhoneNumberUtil.extractPossibleNumber("Num-\uFF11\uFF12\uFF13")); 1360 // If not possible number present, return empty string. 1361 assertEquals("", PhoneNumberUtil.extractPossibleNumber("Num-....")); 1362 // Leading brackets are stripped - these are not used when parsing. 1363 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000")); 1364 1365 // Trailing non-alpha-numeric characters should be removed. 1366 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000..- ..")); 1367 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000.")); 1368 // This case has a trailing RTL char. 1369 assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000\u200F")); 1370 } 1371 1372 public void testMaybeStripNationalPrefix() { 1373 PhoneMetadata metadata = new PhoneMetadata(); 1374 metadata.setNationalPrefixForParsing("34"); 1375 metadata.setGeneralDesc(new PhoneNumberDesc().setNationalNumberPattern("\\d{4,8}")); 1376 StringBuilder numberToStrip = new StringBuilder("34356778"); 1377 String strippedNumber = "356778"; 1378 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null)); 1379 assertEquals("Should have had national prefix stripped.", 1380 strippedNumber, numberToStrip.toString()); 1381 // Retry stripping - now the number should not start with the national prefix, so no more 1382 // stripping should occur. 1383 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null)); 1384 assertEquals("Should have had no change - no national prefix present.", 1385 strippedNumber, numberToStrip.toString()); 1386 // Some countries have no national prefix. Repeat test with none specified. 1387 metadata.setNationalPrefixForParsing(""); 1388 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null)); 1389 assertEquals("Should not strip anything with empty national prefix.", 1390 strippedNumber, numberToStrip.toString()); 1391 // If the resultant number doesn't match the national rule, it shouldn't be stripped. 1392 metadata.setNationalPrefixForParsing("3"); 1393 numberToStrip = new StringBuilder("3123"); 1394 strippedNumber = "3123"; 1395 assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null)); 1396 assertEquals("Should have had no change - after stripping, it wouldn't have matched " + 1397 "the national rule.", 1398 strippedNumber, numberToStrip.toString()); 1399 // Test extracting carrier selection code. 1400 metadata.setNationalPrefixForParsing("0(81)?"); 1401 numberToStrip = new StringBuilder("08122123456"); 1402 strippedNumber = "22123456"; 1403 StringBuilder carrierCode = new StringBuilder(); 1404 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode( 1405 numberToStrip, metadata, carrierCode)); 1406 assertEquals("81", carrierCode.toString()); 1407 assertEquals("Should have had national prefix and carrier code stripped.", 1408 strippedNumber, numberToStrip.toString()); 1409 // If there was a transform rule, check it was applied. 1410 metadata.setNationalPrefixTransformRule("5$15"); 1411 // Note that a capturing group is present here. 1412 metadata.setNationalPrefixForParsing("0(\\d{2})"); 1413 numberToStrip = new StringBuilder("031123"); 1414 String transformedNumber = "5315123"; 1415 assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null)); 1416 assertEquals("Should transform the 031 to a 5315.", 1417 transformedNumber, numberToStrip.toString()); 1418 } 1419 1420 public void testMaybeStripInternationalPrefix() { 1421 String internationalPrefix = "00[39]"; 1422 StringBuilder numberToStrip = new StringBuilder("0034567700-3898003"); 1423 // Note the dash is removed as part of the normalization. 1424 StringBuilder strippedNumber = new StringBuilder("45677003898003"); 1425 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1426 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1427 internationalPrefix)); 1428 assertEquals("The number supplied was not stripped of its international prefix.", 1429 strippedNumber.toString(), numberToStrip.toString()); 1430 // Now the number no longer starts with an IDD prefix, so it should now report 1431 // FROM_DEFAULT_COUNTRY. 1432 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1433 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1434 internationalPrefix)); 1435 1436 numberToStrip = new StringBuilder("00945677003898003"); 1437 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1438 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1439 internationalPrefix)); 1440 assertEquals("The number supplied was not stripped of its international prefix.", 1441 strippedNumber.toString(), numberToStrip.toString()); 1442 // Test it works when the international prefix is broken up by spaces. 1443 numberToStrip = new StringBuilder("00 9 45677003898003"); 1444 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_IDD, 1445 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1446 internationalPrefix)); 1447 assertEquals("The number supplied was not stripped of its international prefix.", 1448 strippedNumber.toString(), numberToStrip.toString()); 1449 // Now the number no longer starts with an IDD prefix, so it should now report 1450 // FROM_DEFAULT_COUNTRY. 1451 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1452 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1453 internationalPrefix)); 1454 1455 // Test the + symbol is also recognised and stripped. 1456 numberToStrip = new StringBuilder("+45677003898003"); 1457 strippedNumber = new StringBuilder("45677003898003"); 1458 assertEquals(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, 1459 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1460 internationalPrefix)); 1461 assertEquals("The number supplied was not stripped of the plus symbol.", 1462 strippedNumber.toString(), numberToStrip.toString()); 1463 1464 // If the number afterwards is a zero, we should not strip this - no country calling code begins 1465 // with 0. 1466 numberToStrip = new StringBuilder("0090112-3123"); 1467 strippedNumber = new StringBuilder("00901123123"); 1468 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1469 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1470 internationalPrefix)); 1471 assertEquals("The number supplied had a 0 after the match so shouldn't be stripped.", 1472 strippedNumber.toString(), numberToStrip.toString()); 1473 // Here the 0 is separated by a space from the IDD. 1474 numberToStrip = new StringBuilder("009 0-112-3123"); 1475 assertEquals(CountryCodeSource.FROM_DEFAULT_COUNTRY, 1476 phoneUtil.maybeStripInternationalPrefixAndNormalize(numberToStrip, 1477 internationalPrefix)); 1478 } 1479 1480 public void testMaybeExtractCountryCode() { 1481 PhoneNumber number = new PhoneNumber(); 1482 PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US); 1483 // Note that for the US, the IDD is 011. 1484 try { 1485 String phoneNumber = "011112-3456789"; 1486 String strippedNumber = "123456789"; 1487 int countryCallingCode = 1; 1488 StringBuilder numberToFill = new StringBuilder(); 1489 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.", 1490 countryCallingCode, 1491 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1492 number)); 1493 assertEquals("Did not figure out CountryCodeSource correctly", 1494 CountryCodeSource.FROM_NUMBER_WITH_IDD, number.getCountryCodeSource()); 1495 // Should strip and normalize national significant number. 1496 assertEquals("Did not strip off the country calling code correctly.", 1497 strippedNumber, 1498 numberToFill.toString()); 1499 } catch (NumberParseException e) { 1500 fail("Should not have thrown an exception: " + e.toString()); 1501 } 1502 number.clear(); 1503 try { 1504 String phoneNumber = "+6423456789"; 1505 int countryCallingCode = 64; 1506 StringBuilder numberToFill = new StringBuilder(); 1507 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.", 1508 countryCallingCode, 1509 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1510 number)); 1511 assertEquals("Did not figure out CountryCodeSource correctly", 1512 CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource()); 1513 } catch (NumberParseException e) { 1514 fail("Should not have thrown an exception: " + e.toString()); 1515 } 1516 number.clear(); 1517 try { 1518 String phoneNumber = "+80012345678"; 1519 int countryCallingCode = 800; 1520 StringBuilder numberToFill = new StringBuilder(); 1521 assertEquals("Did not extract country calling code " + countryCallingCode + " correctly.", 1522 countryCallingCode, 1523 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1524 number)); 1525 assertEquals("Did not figure out CountryCodeSource correctly", 1526 CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN, number.getCountryCodeSource()); 1527 } catch (NumberParseException e) { 1528 fail("Should not have thrown an exception: " + e.toString()); 1529 } 1530 number.clear(); 1531 try { 1532 String phoneNumber = "2345-6789"; 1533 StringBuilder numberToFill = new StringBuilder(); 1534 assertEquals( 1535 "Should not have extracted a country calling code - no international prefix present.", 1536 0, 1537 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number)); 1538 assertEquals("Did not figure out CountryCodeSource correctly", 1539 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource()); 1540 } catch (NumberParseException e) { 1541 fail("Should not have thrown an exception: " + e.toString()); 1542 } 1543 number.clear(); 1544 try { 1545 String phoneNumber = "0119991123456789"; 1546 StringBuilder numberToFill = new StringBuilder(); 1547 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, number); 1548 fail("Should have thrown an exception, no valid country calling code present."); 1549 } catch (NumberParseException e) { 1550 // Expected. 1551 assertEquals("Wrong error type stored in exception.", 1552 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1553 e.getErrorType()); 1554 } 1555 number.clear(); 1556 try { 1557 String phoneNumber = "(1 610) 619 4466"; 1558 int countryCallingCode = 1; 1559 StringBuilder numberToFill = new StringBuilder(); 1560 assertEquals("Should have extracted the country calling code of the region passed in", 1561 countryCallingCode, 1562 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1563 number)); 1564 assertEquals("Did not figure out CountryCodeSource correctly", 1565 CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN, 1566 number.getCountryCodeSource()); 1567 } catch (NumberParseException e) { 1568 fail("Should not have thrown an exception: " + e.toString()); 1569 } 1570 number.clear(); 1571 try { 1572 String phoneNumber = "(1 610) 619 4466"; 1573 int countryCallingCode = 1; 1574 StringBuilder numberToFill = new StringBuilder(); 1575 assertEquals("Should have extracted the country calling code of the region passed in", 1576 countryCallingCode, 1577 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false, 1578 number)); 1579 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource()); 1580 } catch (NumberParseException e) { 1581 fail("Should not have thrown an exception: " + e.toString()); 1582 } 1583 number.clear(); 1584 try { 1585 String phoneNumber = "(1 610) 619 446"; 1586 StringBuilder numberToFill = new StringBuilder(); 1587 assertEquals("Should not have extracted a country calling code - invalid number after " + 1588 "extraction of uncertain country calling code.", 1589 0, 1590 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, false, 1591 number)); 1592 assertFalse("Should not contain CountryCodeSource.", number.hasCountryCodeSource()); 1593 } catch (NumberParseException e) { 1594 fail("Should not have thrown an exception: " + e.toString()); 1595 } 1596 number.clear(); 1597 try { 1598 String phoneNumber = "(1 610) 619"; 1599 StringBuilder numberToFill = new StringBuilder(); 1600 assertEquals("Should not have extracted a country calling code - too short number both " + 1601 "before and after extraction of uncertain country calling code.", 1602 0, 1603 phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true, 1604 number)); 1605 assertEquals("Did not figure out CountryCodeSource correctly", 1606 CountryCodeSource.FROM_DEFAULT_COUNTRY, number.getCountryCodeSource()); 1607 } catch (NumberParseException e) { 1608 fail("Should not have thrown an exception: " + e.toString()); 1609 } 1610 } 1611 1612 public void testParseNationalNumber() throws Exception { 1613 // National prefix attached. 1614 assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); 1615 assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); 1616 // National prefix attached and some formatting present. 1617 assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); 1618 assertEquals(NZ_NUMBER, phoneUtil.parse("03 331 6005", RegionCode.NZ)); 1619 // Test parsing RFC3966 format with a phone context. 1620 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.NZ)); 1621 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.NZ)); 1622 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US)); 1623 // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear 1624 // after the context if present. 1625 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1", 1626 RegionCode.NZ)); 1627 // Test parsing RFC3966 with an ISDN subaddress. 1628 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", 1629 RegionCode.NZ)); 1630 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", RegionCode.NZ)); 1631 // Testing international prefixes. 1632 // Should strip country calling code. 1633 assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); 1634 // Try again, but this time we have an international number with Region Code US. It should 1635 // recognise the country calling code and parse accordingly. 1636 assertEquals(NZ_NUMBER, phoneUtil.parse("01164 3 331 6005", RegionCode.US)); 1637 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.US)); 1638 // We should ignore the leading plus here, since it is not followed by a valid country code but 1639 // instead is followed by the IDD for the US. 1640 assertEquals(NZ_NUMBER, phoneUtil.parse("+01164 3 331 6005", RegionCode.US)); 1641 assertEquals(NZ_NUMBER, phoneUtil.parse("+0064 3 331 6005", RegionCode.NZ)); 1642 assertEquals(NZ_NUMBER, phoneUtil.parse("+ 00 64 3 331 6005", RegionCode.NZ)); 1643 1644 assertEquals(US_LOCAL_NUMBER, 1645 phoneUtil.parse("tel:253-0000;phone-context=www.google.com", RegionCode.US)); 1646 assertEquals(US_LOCAL_NUMBER, 1647 phoneUtil.parse("tel:253-0000;isub=12345;phone-context=www.google.com", RegionCode.US)); 1648 // This is invalid because no "+" sign is present as part of phone-context. The phone context 1649 // is simply ignored in this case just as if it contains a domain. 1650 assertEquals(US_LOCAL_NUMBER, 1651 phoneUtil.parse("tel:2530000;isub=12345;phone-context=1-650", RegionCode.US)); 1652 assertEquals(US_LOCAL_NUMBER, 1653 phoneUtil.parse("tel:2530000;isub=12345;phone-context=1234.com", RegionCode.US)); 1654 1655 PhoneNumber nzNumber = new PhoneNumber(); 1656 nzNumber.setCountryCode(64).setNationalNumber(64123456L); 1657 assertEquals(nzNumber, phoneUtil.parse("64(0)64123456", RegionCode.NZ)); 1658 // Check that using a "/" is fine in a phone number. 1659 assertEquals(DE_NUMBER, phoneUtil.parse("301/23456", RegionCode.DE)); 1660 1661 PhoneNumber usNumber = new PhoneNumber(); 1662 // Check it doesn't use the '1' as a country calling code when parsing if the phone number was 1663 // already possible. 1664 usNumber.setCountryCode(1).setNationalNumber(1234567890L); 1665 assertEquals(usNumber, phoneUtil.parse("123-456-7890", RegionCode.US)); 1666 1667 // Test star numbers. Although this is not strictly valid, we would like to make sure we can 1668 // parse the output we produce when formatting the number. 1669 assertEquals(JP_STAR_NUMBER, phoneUtil.parse("+81 *2345", RegionCode.JP)); 1670 1671 PhoneNumber shortNumber = new PhoneNumber(); 1672 shortNumber.setCountryCode(64).setNationalNumber(12L); 1673 assertEquals(shortNumber, phoneUtil.parse("12", RegionCode.NZ)); 1674 } 1675 1676 public void testParseNumberWithAlphaCharacters() throws Exception { 1677 // Test case with alpha characters. 1678 PhoneNumber tollfreeNumber = new PhoneNumber(); 1679 tollfreeNumber.setCountryCode(64).setNationalNumber(800332005L); 1680 assertEquals(tollfreeNumber, phoneUtil.parse("0800 DDA 005", RegionCode.NZ)); 1681 PhoneNumber premiumNumber = new PhoneNumber(); 1682 premiumNumber.setCountryCode(64).setNationalNumber(9003326005L); 1683 assertEquals(premiumNumber, phoneUtil.parse("0900 DDA 6005", RegionCode.NZ)); 1684 // Not enough alpha characters for them to be considered intentional, so they are stripped. 1685 assertEquals(premiumNumber, phoneUtil.parse("0900 332 6005a", RegionCode.NZ)); 1686 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600a5", RegionCode.NZ)); 1687 assertEquals(premiumNumber, phoneUtil.parse("0900 332 600A5", RegionCode.NZ)); 1688 assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ)); 1689 } 1690 1691 public void testParseMaliciousInput() throws Exception { 1692 // Lots of leading + signs before the possible number. 1693 StringBuilder maliciousNumber = new StringBuilder(6000); 1694 for (int i = 0; i < 6000; i++) { 1695 maliciousNumber.append('+'); 1696 } 1697 maliciousNumber.append("12222-33-244 extensioB 343+"); 1698 try { 1699 phoneUtil.parse(maliciousNumber.toString(), RegionCode.US); 1700 fail("This should not parse without throwing an exception " + maliciousNumber); 1701 } catch (NumberParseException e) { 1702 // Expected this exception. 1703 assertEquals("Wrong error type stored in exception.", 1704 NumberParseException.ErrorType.TOO_LONG, 1705 e.getErrorType()); 1706 } 1707 StringBuilder maliciousNumberWithAlmostExt = new StringBuilder(6000); 1708 for (int i = 0; i < 350; i++) { 1709 maliciousNumberWithAlmostExt.append("200"); 1710 } 1711 maliciousNumberWithAlmostExt.append(" extensiOB 345"); 1712 try { 1713 phoneUtil.parse(maliciousNumberWithAlmostExt.toString(), RegionCode.US); 1714 fail("This should not parse without throwing an exception " + maliciousNumberWithAlmostExt); 1715 } catch (NumberParseException e) { 1716 // Expected this exception. 1717 assertEquals("Wrong error type stored in exception.", 1718 NumberParseException.ErrorType.TOO_LONG, 1719 e.getErrorType()); 1720 } 1721 } 1722 1723 public void testParseWithInternationalPrefixes() throws Exception { 1724 assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ)); 1725 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("011 800 1234 5678", RegionCode.US)); 1726 assertEquals(US_NUMBER, phoneUtil.parse("1-650-253-0000", RegionCode.US)); 1727 // Calling the US number from Singapore by using different service providers 1728 // 1st test: calling using SingTel IDD service (IDD is 001) 1729 assertEquals(US_NUMBER, phoneUtil.parse("0011-650-253-0000", RegionCode.SG)); 1730 // 2nd test: calling using StarHub IDD service (IDD is 008) 1731 assertEquals(US_NUMBER, phoneUtil.parse("0081-650-253-0000", RegionCode.SG)); 1732 // 3rd test: calling using SingTel V019 service (IDD is 019) 1733 assertEquals(US_NUMBER, phoneUtil.parse("0191-650-253-0000", RegionCode.SG)); 1734 // Calling the US number from Poland 1735 assertEquals(US_NUMBER, phoneUtil.parse("0~01-650-253-0000", RegionCode.PL)); 1736 // Using "++" at the start. 1737 assertEquals(US_NUMBER, phoneUtil.parse("++1 (650) 253-0000", RegionCode.PL)); 1738 } 1739 1740 public void testParseNonAscii() throws Exception { 1741 // Using a full-width plus sign. 1742 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B1 (650) 253-0000", RegionCode.SG)); 1743 // Using a soft hyphen U+00AD. 1744 assertEquals(US_NUMBER, phoneUtil.parse("1 (650) 253\u00AD-0000", RegionCode.US)); 1745 // The whole number, including punctuation, is here represented in full-width form. 1746 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + 1747 "\u3000\uFF12\uFF15\uFF13\uFF0D\uFF10\uFF10\uFF10" + 1748 "\uFF10", 1749 RegionCode.SG)); 1750 // Using U+30FC dash instead. 1751 assertEquals(US_NUMBER, phoneUtil.parse("\uFF0B\uFF11\u3000\uFF08\uFF16\uFF15\uFF10\uFF09" + 1752 "\u3000\uFF12\uFF15\uFF13\u30FC\uFF10\uFF10\uFF10" + 1753 "\uFF10", 1754 RegionCode.SG)); 1755 1756 // Using a very strange decimal digit range (Mongolian digits). 1757 assertEquals(US_NUMBER, phoneUtil.parse("\u1811 \u1816\u1815\u1810 " + 1758 "\u1812\u1815\u1813 \u1810\u1810\u1810\u1810", 1759 RegionCode.US)); 1760 } 1761 1762 public void testParseWithLeadingZero() throws Exception { 1763 assertEquals(IT_NUMBER, phoneUtil.parse("+39 02-36618 300", RegionCode.NZ)); 1764 assertEquals(IT_NUMBER, phoneUtil.parse("02-36618 300", RegionCode.IT)); 1765 1766 assertEquals(IT_MOBILE, phoneUtil.parse("345 678 901", RegionCode.IT)); 1767 } 1768 1769 public void testParseNationalNumberArgentina() throws Exception { 1770 // Test parsing mobile numbers of Argentina. 1771 PhoneNumber arNumber = new PhoneNumber(); 1772 arNumber.setCountryCode(54).setNationalNumber(93435551212L); 1773 assertEquals(arNumber, phoneUtil.parse("+54 9 343 555 1212", RegionCode.AR)); 1774 assertEquals(arNumber, phoneUtil.parse("0343 15 555 1212", RegionCode.AR)); 1775 1776 arNumber.clear(); 1777 arNumber.setCountryCode(54).setNationalNumber(93715654320L); 1778 assertEquals(arNumber, phoneUtil.parse("+54 9 3715 65 4320", RegionCode.AR)); 1779 assertEquals(arNumber, phoneUtil.parse("03715 15 65 4320", RegionCode.AR)); 1780 assertEquals(AR_MOBILE, phoneUtil.parse("911 876 54321", RegionCode.AR)); 1781 1782 // Test parsing fixed-line numbers of Argentina. 1783 assertEquals(AR_NUMBER, phoneUtil.parse("+54 11 8765 4321", RegionCode.AR)); 1784 assertEquals(AR_NUMBER, phoneUtil.parse("011 8765 4321", RegionCode.AR)); 1785 1786 arNumber.clear(); 1787 arNumber.setCountryCode(54).setNationalNumber(3715654321L); 1788 assertEquals(arNumber, phoneUtil.parse("+54 3715 65 4321", RegionCode.AR)); 1789 assertEquals(arNumber, phoneUtil.parse("03715 65 4321", RegionCode.AR)); 1790 1791 arNumber.clear(); 1792 arNumber.setCountryCode(54).setNationalNumber(2312340000L); 1793 assertEquals(arNumber, phoneUtil.parse("+54 23 1234 0000", RegionCode.AR)); 1794 assertEquals(arNumber, phoneUtil.parse("023 1234 0000", RegionCode.AR)); 1795 } 1796 1797 public void testParseWithXInNumber() throws Exception { 1798 // Test that having an 'x' in the phone number at the start is ok and that it just gets removed. 1799 assertEquals(AR_NUMBER, phoneUtil.parse("01187654321", RegionCode.AR)); 1800 assertEquals(AR_NUMBER, phoneUtil.parse("(0) 1187654321", RegionCode.AR)); 1801 assertEquals(AR_NUMBER, phoneUtil.parse("0 1187654321", RegionCode.AR)); 1802 assertEquals(AR_NUMBER, phoneUtil.parse("(0xx) 1187654321", RegionCode.AR)); 1803 PhoneNumber arFromUs = new PhoneNumber(); 1804 arFromUs.setCountryCode(54).setNationalNumber(81429712L); 1805 // This test is intentionally constructed such that the number of digit after xx is larger than 1806 // 7, so that the number won't be mistakenly treated as an extension, as we allow extensions up 1807 // to 7 digits. This assumption is okay for now as all the countries where a carrier selection 1808 // code is written in the form of xx have a national significant number of length larger than 7. 1809 assertEquals(arFromUs, phoneUtil.parse("011xx5481429712", RegionCode.US)); 1810 } 1811 1812 public void testParseNumbersMexico() throws Exception { 1813 // Test parsing fixed-line numbers of Mexico. 1814 PhoneNumber mxNumber = new PhoneNumber(); 1815 mxNumber.setCountryCode(52).setNationalNumber(4499780001L); 1816 assertEquals(mxNumber, phoneUtil.parse("+52 (449)978-0001", RegionCode.MX)); 1817 assertEquals(mxNumber, phoneUtil.parse("01 (449)978-0001", RegionCode.MX)); 1818 assertEquals(mxNumber, phoneUtil.parse("(449)978-0001", RegionCode.MX)); 1819 1820 // Test parsing mobile numbers of Mexico. 1821 mxNumber.clear(); 1822 mxNumber.setCountryCode(52).setNationalNumber(13312345678L); 1823 assertEquals(mxNumber, phoneUtil.parse("+52 1 33 1234-5678", RegionCode.MX)); 1824 assertEquals(mxNumber, phoneUtil.parse("044 (33) 1234-5678", RegionCode.MX)); 1825 assertEquals(mxNumber, phoneUtil.parse("045 33 1234-5678", RegionCode.MX)); 1826 } 1827 1828 public void testFailedParseOnInvalidNumbers() { 1829 try { 1830 String sentencePhoneNumber = "This is not a phone number"; 1831 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); 1832 fail("This should not parse without throwing an exception " + sentencePhoneNumber); 1833 } catch (NumberParseException e) { 1834 // Expected this exception. 1835 assertEquals("Wrong error type stored in exception.", 1836 NumberParseException.ErrorType.NOT_A_NUMBER, 1837 e.getErrorType()); 1838 } 1839 try { 1840 String sentencePhoneNumber = "1 Still not a number"; 1841 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); 1842 fail("This should not parse without throwing an exception " + sentencePhoneNumber); 1843 } catch (NumberParseException e) { 1844 // Expected this exception. 1845 assertEquals("Wrong error type stored in exception.", 1846 NumberParseException.ErrorType.NOT_A_NUMBER, 1847 e.getErrorType()); 1848 } 1849 try { 1850 String sentencePhoneNumber = "1 MICROSOFT"; 1851 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); 1852 fail("This should not parse without throwing an exception " + sentencePhoneNumber); 1853 } catch (NumberParseException e) { 1854 // Expected this exception. 1855 assertEquals("Wrong error type stored in exception.", 1856 NumberParseException.ErrorType.NOT_A_NUMBER, 1857 e.getErrorType()); 1858 } 1859 try { 1860 String sentencePhoneNumber = "12 MICROSOFT"; 1861 phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); 1862 fail("This should not parse without throwing an exception " + sentencePhoneNumber); 1863 } catch (NumberParseException e) { 1864 // Expected this exception. 1865 assertEquals("Wrong error type stored in exception.", 1866 NumberParseException.ErrorType.NOT_A_NUMBER, 1867 e.getErrorType()); 1868 } 1869 try { 1870 String tooLongPhoneNumber = "01495 72553301873 810104"; 1871 phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB); 1872 fail("This should not parse without throwing an exception " + tooLongPhoneNumber); 1873 } catch (NumberParseException e) { 1874 // Expected this exception. 1875 assertEquals("Wrong error type stored in exception.", 1876 NumberParseException.ErrorType.TOO_LONG, 1877 e.getErrorType()); 1878 } 1879 try { 1880 String plusMinusPhoneNumber = "+---"; 1881 phoneUtil.parse(plusMinusPhoneNumber, RegionCode.DE); 1882 fail("This should not parse without throwing an exception " + plusMinusPhoneNumber); 1883 } catch (NumberParseException e) { 1884 // Expected this exception. 1885 assertEquals("Wrong error type stored in exception.", 1886 NumberParseException.ErrorType.NOT_A_NUMBER, 1887 e.getErrorType()); 1888 } 1889 try { 1890 String plusStar = "+***"; 1891 phoneUtil.parse(plusStar, RegionCode.DE); 1892 fail("This should not parse without throwing an exception " + plusStar); 1893 } catch (NumberParseException e) { 1894 // Expected this exception. 1895 assertEquals("Wrong error type stored in exception.", 1896 NumberParseException.ErrorType.NOT_A_NUMBER, 1897 e.getErrorType()); 1898 } 1899 try { 1900 String plusStarPhoneNumber = "+*******91"; 1901 phoneUtil.parse(plusStarPhoneNumber, RegionCode.DE); 1902 fail("This should not parse without throwing an exception " + plusStarPhoneNumber); 1903 } catch (NumberParseException e) { 1904 // Expected this exception. 1905 assertEquals("Wrong error type stored in exception.", 1906 NumberParseException.ErrorType.NOT_A_NUMBER, 1907 e.getErrorType()); 1908 } 1909 try { 1910 String tooShortPhoneNumber = "+49 0"; 1911 phoneUtil.parse(tooShortPhoneNumber, RegionCode.DE); 1912 fail("This should not parse without throwing an exception " + tooShortPhoneNumber); 1913 } catch (NumberParseException e) { 1914 // Expected this exception. 1915 assertEquals("Wrong error type stored in exception.", 1916 NumberParseException.ErrorType.TOO_SHORT_NSN, 1917 e.getErrorType()); 1918 } 1919 try { 1920 String invalidCountryCode = "+210 3456 56789"; 1921 phoneUtil.parse(invalidCountryCode, RegionCode.NZ); 1922 fail("This is not a recognised region code: should fail: " + invalidCountryCode); 1923 } catch (NumberParseException e) { 1924 // Expected this exception. 1925 assertEquals("Wrong error type stored in exception.", 1926 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1927 e.getErrorType()); 1928 } 1929 try { 1930 String plusAndIddAndInvalidCountryCode = "+ 00 210 3 331 6005"; 1931 phoneUtil.parse(plusAndIddAndInvalidCountryCode, RegionCode.NZ); 1932 fail("This should not parse without throwing an exception."); 1933 } catch (NumberParseException e) { 1934 // Expected this exception. 00 is a correct IDD, but 210 is not a valid country code. 1935 assertEquals("Wrong error type stored in exception.", 1936 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1937 e.getErrorType()); 1938 } 1939 try { 1940 String someNumber = "123 456 7890"; 1941 phoneUtil.parse(someNumber, RegionCode.ZZ); 1942 fail("'Unknown' region code not allowed: should fail."); 1943 } catch (NumberParseException e) { 1944 // Expected this exception. 1945 assertEquals("Wrong error type stored in exception.", 1946 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1947 e.getErrorType()); 1948 } 1949 try { 1950 String someNumber = "123 456 7890"; 1951 phoneUtil.parse(someNumber, RegionCode.CS); 1952 fail("Deprecated region code not allowed: should fail."); 1953 } catch (NumberParseException e) { 1954 // Expected this exception. 1955 assertEquals("Wrong error type stored in exception.", 1956 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1957 e.getErrorType()); 1958 } 1959 try { 1960 String someNumber = "123 456 7890"; 1961 phoneUtil.parse(someNumber, null); 1962 fail("Null region code not allowed: should fail."); 1963 } catch (NumberParseException e) { 1964 // Expected this exception. 1965 assertEquals("Wrong error type stored in exception.", 1966 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 1967 e.getErrorType()); 1968 } 1969 try { 1970 String someNumber = "0044------"; 1971 phoneUtil.parse(someNumber, RegionCode.GB); 1972 fail("No number provided, only region code: should fail"); 1973 } catch (NumberParseException e) { 1974 // Expected this exception. 1975 assertEquals("Wrong error type stored in exception.", 1976 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1977 e.getErrorType()); 1978 } 1979 try { 1980 String someNumber = "0044"; 1981 phoneUtil.parse(someNumber, RegionCode.GB); 1982 fail("No number provided, only region code: should fail"); 1983 } catch (NumberParseException e) { 1984 // Expected this exception. 1985 assertEquals("Wrong error type stored in exception.", 1986 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1987 e.getErrorType()); 1988 } 1989 try { 1990 String someNumber = "011"; 1991 phoneUtil.parse(someNumber, RegionCode.US); 1992 fail("Only IDD provided - should fail."); 1993 } catch (NumberParseException e) { 1994 // Expected this exception. 1995 assertEquals("Wrong error type stored in exception.", 1996 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 1997 e.getErrorType()); 1998 } 1999 try { 2000 String someNumber = "0119"; 2001 phoneUtil.parse(someNumber, RegionCode.US); 2002 fail("Only IDD provided and then 9 - should fail."); 2003 } catch (NumberParseException e) { 2004 // Expected this exception. 2005 assertEquals("Wrong error type stored in exception.", 2006 NumberParseException.ErrorType.TOO_SHORT_AFTER_IDD, 2007 e.getErrorType()); 2008 } 2009 try { 2010 String emptyNumber = ""; 2011 // Invalid region. 2012 phoneUtil.parse(emptyNumber, RegionCode.ZZ); 2013 fail("Empty string - should fail."); 2014 } catch (NumberParseException e) { 2015 // Expected this exception. 2016 assertEquals("Wrong error type stored in exception.", 2017 NumberParseException.ErrorType.NOT_A_NUMBER, 2018 e.getErrorType()); 2019 } 2020 try { 2021 String nullNumber = null; 2022 // Invalid region. 2023 phoneUtil.parse(nullNumber, RegionCode.ZZ); 2024 fail("Null string - should fail."); 2025 } catch (NumberParseException e) { 2026 // Expected this exception. 2027 assertEquals("Wrong error type stored in exception.", 2028 NumberParseException.ErrorType.NOT_A_NUMBER, 2029 e.getErrorType()); 2030 } catch (NullPointerException e) { 2031 fail("Null string - but should not throw a null pointer exception."); 2032 } 2033 try { 2034 String nullNumber = null; 2035 phoneUtil.parse(nullNumber, RegionCode.US); 2036 fail("Null string - should fail."); 2037 } catch (NumberParseException e) { 2038 // Expected this exception. 2039 assertEquals("Wrong error type stored in exception.", 2040 NumberParseException.ErrorType.NOT_A_NUMBER, 2041 e.getErrorType()); 2042 } catch (NullPointerException e) { 2043 fail("Null string - but should not throw a null pointer exception."); 2044 } 2045 try { 2046 String domainRfcPhoneContext = "tel:555-1234;phone-context=www.google.com"; 2047 phoneUtil.parse(domainRfcPhoneContext, RegionCode.ZZ); 2048 fail("'Unknown' region code not allowed: should fail."); 2049 } catch (NumberParseException e) { 2050 // Expected this exception. 2051 assertEquals("Wrong error type stored in exception.", 2052 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 2053 e.getErrorType()); 2054 } 2055 try { 2056 // This is invalid because no "+" sign is present as part of phone-context. This should not 2057 // succeed in being parsed. 2058 String invalidRfcPhoneContext = "tel:555-1234;phone-context=1-331"; 2059 phoneUtil.parse(invalidRfcPhoneContext, RegionCode.ZZ); 2060 fail("'Unknown' region code not allowed: should fail."); 2061 } catch (NumberParseException e) { 2062 // Expected this exception. 2063 assertEquals("Wrong error type stored in exception.", 2064 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 2065 e.getErrorType()); 2066 } 2067 } 2068 2069 public void testParseNumbersWithPlusWithNoRegion() throws Exception { 2070 // RegionCode.ZZ is allowed only if the number starts with a '+' - then the country calling code 2071 // can be calculated. 2072 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", RegionCode.ZZ)); 2073 // Test with full-width plus. 2074 assertEquals(NZ_NUMBER, phoneUtil.parse("\uFF0B64 3 331 6005", RegionCode.ZZ)); 2075 // Test with normal plus but leading characters that need to be stripped. 2076 assertEquals(NZ_NUMBER, phoneUtil.parse("Tel: +64 3 331 6005", RegionCode.ZZ)); 2077 assertEquals(NZ_NUMBER, phoneUtil.parse("+64 3 331 6005", null)); 2078 assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("+800 1234 5678", null)); 2079 assertEquals(UNIVERSAL_PREMIUM_RATE, phoneUtil.parse("+979 123 456 789", null)); 2080 2081 // Test parsing RFC3966 format with a phone context. 2082 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); 2083 assertEquals(NZ_NUMBER, phoneUtil.parse(" tel:03-331-6005;phone-context=+64", RegionCode.ZZ)); 2084 assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", 2085 RegionCode.ZZ)); 2086 2087 // It is important that we set the carrier code to an empty string, since we used 2088 // ParseAndKeepRawInput and no carrier code was found. 2089 PhoneNumber nzNumberWithRawInput = new PhoneNumber().mergeFrom(NZ_NUMBER). 2090 setRawInput("+64 3 331 6005"). 2091 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN). 2092 setPreferredDomesticCarrierCode(""); 2093 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", 2094 RegionCode.ZZ)); 2095 // Null is also allowed for the region code in these cases. 2096 assertEquals(nzNumberWithRawInput, phoneUtil.parseAndKeepRawInput("+64 3 331 6005", null)); 2097 } 2098 2099 public void testParseExtensions() throws Exception { 2100 PhoneNumber nzNumber = new PhoneNumber(); 2101 nzNumber.setCountryCode(64).setNationalNumber(33316005L).setExtension("3456"); 2102 assertEquals(nzNumber, phoneUtil.parse("03 331 6005 ext 3456", RegionCode.NZ)); 2103 assertEquals(nzNumber, phoneUtil.parse("03-3316005x3456", RegionCode.NZ)); 2104 assertEquals(nzNumber, phoneUtil.parse("03-3316005 int.3456", RegionCode.NZ)); 2105 assertEquals(nzNumber, phoneUtil.parse("03 3316005 #3456", RegionCode.NZ)); 2106 // Test the following do not extract extensions: 2107 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 six-flags", RegionCode.US)); 2108 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("1800 SIX FLAGS", RegionCode.US)); 2109 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("0~0 1800 7493 5247", RegionCode.PL)); 2110 assertEquals(ALPHA_NUMERIC_NUMBER, phoneUtil.parse("(1800) 7493.5247", RegionCode.US)); 2111 // Check that the last instance of an extension token is matched. 2112 PhoneNumber extnNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER).setExtension("1234"); 2113 assertEquals(extnNumber, phoneUtil.parse("0~0 1800 7493 5247 ~1234", RegionCode.PL)); 2114 // Verifying bug-fix where the last digit of a number was previously omitted if it was a 0 when 2115 // extracting the extension. Also verifying a few different cases of extensions. 2116 PhoneNumber ukNumber = new PhoneNumber(); 2117 ukNumber.setCountryCode(44).setNationalNumber(2034567890L).setExtension("456"); 2118 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.NZ)); 2119 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890x456", RegionCode.GB)); 2120 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x456", RegionCode.GB)); 2121 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X456", RegionCode.GB)); 2122 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 2123 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 2124 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 x 456 ", RegionCode.GB)); 2125 assertEquals(ukNumber, phoneUtil.parse("+44 2034567890 X 456", RegionCode.GB)); 2126 assertEquals(ukNumber, phoneUtil.parse("+44-2034567890;ext=456", RegionCode.GB)); 2127 assertEquals(ukNumber, phoneUtil.parse("tel:2034567890;ext=456;phone-context=+44", 2128 RegionCode.ZZ)); 2129 // Full-width extension, "extn" only. 2130 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF45\uFF58\uFF54\uFF4E456", 2131 RegionCode.GB)); 2132 // "xtn" only. 2133 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54\uFF4E456", 2134 RegionCode.GB)); 2135 // "xt" only. 2136 assertEquals(ukNumber, phoneUtil.parse("+442034567890\uFF58\uFF54456", 2137 RegionCode.GB)); 2138 2139 PhoneNumber usWithExtension = new PhoneNumber(); 2140 usWithExtension.setCountryCode(1).setNationalNumber(8009013355L).setExtension("7246433"); 2141 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 x 7246433", RegionCode.US)); 2142 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , ext 7246433", RegionCode.US)); 2143 assertEquals(usWithExtension, 2144 phoneUtil.parse("(800) 901-3355 ,extension 7246433", RegionCode.US)); 2145 assertEquals(usWithExtension, 2146 phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", RegionCode.US)); 2147 // Repeat with the small letter o with acute accent created by combining characters. 2148 assertEquals(usWithExtension, 2149 phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", RegionCode.US)); 2150 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", RegionCode.US)); 2151 assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", RegionCode.US)); 2152 2153 // Test that if a number has two extensions specified, we ignore the second. 2154 PhoneNumber usWithTwoExtensionsNumber = new PhoneNumber(); 2155 usWithTwoExtensionsNumber.setCountryCode(1).setNationalNumber(2121231234L).setExtension("508"); 2156 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/x1234", 2157 RegionCode.US)); 2158 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508/ x1234", 2159 RegionCode.US)); 2160 assertEquals(usWithTwoExtensionsNumber, phoneUtil.parse("(212)123-1234 x508\\x1234", 2161 RegionCode.US)); 2162 2163 // Test parsing numbers in the form (645) 123-1234-910# works, where the last 3 digits before 2164 // the # are an extension. 2165 usWithExtension.clear(); 2166 usWithExtension.setCountryCode(1).setNationalNumber(6451231234L).setExtension("910"); 2167 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234-910#", RegionCode.US)); 2168 // Retry with the same number in a slightly different format. 2169 assertEquals(usWithExtension, phoneUtil.parse("+1 (645) 123 1234 ext. 910#", RegionCode.US)); 2170 } 2171 2172 public void testParseAndKeepRaw() throws Exception { 2173 PhoneNumber alphaNumericNumber = new PhoneNumber().mergeFrom(ALPHA_NUMERIC_NUMBER). 2174 setRawInput("800 six-flags"). 2175 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). 2176 setPreferredDomesticCarrierCode(""); 2177 assertEquals(alphaNumericNumber, 2178 phoneUtil.parseAndKeepRawInput("800 six-flags", RegionCode.US)); 2179 2180 PhoneNumber shorterAlphaNumber = new PhoneNumber(). 2181 setCountryCode(1).setNationalNumber(8007493524L). 2182 setRawInput("1800 six-flag"). 2183 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN). 2184 setPreferredDomesticCarrierCode(""); 2185 assertEquals(shorterAlphaNumber, 2186 phoneUtil.parseAndKeepRawInput("1800 six-flag", RegionCode.US)); 2187 2188 shorterAlphaNumber.setRawInput("+1800 six-flag"). 2189 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN); 2190 assertEquals(shorterAlphaNumber, 2191 phoneUtil.parseAndKeepRawInput("+1800 six-flag", RegionCode.NZ)); 2192 2193 shorterAlphaNumber.setRawInput("001800 six-flag"). 2194 setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_IDD); 2195 assertEquals(shorterAlphaNumber, 2196 phoneUtil.parseAndKeepRawInput("001800 six-flag", RegionCode.NZ)); 2197 2198 // Invalid region code supplied. 2199 try { 2200 phoneUtil.parseAndKeepRawInput("123 456 7890", RegionCode.CS); 2201 fail("Deprecated region code not allowed: should fail."); 2202 } catch (NumberParseException e) { 2203 // Expected this exception. 2204 assertEquals("Wrong error type stored in exception.", 2205 NumberParseException.ErrorType.INVALID_COUNTRY_CODE, 2206 e.getErrorType()); 2207 } 2208 2209 PhoneNumber koreanNumber = new PhoneNumber(); 2210 koreanNumber.setCountryCode(82).setNationalNumber(22123456).setRawInput("08122123456"). 2211 setCountryCodeSource(CountryCodeSource.FROM_DEFAULT_COUNTRY). 2212 setPreferredDomesticCarrierCode("81"); 2213 assertEquals(koreanNumber, phoneUtil.parseAndKeepRawInput("08122123456", RegionCode.KR)); 2214 } 2215 2216 public void testCountryWithNoNumberDesc() { 2217 // Andorra is a country where we don't have PhoneNumberDesc info in the metadata. 2218 PhoneNumber adNumber = new PhoneNumber(); 2219 adNumber.setCountryCode(376).setNationalNumber(12345L); 2220 assertEquals("+376 12345", phoneUtil.format(adNumber, PhoneNumberFormat.INTERNATIONAL)); 2221 assertEquals("+37612345", phoneUtil.format(adNumber, PhoneNumberFormat.E164)); 2222 assertEquals("12345", phoneUtil.format(adNumber, PhoneNumberFormat.NATIONAL)); 2223 assertEquals(PhoneNumberUtil.PhoneNumberType.UNKNOWN, phoneUtil.getNumberType(adNumber)); 2224 assertTrue(phoneUtil.isValidNumber(adNumber)); 2225 2226 // Test dialing a US number from within Andorra. 2227 assertEquals("00 1 650 253 0000", 2228 phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.AD)); 2229 } 2230 2231 public void testUnknownCountryCallingCode() { 2232 assertFalse(phoneUtil.isValidNumber(UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT)); 2233 // It's not very well defined as to what the E164 representation for a number with an invalid 2234 // country calling code is, but just prefixing the country code and national number is about 2235 // the best we can do. 2236 assertEquals("+212345", 2237 phoneUtil.format(UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT, PhoneNumberFormat.E164)); 2238 } 2239 2240 public void testIsNumberMatchMatches() throws Exception { 2241 // Test simple matches where formatting is different, or leading zeroes, or country calling code 2242 // has been specified. 2243 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2244 phoneUtil.isNumberMatch("+64 3 331 6005", "+64 03 331 6005")); 2245 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2246 phoneUtil.isNumberMatch("+800 1234 5678", "+80012345678")); 2247 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2248 phoneUtil.isNumberMatch("+64 03 331-6005", "+64 03331 6005")); 2249 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2250 phoneUtil.isNumberMatch("+643 331-6005", "+64033316005")); 2251 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2252 phoneUtil.isNumberMatch("+643 331-6005", "+6433316005")); 2253 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2254 phoneUtil.isNumberMatch("+64 3 331-6005", "+6433316005")); 2255 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2256 phoneUtil.isNumberMatch("+64 3 331-6005", "tel:+64-3-331-6005;isub=123")); 2257 // Test alpha numbers. 2258 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2259 phoneUtil.isNumberMatch("+1800 siX-Flags", "+1 800 7493 5247")); 2260 // Test numbers with extensions. 2261 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2262 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "+6433316005#1234")); 2263 // Test proto buffers. 2264 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2265 phoneUtil.isNumberMatch(NZ_NUMBER, "+6403 331 6005")); 2266 2267 PhoneNumber nzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER).setExtension("3456"); 2268 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2269 phoneUtil.isNumberMatch(nzNumber, "+643 331 6005 ext 3456")); 2270 // Check empty extensions are ignored. 2271 nzNumber.setExtension(""); 2272 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2273 phoneUtil.isNumberMatch(nzNumber, "+6403 331 6005")); 2274 // Check variant with two proto buffers. 2275 assertEquals("Number " + nzNumber.toString() + " did not match " + NZ_NUMBER.toString(), 2276 PhoneNumberUtil.MatchType.EXACT_MATCH, 2277 phoneUtil.isNumberMatch(nzNumber, NZ_NUMBER)); 2278 2279 // Check raw_input, country_code_source and preferred_domestic_carrier_code are ignored. 2280 PhoneNumber brNumberOne = new PhoneNumber(); 2281 PhoneNumber brNumberTwo = new PhoneNumber(); 2282 brNumberOne.setCountryCode(55).setNationalNumber(3121286979L) 2283 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN) 2284 .setPreferredDomesticCarrierCode("12").setRawInput("012 3121286979"); 2285 brNumberTwo.setCountryCode(55).setNationalNumber(3121286979L) 2286 .setCountryCodeSource(PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY) 2287 .setPreferredDomesticCarrierCode("14").setRawInput("143121286979"); 2288 assertEquals(PhoneNumberUtil.MatchType.EXACT_MATCH, 2289 phoneUtil.isNumberMatch(brNumberOne, brNumberTwo)); 2290 } 2291 2292 public void testIsNumberMatchNonMatches() throws Exception { 2293 // Non-matches. 2294 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2295 phoneUtil.isNumberMatch("03 331 6005", "03 331 6006")); 2296 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2297 phoneUtil.isNumberMatch("+800 1234 5678", "+1 800 1234 5678")); 2298 // Different country calling code, partial number match. 2299 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2300 phoneUtil.isNumberMatch("+64 3 331-6005", "+16433316005")); 2301 // Different country calling code, same number. 2302 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2303 phoneUtil.isNumberMatch("+64 3 331-6005", "+6133316005")); 2304 // Extension different, all else the same. 2305 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2306 phoneUtil.isNumberMatch("+64 3 331-6005 extn 1234", "0116433316005#1235")); 2307 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2308 phoneUtil.isNumberMatch( 2309 "+64 3 331-6005 extn 1234", "tel:+64-3-331-6005;ext=1235")); 2310 // NSN matches, but extension is different - not the same number. 2311 assertEquals(PhoneNumberUtil.MatchType.NO_MATCH, 2312 phoneUtil.isNumberMatch("+64 3 331-6005 ext.1235", "3 331 6005#1234")); 2313 2314 // Invalid numbers that can't be parsed. 2315 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 2316 phoneUtil.isNumberMatch("4", "3 331 6043")); 2317 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 2318 phoneUtil.isNumberMatch("+43", "+64 3 331 6005")); 2319 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 2320 phoneUtil.isNumberMatch("+43", "64 3 331 6005")); 2321 assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, 2322 phoneUtil.isNumberMatch("Dog", "64 3 331 6005")); 2323 } 2324 2325 public void testIsNumberMatchNsnMatches() throws Exception { 2326 // NSN matches. 2327 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2328 phoneUtil.isNumberMatch("+64 3 331-6005", "03 331 6005")); 2329 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2330 phoneUtil.isNumberMatch( 2331 "+64 3 331-6005", "tel:03-331-6005;isub=1234;phone-context=abc.nz")); 2332 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2333 phoneUtil.isNumberMatch(NZ_NUMBER, "03 331 6005")); 2334 // Here the second number possibly starts with the country calling code for New Zealand, 2335 // although we are unsure. 2336 PhoneNumber unchangedNzNumber = new PhoneNumber().mergeFrom(NZ_NUMBER); 2337 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2338 phoneUtil.isNumberMatch(unchangedNzNumber, "(64-3) 331 6005")); 2339 // Check the phone number proto was not edited during the method call. 2340 assertEquals(NZ_NUMBER, unchangedNzNumber); 2341 2342 // Here, the 1 might be a national prefix, if we compare it to the US number, so the resultant 2343 // match is an NSN match. 2344 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2345 phoneUtil.isNumberMatch(US_NUMBER, "1-650-253-0000")); 2346 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2347 phoneUtil.isNumberMatch(US_NUMBER, "6502530000")); 2348 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2349 phoneUtil.isNumberMatch("+1 650-253 0000", "1 650 253 0000")); 2350 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2351 phoneUtil.isNumberMatch("1 650-253 0000", "1 650 253 0000")); 2352 assertEquals(PhoneNumberUtil.MatchType.NSN_MATCH, 2353 phoneUtil.isNumberMatch("1 650-253 0000", "+1 650 253 0000")); 2354 // For this case, the match will be a short NSN match, because we cannot assume that the 1 might 2355 // be a national prefix, so don't remove it when parsing. 2356 PhoneNumber randomNumber = new PhoneNumber(); 2357 randomNumber.setCountryCode(41).setNationalNumber(6502530000L); 2358 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2359 phoneUtil.isNumberMatch(randomNumber, "1-650-253-0000")); 2360 } 2361 2362 public void testIsNumberMatchShortNsnMatches() throws Exception { 2363 // Short NSN matches with the country not specified for either one or both numbers. 2364 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2365 phoneUtil.isNumberMatch("+64 3 331-6005", "331 6005")); 2366 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2367 phoneUtil.isNumberMatch("+64 3 331-6005", "tel:331-6005;phone-context=abc.nz")); 2368 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2369 phoneUtil.isNumberMatch("+64 3 331-6005", 2370 "tel:331-6005;isub=1234;phone-context=abc.nz")); 2371 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2372 phoneUtil.isNumberMatch("+64 3 331-6005", 2373 "tel:331-6005;isub=1234;phone-context=abc.nz;a=%A1")); 2374 // We did not know that the "0" was a national prefix since neither number has a country code, 2375 // so this is considered a SHORT_NSN_MATCH. 2376 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2377 phoneUtil.isNumberMatch("3 331-6005", "03 331 6005")); 2378 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2379 phoneUtil.isNumberMatch("3 331-6005", "331 6005")); 2380 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2381 phoneUtil.isNumberMatch("3 331-6005", "tel:331-6005;phone-context=abc.nz")); 2382 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2383 phoneUtil.isNumberMatch("3 331-6005", "+64 331 6005")); 2384 // Short NSN match with the country specified. 2385 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2386 phoneUtil.isNumberMatch("03 331-6005", "331 6005")); 2387 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2388 phoneUtil.isNumberMatch("1 234 345 6789", "345 6789")); 2389 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2390 phoneUtil.isNumberMatch("+1 (234) 345 6789", "345 6789")); 2391 // NSN matches, country calling code omitted for one number, extension missing for one. 2392 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2393 phoneUtil.isNumberMatch("+64 3 331-6005", "3 331 6005#1234")); 2394 // One has Italian leading zero, one does not. 2395 PhoneNumber italianNumberOne = new PhoneNumber(); 2396 italianNumberOne.setCountryCode(39).setNationalNumber(1234L).setItalianLeadingZero(true); 2397 PhoneNumber italianNumberTwo = new PhoneNumber(); 2398 italianNumberTwo.setCountryCode(39).setNationalNumber(1234L); 2399 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2400 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo)); 2401 // One has an extension, the other has an extension of "". 2402 italianNumberOne.setExtension("1234").clearItalianLeadingZero(); 2403 italianNumberTwo.setExtension(""); 2404 assertEquals(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, 2405 phoneUtil.isNumberMatch(italianNumberOne, italianNumberTwo)); 2406 } 2407 2408 public void testCanBeInternationallyDialled() throws Exception { 2409 // We have no-international-dialling rules for the US in our test metadata that say that 2410 // toll-free numbers cannot be dialled internationally. 2411 assertFalse(phoneUtil.canBeInternationallyDialled(US_TOLLFREE)); 2412 2413 // Normal US numbers can be internationally dialled. 2414 assertTrue(phoneUtil.canBeInternationallyDialled(US_NUMBER)); 2415 2416 // Invalid number. 2417 assertTrue(phoneUtil.canBeInternationallyDialled(US_LOCAL_NUMBER)); 2418 2419 // We have no data for NZ - should return true. 2420 assertTrue(phoneUtil.canBeInternationallyDialled(NZ_NUMBER)); 2421 assertTrue(phoneUtil.canBeInternationallyDialled(INTERNATIONAL_TOLL_FREE)); 2422 } 2423 2424 public void testIsAlphaNumber() throws Exception { 2425 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags")); 2426 assertTrue(phoneUtil.isAlphaNumber("1800 six-flags ext. 1234")); 2427 assertTrue(phoneUtil.isAlphaNumber("+800 six-flags")); 2428 assertTrue(phoneUtil.isAlphaNumber("180 six-flags")); 2429 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234")); 2430 assertFalse(phoneUtil.isAlphaNumber("1 six-flags")); 2431 assertFalse(phoneUtil.isAlphaNumber("18 six-flags")); 2432 assertFalse(phoneUtil.isAlphaNumber("1800 123-1234 extension: 1234")); 2433 assertFalse(phoneUtil.isAlphaNumber("+800 1234-1234")); 2434 } 2435 } 2436