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