1 /* 2 * Copyright (C) 2012 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.Phonenumber.PhoneNumber; 20 import junit.framework.TestCase; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 /** 26 * Unit tests for PhoneNumberToTimeZonesMapper.java 27 * 28 * @author Walter Erquinigo 29 */ 30 public class PhoneNumberToTimeZonesMapperTest extends TestCase { 31 private final PhoneNumberToTimeZonesMapper prefixTimeZonesMapper = 32 new PhoneNumberToTimeZonesMapper(TEST_MAPPING_DATA_DIRECTORY); 33 private static final String TEST_MAPPING_DATA_DIRECTORY = 34 "/com/google/i18n/phonenumbers/timezones/testing_data/"; 35 // Set up some test numbers to re-use. 36 private static final PhoneNumber AU_NUMBER = 37 new PhoneNumber().setCountryCode(61).setNationalNumber(236618300L); 38 private static final PhoneNumber CA_NUMBER = 39 new PhoneNumber().setCountryCode(1).setNationalNumber(6048406565L); 40 private static final PhoneNumber KO_NUMBER = 41 new PhoneNumber().setCountryCode(82).setNationalNumber(22123456L); 42 private static final PhoneNumber KO_INVALID_NUMBER = 43 new PhoneNumber().setCountryCode(82).setNationalNumber(1234L); 44 private static final PhoneNumber US_NUMBER1 = 45 new PhoneNumber().setCountryCode(1).setNationalNumber(6509600000L); 46 private static final PhoneNumber US_NUMBER2 = 47 new PhoneNumber().setCountryCode(1).setNationalNumber(2128120000L); 48 private static final PhoneNumber US_NUMBER3 = 49 new PhoneNumber().setCountryCode(1).setNationalNumber(6174240000L); 50 private static final PhoneNumber US_INVALID_NUMBER = 51 new PhoneNumber().setCountryCode(1).setNationalNumber(123456789L); 52 private static final PhoneNumber NUMBER_WITH_INVALID_COUNTRY_CODE = 53 new PhoneNumber().setCountryCode(999).setNationalNumber(2423651234L); 54 private static final PhoneNumber INTERNATIONAL_TOLL_FREE = 55 new PhoneNumber().setCountryCode(800).setNationalNumber(12345678L); 56 57 // NANPA time zones. 58 private static final String CHICAGO_TZ = "America/Chicago"; 59 private static final String LOS_ANGELES_TZ = "America/Los_Angeles"; 60 private static final String NEW_YORK_TZ = "America/New_York"; 61 private static final String WINNIPEG_TZ = "America/Winnipeg"; 62 // Non NANPA time zones. 63 private static final String SEOUL_TZ = "Asia/Seoul"; 64 private static final String SYDNEY_TZ = "Australia/Sydney"; 65 buildListOfTimeZones(String .... timezones)66 static List<String> buildListOfTimeZones(String ... timezones) { 67 ArrayList<String> timezonesList = new ArrayList<String>(timezones.length); 68 for (String timezone : timezones) { 69 timezonesList.add(timezone); 70 } 71 return timezonesList; 72 } 73 getNanpaTimeZonesList()74 private static List<String> getNanpaTimeZonesList() { 75 return buildListOfTimeZones(NEW_YORK_TZ, CHICAGO_TZ, WINNIPEG_TZ, LOS_ANGELES_TZ); 76 } 77 testGetTimeZonesForNumber()78 public void testGetTimeZonesForNumber() { 79 // Test with invalid numbers even when their country code prefixes exist in the mapper. 80 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 81 prefixTimeZonesMapper.getTimeZonesForNumber(US_INVALID_NUMBER)); 82 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 83 prefixTimeZonesMapper.getTimeZonesForNumber(KO_INVALID_NUMBER)); 84 // Test with valid prefixes. 85 assertEquals(buildListOfTimeZones(SYDNEY_TZ), 86 prefixTimeZonesMapper.getTimeZonesForNumber(AU_NUMBER)); 87 assertEquals(buildListOfTimeZones(SEOUL_TZ), 88 prefixTimeZonesMapper.getTimeZonesForNumber(KO_NUMBER)); 89 assertEquals(buildListOfTimeZones(WINNIPEG_TZ), 90 prefixTimeZonesMapper.getTimeZonesForNumber(CA_NUMBER)); 91 assertEquals(buildListOfTimeZones(LOS_ANGELES_TZ), 92 prefixTimeZonesMapper.getTimeZonesForNumber(US_NUMBER1)); 93 assertEquals(buildListOfTimeZones(NEW_YORK_TZ), 94 prefixTimeZonesMapper.getTimeZonesForNumber(US_NUMBER2)); 95 // Test with an invalid country code. 96 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 97 prefixTimeZonesMapper.getTimeZonesForNumber(NUMBER_WITH_INVALID_COUNTRY_CODE)); 98 // Test with a non geographical phone number. 99 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 100 prefixTimeZonesMapper.getTimeZonesForNumber(INTERNATIONAL_TOLL_FREE)); 101 } 102 testGetTimeZonesForValidNumber()103 public void testGetTimeZonesForValidNumber() { 104 // Test with invalid numbers even when their country code prefixes exist in the mapper. 105 assertEquals(getNanpaTimeZonesList(), 106 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(US_INVALID_NUMBER)); 107 assertEquals(buildListOfTimeZones(SEOUL_TZ), 108 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(KO_INVALID_NUMBER)); 109 // Test with valid prefixes. 110 assertEquals(buildListOfTimeZones(SYDNEY_TZ), 111 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(AU_NUMBER)); 112 assertEquals(buildListOfTimeZones(SEOUL_TZ), 113 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(KO_NUMBER)); 114 assertEquals(buildListOfTimeZones(WINNIPEG_TZ), 115 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(CA_NUMBER)); 116 assertEquals(buildListOfTimeZones(LOS_ANGELES_TZ), 117 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(US_NUMBER1)); 118 assertEquals(buildListOfTimeZones(NEW_YORK_TZ), 119 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber(US_NUMBER2)); 120 // Test with an invalid country code. 121 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 122 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber( 123 NUMBER_WITH_INVALID_COUNTRY_CODE)); 124 // Test with a non geographical phone number. 125 assertEquals(PhoneNumberToTimeZonesMapper.UNKNOWN_TIME_ZONE_LIST, 126 prefixTimeZonesMapper.getTimeZonesForGeographicalNumber( 127 INTERNATIONAL_TOLL_FREE)); 128 } 129 testGetTimeZonesForValidNumberSearchingAtCountryCodeLevel()130 public void testGetTimeZonesForValidNumberSearchingAtCountryCodeLevel() { 131 // Test that the country level time zones are returned when the number passed in is valid but 132 // not covered by any non-country level prefixes in the mapper. 133 assertEquals(prefixTimeZonesMapper.getTimeZonesForNumber(US_NUMBER3), 134 getNanpaTimeZonesList()); 135 } 136 } 137