• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Verifies all of the example numbers in the metadata are valid and of the correct type. If no
34  * example number exists for a particular type, the test still passes.
35  */
36 public class ExampleNumbersTest extends TestCase {
37   private static final Logger LOGGER = Logger.getLogger(ExampleNumbersTest.class.getName());
38   private PhoneNumberUtil phoneNumberUtil =
39       PhoneNumberUtil.createInstance(PhoneNumberUtil.DEFAULT_METADATA_LOADER);
40   private ShortNumberInfo shortNumberInfo = new ShortNumberInfo(phoneNumberUtil);
41   private List<PhoneNumber> invalidCases = new ArrayList<PhoneNumber>();
42   private List<PhoneNumber> wrongTypeCases = new ArrayList<PhoneNumber>();
43 
44   /**
45    * @param exampleNumberRequestedType  type we are requesting an example number for
46    * @param possibleExpectedTypes       acceptable types that this number should match, such as
47    *     FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
48    */
checkNumbersValidAndCorrectType(PhoneNumberType exampleNumberRequestedType, Set<PhoneNumberType> possibleExpectedTypes)49   private void checkNumbersValidAndCorrectType(PhoneNumberType exampleNumberRequestedType,
50                                                Set<PhoneNumberType> possibleExpectedTypes) {
51     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
52       PhoneNumber exampleNumber =
53           phoneNumberUtil.getExampleNumberForType(regionCode, exampleNumberRequestedType);
54       if (exampleNumber != null) {
55         if (!phoneNumberUtil.isValidNumber(exampleNumber)) {
56           invalidCases.add(exampleNumber);
57           LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
58         } else {
59           // We know the number is valid, now we check the type.
60           PhoneNumberType exampleNumberType = phoneNumberUtil.getNumberType(exampleNumber);
61           if (!possibleExpectedTypes.contains(exampleNumberType)) {
62             wrongTypeCases.add(exampleNumber);
63             LOGGER.log(Level.SEVERE, "Wrong type for " +
64                        exampleNumber.toString() +
65                        ": got " + exampleNumberType);
66             LOGGER.log(Level.WARNING, "Expected types: ");
67             for (PhoneNumberType type : possibleExpectedTypes) {
68               LOGGER.log(Level.WARNING, type.toString());
69             }
70           }
71         }
72       }
73     }
74   }
75 
testFixedLine()76   public void testFixedLine() throws Exception {
77     Set<PhoneNumberType> fixedLineTypes = EnumSet.of(PhoneNumberType.FIXED_LINE,
78                                                      PhoneNumberType.FIXED_LINE_OR_MOBILE);
79     checkNumbersValidAndCorrectType(PhoneNumberType.FIXED_LINE, fixedLineTypes);
80     assertEquals(0, invalidCases.size());
81     assertEquals(0, wrongTypeCases.size());
82   }
83 
testMobile()84   public void testMobile() throws Exception {
85     Set<PhoneNumberType> mobileTypes = EnumSet.of(PhoneNumberType.MOBILE,
86                                                   PhoneNumberType.FIXED_LINE_OR_MOBILE);
87     checkNumbersValidAndCorrectType(PhoneNumberType.MOBILE, mobileTypes);
88     assertEquals(0, invalidCases.size());
89     assertEquals(0, wrongTypeCases.size());
90   }
91 
testTollFree()92   public void testTollFree() throws Exception {
93     Set<PhoneNumberType> tollFreeTypes = EnumSet.of(PhoneNumberType.TOLL_FREE);
94     checkNumbersValidAndCorrectType(PhoneNumberType.TOLL_FREE, tollFreeTypes);
95     assertEquals(0, invalidCases.size());
96     assertEquals(0, wrongTypeCases.size());
97   }
98 
testPremiumRate()99   public void testPremiumRate() throws Exception {
100     Set<PhoneNumberType> premiumRateTypes = EnumSet.of(PhoneNumberType.PREMIUM_RATE);
101     checkNumbersValidAndCorrectType(PhoneNumberType.PREMIUM_RATE, premiumRateTypes);
102     assertEquals(0, invalidCases.size());
103     assertEquals(0, wrongTypeCases.size());
104   }
105 
testVoip()106   public void testVoip() throws Exception {
107     Set<PhoneNumberType> voipTypes = EnumSet.of(PhoneNumberType.VOIP);
108     checkNumbersValidAndCorrectType(PhoneNumberType.VOIP, voipTypes);
109     assertEquals(0, invalidCases.size());
110     assertEquals(0, wrongTypeCases.size());
111   }
112 
testPager()113   public void testPager() throws Exception {
114     Set<PhoneNumberType> pagerTypes = EnumSet.of(PhoneNumberType.PAGER);
115     checkNumbersValidAndCorrectType(PhoneNumberType.PAGER, pagerTypes);
116     assertEquals(0, invalidCases.size());
117     assertEquals(0, wrongTypeCases.size());
118   }
119 
testUan()120   public void testUan() throws Exception {
121     Set<PhoneNumberType> uanTypes = EnumSet.of(PhoneNumberType.UAN);
122     checkNumbersValidAndCorrectType(PhoneNumberType.UAN, uanTypes);
123     assertEquals(0, invalidCases.size());
124     assertEquals(0, wrongTypeCases.size());
125   }
126 
testVoicemail()127   public void testVoicemail() throws Exception {
128     Set<PhoneNumberType> voicemailTypes = EnumSet.of(PhoneNumberType.VOICEMAIL);
129     checkNumbersValidAndCorrectType(PhoneNumberType.VOICEMAIL, voicemailTypes);
130     assertEquals(0, invalidCases.size());
131     assertEquals(0, wrongTypeCases.size());
132   }
133 
testSharedCost()134   public void testSharedCost() throws Exception {
135     Set<PhoneNumberType> sharedCostTypes = EnumSet.of(PhoneNumberType.SHARED_COST);
136     checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes);
137     assertEquals(0, invalidCases.size());
138     assertEquals(0, wrongTypeCases.size());
139   }
140 
testCanBeInternationallyDialled()141   public void testCanBeInternationallyDialled() throws Exception {
142     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
143       PhoneNumber exampleNumber = null;
144       PhoneNumberDesc desc =
145           phoneNumberUtil.getMetadataForRegion(regionCode).getNoInternationalDialling();
146       try {
147         if (desc.hasExampleNumber()) {
148           exampleNumber = phoneNumberUtil.parse(desc.getExampleNumber(), regionCode);
149         }
150       } catch (NumberParseException e) {
151         LOGGER.log(Level.SEVERE, e.toString());
152       }
153       if (exampleNumber != null && phoneNumberUtil.canBeInternationallyDialled(exampleNumber)) {
154         wrongTypeCases.add(exampleNumber);
155         LOGGER.log(Level.SEVERE, "Number " + exampleNumber.toString()
156                    + " should not be internationally diallable");
157       }
158     }
159     assertEquals(0, wrongTypeCases.size());
160   }
161 
testGlobalNetworkNumbers()162   public void testGlobalNetworkNumbers() throws Exception {
163     for (Integer callingCode : phoneNumberUtil.getSupportedGlobalNetworkCallingCodes()) {
164       PhoneNumber exampleNumber =
165           phoneNumberUtil.getExampleNumberForNonGeoEntity(callingCode);
166       assertNotNull("No example phone number for calling code " + callingCode, exampleNumber);
167       if (!phoneNumberUtil.isValidNumber(exampleNumber)) {
168         invalidCases.add(exampleNumber);
169         LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
170       }
171     }
172     assertEquals(0, invalidCases.size());
173   }
174 
testEveryRegionHasAnExampleNumber()175   public void testEveryRegionHasAnExampleNumber() throws Exception {
176     for (String regionCode : phoneNumberUtil.getSupportedRegions()) {
177       PhoneNumber exampleNumber = phoneNumberUtil.getExampleNumber(regionCode);
178       assertNotNull("None found for region " + regionCode, exampleNumber);
179     }
180   }
181 
testShortNumbersValidAndCorrectCost()182   public void testShortNumbersValidAndCorrectCost() throws Exception {
183     List<String> invalidStringCases = new ArrayList<String>();
184     for (String regionCode : shortNumberInfo.getSupportedRegions()) {
185       String exampleShortNumber = shortNumberInfo.getExampleShortNumber(regionCode);
186       if (!shortNumberInfo.isValidShortNumberForRegion(exampleShortNumber, regionCode)) {
187         String invalidStringCase = "region_code: " + regionCode + ", national_number: " +
188             exampleShortNumber;
189         invalidStringCases.add(invalidStringCase);
190         LOGGER.log(Level.SEVERE, "Failed validation for string " + invalidStringCase);
191       }
192       PhoneNumber phoneNumber = phoneNumberUtil.parse(exampleShortNumber, regionCode);
193       if (!shortNumberInfo.isValidShortNumber(phoneNumber)) {
194         invalidCases.add(phoneNumber);
195         LOGGER.log(Level.SEVERE, "Failed validation for " + phoneNumber.toString());
196       }
197 
198       for (ShortNumberInfo.ShortNumberCost cost : ShortNumberInfo.ShortNumberCost.values()) {
199         exampleShortNumber = shortNumberInfo.getExampleShortNumberForCost(regionCode, cost);
200         if (!exampleShortNumber.equals("")) {
201           if (cost != shortNumberInfo.getExpectedCostForRegion(exampleShortNumber, regionCode)) {
202             wrongTypeCases.add(phoneNumber);
203             LOGGER.log(Level.SEVERE, "Wrong cost for " + phoneNumber.toString());
204           }
205         }
206       }
207     }
208     assertEquals(0, invalidStringCases.size());
209     assertEquals(0, invalidCases.size());
210     assertEquals(0, wrongTypeCases.size());
211   }
212 
testEmergency()213   public void testEmergency() throws Exception {
214     int wrongTypeCounter = 0;
215     for (String regionCode : shortNumberInfo.getSupportedRegions()) {
216       PhoneNumberDesc desc =
217           MetadataManager.getShortNumberMetadataForRegion(regionCode).getEmergency();
218       if (desc.hasExampleNumber()) {
219         String exampleNumber = desc.getExampleNumber();
220         if (!exampleNumber.matches(desc.getPossibleNumberPattern()) ||
221             !shortNumberInfo.isEmergencyNumber(exampleNumber, regionCode)) {
222           wrongTypeCounter++;
223           LOGGER.log(Level.SEVERE, "Emergency example number test failed for " + regionCode);
224         } else if (shortNumberInfo.getExpectedCostForRegion(exampleNumber, regionCode) !=
225                        ShortNumberInfo.ShortNumberCost.TOLL_FREE) {
226           wrongTypeCounter++;
227           LOGGER.log(Level.WARNING, "Emergency example number not toll free for " + regionCode);
228         }
229       }
230     }
231     assertEquals(0, wrongTypeCounter);
232   }
233 
testCarrierSpecificShortNumbers()234   public void testCarrierSpecificShortNumbers() throws Exception {
235     int wrongTagCounter = 0;
236     for (String regionCode : shortNumberInfo.getSupportedRegions()) {
237       // Test the carrier-specific tag.
238       PhoneNumberDesc desc =
239           MetadataManager.getShortNumberMetadataForRegion(regionCode).getCarrierSpecific();
240       if (desc.hasExampleNumber()) {
241         String exampleNumber = desc.getExampleNumber();
242         PhoneNumber carrierSpecificNumber = phoneNumberUtil.parse(exampleNumber, regionCode);
243         if (!exampleNumber.matches(desc.getPossibleNumberPattern()) ||
244             !shortNumberInfo.isCarrierSpecific(carrierSpecificNumber)) {
245           wrongTagCounter++;
246           LOGGER.log(Level.SEVERE, "Carrier-specific test failed for " + regionCode);
247         }
248       }
249       // TODO: Test other tags here.
250     }
251     assertEquals(0, wrongTagCounter);
252   }
253 }
254