Home | History | Annotate | Download | only in geocoding
      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.geocoding;
     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 AreaCodeMap.java
     32  *
     33  * @author Shaopeng Jia
     34  */
     35 public class AreaCodeMapTest extends TestCase {
     36   private final AreaCodeMap areaCodeMapForUS = new AreaCodeMap();
     37   private final AreaCodeMap areaCodeMapForIT = new AreaCodeMap();
     38   private PhoneNumber number = new PhoneNumber();
     39 
     40   public AreaCodeMapTest() {
     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     areaCodeMapForUS.readAreaCodeMap(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     areaCodeMapForIT.readAreaCodeMap(sortedMapForIT);
     65   }
     66 
     67   private static SortedMap<Integer, String> createDefaultStorageMapCandidate() {
     68     SortedMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
     69     // Make the area codes 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     AreaCodeMapStorageStrategy mapStorage =
     86         new AreaCodeMap().getSmallerMapStorage(createDefaultStorageMapCandidate());
     87     assertFalse(mapStorage instanceof FlyweightMapStorage);
     88   }
     89 
     90   public void testGetSmallerMapStorageChoosesFlyweightImpl() {
     91     AreaCodeMapStorageStrategy mapStorage =
     92         new AreaCodeMap().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", areaCodeMapForUS.lookup(number));
    100   }
    101 
    102   public void testLookupNumber_NJ() {
    103     number.setCountryCode(1).setNationalNumber(2016641234L);
    104     assertEquals("Westwood, NJ", areaCodeMapForUS.lookup(number));
    105   }
    106 
    107   public void testLookupNumber_NY() {
    108     number.setCountryCode(1).setNationalNumber(2126641234L);
    109     assertEquals("New York", areaCodeMapForUS.lookup(number));
    110   }
    111 
    112   public void testLookupNumber_CA_1() {
    113     number.setCountryCode(1).setNationalNumber(6503451234L);
    114     assertEquals("San Mateo, CA", areaCodeMapForUS.lookup(number));
    115   }
    116 
    117   public void testLookupNumber_CA_2() {
    118     number.setCountryCode(1).setNationalNumber(6502531234L);
    119     assertEquals("California", areaCodeMapForUS.lookup(number));
    120   }
    121 
    122   public void testLookupNumberFound_TX() {
    123     number.setCountryCode(1).setNationalNumber(9724801234L);
    124     assertEquals("Richardson, TX", areaCodeMapForUS.lookup(number));
    125   }
    126 
    127   public void testLookupNumberNotFound_TX() {
    128     number.setCountryCode(1).setNationalNumber(9724811234L);
    129     assertNull(areaCodeMapForUS.lookup(number));
    130   }
    131 
    132   public void testLookupNumber_CH() {
    133     number.setCountryCode(41).setNationalNumber(446681300L);
    134     assertNull(areaCodeMapForUS.lookup(number));
    135   }
    136 
    137   public void testLookupNumber_IT() {
    138     number.setCountryCode(39).setNationalNumber(212345678L).setItalianLeadingZero(true);
    139     assertEquals("Milan", areaCodeMapForIT.lookup(number));
    140 
    141     number.setNationalNumber(612345678L);
    142     assertEquals("Rome", areaCodeMapForIT.lookup(number));
    143 
    144     number.setNationalNumber(3211234L);
    145     assertEquals("Novara", areaCodeMapForIT.lookup(number));
    146 
    147     // A mobile number
    148     number.setNationalNumber(321123456L).setItalianLeadingZero(false);
    149     assertNull(areaCodeMapForIT.lookup(number));
    150 
    151     // An invalid number (too short)
    152     number.setNationalNumber(321123L).setItalianLeadingZero(true);
    153     assertEquals("Novara", areaCodeMapForIT.lookup(number));
    154   }
    155 
    156   /**
    157    * Creates a new area code map serializing the provided area code map to a stream and then reading
    158    * this stream. The resulting area code map is expected to be strictly equal to the provided one
    159    * from which it was generated.
    160    */
    161   private static AreaCodeMap createNewAreaCodeMap(AreaCodeMap areaCodeMap) throws IOException {
    162     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    163     ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    164     areaCodeMap.writeExternal(objectOutputStream);
    165     objectOutputStream.flush();
    166 
    167     AreaCodeMap newAreaCodeMap = new AreaCodeMap();
    168     newAreaCodeMap.readExternal(
    169         new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
    170     return newAreaCodeMap;
    171   }
    172 
    173   public void testReadWriteExternalWithDefaultStrategy() throws IOException {
    174     AreaCodeMap localAreaCodeMap = new AreaCodeMap();
    175     localAreaCodeMap.readAreaCodeMap(createDefaultStorageMapCandidate());
    176     assertFalse(localAreaCodeMap.getAreaCodeMapStorage() instanceof FlyweightMapStorage);
    177 
    178     AreaCodeMap newAreaCodeMap;
    179     newAreaCodeMap = createNewAreaCodeMap(localAreaCodeMap);
    180     assertEquals(localAreaCodeMap.toString(), newAreaCodeMap.toString());
    181   }
    182 
    183   public void testReadWriteExternalWithFlyweightStrategy() throws IOException {
    184     AreaCodeMap localAreaCodeMap = new AreaCodeMap();
    185     localAreaCodeMap.readAreaCodeMap(createFlyweightStorageMapCandidate());
    186     assertTrue(localAreaCodeMap.getAreaCodeMapStorage() instanceof FlyweightMapStorage);
    187 
    188     AreaCodeMap newAreaCodeMap;
    189     newAreaCodeMap = createNewAreaCodeMap(localAreaCodeMap);
    190     assertEquals(localAreaCodeMap.toString(), newAreaCodeMap.toString());
    191   }
    192 }
    193