1 /* 2 * Copyright (C) 2009 Google Inc. 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 testSharedCost() throws Exception { 145 Set<PhoneNumberType> sharedCostTypes = EnumSet.of(PhoneNumberType.SHARED_COST); 146 checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes); 147 assertEquals(0, invalidCases.size()); 148 assertEquals(0, wrongTypeCases.size()); 149 } 150 151 public void testCanBeInternationallyDialled() throws Exception { 152 for (String regionCode : phoneNumberUtil.getSupportedRegions()) { 153 PhoneNumber exampleNumber = null; 154 PhoneNumberDesc desc = 155 phoneNumberUtil.getMetadataForRegion(regionCode).getNoInternationalDialling(); 156 try { 157 if (desc.hasExampleNumber()) { 158 exampleNumber = phoneNumberUtil.parse(desc.getExampleNumber(), regionCode); 159 } 160 } catch (NumberParseException e) { 161 LOGGER.log(Level.SEVERE, e.toString()); 162 } 163 if (exampleNumber != null && phoneNumberUtil.canBeInternationallyDialled(exampleNumber)) { 164 wrongTypeCases.add(exampleNumber); 165 } 166 } 167 assertEquals(0, wrongTypeCases.size()); 168 } 169 } 170