Home | History | Annotate | Download | only in prefixmapper
      1 /*
      2  * Copyright (C) 2011 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.SortedMap;
     28 import java.util.TreeMap;
     29 
     30 /**
     31  * Unittests for PhonePrefixMap.java
     32  *
     33  * @author Shaopeng Jia
     34  */
     35 public class PhonePrefixMapTest extends TestCase {
     36   private final PhonePrefixMap phonePrefixMapForUS = new PhonePrefixMap();
     37   private final PhonePrefixMap phonePrefixMapForIT = new PhonePrefixMap();
     38   private PhoneNumber number = new PhoneNumber();
     39 
     40   public PhonePrefixMapTest() {
     41     SortedMap<Integer, String> sortedMapForUS = new TreeMap<Integer, String>();
     42     sortedMapForUS.put(1212, "New York");
     43     sortedMapForUS.put(1480, "Arizona");
     44     sortedMapForUS.put(1650, "California");
     45     sortedMapForUS.put(1907, "Alaska");
     46     sortedMapForUS.put(1201664, "Westwood, NJ");
     47     sortedMapForUS.put(1480893, "Phoenix, AZ");
     48     sortedMapForUS.put(1501372, "Little Rock, AR");
     49     sortedMapForUS.put(1626308, "Alhambra, CA");
     50     sortedMapForUS.put(1650345, "San Mateo, CA");
     51     sortedMapForUS.put(1867993, "Dawson, YT");
     52     sortedMapForUS.put(1972480, "Richardson, TX");
     53 
     54     phonePrefixMapForUS.readPhonePrefixMap(sortedMapForUS);
     55 
     56     SortedMap<Integer, String> sortedMapForIT = new TreeMap<Integer, String>();
     57     sortedMapForIT.put(3902, "Milan");
     58     sortedMapForIT.put(3906, "Rome");
     59     sortedMapForIT.put(39010, "Genoa");
     60     sortedMapForIT.put(390131, "Alessandria");
     61     sortedMapForIT.put(390321, "Novara");
     62     sortedMapForIT.put(390975, "Potenza");
     63 
     64     phonePrefixMapForIT.readPhonePrefixMap(sortedMapForIT);
     65   }
     66 
     67   private static SortedMap<Integer, String> createDefaultStorageMapCandidate() {
     68     SortedMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
     69     // Make the phone prefixs bigger to store them using integer.
     70     sortedMap.put(121212345, "New York");
     71     sortedMap.put(148034434, "Arizona");
     72     return sortedMap;
     73   }
     74 
     75   private static SortedMap<Integer, String> createFlyweightStorageMapCandidate() {
     76     SortedMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
     77     sortedMap.put(1212, "New York");
     78     sortedMap.put(1213, "New York");
     79     sortedMap.put(1214, "New York");
     80     sortedMap.put(1480, "Arizona");
     81     return sortedMap;
     82   }
     83 
     84   public void testGetSmallerMapStorageChoosesDefaultImpl() {
     85     PhonePrefixMapStorageStrategy mapStorage =
     86         new PhonePrefixMap().getSmallerMapStorage(createDefaultStorageMapCandidate());
     87     assertFalse(mapStorage instanceof FlyweightMapStorage);
     88   }
     89 
     90   public void testGetSmallerMapStorageChoosesFlyweightImpl() {
     91     PhonePrefixMapStorageStrategy mapStorage =
     92         new PhonePrefixMap().getSmallerMapStorage(createFlyweightStorageMapCandidate());
     93     assertTrue(mapStorage instanceof FlyweightMapStorage);
     94   }
     95 
     96   public void testLookupInvalidNumber_US() {
     97     // central office code cannot start with 1.
     98     number.setCountryCode(1).setNationalNumber(2121234567L);
     99     assertEquals("New York", phonePrefixMapForUS.lookup(number));
    100   }
    101 
    102   public void testLookupNumber_NJ() {
    103     number.setCountryCode(1).setNationalNumber(2016641234L);
    104     assertEquals("Westwood, NJ", phonePrefixMapForUS.lookup(number));
    105   }
    106 
    107   public void testLookupNumber_NY() {
    108     number.setCountryCode(1).setNationalNumber(2126641234L);
    109     assertEquals("New York", phonePrefixMapForUS.lookup(number));
    110   }
    111 
    112   public void testLookupNumber_CA_1() {
    113     number.setCountryCode(1).setNationalNumber(6503451234L);
    114     assertEquals("San Mateo, CA", phonePrefixMapForUS.lookup(number));
    115   }
    116 
    117   public void testLookupNumber_CA_2() {
    118     number.setCountryCode(1).setNationalNumber(6502531234L);
    119     assertEquals("California", phonePrefixMapForUS.lookup(number));
    120   }
    121 
    122   public void testLookupNumberFound_TX() {
    123     number.setCountryCode(1).setNationalNumber(9724801234L);
    124     assertEquals("Richardson, TX", phonePrefixMapForUS.lookup(number));
    125   }
    126 
    127   public void testLookupNumberNotFound_TX() {
    128     number.setCountryCode(1).setNationalNumber(9724811234L);
    129     assertNull(phonePrefixMapForUS.lookup(number));
    130   }
    131 
    132   public void testLookupNumber_CH() {
    133     number.setCountryCode(41).setNationalNumber(446681300L);
    134     assertNull(phonePrefixMapForUS.lookup(number));
    135   }
    136 
    137   public void testLookupNumber_IT() {
    138     number.setCountryCode(39).setNationalNumber(212345678L).setItalianLeadingZero(true);
    139     assertEquals("Milan", phonePrefixMapForIT.lookup(number));
    140 
    141     number.setNationalNumber(612345678L);
    142     assertEquals("Rome", phonePrefixMapForIT.lookup(number));
    143 
    144     number.setNationalNumber(3211234L);
    145     assertEquals("Novara", phonePrefixMapForIT.lookup(number));
    146 
    147     // A mobile number
    148     number.setNationalNumber(321123456L).setItalianLeadingZero(false);
    149     assertNull(phonePrefixMapForIT.lookup(number));
    150 
    151     // An invalid number (too short)
    152     number.setNationalNumber(321123L).setItalianLeadingZero(true);
    153     assertEquals("Novara", phonePrefixMapForIT.lookup(number));
    154   }
    155 
    156   /**
    157    * Creates a new phone prefix map serializing the provided phone prefix map to a stream and then
    158    * reading this stream. The resulting phone prefix map is expected to be strictly equal to the
    159    * provided one from which it was generated.
    160    */
    161   private static PhonePrefixMap createNewPhonePrefixMap(
    162       PhonePrefixMap phonePrefixMap) throws IOException {
    163     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    164     ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    165     phonePrefixMap.writeExternal(objectOutputStream);
    166     objectOutputStream.flush();
    167 
    168     PhonePrefixMap newPhonePrefixMap = new PhonePrefixMap();
    169     newPhonePrefixMap.readExternal(
    170         new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
    171     return newPhonePrefixMap;
    172   }
    173 
    174   public void testReadWriteExternalWithDefaultStrategy() throws IOException {
    175     PhonePrefixMap localPhonePrefixMap = new PhonePrefixMap();
    176     localPhonePrefixMap.readPhonePrefixMap(createDefaultStorageMapCandidate());
    177     assertFalse(localPhonePrefixMap.getPhonePrefixMapStorage() instanceof FlyweightMapStorage);
    178 
    179     PhonePrefixMap newPhonePrefixMap;
    180     newPhonePrefixMap = createNewPhonePrefixMap(localPhonePrefixMap);
    181     assertEquals(localPhonePrefixMap.toString(), newPhonePrefixMap.toString());
    182   }
    183 
    184   public void testReadWriteExternalWithFlyweightStrategy() throws IOException {
    185     PhonePrefixMap localPhonePrefixMap = new PhonePrefixMap();
    186     localPhonePrefixMap.readPhonePrefixMap(createFlyweightStorageMapCandidate());
    187     assertTrue(localPhonePrefixMap.getPhonePrefixMapStorage() instanceof FlyweightMapStorage);
    188 
    189     PhonePrefixMap newPhonePrefixMap;
    190     newPhonePrefixMap = createNewPhonePrefixMap(localPhonePrefixMap);
    191     assertEquals(localPhonePrefixMap.toString(), newPhonePrefixMap.toString());
    192   }
    193 }
    194