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.android.i18n.phonenumbers;
     18 
     19 import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
     20 import com.android.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
     21 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.util.ArrayList;
     26 import java.util.EnumSet;
     27 import java.util.List;
     28 import java.util.Set;
     29 import java.util.logging.Level;
     30 import java.util.logging.Logger;
     31 
     32 /**
     33  * @author Lara Rennie
     34  *
     35  * Verifies all of the example numbers in the metadata are valid and of the correct type. If no
     36  * example number exists for a particular type, the test still passes.
     37  */
     38 public class ExampleNumbersTest extends TestCase {
     39   private static final Logger LOGGER = Logger.getLogger(ExampleNumbersTest.class.getName());
     40   private PhoneNumberUtil phoneNumberUtil;
     41   private List<PhoneNumber> invalidCases = new ArrayList<PhoneNumber>();
     42   private List<PhoneNumber> wrongTypeCases = new ArrayList<PhoneNumber>();
     43 
     44   public ExampleNumbersTest() {
     45     PhoneNumberUtil.resetInstance();
     46     phoneNumberUtil = PhoneNumberUtil.getInstance();
     47   }
     48 
     49   @Override
     50   protected void setUp() throws Exception {
     51     super.setUp();
     52     invalidCases.clear();
     53     wrongTypeCases.clear();
     54   }
     55 
     56   @Override
     57   protected void tearDown() throws Exception {
     58     super.tearDown();
     59   }
     60 
     61   /**
     62    * @param exampleNumberRequestedType  type we are requesting an example number for
     63    * @param possibleExpectedTypes       acceptable types that this number should match, such as
     64    *     FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
     65    */
     66   private void checkNumbersValidAndCorrectType(PhoneNumberType exampleNumberRequestedType,
     67                                                Set<PhoneNumberType> possibleExpectedTypes) {
     68     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
     69       PhoneNumber exampleNumber =
     70           phoneNumberUtil.getExampleNumberForType(regionCode, exampleNumberRequestedType);
     71       if (exampleNumber != null) {
     72         if (!phoneNumberUtil.isValidNumber(exampleNumber)) {
     73           invalidCases.add(exampleNumber);
     74           LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
     75         } else {
     76           // We know the number is valid, now we check the type.
     77           PhoneNumberType exampleNumberType = phoneNumberUtil.getNumberType(exampleNumber);
     78           if (!possibleExpectedTypes.contains(exampleNumberType)) {
     79             wrongTypeCases.add(exampleNumber);
     80             LOGGER.log(Level.SEVERE, "Wrong type for " +
     81                        exampleNumber.toString() +
     82                        ": got " + exampleNumberType);
     83             LOGGER.log(Level.WARNING, "Expected types: ");
     84             for (PhoneNumberType type : possibleExpectedTypes) {
     85               LOGGER.log(Level.WARNING, type.toString());
     86             }
     87           }
     88         }
     89       }
     90     }
     91   }
     92 
     93   public void testFixedLine() throws Exception {
     94     Set<PhoneNumberType> fixedLineTypes = EnumSet.of(PhoneNumberType.FIXED_LINE,
     95                                                      PhoneNumberType.FIXED_LINE_OR_MOBILE);
     96     checkNumbersValidAndCorrectType(PhoneNumberType.FIXED_LINE, fixedLineTypes);
     97     assertEquals(0, invalidCases.size());
     98     assertEquals(0, wrongTypeCases.size());
     99   }
    100 
    101   public void testMobile() throws Exception {
    102     Set<PhoneNumberType> mobileTypes = EnumSet.of(PhoneNumberType.MOBILE,
    103                                                   PhoneNumberType.FIXED_LINE_OR_MOBILE);
    104     checkNumbersValidAndCorrectType(PhoneNumberType.MOBILE, mobileTypes);
    105     assertEquals(0, invalidCases.size());
    106     assertEquals(0, wrongTypeCases.size());
    107   }
    108 
    109   public void testTollFree() throws Exception {
    110     Set<PhoneNumberType> tollFreeTypes = EnumSet.of(PhoneNumberType.TOLL_FREE);
    111     checkNumbersValidAndCorrectType(PhoneNumberType.TOLL_FREE, tollFreeTypes);
    112     assertEquals(0, invalidCases.size());
    113     assertEquals(0, wrongTypeCases.size());
    114   }
    115 
    116   public void testPremiumRate() throws Exception {
    117     Set<PhoneNumberType> premiumRateTypes = EnumSet.of(PhoneNumberType.PREMIUM_RATE);
    118     checkNumbersValidAndCorrectType(PhoneNumberType.PREMIUM_RATE, premiumRateTypes);
    119     assertEquals(0, invalidCases.size());
    120     assertEquals(0, wrongTypeCases.size());
    121   }
    122 
    123   public void testVoip() throws Exception {
    124     Set<PhoneNumberType> voipTypes = EnumSet.of(PhoneNumberType.VOIP);
    125     checkNumbersValidAndCorrectType(PhoneNumberType.VOIP, voipTypes);
    126     assertEquals(0, invalidCases.size());
    127     assertEquals(0, wrongTypeCases.size());
    128   }
    129 
    130   public void testPager() throws Exception {
    131     Set<PhoneNumberType> pagerTypes = EnumSet.of(PhoneNumberType.PAGER);
    132     checkNumbersValidAndCorrectType(PhoneNumberType.PAGER, pagerTypes);
    133     assertEquals(0, invalidCases.size());
    134     assertEquals(0, wrongTypeCases.size());
    135   }
    136 
    137   public void testUan() throws Exception {
    138     Set<PhoneNumberType> uanTypes = EnumSet.of(PhoneNumberType.UAN);
    139     checkNumbersValidAndCorrectType(PhoneNumberType.UAN, uanTypes);
    140     assertEquals(0, invalidCases.size());
    141     assertEquals(0, wrongTypeCases.size());
    142   }
    143 
    144   public void testVoicemail() throws Exception {
    145     Set<PhoneNumberType> voicemailTypes = EnumSet.of(PhoneNumberType.VOICEMAIL);
    146     checkNumbersValidAndCorrectType(PhoneNumberType.VOICEMAIL, voicemailTypes);
    147     assertEquals(0, invalidCases.size());
    148     assertEquals(0, wrongTypeCases.size());
    149   }
    150 
    151   public void testSharedCost() throws Exception {
    152     Set<PhoneNumberType> sharedCostTypes = EnumSet.of(PhoneNumberType.SHARED_COST);
    153     checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes);
    154     assertEquals(0, invalidCases.size());
    155     assertEquals(0, wrongTypeCases.size());
    156   }
    157 
    158   public void testCanBeInternationallyDialled() throws Exception {
    159     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
    160       PhoneNumber exampleNumber = null;
    161       PhoneNumberDesc desc =
    162           phoneNumberUtil.getMetadataForRegion(regionCode).getNoInternationalDialling();
    163       try {
    164         if (desc.hasExampleNumber()) {
    165           exampleNumber = phoneNumberUtil.parse(desc.getExampleNumber(), regionCode);
    166         }
    167       } catch (NumberParseException e) {
    168         LOGGER.log(Level.SEVERE, e.toString());
    169       }
    170       if (exampleNumber != null && phoneNumberUtil.canBeInternationallyDialled(exampleNumber)) {
    171         wrongTypeCases.add(exampleNumber);
    172         LOGGER.log(Level.SEVERE, "Number " + exampleNumber.toString()
    173                    + " should not be internationally diallable");
    174       }
    175     }
    176     assertEquals(0, wrongTypeCases.size());
    177   }
    178 
    179   public void testEmergency() throws Exception {
    180     ShortNumberUtil shortUtil = new ShortNumberUtil(phoneNumberUtil);
    181     int wrongTypeCounter = 0;
    182     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
    183       PhoneNumberDesc desc =
    184           phoneNumberUtil.getMetadataForRegion(regionCode).getEmergency();
    185       if (desc.hasExampleNumber()) {
    186         String exampleNumber = desc.getExampleNumber();
    187         if (!exampleNumber.matches(desc.getPossibleNumberPattern()) ||
    188             !shortUtil.isEmergencyNumber(exampleNumber, regionCode)) {
    189           wrongTypeCounter++;
    190           LOGGER.log(Level.SEVERE, "Emergency example number test failed for " + regionCode);
    191         }
    192       }
    193     }
    194     assertEquals(0, wrongTypeCounter);
    195   }
    196 
    197   public void testGlobalNetworkNumbers() throws Exception {
    198     for (Integer callingCode : phoneNumberUtil.getSupportedGlobalNetworkCallingCodes()) {
    199       PhoneNumber exampleNumber =
    200           phoneNumberUtil.getExampleNumberForNonGeoEntity(callingCode);
    201       assertNotNull("No example phone number for calling code " + callingCode, exampleNumber);
    202       if (!phoneNumberUtil.isValidNumber(exampleNumber)) {
    203         invalidCases.add(exampleNumber);
    204         LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
    205       }
    206     }
    207   }
    208 
    209   public void testEveryRegionHasAnExampleNumber() throws Exception {
    210     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
    211       PhoneNumber exampleNumber = phoneNumberUtil.getExampleNumber(regionCode);
    212       assertNotNull("None found for region " + regionCode, exampleNumber);
    213     }
    214   }
    215 }
    216