Home | History | Annotate | Download | only in prefixmapper
      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.android.i18n.phonenumbers.prefixmapper;
     18 
     19 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
     20 import junit.framework.TestCase;
     21 
     22 import java.io.ByteArrayInputStream;
     23 import java.io.ByteArrayOutputStream;
     24 import java.io.IOException;
     25 import java.io.ObjectInputStream;
     26 import java.io.ObjectOutputStream;
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 import java.util.SortedMap;
     30 import java.util.TreeMap;
     31 
     32 /**
     33  * Unittests for PrefixTimeZonesMap.java
     34  */
     35 public class PrefixTimeZonesMapTest extends TestCase {
     36   private final PrefixTimeZonesMap prefixTimeZonesMapForUS = new PrefixTimeZonesMap();
     37   private final PrefixTimeZonesMap prefixTimeZonesMapForRU = new PrefixTimeZonesMap();
     38 
     39   // US time zones
     40   private static final String CHICAGO_TZ = "America/Chicago";
     41   private static final String DENVER_TZ = "America/Denver";
     42   private static final String LOS_ANGELES_TZ = "America/Los_Angeles";
     43   private static final String NEW_YORK_TZ = "America/New_York";
     44 
     45   // Russian time zones
     46   private static final String IRKUTSK_TZ = "Asia/Irkutsk";
     47   private static final String MOSCOW_TZ = "Europe/Moscow";
     48   private static final String VLADIVOSTOK_TZ = "Asia/Vladivostok";
     49   private static final String YEKATERINBURG_TZ = "Asia/Yekaterinburg";
     50 
     51   public PrefixTimeZonesMapTest() {
     52     SortedMap<Integer, String> sortedMapForUS = new TreeMap<Integer, String>();
     53     sortedMapForUS.put(1, NEW_YORK_TZ + "&" + CHICAGO_TZ + "&" + LOS_ANGELES_TZ + "&" + DENVER_TZ);
     54     sortedMapForUS.put(1201, NEW_YORK_TZ);
     55     sortedMapForUS.put(1205, CHICAGO_TZ);
     56     sortedMapForUS.put(1208292, LOS_ANGELES_TZ);
     57     sortedMapForUS.put(1208234, DENVER_TZ);
     58     sortedMapForUS.put(1541367, LOS_ANGELES_TZ);
     59     sortedMapForUS.put(1423843, NEW_YORK_TZ);
     60     sortedMapForUS.put(1402721, CHICAGO_TZ);
     61     sortedMapForUS.put(1208888, DENVER_TZ);
     62 
     63     prefixTimeZonesMapForUS.readPrefixTimeZonesMap(sortedMapForUS);
     64 
     65     SortedMap<Integer, String> sortedMapForRU = new TreeMap<Integer, String>();
     66     sortedMapForRU.put(7421, VLADIVOSTOK_TZ);
     67     sortedMapForRU.put(7879, MOSCOW_TZ);
     68     sortedMapForRU.put(7342, YEKATERINBURG_TZ);
     69     sortedMapForRU.put(7395, IRKUTSK_TZ);
     70 
     71     prefixTimeZonesMapForRU.readPrefixTimeZonesMap(sortedMapForRU);
     72   }
     73 
     74   static List<String> buildListOfTimeZones(String ... timezones) {
     75     ArrayList<String> timezonesList = new ArrayList<String>(timezones.length);
     76     for (String timezone : timezones) {
     77       timezonesList.add(timezone);
     78     }
     79     return timezonesList;
     80   }
     81 
     82   private static SortedMap<Integer, String> createMapCandidate() {
     83     SortedMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
     84     sortedMap.put(1212, NEW_YORK_TZ);
     85     sortedMap.put(1213, NEW_YORK_TZ);
     86     sortedMap.put(1214, NEW_YORK_TZ);
     87     sortedMap.put(1480, CHICAGO_TZ);
     88     return sortedMap;
     89   }
     90 
     91   public void testLookupTimeZonesForNumberCountryLevel_US() {
     92     PhoneNumber number = new PhoneNumber();
     93     number.setCountryCode(1).setNationalNumber(1000000000L);
     94     assertEquals(buildListOfTimeZones(NEW_YORK_TZ, CHICAGO_TZ, LOS_ANGELES_TZ, DENVER_TZ),
     95                  prefixTimeZonesMapForUS.lookupTimeZonesForNumber(number));
     96   }
     97 
     98   public void testLookupTimeZonesForNumber_ValidNumber_Chicago() {
     99     PhoneNumber number = new PhoneNumber();
    100     number.setCountryCode(1).setNationalNumber(2051235458L);
    101     assertEquals(buildListOfTimeZones(CHICAGO_TZ),
    102                  prefixTimeZonesMapForUS.lookupTimeZonesForNumber(number));
    103   }
    104 
    105   public void testLookupTimeZonesForNumber_LA() {
    106     PhoneNumber number = new PhoneNumber();
    107     number.setCountryCode(1).setNationalNumber(2082924565L);
    108     assertEquals(buildListOfTimeZones(LOS_ANGELES_TZ),
    109                  prefixTimeZonesMapForUS.lookupTimeZonesForNumber(number));
    110   }
    111 
    112   public void testLookupTimeZonesForNumber_NY() {
    113     PhoneNumber number = new PhoneNumber();
    114     number.setCountryCode(1).setNationalNumber(2016641234L);
    115     assertEquals(buildListOfTimeZones(NEW_YORK_TZ),
    116                  prefixTimeZonesMapForUS.lookupTimeZonesForNumber(number));
    117   }
    118 
    119   public void testLookupTimeZonesForNumber_CH() {
    120     PhoneNumber number = new PhoneNumber();
    121     number.setCountryCode(41).setNationalNumber(446681300L);
    122     assertEquals(buildListOfTimeZones(),
    123                  prefixTimeZonesMapForUS.lookupTimeZonesForNumber(number));
    124   }
    125 
    126   public void testLookupTimeZonesForNumber_RU() {
    127     PhoneNumber number = new PhoneNumber();
    128     number.setCountryCode(7).setNationalNumber(87945154L);
    129     assertEquals(buildListOfTimeZones(MOSCOW_TZ),
    130                  prefixTimeZonesMapForRU.lookupTimeZonesForNumber(number));
    131 
    132     number.setNationalNumber(421548578L);
    133     assertEquals(buildListOfTimeZones(VLADIVOSTOK_TZ),
    134                  prefixTimeZonesMapForRU.lookupTimeZonesForNumber(number));
    135 
    136     number.setNationalNumber(342457897L);
    137     assertEquals(buildListOfTimeZones(YEKATERINBURG_TZ),
    138                  prefixTimeZonesMapForRU.lookupTimeZonesForNumber(number));
    139 
    140     // A mobile number
    141     number.setNationalNumber(9342457897L);
    142     assertEquals(buildListOfTimeZones(),
    143                  prefixTimeZonesMapForRU.lookupTimeZonesForNumber(number));
    144 
    145     // An invalid number (too short)
    146     number.setNationalNumber(3951L);
    147     assertEquals(buildListOfTimeZones(IRKUTSK_TZ),
    148                  prefixTimeZonesMapForRU.lookupTimeZonesForNumber(number));
    149   }
    150 
    151   /**
    152    * Creates a new PrefixTimeZonesMap serializing the provided map to a stream and then reading
    153    * this stream. The resulting PrefixTimeZonesMap is expected to be strictly equal to the provided
    154    * one from which it was generated.
    155    */
    156   private static PrefixTimeZonesMap createNewPrefixTimeZonesMap(
    157       PrefixTimeZonesMap prefixTimeZonesMap) throws IOException {
    158     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    159     ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    160     prefixTimeZonesMap.writeExternal(objectOutputStream);
    161     objectOutputStream.flush();
    162 
    163     PrefixTimeZonesMap newPrefixTimeZonesMap = new PrefixTimeZonesMap();
    164     newPrefixTimeZonesMap.readExternal(
    165         new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
    166     return newPrefixTimeZonesMap;
    167   }
    168 
    169   public void testReadWriteExternal() throws IOException {
    170     PrefixTimeZonesMap localPrefixTimeZonesMap = new PrefixTimeZonesMap();
    171     localPrefixTimeZonesMap.readPrefixTimeZonesMap(createMapCandidate());
    172 
    173     PrefixTimeZonesMap newPrefixTimeZonesMap = createNewPrefixTimeZonesMap(localPrefixTimeZonesMap);
    174     assertEquals(localPrefixTimeZonesMap.toString(), newPrefixTimeZonesMap.toString());
    175   }
    176 }
    177