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